a few component changes before separating them from shapes
This commit is contained in:
parent
4b1fdf8e04
commit
83c7968d48
3 changed files with 59 additions and 97 deletions
25
src/ptk.c
25
src/ptk.c
|
@ -185,28 +185,17 @@ PtkHandle ptk_clickable(PtkHandle hitbox, MouseButtonCallback on_press) {
|
||||||
return (PtkHandle)ret;
|
return (PtkHandle)ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void init_components(PtkHandle root) {
|
void init_component(PtkHandle component) {
|
||||||
switch (root->type) {
|
vk_component(component);
|
||||||
case PTK_COMPONENT_TYPE_BOX: {
|
for (size_t i = 0; i < component->children.size; ++i) {
|
||||||
vk_box((PtkBox *)root);
|
PtkHandle child = component->children.data[i];
|
||||||
} break;
|
child->parent = component;
|
||||||
case PTK_COMPONENT_TYPE_TRIANGLE: {
|
init_component(child);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int ptk_run(PtkHandle root) {
|
int ptk_run(PtkHandle root) {
|
||||||
init_components(root);
|
init_component(root);
|
||||||
|
|
||||||
while (!glfwWindowShouldClose(g_window)) {
|
while (!glfwWindowShouldClose(g_window)) {
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
|
|
|
@ -3,8 +3,6 @@
|
||||||
#include <ptk_vk/components.h>
|
#include <ptk_vk/components.h>
|
||||||
#include <ptk_vk/init.h>
|
#include <ptk_vk/init.h>
|
||||||
|
|
||||||
PTK_LIST_DEFINE(PtkHandle);
|
|
||||||
|
|
||||||
PTK_LIST(PtkHandle) m_components;
|
PTK_LIST(PtkHandle) m_components;
|
||||||
|
|
||||||
PTK_LIST(Vertex) g_vertices;
|
PTK_LIST(Vertex) g_vertices;
|
||||||
|
@ -22,12 +20,12 @@ void vk_components_init(void) {
|
||||||
g_vertices = PTK_LIST_NEW(Vertex, 1);
|
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) {
|
for (size_t i = 0; i < 3; ++i) {
|
||||||
Vertex v = {
|
Vertex v = {
|
||||||
.pos = triangle->vertices[i],
|
.pos = triangle->vertices[i],
|
||||||
.color = triangle->color,
|
.color = triangle->color,
|
||||||
.shape_type = type,
|
.shape_type = triangle->type,
|
||||||
.uv = uvs[i + uvs_offset],
|
.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) {
|
void rect(PtkRect *rect) {
|
||||||
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) {
|
|
||||||
PtkPos t1_positions[3];
|
PtkPos t1_positions[3];
|
||||||
// top left
|
// top left
|
||||||
t1_positions[0] = (PtkPos){ .x = rect->top_left.x, .y = rect->top_left.y };
|
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
|
// top right
|
||||||
t1_positions[2] = (PtkPos){ .x = rect->top_left.x, .y = rect->top_left.y + rect->size.h };
|
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);
|
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];
|
PtkPos t2_positions[3];
|
||||||
// bottom left
|
// bottom left
|
||||||
|
@ -62,18 +54,13 @@ void _vk_rect(PtkRect *rect, PtkComponentType type) {
|
||||||
// bottom right
|
// bottom right
|
||||||
t2_positions[2] = (PtkPos){ .x = rect->top_left.x + rect->size.w, .y = rect->top_left.y + rect->size.h };
|
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);
|
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();
|
vk_transfer_vertex_data();
|
||||||
}
|
}
|
||||||
|
|
||||||
void vk_rect(PtkRect *rect) {
|
void ellipse(PtkEllipse *ellipse) {
|
||||||
PTK_LIST_ADD(PtkHandle, m_components, rect);
|
|
||||||
|
|
||||||
_vk_rect(rect, PTK_COMPONENT_TYPE_RECT);
|
|
||||||
}
|
|
||||||
|
|
||||||
void _vk_ellipse(PtkEllipse *ellipse, PtkComponentType type) {
|
|
||||||
PtkPos top_left = {
|
PtkPos top_left = {
|
||||||
.x = ellipse->center.x - ellipse->radii.w,
|
.x = ellipse->center.x - ellipse->radii.w,
|
||||||
.y = ellipse->center.y - ellipse->radii.h,
|
.y = ellipse->center.y - ellipse->radii.h,
|
||||||
|
@ -84,65 +71,55 @@ void _vk_ellipse(PtkEllipse *ellipse, PtkComponentType type) {
|
||||||
.h = ellipse->radii.h * 2.0f,
|
.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) {
|
void vk_component(PtkHandle component) {
|
||||||
PTK_LIST_ADD(PtkHandle, m_components, ellipse);
|
if (
|
||||||
|
component->type == PTK_COMPONENT_TYPE_BOX ||
|
||||||
|
component->type == PTK_COMPONENT_TYPE_CLICKABLE
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
_vk_ellipse(ellipse, PTK_COMPONENT_TYPE_ELLIPSE);
|
PTK_LIST_ADD(PtkHandle, m_components, component);
|
||||||
}
|
|
||||||
|
|
||||||
void _vk_box(PtkBox *box, PtkComponentType type) {
|
switch (component->type) {
|
||||||
for (size_t i = 0; i < box->child_count; ++i) {
|
case PTK_COMPONENT_TYPE_TRIANGLE:
|
||||||
PtkHandle child = box->children[i];
|
triangle((PtkTriangle *)component, 0);
|
||||||
switch (child->type) {
|
case PTK_COMPONENT_TYPE_RECT:
|
||||||
case PTK_COMPONENT_TYPE_BOX: {
|
rect((PtkRect *)component);
|
||||||
_vk_box((PtkBox *)child, type);
|
case PTK_COMPONENT_TYPE_ELLIPSE:
|
||||||
} break;
|
ellipse((PtkEllipse *)component);
|
||||||
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;
|
|
||||||
default:
|
default:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
vk_transfer_vertex_data();
|
||||||
}
|
}
|
||||||
|
|
||||||
void vk_box(PtkBox *box) {
|
void vk_clickable(PtkClickable *clickable) {
|
||||||
PTK_LIST_ADD(PtkHandle, m_components, box);
|
PTK_LIST_ADD(PtkHandle, m_components, clickable);
|
||||||
|
|
||||||
_vk_box(box, PTK_COMPONENT_TYPE_BOX);
|
switch (clickable->hitbox->type) {
|
||||||
}
|
// case PTK_COMPONENT_TYPE_BOX: {
|
||||||
|
// _vk_box((PtkComponent *)clickable->hitbox);
|
||||||
void vk_button(PtkButton *button) {
|
// } break;
|
||||||
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;
|
|
||||||
case PTK_COMPONENT_TYPE_TRIANGLE: {
|
case PTK_COMPONENT_TYPE_TRIANGLE: {
|
||||||
_vk_triangle((PtkTriangle *)button->hitbox, PTK_COMPONENT_TYPE_BUTTON, 0);
|
triangle((PtkTriangle *)clickable->hitbox, 0);
|
||||||
} break;
|
} break;
|
||||||
case PTK_COMPONENT_TYPE_RECT: {
|
case PTK_COMPONENT_TYPE_RECT: {
|
||||||
_vk_rect((PtkRect *)button->hitbox, PTK_COMPONENT_TYPE_BUTTON);
|
rect((PtkRect *)clickable->hitbox);
|
||||||
} break;
|
} break;
|
||||||
case PTK_COMPONENT_TYPE_ELLIPSE: {
|
case PTK_COMPONENT_TYPE_ELLIPSE: {
|
||||||
_vk_ellipse((PtkEllipse *)button->hitbox, PTK_COMPONENT_TYPE_BUTTON);
|
ellipse((PtkEllipse *)clickable->hitbox);
|
||||||
} break;
|
} break;
|
||||||
default:
|
default:
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool triangle_intersects(PtkTriangle t, PtkPos p) {
|
inline bool triangle_intersects(PtkTriangle t, PtkPos p) {
|
||||||
PtkPos p0 = t.vertices[0];
|
PtkPos p0 = t.vertices[0];
|
||||||
PtkPos p1 = t.vertices[1];
|
PtkPos p1 = t.vertices[1];
|
||||||
PtkPos p2 = t.vertices[2];
|
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;
|
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
|
bool intersects_x = r.top_left.x <= p.x
|
||||||
&& r.top_left.x + r.top_left.w >= 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;
|
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 x = p.x - e.center.x;
|
||||||
float rx = e.radii.x;
|
float rx = e.radii.x;
|
||||||
float y = p.y - e.center.y;
|
float y = p.y - e.center.y;
|
||||||
|
@ -175,9 +152,9 @@ bool ellipse_intersects(PtkEllipse e, PtkPos p) {
|
||||||
bool intersects(PtkHandle component, PtkPos point) {
|
bool intersects(PtkHandle component, PtkPos point) {
|
||||||
switch (component->type) {
|
switch (component->type) {
|
||||||
case PTK_COMPONENT_TYPE_BOX: {
|
case PTK_COMPONENT_TYPE_BOX: {
|
||||||
PtkBox *box = (PtkBox *)component;
|
PtkComponent *box = (PtkComponent *)component;
|
||||||
for (size_t i = 0; i < box->child_count; ++i) {
|
for (size_t i = 0; i < box->children.size; ++i) {
|
||||||
if (intersects(box->children[i], point)) {
|
if (intersects(box->children.data[i], point)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -190,22 +167,22 @@ bool intersects(PtkHandle component, PtkPos point) {
|
||||||
return rect_intersects(*(PtkRect *)component, point);
|
return rect_intersects(*(PtkRect *)component, point);
|
||||||
case PTK_COMPONENT_TYPE_ELLIPSE:
|
case PTK_COMPONENT_TYPE_ELLIPSE:
|
||||||
return ellipse_intersects(*(PtkEllipse *)component, point);
|
return ellipse_intersects(*(PtkEllipse *)component, point);
|
||||||
case PTK_COMPONENT_TYPE_BUTTON:
|
case PTK_COMPONENT_TYPE_CLICKABLE:
|
||||||
return intersects(((PtkButton *)component)->hitbox, point);
|
return intersects(((PtkClickable *)component)->hitbox, point);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void vk_handle_mouse_button_input(PtkPos cursor_pos, int button, int action, int mods) {
|
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) {
|
for (size_t i = 0; i < m_components.size; ++i) {
|
||||||
PtkHandle current = m_components.data[i];
|
PtkHandle current = m_components.data[i];
|
||||||
if (current->type != PTK_COMPONENT_TYPE_BUTTON) {
|
if (current->type != PTK_COMPONENT_TYPE_CLICKABLE) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
PtkButton *b = (PtkButton *)current;
|
PtkClickable *c = (PtkClickable *)current;
|
||||||
|
|
||||||
if (intersects((PtkHandle)b, cursor_pos)) {
|
if (intersects((PtkHandle)c, cursor_pos)) {
|
||||||
b->on_press(button, action, mods);
|
c->on_press(button, action, mods);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,11 +17,7 @@ extern PTK_LIST(Vertex) g_vertices;
|
||||||
|
|
||||||
void vk_components_init(void);
|
void vk_components_init(void);
|
||||||
|
|
||||||
void vk_box(PtkBox *box);
|
void vk_component(PtkHandle component);
|
||||||
void vk_triangle(PtkTriangle *triangle);
|
|
||||||
void vk_rect(PtkRect *rect);
|
|
||||||
void vk_ellipse(PtkEllipse *ellipse);
|
|
||||||
void vk_button(PtkButton *button);
|
|
||||||
|
|
||||||
void vk_handle_mouse_button_input(PtkPos cursor_pos, int button, int action, int mode);
|
void vk_handle_mouse_button_input(PtkPos cursor_pos, int button, int action, int mode);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue