a few component changes before separating them from shapes

This commit is contained in:
jacekpoz 2024-08-13 00:03:51 +02:00
parent 4b1fdf8e04
commit 83c7968d48
Signed by: poz
SSH key fingerprint: SHA256:JyLeVWE4bF3tDnFeUpUaJsPsNlJyBldDGV/dIKSLyN8
3 changed files with 59 additions and 97 deletions

View file

@ -185,28 +185,17 @@ PtkHandle ptk_clickable(PtkHandle hitbox, MouseButtonCallback on_press) {
return (PtkHandle)ret;
}
void init_components(PtkHandle root) {
switch (root->type) {
case PTK_COMPONENT_TYPE_BOX: {
vk_box((PtkBox *)root);
} break;
case PTK_COMPONENT_TYPE_TRIANGLE: {
vk_triangle((PtkTriangle *)root);
} break;
case PTK_COMPONENT_TYPE_RECT: {
vk_rect((PtkRect *)root);
} break;
case PTK_COMPONENT_TYPE_ELLIPSE: {
vk_ellipse((PtkEllipse *)root);
} break;
case PTK_COMPONENT_TYPE_BUTTON: {
vk_button((PtkButton *)root);
} break;
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_components(root);
init_component(root);
while (!glfwWindowShouldClose(g_window)) {
glfwPollEvents();

View file

@ -3,8 +3,6 @@
#include <ptk_vk/components.h>
#include <ptk_vk/init.h>
PTK_LIST_DEFINE(PtkHandle);
PTK_LIST(PtkHandle) m_components;
PTK_LIST(Vertex) g_vertices;
@ -22,12 +20,12 @@ void vk_components_init(void) {
g_vertices = PTK_LIST_NEW(Vertex, 1);
}
void _vk_triangle(PtkTriangle *triangle, PtkComponentType type, size_t uvs_offset) {
void triangle(PtkTriangle *triangle, size_t uvs_offset) {
for (size_t i = 0; i < 3; ++i) {
Vertex v = {
.pos = triangle->vertices[i],
.color = triangle->color,
.shape_type = type,
.shape_type = triangle->type,
.uv = uvs[i + uvs_offset],
};
@ -35,14 +33,7 @@ void _vk_triangle(PtkTriangle *triangle, PtkComponentType type, size_t uvs_offse
}
}
void vk_triangle(PtkTriangle *triangle) {
PTK_LIST_ADD(PtkHandle, m_components, triangle);
_vk_triangle(triangle, PTK_COMPONENT_TYPE_TRIANGLE, 0);
vk_transfer_vertex_data();
}
void _vk_rect(PtkRect *rect, PtkComponentType type) {
void rect(PtkRect *rect) {
PtkPos t1_positions[3];
// top left
t1_positions[0] = (PtkPos){ .x = rect->top_left.x, .y = rect->top_left.y };
@ -51,8 +42,9 @@ void _vk_rect(PtkRect *rect, PtkComponentType type) {
// top right
t1_positions[2] = (PtkPos){ .x = rect->top_left.x, .y = rect->top_left.y + rect->size.h };
PtkTriangle *t1 = (PtkTriangle *)ptk_triangle((PtkPos *)t1_positions, rect->color);
t1->type = rect->type;
_vk_triangle(t1, type, 0);
triangle(t1, 0);
PtkPos t2_positions[3];
// bottom left
@ -62,18 +54,13 @@ void _vk_rect(PtkRect *rect, PtkComponentType type) {
// bottom right
t2_positions[2] = (PtkPos){ .x = rect->top_left.x + rect->size.w, .y = rect->top_left.y + rect->size.h };
PtkTriangle *t2 = (PtkTriangle *)ptk_triangle((PtkPos *)t2_positions, rect->color);
t2->type = rect->type;
_vk_triangle(t2, type, 1);
triangle(t2, 1);
vk_transfer_vertex_data();
}
void vk_rect(PtkRect *rect) {
PTK_LIST_ADD(PtkHandle, m_components, rect);
_vk_rect(rect, PTK_COMPONENT_TYPE_RECT);
}
void _vk_ellipse(PtkEllipse *ellipse, PtkComponentType type) {
void ellipse(PtkEllipse *ellipse) {
PtkPos top_left = {
.x = ellipse->center.x - ellipse->radii.w,
.y = ellipse->center.y - ellipse->radii.h,
@ -84,65 +71,55 @@ void _vk_ellipse(PtkEllipse *ellipse, PtkComponentType type) {
.h = ellipse->radii.h * 2.0f,
};
_vk_rect((PtkRect *)ptk_rect(top_left, size, ellipse->color), type);
rect((PtkRect *)ptk_rect(top_left, size, ellipse->color));
}
void vk_ellipse(PtkEllipse *ellipse) {
PTK_LIST_ADD(PtkHandle, m_components, ellipse);
_vk_ellipse(ellipse, PTK_COMPONENT_TYPE_ELLIPSE);
void vk_component(PtkHandle component) {
if (
component->type == PTK_COMPONENT_TYPE_BOX ||
component->type == PTK_COMPONENT_TYPE_CLICKABLE
) {
return;
}
void _vk_box(PtkBox *box, PtkComponentType type) {
for (size_t i = 0; i < box->child_count; ++i) {
PtkHandle child = box->children[i];
switch (child->type) {
case PTK_COMPONENT_TYPE_BOX: {
_vk_box((PtkBox *)child, type);
} break;
case PTK_COMPONENT_TYPE_TRIANGLE: {
_vk_triangle((PtkTriangle *)child, type, 0);
} break;
case PTK_COMPONENT_TYPE_RECT: {
_vk_rect((PtkRect *)child, type);
} break;
case PTK_COMPONENT_TYPE_ELLIPSE: {
_vk_ellipse((PtkEllipse *)child, type);
} break;
PTK_LIST_ADD(PtkHandle, m_components, component);
switch (component->type) {
case PTK_COMPONENT_TYPE_TRIANGLE:
triangle((PtkTriangle *)component, 0);
case PTK_COMPONENT_TYPE_RECT:
rect((PtkRect *)component);
case PTK_COMPONENT_TYPE_ELLIPSE:
ellipse((PtkEllipse *)component);
default:
return;
}
}
vk_transfer_vertex_data();
}
void vk_box(PtkBox *box) {
PTK_LIST_ADD(PtkHandle, m_components, box);
void vk_clickable(PtkClickable *clickable) {
PTK_LIST_ADD(PtkHandle, m_components, clickable);
_vk_box(box, PTK_COMPONENT_TYPE_BOX);
}
void vk_button(PtkButton *button) {
PTK_LIST_ADD(PtkHandle, m_components, button);
switch (button->hitbox->type) {
case PTK_COMPONENT_TYPE_BOX: {
_vk_box((PtkBox *)button->hitbox, PTK_COMPONENT_TYPE_BUTTON);
} break;
switch (clickable->hitbox->type) {
// case PTK_COMPONENT_TYPE_BOX: {
// _vk_box((PtkComponent *)clickable->hitbox);
// } break;
case PTK_COMPONENT_TYPE_TRIANGLE: {
_vk_triangle((PtkTriangle *)button->hitbox, PTK_COMPONENT_TYPE_BUTTON, 0);
triangle((PtkTriangle *)clickable->hitbox, 0);
} break;
case PTK_COMPONENT_TYPE_RECT: {
_vk_rect((PtkRect *)button->hitbox, PTK_COMPONENT_TYPE_BUTTON);
rect((PtkRect *)clickable->hitbox);
} break;
case PTK_COMPONENT_TYPE_ELLIPSE: {
_vk_ellipse((PtkEllipse *)button->hitbox, PTK_COMPONENT_TYPE_BUTTON);
ellipse((PtkEllipse *)clickable->hitbox);
} break;
default:
return;
}
}
bool triangle_intersects(PtkTriangle t, PtkPos p) {
inline bool triangle_intersects(PtkTriangle t, PtkPos p) {
PtkPos p0 = t.vertices[0];
PtkPos p1 = t.vertices[1];
PtkPos p2 = t.vertices[2];
@ -154,7 +131,7 @@ bool triangle_intersects(PtkTriangle t, PtkPos p) {
return s > 0 && r > 0 && (s + r) < 2 * a * sign;
}
bool rect_intersects(PtkRect r, PtkPos p) {
inline bool rect_intersects(PtkRect r, PtkPos p) {
bool intersects_x = r.top_left.x <= p.x
&& r.top_left.x + r.top_left.w >= p.x;
@ -164,7 +141,7 @@ bool rect_intersects(PtkRect r, PtkPos p) {
return intersects_x && intersects_y;
}
bool ellipse_intersects(PtkEllipse e, PtkPos p) {
inline bool ellipse_intersects(PtkEllipse e, PtkPos p) {
float x = p.x - e.center.x;
float rx = e.radii.x;
float y = p.y - e.center.y;
@ -175,9 +152,9 @@ bool ellipse_intersects(PtkEllipse e, PtkPos p) {
bool intersects(PtkHandle component, PtkPos point) {
switch (component->type) {
case PTK_COMPONENT_TYPE_BOX: {
PtkBox *box = (PtkBox *)component;
for (size_t i = 0; i < box->child_count; ++i) {
if (intersects(box->children[i], point)) {
PtkComponent *box = (PtkComponent *)component;
for (size_t i = 0; i < box->children.size; ++i) {
if (intersects(box->children.data[i], point)) {
return true;
}
}
@ -190,22 +167,22 @@ bool intersects(PtkHandle component, PtkPos point) {
return rect_intersects(*(PtkRect *)component, point);
case PTK_COMPONENT_TYPE_ELLIPSE:
return ellipse_intersects(*(PtkEllipse *)component, point);
case PTK_COMPONENT_TYPE_BUTTON:
return intersects(((PtkButton *)component)->hitbox, point);
case PTK_COMPONENT_TYPE_CLICKABLE:
return intersects(((PtkClickable *)component)->hitbox, point);
}
}
void vk_handle_mouse_button_input(PtkPos cursor_pos, int button, int action, int mods) {
for (size_t i = 0; i < m_components.size; ++i) {
PtkHandle current = m_components.data[i];
if (current->type != PTK_COMPONENT_TYPE_BUTTON) {
if (current->type != PTK_COMPONENT_TYPE_CLICKABLE) {
continue;
}
PtkButton *b = (PtkButton *)current;
PtkClickable *c = (PtkClickable *)current;
if (intersects((PtkHandle)b, cursor_pos)) {
b->on_press(button, action, mods);
if (intersects((PtkHandle)c, cursor_pos)) {
c->on_press(button, action, mods);
}
}
}

View file

@ -17,11 +17,7 @@ extern PTK_LIST(Vertex) g_vertices;
void vk_components_init(void);
void vk_box(PtkBox *box);
void vk_triangle(PtkTriangle *triangle);
void vk_rect(PtkRect *rect);
void vk_ellipse(PtkEllipse *ellipse);
void vk_button(PtkButton *button);
void vk_component(PtkHandle component);
void vk_handle_mouse_button_input(PtkPos cursor_pos, int button, int action, int mode);