buttons darnek when held and sliders can be controlled through mouse
This commit is contained in:
parent
97a1427a02
commit
75cc8da7ee
3 changed files with 44 additions and 12 deletions
41
mmn_17.c
41
mmn_17.c
|
@ -7,16 +7,18 @@
|
|||
#include <math.h>
|
||||
#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,20 +100,42 @@ 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, button, state);
|
||||
ui_slider_onclick(s, x, y);
|
||||
draggedSlider = s;
|
||||
}
|
||||
}
|
||||
}
|
||||
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) {
|
||||
for(ui_slider* s = sliders; s < sliders + sizeof sliders / sizeof(ui_slider); s += 1) {
|
||||
|
@ -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);
|
||||
|
|
10
ui.c
10
ui.c
|
@ -7,6 +7,7 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
|
|
3
ui.h
3
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
|
Loading…
Reference in a new issue