summaryrefslogtreecommitdiff
path: root/ui.hpp
diff options
context:
space:
mode:
authorquou <quou@disroot.org>2024-12-30 17:01:55 +1100
committerquou <quou@disroot.org>2024-12-30 17:01:55 +1100
commit0f0167c0211acd69c104ab6c7c1caa8fa85d7e4f (patch)
tree9dc472a310d7f91a9456b87dee9e0b3e731d376c /ui.hpp
parente8baea58dd5c92b62c4eef1b5c1ca9648f44e7d7 (diff)
start UI framework
Diffstat (limited to 'ui.hpp')
-rw-r--r--ui.hpp73
1 files changed, 72 insertions, 1 deletions
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 <new>
+
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 <typename T, typename... Args>
+ 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