add helper component functions that take an id

This commit is contained in:
jacekpoz 2024-09-10 10:16:40 +02:00
parent a6d741b995
commit 73d14d1908
Signed by: poz
SSH key fingerprint: SHA256:JyLeVWE4bF3tDnFeUpUaJsPsNlJyBldDGV/dIKSLyN8
3 changed files with 88 additions and 51 deletions

View file

@ -164,74 +164,31 @@ static uint64_t get_component_id(void) {
} }
PtkHandle ptk_box(const size_t child_count, PtkHandle *children) { PtkHandle ptk_box(const size_t child_count, PtkHandle *children) {
PtkComponent *ret = malloc(sizeof(PtkComponent)); return box_component(get_component_id(), child_count, children);
*ret = (PtkComponent){
.id = get_component_id(),
.type = PTK_COMPONENT_TYPE_BOX,
.children = PTK_LIST_NEW(PtkHandle, child_count),
};
PTK_LIST_ADD_ALL_P(PtkHandle, ret->children, children, child_count);
return (PtkHandle)ret;
} }
PtkHandle ptk_triangle(const PtkPos vertices[3], const PtkRGB color) { PtkHandle ptk_triangle(const PtkPos vertices[3], const PtkRGB color) {
PtkTriangle *ret = malloc(sizeof(PtkTriangle)); return triangle_component(get_component_id(), vertices, color);
*ret = (PtkTriangle){
.id = get_component_id(),
.type = PTK_COMPONENT_TYPE_TRIANGLE,
.color = color,
};
memcpy(ret->vertices, vertices, sizeof(PtkPos) * 3);
return (PtkHandle)ret;
} }
PtkHandle ptk_rect(const PtkPos top_left, const PtkSize size, const PtkRGB color) { PtkHandle ptk_rect(const PtkPos top_left, const PtkSize size, const PtkRGB color) {
PtkRect *ret = malloc(sizeof(PtkRect)); return rect_component(get_component_id(), top_left, size, color);
*ret = (PtkRect){
.id = get_component_id(),
.type = PTK_COMPONENT_TYPE_RECT,
.top_left = top_left,
.size = size,
.color = color,
};
return (PtkHandle)ret;
} }
PtkHandle ptk_square(const PtkPos top_left, const float size, const PtkRGB color) { PtkHandle ptk_square(const PtkPos top_left, const float size, const PtkRGB color) {
return ptk_rect(top_left, (PtkSize){ .w = size, .h = size }, color); return square_component(get_component_id(), top_left, size, color);
} }
PtkHandle ptk_ellipse(const PtkPos center, const PtkSize radii, const PtkRGB color) { PtkHandle ptk_ellipse(const PtkPos center, const PtkSize radii, const PtkRGB color) {
PtkEllipse *ret = malloc(sizeof(PtkEllipse)); return ellipse_component(get_component_id(), center, radii, color);
*ret = (PtkEllipse){
.id = get_component_id(),
.type = PTK_COMPONENT_TYPE_ELLIPSE,
.center = center,
.radii = radii,
.color = color,
};
return (PtkHandle)ret;
} }
PtkHandle ptk_circle(const PtkPos center, const float radius, const PtkRGB color) { PtkHandle ptk_circle(const PtkPos center, const float radius, const PtkRGB color) {
return ptk_ellipse(center, (PtkSize){ .w = radius, .h = radius }, color); return circle_component(get_component_id(), center, radius, color);
} }
PtkHandle ptk_clickable(PtkHandle hitbox, const MouseButtonCallback on_press) { PtkHandle ptk_clickable(PtkHandle hitbox, const MouseButtonCallback on_press) {
PtkClickable *ret = malloc(sizeof(PtkClickable)); return clickable_component(get_component_id(), hitbox, on_press);
*ret = (PtkClickable){
.id = get_component_id(),
.type = PTK_COMPONENT_TYPE_CLICKABLE,
.children = PTK_LIST_NEW(PtkHandle, 1),
.on_press = on_press,
};
PTK_LIST_ADD(PtkHandle, ret->children, hitbox);
return (PtkHandle)ret;
} }
int ptk_run(PtkHandle root) { int ptk_run(PtkHandle root) {

View file

@ -6,6 +6,7 @@
#include <ptk_vk/init.h> #include <ptk_vk/init.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <string.h>
PTK_LIST_DEFINE(bool); PTK_LIST_DEFINE(bool);
@ -47,6 +48,77 @@ void vk_init_vertices(void) {
g_indices = PTK_LIST_NEW(uint32_t, 0); g_indices = PTK_LIST_NEW(uint32_t, 0);
} }
PtkHandle box_component(const uint64_t id, const size_t child_count, PtkHandle *children) {
PtkComponent *ret = malloc(sizeof(PtkComponent));
*ret = (PtkComponent){
.id = id,
.type = PTK_COMPONENT_TYPE_BOX,
.children = PTK_LIST_NEW(PtkHandle, child_count),
};
PTK_LIST_ADD_ALL_P(PtkHandle, ret->children, children, child_count);
return (PtkHandle)ret;
}
PtkHandle triangle_component(const uint64_t id, const PtkPos vertices[3], const PtkRGB color) {
PtkTriangle *ret = malloc(sizeof(PtkTriangle));
*ret = (PtkTriangle){
.id = id,
.type = PTK_COMPONENT_TYPE_TRIANGLE,
.color = color,
};
memcpy(ret->vertices, vertices, sizeof(PtkPos) * 3);
return (PtkHandle)ret;
}
PtkHandle rect_component(const uint64_t id, const PtkPos top_left, const PtkSize size, const PtkRGB color) {
PtkRect *ret = malloc(sizeof(PtkRect));
*ret = (PtkRect){
.id = id,
.type = PTK_COMPONENT_TYPE_RECT,
.top_left = top_left,
.size = size,
.color = color,
};
return (PtkHandle)ret;
}
PtkHandle square_component(const uint64_t id, const PtkPos top_left, const float size, const PtkRGB color) {
return rect_component(id, top_left, (PtkSize){ .w = size, .h = size }, color);
}
PtkHandle ellipse_component(const uint64_t id, const PtkPos center, const PtkSize radii, const PtkRGB color) {
PtkEllipse *ret = malloc(sizeof(PtkEllipse));
*ret = (PtkEllipse){
.id = id,
.type = PTK_COMPONENT_TYPE_ELLIPSE,
.center = center,
.radii = radii,
.color = color,
};
return (PtkHandle)ret;
}
PtkHandle circle_component(const uint64_t id, const PtkPos center, const float radius, const PtkRGB color) {
return ellipse_component(id, center, (PtkSize){ .w = radius, .h = radius }, color);
}
PtkHandle clickable_component(const uint64_t id, PtkHandle hitbox, const MouseButtonCallback on_press) {
PtkClickable *ret = malloc(sizeof(PtkClickable));
*ret = (PtkClickable){
.id = id,
.type = PTK_COMPONENT_TYPE_CLICKABLE,
.children = PTK_LIST_NEW(PtkHandle, 1),
.on_press = on_press,
};
PTK_LIST_ADD(PtkHandle, ret->children, hitbox);
return (PtkHandle)ret;
}
void triangle(const PtkTriangle *triangle) { void triangle(const PtkTriangle *triangle) {
PTK_LIST_SET(Vertices, m_vertices_cache, triangle->id, PTK_LIST_NEW(Vertex, 3)); PTK_LIST_SET(Vertices, m_vertices_cache, triangle->id, PTK_LIST_NEW(Vertex, 3));
PTK_LIST_SET(Indices, m_indices_cache, triangle->id, PTK_LIST_NEW(uint32_t, 3)); PTK_LIST_SET(Indices, m_indices_cache, triangle->id, PTK_LIST_NEW(uint32_t, 3));
@ -152,7 +224,7 @@ void ellipse(const PtkEllipse *ellipse) {
.h = ellipse->radii.h * 2.0f, .h = ellipse->radii.h * 2.0f,
}; };
PtkRect *r = (PtkRect *)ptk_rect(top_left, size, ellipse->color); PtkRect *r = (PtkRect *)rect_component(ellipse->id, top_left, size, ellipse->color);
r->type = ellipse->type; r->type = ellipse->type;
rect(r); rect(r);

View file

@ -21,6 +21,14 @@ extern PTK_LIST(uint32_t) g_indices;
void vk_init_vertices(void); void vk_init_vertices(void);
PtkHandle box_component(const uint64_t id, const size_t child_count, PtkHandle *children);
PtkHandle triangle_component(const uint64_t id, const PtkPos vertices[3], const PtkRGB color);
PtkHandle rect_component(const uint64_t id, const PtkPos top_left, const PtkSize size, const PtkRGB color);
PtkHandle square_component(const uint64_t id, const PtkPos top_left, const float size, const PtkRGB color);
PtkHandle ellipse_component(const uint64_t id, const PtkPos center, const PtkSize radii, const PtkRGB color);
PtkHandle circle_component(const uint64_t id, const PtkPos center, const float radius, const PtkRGB color);
PtkHandle clickable_component(const uint64_t id, PtkHandle hitbox, const MouseButtonCallback on_press);
void vk_init_components(PtkHandle root); void vk_init_components(PtkHandle root);
void vk_update_components(void); void vk_update_components(void);