diff --git a/engine/gf_draw_common.c b/engine/gf_draw_common.c index b81c83e..2d6a104 100644 --- a/engine/gf_draw_common.c +++ b/engine/gf_draw_common.c @@ -81,15 +81,23 @@ void gf_draw_reshape(gf_draw_t* draw) { gf_draw_driver_reshape(draw); } gf_gui_id_t button1 = -1; gf_gui_id_t button2 = -1; +void gf_button_callback(gf_engine_t* engine, gf_draw_t* draw, gf_gui_id_t id, int type) { + if(type == GF_GUI_PRESS) { + gf_log_function(engine, "GUI component %d was pressed", id); + } +} + /* Runs every frame */ void gf_draw_frame(gf_draw_t* draw) { gf_graphic_color_t color; color.r = color.g = color.b = color.a = 255; if(button1 == -1) { button1 = gf_gui_create_button(draw->gui, 0, 0, 200, 50, "\"Test\" text"); + gf_gui_set_callback(draw->gui, button1, gf_button_callback); } if(button2 == -1) { button2 = gf_gui_create_button(draw->gui, 100, 25, 200, 50, "\"Test\" text"); + gf_gui_set_callback(draw->gui, button2, gf_button_callback); } if(draw->draw_3d) { gf_graphic_draw_texture_polygon(draw, test_texture, color, GF_GRAPHIC_3D, 4, diff --git a/engine/gf_gui.c b/engine/gf_gui.c index df962f8..ffd0b4c 100644 --- a/engine/gf_gui.c +++ b/engine/gf_gui.c @@ -84,7 +84,9 @@ gf_gui_id_t gf_gui_create_button(gf_gui_t* gui, double x, double y, double w, do c->width = w; c->height = h; - c->pressed = 0; + c->pressed = 0; + c->callback = NULL; + c->u.button.text = malloc(strlen(text) + 1); strcpy(c->u.button.text, text); return id; @@ -127,8 +129,12 @@ void gf_gui_render(gf_gui_t* gui) { } } if((gui->pressed != -1) && !(input->mouse_flag & GF_INPUT_MOUSE_LEFT_MASK)) { - gf_log_function(gui->engine, "GUI component %d was pressed", gui->pressed); + if(gui->area[gui->pressed].callback != NULL) { + gui->area[gui->pressed].callback(gui->engine, gui->draw, gui->pressed, GF_GUI_PRESS); + } gui->area[gui->pressed].pressed = 1; gui->pressed = -1; } } + +void gf_gui_set_callback(gf_gui_t* gui, gf_gui_id_t id, gf_gui_callback_t callback) { gui->area[id].callback = callback; } diff --git a/engine/include/gf_gui.h b/engine/include/gf_gui.h index 699eec1..14903a0 100644 --- a/engine/include/gf_gui.h +++ b/engine/include/gf_gui.h @@ -74,7 +74,7 @@ GF_EXPORT void gf_gui_render(gf_gui_t* gui); * @param id Component ID to be returned * @return Pointer to component */ -gf_gui_component_t* gf_gui_first_unused(gf_gui_t* gui, gf_gui_id_t* id); +GF_EXPORT gf_gui_component_t* gf_gui_first_unused(gf_gui_t* gui, gf_gui_id_t* id); /** * @~english @@ -88,4 +88,13 @@ gf_gui_component_t* gf_gui_first_unused(gf_gui_t* gui, gf_gui_id_t* id); */ GF_EXPORT void gf_gui_draw_box(gf_gui_t* gui, int mul, double x, double y, double w, double h); +/** + * @~english + * @brief Set callback + * @param gui GUI + * @param id Component ID + * @param callback Callback + */ +GF_EXPORT void gf_gui_set_callback(gf_gui_t* gui, gf_gui_id_t id, gf_gui_callback_t callback); + #endif diff --git a/engine/include/gf_type/gui.h b/engine/include/gf_type/gui.h index 84a378c..facabad 100644 --- a/engine/include/gf_type/gui.h +++ b/engine/include/gf_type/gui.h @@ -16,6 +16,14 @@ */ typedef int gf_gui_id_t; +/** + * @~english + * @brief GUI events + */ +enum GF_GUI_EVENTS { + GF_GUI_PRESS = 0 +}; + #ifdef GF_EXPOSE_GUI typedef struct gf_gui_button_t gf_gui_button_t; @@ -31,6 +39,8 @@ typedef union gf_gui_union_t gf_gui_union_t; /* Standard */ +typedef void (*gf_gui_callback_t)(gf_engine_t* engine, gf_draw_t* draw, gf_gui_id_t id, int type); + /** * @~english * @brief Max components engine GUI can handle @@ -89,20 +99,24 @@ typedef union gf_gui_union_t { * @var gf_gui_component_t::height * @brief Height of component * - * @var gf_gui_component_t::u - * @brief Component union - * * @var gf_gui_component_t::pressed * @brief `1` if pressed, otherwise `0` + * + * @var gf_gui_component_t::callback + * @brief Event callback + * + * @var gf_gui_component_t::u + * @brief Component union */ GF_DECLARE_TYPE(gui_component, { - int type; - double x; - double y; - double width; - double height; - int pressed; - gf_gui_union_t u; + int type; + double x; + double y; + double width; + double height; + int pressed; + gf_gui_callback_t callback; + gf_gui_union_t u; }); /** @@ -129,6 +143,8 @@ GF_DECLARE_TYPE(gui, { gf_gui_component_t area[GF_GUI_MAX_COMPONENTS]; }); #else +typedef void* gf_gui_callback_t; + typedef void gf_gui_button_t; typedef void gf_gui_t;