From 0f0167c0211acd69c104ab6c7c1caa8fa85d7e4f Mon Sep 17 00:00:00 2001 From: quou Date: Mon, 30 Dec 2024 17:01:55 +1100 Subject: start UI framework --- ui.hpp | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) (limited to 'ui.hpp') diff --git a/ui.hpp b/ui.hpp index 2b671ae..432b615 100644 --- a/ui.hpp +++ b/ui.hpp @@ -1,9 +1,12 @@ #ifndef ui_hpp #define ui_hpp +#include "app.hpp" #include "maths.hpp" #include "video.hpp" +#include + struct Arena; struct UI { struct Vertex { @@ -24,6 +27,10 @@ struct UI { Rect() = default; Rect(int x, int y, int w, int h); + + void clip(const Rect& other); + void shrink(int a); + bool contains(int px, int py); }; struct Vertex_Buffer { @@ -77,6 +84,8 @@ struct UI { Vertex_Buffer* next; }; + struct Element; + Heap* heap; Device* device; Texture_Id atlas; @@ -87,6 +96,7 @@ struct UI { Vertex_Buffer mesh; Pipeline* pipeline; Render_Pass* render_pass; + Element* root, * hot, * hovered; struct UI_CBuffer { m4f projection; @@ -109,8 +119,69 @@ struct UI { Shader_Id sh ); void destroy(); - void update(Arena* s); + void update(Arena* s, const App& app); void render(Arena* s, Texture_Id target); + + void draw_container(const Rect& r); + void draw_containeri(const Rect& r); + + Element* alloc_element(size_t size); + template + T* create_element(Element* parent, Args... args) { + T* e = (T*)alloc_element(sizeof(T)); + new (e) T(this, parent, args...); + return e; + } + + struct Message { + enum class Type { + click + } type; + }; + + typedef int (*Message_Handler)(Element* e, const Message& m); + + struct Element { + UI* ui; + Element* parent; + Element** children; + Message_Handler handler; + Rect bound, clip; + int child_count; + + Element(UI* ui, Element* parent); + virtual ~Element(); + + virtual Rect layout(const Rect& avail); + void update(const App& app); + virtual void on_render(); + virtual void on_message(const Message& msg); + + void add_child(Element* ch); + void message(const Message& msg); + void render(); + }; + + struct Container : Element { + int padding; + Container(UI* ui, Element* parent); + + Rect layout(const Rect& avail) override; + }; + + struct Toolbar : Element { + int padding; + Toolbar(UI* ui, Element* parent); + Rect layout(const Rect& avail) override; + void on_render() override; + }; + + struct Button : Element { + char* text; + Button(UI* ui, Element* parent, const char* label); + Rect layout(const Rect& avail) override; + void on_render() override; + }; }; #endif -- cgit v1.2.3-54-g00ecf