improve component handling

This commit is contained in:
jacekpoz 2024-08-13 16:43:42 +02:00
parent daf5f0c4cc
commit fa136d89a9
Signed by: poz
SSH key fingerprint: SHA256:JyLeVWE4bF3tDnFeUpUaJsPsNlJyBldDGV/dIKSLyN8
3 changed files with 31 additions and 27 deletions

View file

@ -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();

View file

@ -5,6 +5,7 @@
#include <ptk_vk/components.h>
#include <ptk_vk/init.h>
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();
}
}
}

View file

@ -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);