From fa136d89a9d3e6c85b8dbd5e2b08e800a4684782 Mon Sep 17 00:00:00 2001 From: jacekpoz Date: Tue, 13 Aug 2024 16:43:42 +0200 Subject: [PATCH] improve component handling --- src/ptk.c | 13 ++----------- src/ptk_vk/components.c | 39 +++++++++++++++++++++++++-------------- src/ptk_vk/components.h | 6 ++++-- 3 files changed, 31 insertions(+), 27 deletions(-) diff --git a/src/ptk.c b/src/ptk.c index 3e39d89..f2e674b 100644 --- a/src/ptk.c +++ b/src/ptk.c @@ -122,7 +122,7 @@ bool ptk_init(const size_t width, const size_t height, const char *title, const glfwSetKeyCallback(m_window, key_callback); glfwSetMouseButtonCallback(m_window, mouse_button_callback); - vk_components_init(); + vk_init_vertices(); if (!vk_init(m_window, width, height, title, application_version)) { PTK_ERR("failed initializing vulkan"); @@ -198,17 +198,8 @@ PtkHandle ptk_clickable(PtkHandle hitbox, const MouseButtonCallback on_press) { return (PtkHandle)ret; } -void init_component(PtkHandle component) { - vk_component(component); - for (size_t i = 0; i < component->children.size; ++i) { - PtkHandle child = component->children.data[i]; - child->parent = component; - init_component(child); - } -} - int ptk_run(PtkHandle root) { - init_component(root); + vk_init_components(root); while (!glfwWindowShouldClose(m_window)) { glfwPollEvents(); diff --git a/src/ptk_vk/components.c b/src/ptk_vk/components.c index 660c010..cc6ea40 100644 --- a/src/ptk_vk/components.c +++ b/src/ptk_vk/components.c @@ -5,6 +5,7 @@ #include #include +static PtkHandle m_root_component; static PTK_LIST(PtkHandle) m_components; PTK_LIST(Vertex) g_vertices; @@ -16,7 +17,7 @@ static const PtkVec2 uvs[] = { { .x = 1.0f, .y = 1.0f }, }; -void vk_components_init(void) { +void vk_init_vertices(void) { m_components = PTK_LIST_NEW(PtkHandle, 1); g_vertices = PTK_LIST_NEW(Vertex, 1); @@ -83,28 +84,37 @@ void ellipse(const PtkEllipse *ellipse) { rect(r); } -void vk_component(PtkHandle component) { - if (component->type == PTK_COMPONENT_TYPE_BOX) { - return; - } +void component(PtkHandle c) { + PTK_LIST_ADD(PtkHandle, m_components, c); - PTK_LIST_ADD(PtkHandle, m_components, component); - - switch (component->type) { + switch (c->type) { case PTK_COMPONENT_TYPE_TRIANGLE: { - triangle((PtkTriangle *)component, 0); + triangle((PtkTriangle *)c, 0); } break; case PTK_COMPONENT_TYPE_RECT: { - rect((PtkRect *)component); + rect((PtkRect *)c); } break; case PTK_COMPONENT_TYPE_ELLIPSE: { - ellipse((PtkEllipse *)component); + ellipse((PtkEllipse *)c); } break; - default: { - return; - } + default: break; } + for (size_t i = 0; i < c->children.size; ++i) { + PtkHandle child = c->children.data[i]; + child->parent = c; + component(child); + } +} +void vk_init_components(PtkHandle root) { + m_root_component = root; + vk_update_components(); +} + +void vk_update_components(void) { + PTK_LIST_CLEAR(m_components); + PTK_LIST_CLEAR(g_vertices); + component(m_root_component); vk_transfer_vertex_data(); } @@ -171,6 +181,7 @@ void vk_handle_mouse_button_input(const PtkPos cursor_pos, const int button, con if (intersects((PtkHandle)c, cursor_pos)) { c->on_press(button, action, mods); + vk_update_components(); } } } diff --git a/src/ptk_vk/components.h b/src/ptk_vk/components.h index 941c9c0..79373c1 100644 --- a/src/ptk_vk/components.h +++ b/src/ptk_vk/components.h @@ -17,9 +17,11 @@ PTK_LIST_DEFINE(Vertex); extern PTK_LIST(Vertex) g_vertices; -void vk_components_init(void); +void vk_init_vertices(void); -void vk_component(PtkHandle component); +void vk_init_components(PtkHandle root); + +void vk_update_components(void); void vk_handle_mouse_button_input(const PtkPos cursor_pos, const int button, const int action, const int mode);