mirror of
https://github.com/nishiowo/nishbox
synced 2025-04-21 20:24:39 +00:00
add callback system to gui
This commit is contained in:
parent
385a776a2e
commit
6095b0ec62
@ -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 button1 = -1;
|
||||||
gf_gui_id_t button2 = -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 */
|
/* Runs every frame */
|
||||||
void gf_draw_frame(gf_draw_t* draw) {
|
void gf_draw_frame(gf_draw_t* draw) {
|
||||||
gf_graphic_color_t color;
|
gf_graphic_color_t color;
|
||||||
color.r = color.g = color.b = color.a = 255;
|
color.r = color.g = color.b = color.a = 255;
|
||||||
if(button1 == -1) {
|
if(button1 == -1) {
|
||||||
button1 = gf_gui_create_button(draw->gui, 0, 0, 200, 50, "\"Test\" text");
|
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) {
|
if(button2 == -1) {
|
||||||
button2 = gf_gui_create_button(draw->gui, 100, 25, 200, 50, "\"Test\" text");
|
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) {
|
if(draw->draw_3d) {
|
||||||
gf_graphic_draw_texture_polygon(draw, test_texture, color, GF_GRAPHIC_3D, 4,
|
gf_graphic_draw_texture_polygon(draw, test_texture, color, GF_GRAPHIC_3D, 4,
|
||||||
|
@ -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->width = w;
|
||||||
c->height = h;
|
c->height = h;
|
||||||
|
|
||||||
c->pressed = 0;
|
c->pressed = 0;
|
||||||
|
c->callback = NULL;
|
||||||
|
|
||||||
c->u.button.text = malloc(strlen(text) + 1);
|
c->u.button.text = malloc(strlen(text) + 1);
|
||||||
strcpy(c->u.button.text, text);
|
strcpy(c->u.button.text, text);
|
||||||
return id;
|
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)) {
|
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->area[gui->pressed].pressed = 1;
|
||||||
gui->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; }
|
||||||
|
@ -74,7 +74,7 @@ GF_EXPORT void gf_gui_render(gf_gui_t* gui);
|
|||||||
* @param id Component ID to be returned
|
* @param id Component ID to be returned
|
||||||
* @return Pointer to component
|
* @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
|
* @~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);
|
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
|
#endif
|
||||||
|
@ -16,6 +16,14 @@
|
|||||||
*/
|
*/
|
||||||
typedef int gf_gui_id_t;
|
typedef int gf_gui_id_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @~english
|
||||||
|
* @brief GUI events
|
||||||
|
*/
|
||||||
|
enum GF_GUI_EVENTS {
|
||||||
|
GF_GUI_PRESS = 0
|
||||||
|
};
|
||||||
|
|
||||||
#ifdef GF_EXPOSE_GUI
|
#ifdef GF_EXPOSE_GUI
|
||||||
typedef struct gf_gui_button_t gf_gui_button_t;
|
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 */
|
/* Standard */
|
||||||
|
|
||||||
|
typedef void (*gf_gui_callback_t)(gf_engine_t* engine, gf_draw_t* draw, gf_gui_id_t id, int type);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @~english
|
* @~english
|
||||||
* @brief Max components engine GUI can handle
|
* @brief Max components engine GUI can handle
|
||||||
@ -89,20 +99,24 @@ typedef union gf_gui_union_t {
|
|||||||
* @var gf_gui_component_t::height
|
* @var gf_gui_component_t::height
|
||||||
* @brief Height of component
|
* @brief Height of component
|
||||||
*
|
*
|
||||||
* @var gf_gui_component_t::u
|
|
||||||
* @brief Component union
|
|
||||||
*
|
|
||||||
* @var gf_gui_component_t::pressed
|
* @var gf_gui_component_t::pressed
|
||||||
* @brief `1` if pressed, otherwise `0`
|
* @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, {
|
GF_DECLARE_TYPE(gui_component, {
|
||||||
int type;
|
int type;
|
||||||
double x;
|
double x;
|
||||||
double y;
|
double y;
|
||||||
double width;
|
double width;
|
||||||
double height;
|
double height;
|
||||||
int pressed;
|
int pressed;
|
||||||
gf_gui_union_t u;
|
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];
|
gf_gui_component_t area[GF_GUI_MAX_COMPONENTS];
|
||||||
});
|
});
|
||||||
#else
|
#else
|
||||||
|
typedef void* gf_gui_callback_t;
|
||||||
|
|
||||||
typedef void gf_gui_button_t;
|
typedef void gf_gui_button_t;
|
||||||
|
|
||||||
typedef void gf_gui_t;
|
typedef void gf_gui_t;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user