diff --git a/src/ptk.c b/src/ptk.c index cd25d1d..0033c2d 100644 --- a/src/ptk.c +++ b/src/ptk.c @@ -164,74 +164,31 @@ static uint64_t get_component_id(void) { } PtkHandle ptk_box(const size_t child_count, PtkHandle *children) { - PtkComponent *ret = malloc(sizeof(PtkComponent)); - *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; + return box_component(get_component_id(), child_count, children); } PtkHandle ptk_triangle(const PtkPos vertices[3], const PtkRGB color) { - PtkTriangle *ret = malloc(sizeof(PtkTriangle)); - *ret = (PtkTriangle){ - .id = get_component_id(), - .type = PTK_COMPONENT_TYPE_TRIANGLE, - .color = color, - }; - memcpy(ret->vertices, vertices, sizeof(PtkPos) * 3); - - return (PtkHandle)ret; + return triangle_component(get_component_id(), vertices, color); } PtkHandle ptk_rect(const PtkPos top_left, const PtkSize size, const PtkRGB color) { - PtkRect *ret = malloc(sizeof(PtkRect)); - *ret = (PtkRect){ - .id = get_component_id(), - .type = PTK_COMPONENT_TYPE_RECT, - .top_left = top_left, - .size = size, - .color = color, - }; - - return (PtkHandle)ret; + return rect_component(get_component_id(), top_left, size, 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) { - PtkEllipse *ret = malloc(sizeof(PtkEllipse)); - *ret = (PtkEllipse){ - .id = get_component_id(), - .type = PTK_COMPONENT_TYPE_ELLIPSE, - .center = center, - .radii = radii, - .color = color, - }; - - return (PtkHandle)ret; + return ellipse_component(get_component_id(), center, radii, 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) { - PtkClickable *ret = malloc(sizeof(PtkClickable)); - *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; + return clickable_component(get_component_id(), hitbox, on_press); } int ptk_run(PtkHandle root) { diff --git a/src/ptk_vk/components.c b/src/ptk_vk/components.c index e2bd914..78102ad 100644 --- a/src/ptk_vk/components.c +++ b/src/ptk_vk/components.c @@ -6,6 +6,7 @@ #include #include +#include PTK_LIST_DEFINE(bool); @@ -47,6 +48,77 @@ void vk_init_vertices(void) { 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) { 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)); @@ -152,7 +224,7 @@ void ellipse(const PtkEllipse *ellipse) { .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; rect(r); diff --git a/src/ptk_vk/components.h b/src/ptk_vk/components.h index f684da4..3f793e4 100644 --- a/src/ptk_vk/components.h +++ b/src/ptk_vk/components.h @@ -21,6 +21,14 @@ extern PTK_LIST(uint32_t) g_indices; 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_update_components(void);