make on_press only activate when action is GLFW_PRESS

This commit is contained in:
jacekpoz 2024-08-13 17:01:03 +02:00
parent e2a0e79df8
commit 061e5799f9
Signed by: poz
SSH key fingerprint: SHA256:JyLeVWE4bF3tDnFeUpUaJsPsNlJyBldDGV/dIKSLyN8
3 changed files with 19 additions and 15 deletions

View file

@ -8,22 +8,20 @@ static const float color_step = 10.0f;
static PtkRect *r; static PtkRect *r;
static void on_press(const int button, const int action, const int mods) { static void on_press(const int button, const int mods) {
(void)button; (void)mods; (void)button; (void)mods;
if (action == GLFW_PRESS) { PTK_DEBUG("pressed!");
PTK_DEBUG("pressed!"); r->color.b += color_step / 255.0f;
r->color.b += color_step / 255.0f; if (r->color.b > 1.0f) {
if (r->color.b > 1.0f) { r->color.b = 0.0f;
r->color.b = 0.0f;
r->color.g += color_step / 255.0f; r->color.g += color_step / 255.0f;
if (r->color.g > 1.0f) { if (r->color.g > 1.0f) {
r->color.g = 0.0f; r->color.g = 0.0f;
r->color.r += color_step / 255.0f; r->color.r += color_step / 255.0f;
if (r->color.r > 1.0f) { if (r->color.r > 1.0f) {
r->color.r = 0.0f; r->color.r = 0.0f;
}
} }
} }
} }

View file

@ -56,7 +56,7 @@ PTK_COMPONENT_DEFINE(PtkEllipse,
PtkColor color; PtkColor color;
); );
typedef void (*MouseButtonCallback)(const int button, const int action, const int mods); typedef void (*MouseButtonCallback)(const int button, const int mods);
PTK_COMPONENT_DEFINE(PtkClickable, PTK_COMPONENT_DEFINE(PtkClickable,
MouseButtonCallback on_press; MouseButtonCallback on_press;

View file

@ -5,6 +5,8 @@
#include <ptk_vk/components.h> #include <ptk_vk/components.h>
#include <ptk_vk/init.h> #include <ptk_vk/init.h>
#include <GLFW/glfw3.h>
static PtkHandle m_root_component; static PtkHandle m_root_component;
static PTK_LIST(PtkHandle) m_components; static PTK_LIST(PtkHandle) m_components;
@ -171,6 +173,10 @@ bool intersects(const PtkHandle component, const PtkPos point) {
} }
void vk_handle_mouse_button_input(const PtkPos cursor_pos, const int button, const int action, const int mods) { void vk_handle_mouse_button_input(const PtkPos cursor_pos, const int button, const int action, const int mods) {
if (action == GLFW_RELEASE) {
return;
}
for (size_t i = 0; i < m_components.size; ++i) { for (size_t i = 0; i < m_components.size; ++i) {
const PtkHandle current = m_components.data[i]; const PtkHandle current = m_components.data[i];
if (current->type != PTK_COMPONENT_TYPE_CLICKABLE) { if (current->type != PTK_COMPONENT_TYPE_CLICKABLE) {
@ -180,7 +186,7 @@ void vk_handle_mouse_button_input(const PtkPos cursor_pos, const int button, con
const PtkClickable *c = (PtkClickable *)current; const PtkClickable *c = (PtkClickable *)current;
if (intersects((PtkHandle)c, cursor_pos)) { if (intersects((PtkHandle)c, cursor_pos)) {
c->on_press(button, action, mods); c->on_press(button, mods);
vk_update_components(); vk_update_components();
} }
} }