From 75cc8da7ee8abdcb41b5b62395d71ce069b8a8d0 Mon Sep 17 00:00:00 2001 From: Rusty Striker Date: Thu, 6 Jul 2023 11:24:46 +0300 Subject: [PATCH] buttons darnek when held and sliders can be controlled through mouse --- mmn_17.c | 43 ++++++++++++++++++++++++++++++++++--------- ui.c | 10 ++++++++-- ui.h | 3 ++- 3 files changed, 44 insertions(+), 12 deletions(-) diff --git a/mmn_17.c b/mmn_17.c index eef594c..4bdec15 100644 --- a/mmn_17.c +++ b/mmn_17.c @@ -7,16 +7,18 @@ #include #include "ui.h" -void testButtonClick(); - +int lastMouseButton = GLUT_LEFT_BUTTON; +ui_slider* draggedSlider = NULL; // Ui stuff +void testButtonClick(); ui_button buttons[] = { { /* pos */ { { 0.5, 0.5, 0.0 }, { -50.0, -25.0, 0.0 } }, /* size */ { 100.0, 50.0, 0.0 }, /* onclick */ testButtonClick, - /* text */ "Test :)" + /* text */ "Test :)", + 0 } }; ui_slider sliders[] = { @@ -98,19 +100,41 @@ void display(void) { } void mouseEvent(int button, int state, int x, int y) { - if(state == 0) { + if(state == 0) lastMouseButton = button; + if(state == 0 && button == GLUT_LEFT_BUTTON) { for(ui_button* b = buttons; b < buttons + sizeof buttons / sizeof(ui_button); b += 1) { if(ui_button_mouse_over(b, x, y) && b->onClick != NULL) { - b->onClick(); - return; + b->clicked = 1; + } + } + for(ui_slider* s = sliders; s < sliders + sizeof sliders / sizeof(ui_slider); s += 1) { + if(ui_slider_mouse_over(s, x, y)) { + ui_slider_onclick(s, x, y); + draggedSlider = s; } } } - for(ui_slider* s = sliders; s < sliders + sizeof sliders / sizeof(ui_slider); s += 1) { - if(ui_slider_mouse_over(s, x, y)) { - ui_slider_onclick(s, x, y, button, state); + else if(state == 1 && button == GLUT_LEFT_BUTTON) { + draggedSlider = NULL; + for(ui_button* b = buttons; b < buttons + sizeof buttons / sizeof(ui_button); b += 1) { + b->clicked = 0; + if(ui_button_mouse_over(b, x, y) && b->onClick != NULL) { + b->onClick(); + } } } + glutPostRedisplay(); +} + +void mouseMotionEvent(int x, int y) { + if(lastMouseButton == GLUT_LEFT_BUTTON) { + if(draggedSlider != NULL) { + if(ui_slider_mouse_over(draggedSlider, x, y)) { + ui_slider_onclick(draggedSlider, x, y); + } + } + } + glutPostRedisplay(); } void mouseWheelEvent(int wheel, int dir, int x, int y) { @@ -147,6 +171,7 @@ int main(int argc, char** argv) { glutMouseFunc(mouseEvent); glutKeyboardFunc(keyboardEvent); glutMouseWheelFunc(mouseWheelEvent); + glutMotionFunc(mouseMotionEvent); /* set projection and camera */ glMatrixMode(GL_PROJECTION); diff --git a/ui.c b/ui.c index b217500..8b39c04 100644 --- a/ui.c +++ b/ui.c @@ -7,6 +7,7 @@ #include #define BACK_COLOR 0.3, 0.35, 0.35 +#define BACK_CLICKED_COLOR 0.25, 0.3, 0.3 void ui_button_draw(ui_button* b) { int wWidth = glutGet(GLUT_WINDOW_WIDTH); @@ -14,7 +15,7 @@ void ui_button_draw(ui_button* b) { vec3 pos = vec3_add(b->position.absolute, vec3_new(b->position.relative.x * wWidth, b->position.relative.y * wHeight, 0.0)); vec3 ext = b->size; - glColor3f(BACK_COLOR); + glColorVec(b->clicked ? vec3_new(BACK_CLICKED_COLOR) : vec3_new(BACK_COLOR)); glBegin(GL_QUADS); glVertexVec(pos); glVertex3f(pos.x + ext.x, pos.y, pos.z); @@ -89,6 +90,11 @@ char ui_slider_mouse_over(ui_slider* s, int mouseX, int mouseY) { return m.x > 0.0 && m.x < ext.x && m.y > 0.0 && m.y < ext.y; } -void ui_slider_onclick(ui_slider* s, int mouseX, int mouseY, int button, int state) { +void ui_slider_onclick(ui_slider* s, int mouseX, int mouseY) { + // we assume mouse is already hovering + int wWidth = glutGet(GLUT_WINDOW_WIDTH); + int wHeight = glutGet(GLUT_WINDOW_HEIGHT); + vec3 pos = vec3_add(s->position.absolute, vec3_new(s->position.relative.x * wWidth, s->position.relative.y * wHeight, 0.0)); + s->value = ((float)mouseX - pos.x) / s->size.x; } diff --git a/ui.h b/ui.h index 41e6225..5e2a26b 100644 --- a/ui.h +++ b/ui.h @@ -13,6 +13,7 @@ typedef struct _ui_button { vec3 size; // size in pixels - if screen is too small i am not sure if it is a smart idea to make everything smaller void (*onClick)(); // instead of passing stuff in, im just gonna put every state variable in global scope to ensure everything lives... char* text; + char clicked; } ui_button; typedef struct _ui_slider { @@ -27,6 +28,6 @@ char ui_button_mouse_over(ui_button* b, int mouseX, int mouseY); void ui_slider_draw(ui_slider* s); char ui_slider_mouse_over(ui_slider* s, int mouseX, int mouseY); -void ui_slider_onclick(ui_slider* s, int mouseX, int mouseY, int button, int state); +void ui_slider_onclick(ui_slider* s, int mouseX, int mouseY); #endif \ No newline at end of file