Skip to content

Commit 23184a0

Browse files
committed
Add working button
1 parent 183d42f commit 23184a0

File tree

1 file changed

+52
-25
lines changed

1 file changed

+52
-25
lines changed

src/gui.h

+52-25
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ typedef struct {
129129
gl_float_t b;
130130
} gl_color_t;
131131

132+
typedef struct {
133+
gl_int_t x;
134+
gl_int_t y;
135+
gl_int_t w;
136+
gl_int_t h;
137+
} gl_bounds_t;
138+
132139
int file_exists(const char *fp);
133140
char *load_file(const char *fp);
134141

@@ -575,12 +582,7 @@ typedef struct {
575582
gl_color_t color;
576583
gl_color_t color_hover;
577584
gl_color_t color_press;
578-
int x;
579-
int y;
580-
float width;
581-
float height;
582-
int is_pressed;
583-
int is_hovered;
585+
gl_bounds_t bounds;
584586

585587
gl_uint_t program_id;
586588
gl_uint_t vao;
@@ -589,7 +591,7 @@ typedef struct {
589591
} ui_button_t;
590592
void ui_button_setup(ui_button_t *button);
591593
void ui_button_cleanup(const ui_button_t *button);
592-
int ui_button_draw(const ui_button_t *button);
594+
int ui_button_draw(const gui_t *gui, const ui_button_t *button);
593595

594596
#endif // GUI_H
595597

@@ -3582,7 +3584,7 @@ void gui_loop(gui_t *gui) {
35823584
// gl_points3d_draw(points, &gui->camera, num_points);
35833585
// gl_text_draw(text, &gui->camera, "Here!", 10.0f, 100.0f, 1.0f);
35843586
// gl_image_draw(image);
3585-
ui_button_draw(&button);
3587+
ui_button_draw(gui, &button);
35863588

35873589
// Update
35883590
glfwPollEvents();
@@ -3639,12 +3641,10 @@ void ui_button_setup(ui_button_t *button) {
36393641
button->color = (gl_color_t){1.0f, 1.0f, 1.0f};
36403642
button->color_hover = (gl_color_t){1.0f, 0.0f, 0.0f};
36413643
button->color_press = (gl_color_t){0.0f, 0.0f, 1.0f};
3642-
button->x = 20;
3643-
button->y = 600;
3644-
button->width = 200.0f;
3645-
button->height = 100.0f;
3646-
button->is_pressed = 0;
3647-
button->is_hovered = 0;
3644+
button->bounds.x = 20;
3645+
button->bounds.y = 600;
3646+
button->bounds.w = 200.0f;
3647+
button->bounds.h = 100.0f;
36483648

36493649
button->program_id = -1;
36503650
button->vao = -1;
@@ -3659,11 +3659,15 @@ void ui_button_setup(ui_button_t *button) {
36593659

36603660
// Vertices
36613661
// clang-format off
3662+
const int x = button->bounds.x;
3663+
const int y = button->bounds.y;
3664+
const int w = button->bounds.w;
3665+
const int h = button->bounds.h;
36623666
float vertices[4 * 2] = {0};
3663-
pixel2ndc(button->x , button->y + button->height, 1024, 768, &vertices[0], &vertices[1]); // Bottom left
3664-
pixel2ndc(button->x + button->width, button->y + button->height, 1024, 768, &vertices[2], &vertices[3]); // Bottom right
3665-
pixel2ndc(button->x + button->width, button->y , 1024, 768, &vertices[4], &vertices[5]); // Top right
3666-
pixel2ndc(button->x , button->y , 1024, 768, &vertices[6], &vertices[7]); // Top left
3667+
pixel2ndc(x , y + h, 1024, 768, &vertices[0], &vertices[1]); // Bottom left
3668+
pixel2ndc(x + w, y + h, 1024, 768, &vertices[2], &vertices[3]); // Bottom right
3669+
pixel2ndc(x + w, y , 1024, 768, &vertices[4], &vertices[5]); // Top right
3670+
pixel2ndc(x , y , 1024, 768, &vertices[6], &vertices[7]); // Top left
36673671
// clang-format on
36683672

36693673
// VAO
@@ -3691,18 +3695,41 @@ void ui_button_cleanup(const ui_button_t *button) {
36913695
glDeleteBuffers(1, &button->vbo);
36923696
}
36933697

3694-
int ui_button_draw(const ui_button_t *button) {
3698+
static int ui_intercept(const gui_t *gui, const gl_bounds_t *bounds) {
3699+
const int x = bounds->x;
3700+
const int y = bounds->y;
3701+
const int w = bounds->w;
3702+
const int h = bounds->h;
3703+
int within_x = (x <= gui->cursor_x && gui->cursor_x <= x + w);
3704+
int within_y = (y <= gui->cursor_y && gui->cursor_y <= y + h);
3705+
return (within_x && within_y) ? 1 : 0;
3706+
}
3707+
3708+
int ui_button_draw(const gui_t *gui, const ui_button_t *button) {
3709+
// Button color
3710+
const int button_hover = ui_intercept(gui, &button->bounds);
3711+
const int button_pressed = gui->left_click;
3712+
int button_on = 0;
3713+
gl_color_t color = button->color;
3714+
if (button_hover == 1 && button_pressed == 0) {
3715+
color = button->color_hover;
3716+
} else if (button_hover == 1 && button_pressed == 1) {
3717+
color = button->color_press;
3718+
button_on = 1;
3719+
}
3720+
3721+
// Shader program
36953722
glUseProgram(button->program_id);
3696-
gl_prog_set_color(button->program_id, "color", button->color);
3697-
gl_prog_set_float(button->program_id, "window_width", 1024.0f);
3698-
gl_prog_set_float(button->program_id, "window_height", 768.0f);
3699-
gl_prog_set_float(button->program_id, "button_width", button->width);
3700-
gl_prog_set_float(button->program_id, "button_height", button->height);
3723+
gl_prog_set_color(button->program_id, "color", color);
3724+
gl_prog_set_float(button->program_id, "window_width", gui->window_width);
3725+
gl_prog_set_float(button->program_id, "window_height", gui->window_height);
3726+
gl_prog_set_float(button->program_id, "button_width", button->bounds.w);
3727+
gl_prog_set_float(button->program_id, "button_height", button->bounds.h);
37013728
glBindVertexArray(button->vao);
37023729
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
37033730
glBindVertexArray(0);
37043731

3705-
return 0;
3732+
return button_on;
37063733
}
37073734

37083735
#endif // GUI_IMPLEMENTATION

0 commit comments

Comments
 (0)