@@ -129,6 +129,13 @@ typedef struct {
129
129
gl_float_t b ;
130
130
} gl_color_t ;
131
131
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
+
132
139
int file_exists (const char * fp );
133
140
char * load_file (const char * fp );
134
141
@@ -575,12 +582,7 @@ typedef struct {
575
582
gl_color_t color ;
576
583
gl_color_t color_hover ;
577
584
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 ;
584
586
585
587
gl_uint_t program_id ;
586
588
gl_uint_t vao ;
@@ -589,7 +591,7 @@ typedef struct {
589
591
} ui_button_t ;
590
592
void ui_button_setup (ui_button_t * button );
591
593
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 );
593
595
594
596
#endif // GUI_H
595
597
@@ -3582,7 +3584,7 @@ void gui_loop(gui_t *gui) {
3582
3584
// gl_points3d_draw(points, &gui->camera, num_points);
3583
3585
// gl_text_draw(text, &gui->camera, "Here!", 10.0f, 100.0f, 1.0f);
3584
3586
// gl_image_draw(image);
3585
- ui_button_draw (& button );
3587
+ ui_button_draw (gui , & button );
3586
3588
3587
3589
// Update
3588
3590
glfwPollEvents ();
@@ -3639,12 +3641,10 @@ void ui_button_setup(ui_button_t *button) {
3639
3641
button -> color = (gl_color_t ){1.0f , 1.0f , 1.0f };
3640
3642
button -> color_hover = (gl_color_t ){1.0f , 0.0f , 0.0f };
3641
3643
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 ;
3648
3648
3649
3649
button -> program_id = -1 ;
3650
3650
button -> vao = -1 ;
@@ -3659,11 +3659,15 @@ void ui_button_setup(ui_button_t *button) {
3659
3659
3660
3660
// Vertices
3661
3661
// 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 ;
3662
3666
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
3667
3671
// clang-format on
3668
3672
3669
3673
// VAO
@@ -3691,18 +3695,41 @@ void ui_button_cleanup(const ui_button_t *button) {
3691
3695
glDeleteBuffers (1 , & button -> vbo );
3692
3696
}
3693
3697
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
3695
3722
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 );
3701
3728
glBindVertexArray (button -> vao );
3702
3729
glDrawArrays (GL_TRIANGLE_FAN , 0 , 4 );
3703
3730
glBindVertexArray (0 );
3704
3731
3705
- return 0 ;
3732
+ return button_on ;
3706
3733
}
3707
3734
3708
3735
#endif // GUI_IMPLEMENTATION
0 commit comments