initial tree impl without any updating logic

This commit is contained in:
jacekpoz 2024-09-09 21:11:52 +02:00
parent 8581e336d5
commit be1ae4c531
Signed by: poz
SSH key fingerprint: SHA256:JyLeVWE4bF3tDnFeUpUaJsPsNlJyBldDGV/dIKSLyN8
4 changed files with 64 additions and 18 deletions

View file

@ -32,6 +32,7 @@ PTK_LIST_DEFINE(PtkHandle);
#define PTK_COMPONENT_DEFINE(name, ...) \
typedef struct name {\
uint64_t id;\
PtkComponentType type;\
PtkHandle parent;\
PTK_LIST(PtkHandle) children;\

View file

@ -142,13 +142,17 @@ bool ptk_init(const size_t width, const size_t height, const char *title, const
return true;
}
static uint64_t m_component_count = 0;
PtkHandle ptk_box(const size_t child_count, PtkHandle *children) {
PtkComponent *ret = malloc(sizeof(PtkComponent));
*ret = (PtkComponent){
.id = m_component_count,
.type = PTK_COMPONENT_TYPE_BOX,
.children = PTK_LIST_NEW(PtkHandle, child_count),
};
PTK_LIST_ADD_ALL_P(PtkHandle, ret->children, children, child_count);
m_component_count += 1;
return (PtkHandle)ret;
}
@ -156,10 +160,12 @@ PtkHandle ptk_box(const size_t child_count, PtkHandle *children) {
PtkHandle ptk_triangle(const PtkPos vertices[3], const PtkRGB color) {
PtkTriangle *ret = malloc(sizeof(PtkTriangle));
*ret = (PtkTriangle){
.id = m_component_count,
.type = PTK_COMPONENT_TYPE_TRIANGLE,
.color = color,
};
memcpy(ret->vertices, vertices, sizeof(PtkPos) * 3);
m_component_count += 1;
return (PtkHandle)ret;
}
@ -167,11 +173,13 @@ PtkHandle ptk_triangle(const PtkPos vertices[3], const PtkRGB color) {
PtkHandle ptk_rect(const PtkPos top_left, const PtkSize size, const PtkRGB color) {
PtkRect *ret = malloc(sizeof(PtkRect));
*ret = (PtkRect){
.id = m_component_count,
.type = PTK_COMPONENT_TYPE_RECT,
.top_left = top_left,
.size = size,
.color = color,
};
m_component_count += 1;
return (PtkHandle)ret;
}
@ -183,11 +191,13 @@ PtkHandle ptk_square(const PtkPos top_left, const float size, const PtkRGB color
PtkHandle ptk_ellipse(const PtkPos center, const PtkSize radii, const PtkRGB color) {
PtkEllipse *ret = malloc(sizeof(PtkEllipse));
*ret = (PtkEllipse){
.id = m_component_count,
.type = PTK_COMPONENT_TYPE_ELLIPSE,
.center = center,
.radii = radii,
.color = color,
};
m_component_count += 1;
return (PtkHandle)ret;
}
@ -199,11 +209,13 @@ PtkHandle ptk_circle(const PtkPos center, const float radius, const PtkRGB color
PtkHandle ptk_clickable(PtkHandle hitbox, const MouseButtonCallback on_press) {
PtkClickable *ret = malloc(sizeof(PtkClickable));
*ret = (PtkClickable){
.id = m_component_count,
.type = PTK_COMPONENT_TYPE_CLICKABLE,
.children = PTK_LIST_NEW(PtkHandle, 1),
.on_press = on_press,
};
PTK_LIST_ADD(PtkHandle, ret->children, hitbox);
m_component_count += 1;
return (PtkHandle)ret;
}

View file

@ -23,6 +23,14 @@
#define PTK_LIST_REMOVE_AT(T, list, index) _remove_at_PtkList((void *)list.data, &list.size, index, sizeof(T))
#define PTK_LIST_SET(T, list, index, elem) do {\
while (list.allocated <= index) {\
/* let's hope this doesn't fail !!! */\
_grow_PtkList((void *)&list.data, &list.allocated, sizeof(T));\
}\
list.data[index] = elem;\
} while (0)
bool _grow_PtkList(void **data, uint32_t *allocated, size_t element_size);
bool _add_PtkList(void **data, uint32_t *size, uint32_t *allocated, void *elements, size_t element_count, size_t element_size);
bool _remove_PtkList(void *data, uint32_t *size, void *elements, size_t element_count, size_t element_size);

View file

@ -7,8 +7,16 @@
#include <GLFW/glfw3.h>
PTK_LIST_DEFINE(bool);
static PtkHandle m_root_component;
static PTK_LIST(PtkHandle) m_components;
// TODO: figure out a way to actually use bool here
// this happens because PTK_LIST_DEFINE and PTK_LIST_NEW
// use PTK_LIST themselves, bool gets put through the first
// and turned into _Bool which is then used as the actual
// type's name; using PTK_LIST directly only processes it once
// so we have to use _Bool here
static PTK_LIST(_Bool) m_update;
PTK_LIST(Vertex) g_vertices;
PTK_LIST(uint32_t) g_indices;
@ -21,7 +29,7 @@ static const PtkVec2 uvs[] = {
};
void vk_init_vertices(void) {
m_components = PTK_LIST_NEW(PtkHandle, 1);
m_update = PTK_LIST_NEW(bool, 1);
g_vertices = PTK_LIST_NEW(Vertex, 0);
g_indices = PTK_LIST_NEW(uint32_t, 0);
@ -117,7 +125,7 @@ void ellipse(const PtkEllipse *ellipse) {
}
void component(PtkHandle c) {
PTK_LIST_ADD(PtkHandle, m_components, c);
PTK_LIST_SET(bool, m_update, c->id, false);
switch (c->type) {
case PTK_COMPONENT_TYPE_TRIANGLE: {
@ -143,7 +151,6 @@ void vk_init_components(PtkHandle root) {
}
void vk_update_components(void) {
PTK_LIST_CLEAR(m_components);
PTK_LIST_CLEAR(g_vertices);
PTK_LIST_CLEAR(g_indices);
component(m_root_component);
@ -202,31 +209,49 @@ bool intersects(const PtkHandle component, const PtkPos point) {
}
}
void update_component(PtkHandle c) {
PTK_LIST_SET(bool, m_update, c->id, true);
if (c->parent != PTK_NULL_HANDLE) {
update_component(c->parent);
}
}
void handle_mouse(PtkHandle c, const PtkPos cursor_pos, const int button, const int mods) {
if (c->type != PTK_COMPONENT_TYPE_CLICKABLE) {
PTK_LIST_FOR_EACH(PtkHandle, c->children, child, {
handle_mouse(child, cursor_pos, button, mods);
})
return;
}
PtkClickable *clickable = (PtkClickable *)c;
if (intersects((PtkHandle)clickable, cursor_pos)) {
clickable->on_press(clickable, button, mods);
update_component((PtkHandle)clickable);
vk_update_components();
}
}
void vk_handle_mouse_button_input(const PtkPos cursor_pos, const int button, const int action, const int mods) {
if (action == GLFW_RELEASE) {
return;
}
PTK_LIST_FOR_EACH(PtkHandle, m_components, current, {
if (current->type != PTK_COMPONENT_TYPE_CLICKABLE) {
continue;
}
handle_mouse(m_root_component, cursor_pos, button, mods);
}
PtkClickable *c = (PtkClickable *)current;
if (intersects((PtkHandle)c, cursor_pos)) {
c->on_press(c, button, mods);
vk_update_components();
}
void cleanup_component(PtkHandle c) {
PTK_LIST_FOR_EACH(PtkHandle, c->children, child, {
cleanup_component(child);
})
free(c);
}
void vk_components_cleanup(void) {
PTK_LIST_FOR_EACH(PtkHandle, m_components, c, {
free(c);
})
cleanup_component(m_root_component);
PTK_LIST_FREE(m_components);
PTK_LIST_FREE(g_vertices);
PTK_LIST_FREE(g_indices);
}