From 120d3051580ccebb017a5231b92e1dc18ceeb159 Mon Sep 17 00:00:00 2001 From: dwk Date: Sat, 17 May 2008 16:32:29 -0600 Subject: [PATCH] Created common signature for panel event handling functions. Made all panel event handlers have the same signature: typedef int (*panel_event_func_t)(struct xuni_t *xuni, struct panel_data_t *data); Created a structure, widgets.h::panel_event_t, which handles the event-specific variables, much like struct widget_t, with a union of structures. (This needs to be converted to a union of pointers to structures to save space.) Note that many events do not use the int return value of this signature. Updated all panel event handlers to match the new function signature. Any parameters were translated into equivalent variables, with as little effort as possible. At the same time, renamed all panel event handlers to _. In other words: - init_game() became game_init(); - game_perform_click() became game_click(); - and so on. Also note that several functions could use the new panel_event_recursive() in loop.c instead of old, specific functions like call_perform_click_func(). There are many other optimizations that could be made now that a panel event handler pointer can be created. --- TODO | 2 + gui/data/game.xml | 16 +++---- gui/data/menu.xml | 10 ++-- gui/data/options.xml | 23 +++++---- src/editor/editor.c | 93 ++++++++++++++++++------------------ src/loop.c | 131 ++++++++++++++++++++++++++++++++++++--------------- src/loop.h | 17 +++++-- src/test/game.c | 34 +++++++------ src/test/game.h | 49 +++++++++---------- src/test/menu.c | 29 +++++++----- src/test/menu.h | 52 ++++++++++---------- src/test/options.c | 53 +++++++++++++-------- src/test/options.h | 58 ++++++++++------------- src/widget/panel.c | 51 ++++++++++---------- src/widget/widgets.h | 84 +++++++++++++++++++++++++++------ 15 files changed, 417 insertions(+), 285 deletions(-) rewrite src/test/game.h (72%) rewrite src/test/menu.h (62%) rewrite src/test/options.h (80%) diff --git a/TODO b/TODO index be996a1..1df66f4 100644 --- a/TODO +++ b/TODO @@ -337,3 +337,5 @@ When textboxes revert, the text jumps to the end. This is because clear_active() Make the images for the glowbox theme smaller. Need to test call_blit_surface(). + +Test STATIC_LOADSO_FUNC, because some functions were renamed. diff --git a/gui/data/game.xml b/gui/data/game.xml index 6dfc70e..3644b6a 100644 --- a/gui/data/game.xml +++ b/gui/data/game.xml @@ -7,12 +7,12 @@ 0 - init_game - start_game + game_init + game_start game_event - game_perform_click - paint_game - free_game + game_click + game_paint + game_free @@ -96,10 +96,10 @@ 0 - start_game + game_start game_event - game_perform_click - paint_game + game_click + game_paint diff --git a/gui/data/menu.xml b/gui/data/menu.xml index 7366850..8db81f7 100644 --- a/gui/data/menu.xml +++ b/gui/data/menu.xml @@ -8,12 +8,12 @@ 0 - init_menu - start_menu + menu_init + menu_start menu_event - menu_perform_click - paint_menu - free_main_menu + menu_click + menu_paint + menu_free diff --git a/gui/data/options.xml b/gui/data/options.xml index 8c01121..c44a7f2 100644 --- a/gui/data/options.xml +++ b/gui/data/options.xml @@ -7,13 +7,12 @@ 0 - init_options - start_options + options_init + options_start options_event - options_perform_click - - paint_options - free_options + options_click + options_paint + options_free @@ -68,11 +67,11 @@ 0 - start_options + options_start options_event - options_perform_click + options_click options_graphics_deactivate - paint_options + options_paint @@ -143,11 +142,11 @@ 0 - start_options + options_start options_event - options_perform_click + options_click options_theme_deactivate - paint_options + options_paint diff --git a/src/editor/editor.c b/src/editor/editor.c index 9205ad0..d5c7630 100644 --- a/src/editor/editor.c +++ b/src/editor/editor.c @@ -73,22 +73,18 @@ static void save_resource(struct resource_t *resource) { /*write_resource(resource, SETTINGS_FILE ".generated");*/ } -static void editor_init(void *vdata, struct xuni_t *xuni, - struct widget_t *panel, struct resource_t *settings); -static void editor_start(void *vdata, struct xuni_t *xuni); -static int editor_event(void *vdata, struct xuni_t *xuni, panel_type_t *mode, - SDL_Event *event); -static void handle_cancel_action(struct xuni_t *xuni, +static int editor_init(struct xuni_t *xuni, struct panel_data_t *data); +static int editor_start(struct xuni_t *xuni, struct panel_data_t *data); +static int editor_event(struct xuni_t *xuni, struct panel_data_t *data); +static int editor_click(struct xuni_t *xuni, struct panel_data_t *data); +static int editor_deactivate(struct xuni_t *xuni, struct panel_data_t *data); +static int editor_paint(struct xuni_t *xuni, struct panel_data_t *data); +static int editor_free(struct xuni_t *xuni, struct panel_data_t *data); + +static void handle_cancel_action(struct xuni_t *xuni, struct editor_data_t *data); static void set_editor_mode(struct xuni_t *xuni, struct editor_data_t *data, enum editor_mode_t mode); -static int editor_perform_click(struct widget_t *widget, panel_type_t *mode, - void *vdata, struct xuni_t *xuni); -static void editor_deactivate(void *vdata, struct xuni_t *xuni, - struct widget_t *widget); -static void editor_paint(void *vdata, panel_type_t mode, - struct xuni_t *xuni); -static void editor_free(void *vdata, struct xuni_t *xuni); enum wid_t { WID_QUIT, @@ -112,7 +108,7 @@ static void init_loop_data(struct gui_t *gui) { data = xuni_memory_allocate(sizeof(struct editor_data_t)); set_panel_callbacks(widget_nameid_access(gui->widget, PANEL_EDITOR), data, 0, editor_init, editor_start, editor_event, - set_default_widget_sel, editor_perform_click, editor_deactivate, + default_panel_sel, editor_click, editor_deactivate, editor_paint, editor_free); } @@ -129,10 +125,9 @@ static const char *get_mode_name(enum editor_mode_t mode) { else return "ERROR"; } -static void editor_init(void *vdata, struct xuni_t *xuni, - struct widget_t *panel, struct resource_t *settings) { - - struct editor_data_t *data = vdata; +static int editor_init(struct xuni_t *xuni, struct panel_data_t *data) { + struct editor_data_t *edata = data->data; + struct widget_t *panel = data->event[PANEL_EVENT_INIT].p.init.panel; init_wid(panel, panel, WID_QUIT, "quit"); init_wid(panel, panel, WID_MODE_LABEL, "mode label"); @@ -152,19 +147,26 @@ static void editor_init(void *vdata, struct xuni_t *xuni, add_widget_accelerator(xuni, panel, widget_nameid_access(panel, WID_CANCEL_ACTION), SDLK_ESCAPE, KMOD_NONE); - data->mode = EDITOR_MODE_TEST; + edata->mode = EDITOR_MODE_TEST; + + return 0; } -static void editor_start(void *vdata, struct xuni_t *xuni) { +static int editor_start(struct xuni_t *xuni, struct panel_data_t *data) { set_caption("xuni editor"); + + return 0; } -static int editor_event(void *vdata, struct xuni_t *xuni, panel_type_t *mode, - SDL_Event *event) { +static int editor_event(struct xuni_t *xuni, struct panel_data_t *data) { +/*static int editor_event(void *vdata, struct xuni_t *xuni, panel_type_t *mode, + SDL_Event *event) {*/ - struct editor_data_t *data = vdata; + struct editor_data_t *edata = data->data; struct widget_t *cbox; int repaint = 0; + panel_type_t *mode = data->event[PANEL_EVENT_EVENT].p.event.mode; + SDL_Event *event = data->event[PANEL_EVENT_EVENT].p.event.event; switch(event->type) { case SDL_QUIT: @@ -189,14 +191,14 @@ static int editor_event(void *vdata, struct xuni_t *xuni, panel_type_t *mode, if(widget_nameid_follow(xuni->gui->widget, PANEL_EDITOR, WID_AREA_BOX, (size_t)-1)->sel) { - char buffer[BUFSIZ], **data; + char buffer[BUFSIZ], **bdata; struct widget_t *label; struct widget_t *box = widget_nameid_follow(xuni->gui->widget, PANEL_EDITOR, WID_AREA, (size_t)-1); label = widget_nameid_follow(xuni->gui->widget, PANEL_EDITOR, WID_POSITION_LABEL, (size_t)-1); - data = (char **)&label->p.label->text; + bdata = (char **)&label->p.label->text; sprintf(buffer, "(%.2f,%.2f)", (event->motion.x - box->pos->real.x) @@ -206,15 +208,15 @@ static int editor_event(void *vdata, struct xuni_t *xuni, panel_type_t *mode, / (xuni->smode->height * (box->pos->scale.h / 100.0)) * 100.0); - xuni_memory_free(*data); - *data = xuni_memory_duplicate_string(buffer); + xuni_memory_free(*bdata); + *bdata = xuni_memory_duplicate_string(buffer); widget_event(xuni, label, WIDGET_EVENT_RESCALE); repaint = 1; } - if(data->mode == EDITOR_MODE_ADD_BUTTON) { + if(edata->mode == EDITOR_MODE_ADD_BUTTON) { cbox = widget_nameid_follow(xuni->gui->widget, PANEL_EDITOR, WID_AREA_CURSOR_BOX, (size_t)-1); if(cbox->visibility & WIDGET_VISIBILITY_VISIBLE) { @@ -320,10 +322,10 @@ static void set_all_widget_visibilities(struct widget_t *widget, } } -static int editor_perform_click(struct widget_t *widget, panel_type_t *mode, - void *vdata, struct xuni_t *xuni) { - - struct editor_data_t *data = vdata; +static int editor_click(struct xuni_t *xuni, struct panel_data_t *data) { + struct editor_data_t *edata = data->data; + panel_type_t *mode = data->event[PANEL_EVENT_CLICK].p.click.mode; + struct widget_t *widget = data->event[PANEL_EVENT_CLICK].p.click.widget; struct widget_t *cbox; int repaint = 0; int xp, yp; @@ -336,11 +338,11 @@ static int editor_perform_click(struct widget_t *widget, panel_type_t *mode, widget_nameid_follow(xuni->gui->widget, PANEL_EDITOR, WID_AREA_BOX, (size_t)-1)->visibility &= ~WIDGET_VISIBILITY_CLICKABLE; - set_editor_mode(xuni, data, EDITOR_MODE_TEST); + set_editor_mode(xuni, edata, EDITOR_MODE_TEST); break; case WID_CANCEL_ACTION: - handle_cancel_action(xuni, data); + handle_cancel_action(xuni, edata); repaint = 1; break; @@ -348,7 +350,7 @@ static int editor_perform_click(struct widget_t *widget, panel_type_t *mode, widget_nameid_follow(xuni->gui->widget, PANEL_EDITOR, WID_AREA_BOX, (size_t)-1)->visibility &= ~WIDGET_VISIBILITY_CLICKABLE; - set_editor_mode(xuni, data, EDITOR_MODE_DELETE_WIDGET); + set_editor_mode(xuni, edata, EDITOR_MODE_DELETE_WIDGET); break; case WID_MODE_ADD_BUTTON: @@ -359,11 +361,11 @@ static int editor_perform_click(struct widget_t *widget, panel_type_t *mode, PANEL_EDITOR, WID_AREA_BOX, (size_t)-1), WIDGET_VISIBILITY_SELABLE);*/ - set_editor_mode(xuni, data, EDITOR_MODE_ADD_BUTTON); + set_editor_mode(xuni, edata, EDITOR_MODE_ADD_BUTTON); break; case WID_AREA_BOX: - if(data->mode == EDITOR_MODE_ADD_BUTTON) { + if(edata->mode == EDITOR_MODE_ADD_BUTTON) { cbox = widget_nameid_access(widget->base->base, WID_AREA_CURSOR_BOX); @@ -407,7 +409,7 @@ static int editor_perform_click(struct widget_t *widget, panel_type_t *mode, if(widget_is_parent(widget_nameid_follow(xuni->gui->widget, PANEL_EDITOR, WID_AREA, (size_t)-1), widget)) { - if(data->mode == EDITOR_MODE_DELETE_WIDGET) { + if(edata->mode == EDITOR_MODE_DELETE_WIDGET) { delete_widget_pointer(xuni, widget); } } @@ -418,14 +420,11 @@ static int editor_perform_click(struct widget_t *widget, panel_type_t *mode, return repaint; } -static void editor_deactivate(void *vdata, struct xuni_t *xuni, - struct widget_t *widget) { - +static int editor_deactivate(struct xuni_t *xuni, struct panel_data_t *data) { + return 0; } -static void editor_paint(void *vdata, panel_type_t mode, - struct xuni_t *xuni) { - +static int editor_paint(struct xuni_t *xuni, struct panel_data_t *data) { /*struct widget_t *area = widget_nameid_follow(xuni->gui->widget, PANEL_EDITOR, WID_AREA, (size_t)-1);*/ @@ -442,10 +441,12 @@ static void editor_paint(void *vdata, panel_type_t mode, }*/ update_screen(xuni); + + return 0; } -static void editor_free(void *vdata, struct xuni_t *xuni) { - +static int editor_free(struct xuni_t *xuni, struct panel_data_t *data) { + return 0; } #ifdef LOADSO_STATIC_VERSION diff --git a/src/loop.c b/src/loop.c index eeea04a..63ef624 100644 --- a/src/loop.c +++ b/src/loop.c @@ -16,8 +16,6 @@ #include "widget/widgets.h" #include "widget/listbox.h" -static void call_event_funcs(struct xuni_t *xuni, SDL_Event *event, - struct widget_t *widget, panel_type_t *mode); static int call_perform_click_func(struct xuni_t *xuni, struct panel_data_t *panel, struct widget_t *widget, panel_type_t *mode); static void call_free_funcs(struct widget_t *widget, struct xuni_t *xuni); @@ -43,6 +41,14 @@ static int perform_loop_click(struct xuni_t *xuni, panel_type_t *mode, static int event_key_down(struct xuni_t *xuni, SDL_keysym *keysym, panel_type_t *mode, int *handled); +/*! Calls the registered initialization functions for \a widget and all of its + sub-widgets. Only operates on widgets of type panel. + + \param xuni A pointer to the main xuni structure. + \param widget The widget to call the initialization function for, if it is + a panel widget. + \param settings A pointer to the resource tree. +*/ void call_init_funcs(struct xuni_t *xuni, struct widget_t *widget, struct resource_t *settings) { @@ -50,9 +56,14 @@ void call_init_funcs(struct xuni_t *xuni, struct widget_t *widget, if(!widget) return; - if(widget->type == WIDGET_PANEL && widget->p.panel->init_func) { - (*widget->p.panel->init_func)(widget->p.panel->data, xuni, widget, - settings); + if(widget->type == WIDGET_PANEL + && widget->p.panel->event[PANEL_EVENT_INIT].handler) { + + widget->p.panel->event[PANEL_EVENT_INIT].p.init.settings = settings; + widget->p.panel->event[PANEL_EVENT_INIT].p.init.panel = widget; + + (*widget->p.panel->event[PANEL_EVENT_INIT].handler) + (xuni, widget->p.panel); } if(widget->compose) { @@ -62,24 +73,26 @@ void call_init_funcs(struct xuni_t *xuni, struct widget_t *widget, } } -static void call_event_funcs(struct xuni_t *xuni, SDL_Event *event, - struct widget_t *widget, panel_type_t *mode) { +int panel_event_recursive(struct xuni_t *xuni, struct panel_event_t *data, + enum panel_event_type_t type, struct widget_t *widget) { + int r = 0; size_t x; - if(!widget) return; + if(!widget) return 0; - if(widget->type == WIDGET_PANEL && widget->p.panel->event_func) { - /* !!! return value ignored */ - (*widget->p.panel->event_func)(widget->p.panel->data, xuni, mode, - event); + if(widget->type == WIDGET_PANEL && data->handler) { + (*data->handler)(xuni, widget->p.panel); } if(widget->compose) { for(x = 0; x < widget->compose->widgets; x ++) { - call_event_funcs(xuni, event, widget->compose->widget[x], mode); + r = panel_event_recursive(xuni, data, type, + widget->compose->widget[x]) || r; } } + + return r; } static int call_perform_click_func(struct xuni_t *xuni, @@ -93,10 +106,13 @@ static int call_perform_click_func(struct xuni_t *xuni, example, clicking on the options graphics panel triggers the fullscreen checkbox, as PANEL_OPTIONS_GRAPHICS == WID_GRAPHICS_FULLSCREEN */ - if(widget && panel->perform_click_func + if(widget && panel->event[PANEL_EVENT_CLICK].handler && widget_nameid_access(xuni->gui->widget, *mode) != widget) { - return (*panel->perform_click_func)(widget, mode, panel->data, xuni); + panel->event[PANEL_EVENT_CLICK].p.click.mode = mode; + panel->event[PANEL_EVENT_CLICK].p.click.widget = widget; + + return (*panel->event[PANEL_EVENT_CLICK].handler)(xuni, panel); } return 0; @@ -107,9 +123,12 @@ void call_deactivate_func(struct xuni_t *xuni, struct widget_t *widget) { while(base && base->type != WIDGET_PANEL) base = base->base; - if(base && base->p.panel->deactivate_func) { - (*base->p.panel->deactivate_func) - (base->p.panel->data, xuni, widget); + if(base && base->p.panel->event[PANEL_EVENT_DEACTIVATE].handler) { + base->p.panel->event[PANEL_EVENT_DEACTIVATE].p.deactivate.widget + = widget; + + (*base->p.panel->event[PANEL_EVENT_DEACTIVATE].handler) + (xuni, base->p.panel); } } @@ -119,8 +138,9 @@ static void call_free_funcs(struct widget_t *widget, struct xuni_t *xuni) { if(!widget) return; if(widget->type == WIDGET_PANEL) { - if(widget->p.panel->free_func) { - (*widget->p.panel->free_func)(widget->p.panel->data, xuni); + if(widget->p.panel->event[PANEL_EVENT_FREE].handler) { + (*widget->p.panel->event[PANEL_EVENT_FREE].handler) + (xuni, widget->p.panel); } /* this is handled by free_widget() */ @@ -136,6 +156,14 @@ static void call_free_funcs(struct widget_t *widget, struct xuni_t *xuni) { /*#define HIDE_COMBOBOX_POPUPS*/ +int default_panel_sel(struct xuni_t *xuni, struct panel_data_t *data) { + struct panel_event_sel_t *sel = &data->event[PANEL_EVENT_SEL].p.sel; + + return set_default_widget_sel(xuni, sel->mode, sel->xp, sel->yp, + sel->click, data->data); +} + +/* !!! this function is obsolete */ int set_default_widget_sel(struct xuni_t *xuni, panel_type_t mode, int xp, int yp, int click, void *vdata) { @@ -191,22 +219,34 @@ int set_default_widget_sel(struct xuni_t *xuni, panel_type_t mode, } void set_panel_callbacks(struct widget_t *widget, void *vdata, - int frameupdate, init_func_t init_func, start_func_t start_func, - event_func_t event_func, set_widget_sel_func_t set_widget_sel_func, - perform_click_func_t perform_click_func, - deactivate_func_t deactivate_func, paint_func_t paint_func, - free_func_t free_func) { + int frameupdate, + panel_event_func_t init_func, + panel_event_func_t start_func, + panel_event_func_t event_func, + panel_event_func_t set_widget_sel_func, + panel_event_func_t perform_click_func, + panel_event_func_t deactivate_func, + panel_event_func_t paint_func, + panel_event_func_t free_func) { widget->p.panel->data = vdata; widget->p.panel->frameupdate = frameupdate; - widget->p.panel->init_func = init_func; + /*widget->p.panel->init_func = init_func; widget->p.panel->start_func = start_func; widget->p.panel->event_func = event_func; widget->p.panel->set_widget_sel_func = set_widget_sel_func; widget->p.panel->perform_click_func = perform_click_func; widget->p.panel->deactivate_func = deactivate_func; widget->p.panel->paint_func = paint_func; - widget->p.panel->free_func = free_func; + widget->p.panel->free_func = free_func;*/ + widget->p.panel->event[PANEL_EVENT_INIT].handler = init_func; + widget->p.panel->event[PANEL_EVENT_START].handler = start_func; + widget->p.panel->event[PANEL_EVENT_EVENT].handler = event_func; + widget->p.panel->event[PANEL_EVENT_SEL].handler = set_widget_sel_func; + widget->p.panel->event[PANEL_EVENT_CLICK].handler = perform_click_func; + widget->p.panel->event[PANEL_EVENT_DEACTIVATE].handler = deactivate_func; + widget->p.panel->event[PANEL_EVENT_PAINT].handler = paint_func; + widget->p.panel->event[PANEL_EVENT_FREE].handler = free_func; widget->p.panel->nameid = 0; @@ -280,9 +320,10 @@ void main_loop(struct xuni_t *xuni, struct xuni_callback_t *always, if(xuni->smode->focus) { if(repaint || panel->frameupdate) { - if(panel->paint_func) { - (*panel->paint_func) - (panel->data, mode, xuni); + if(panel->event[PANEL_EVENT_PAINT].handler) { + panel->event[PANEL_EVENT_PAINT].p.paint.mode = mode; + + (*panel->event[PANEL_EVENT_PAINT].handler)(xuni, panel); } } } @@ -406,8 +447,8 @@ static void set_loop_caption(panel_type_t mode, struct xuni_t *xuni) { widget_nameid_access(xuni->gui->widget, mode)->visibility |= WIDGET_VISIBILITY_VISIBLE; - if(paneldata->start_func) { - (*paneldata->start_func)(paneldata->data, xuni); + if(paneldata->event[PANEL_EVENT_START].handler) { + (*paneldata->event[PANEL_EVENT_START].handler)(xuni, paneldata); } setup_theme_cursor(xuni, widget_nameid_access(xuni->gui->widget, mode)); @@ -501,7 +542,14 @@ static int process_event(SDL_Event *event, panel_type_t *mode, if(!resize_screen(xuni->smode, &event->resize)) { widget_event(xuni, xuni->gui->widget, WIDGET_EVENT_RESCALE); - call_event_funcs(xuni, event, xuni->gui->widget, mode); + xuni->gui->widget->p.panel + ->event[PANEL_EVENT_EVENT].p.event.mode = mode; + xuni->gui->widget->p.panel + ->event[PANEL_EVENT_EVENT].p.event.event = event; + + panel_event_recursive(xuni, + &xuni->gui->widget->p.panel->event[PANEL_EVENT_EVENT], + PANEL_EVENT_EVENT, xuni->gui->widget); } if(focus_changed(xuni->smode, SDL_APPACTIVE)) { @@ -549,8 +597,11 @@ static int process_event(SDL_Event *event, panel_type_t *mode, break; } - if(!handled && panel->event_func) { - repaint |= (*panel->event_func)(panel->data, xuni, mode, event); + if(!handled && panel->event[PANEL_EVENT_EVENT].handler) { + panel->event[PANEL_EVENT_EVENT].p.event.mode = mode; + panel->event[PANEL_EVENT_EVENT].p.event.event = event; + + repaint |= (*panel->event[PANEL_EVENT_EVENT].handler)(xuni, panel); } return repaint; @@ -594,10 +645,14 @@ static int set_loop_widget_sel(panel_type_t mode, int xp, int yp, struct panel_data_t *panel = widget_nameid_access(xuni->gui->widget, mode)->p.panel; - if(!panel->set_widget_sel_func) return 0; + if(!panel->event[PANEL_EVENT_SEL].handler) return 0; + + panel->event[PANEL_EVENT_SEL].p.sel.mode = mode; + panel->event[PANEL_EVENT_SEL].p.sel.xp = xp; + panel->event[PANEL_EVENT_SEL].p.sel.yp = yp; + panel->event[PANEL_EVENT_SEL].p.sel.click = click; - return (*panel->set_widget_sel_func)(xuni, mode, xp, yp, click, - panel->data); + return (*panel->event[PANEL_EVENT_SEL].handler)(xuni, panel); } /*! Determines whether any accelerator key combinations have been or are being diff --git a/src/loop.h b/src/loop.h index 91b6f27..6b6c2b9 100644 --- a/src/loop.h +++ b/src/loop.h @@ -11,17 +11,24 @@ void call_init_funcs(struct xuni_t *xuni, struct widget_t *widget, struct resource_t *settings); +int panel_event_recursive(struct xuni_t *xuni, struct panel_event_t *data, + enum panel_event_type_t type, struct widget_t *widget); void call_deactivate_func(struct xuni_t *xuni, struct widget_t *widget); +int default_panel_sel(struct xuni_t *xuni, struct panel_data_t *data); int set_default_widget_sel(struct xuni_t *xuni, panel_type_t mode, int xp, int yp, int click, void *vdata); void set_panel_callbacks(struct widget_t *widget, void *vdata, - int frameupdate, init_func_t init_func, start_func_t start_func, - event_func_t event_func, set_widget_sel_func_t set_widget_sel_func, - perform_click_func_t perform_click_func, - deactivate_func_t deactivate_func, paint_func_t paint_func, - free_func_t free_func); + int frameupdate, + panel_event_func_t init_func, + panel_event_func_t start_func, + panel_event_func_t event_func, + panel_event_func_t set_widget_sel_func, + panel_event_func_t perform_click_func, + panel_event_func_t deactivate_func, + panel_event_func_t paint_func, + panel_event_func_t free_func); void execute_callback(struct xuni_t *xuni, struct xuni_callback_t *callback); void main_loop(struct xuni_t *xuni, struct xuni_callback_t *always, diff --git a/src/test/game.c b/src/test/game.c index 11b703e..87323c7 100644 --- a/src/test/game.c +++ b/src/test/game.c @@ -26,10 +26,8 @@ enum wid_t { WIDS }; -void init_game(void *vdata, struct xuni_t *xuni, - struct widget_t *panel, struct resource_t *settings) { - - /*struct game_data_t *data = vdata;*/ +int game_init(struct xuni_t *xuni, struct panel_data_t *data) { + struct widget_t *panel = data->event[PANEL_EVENT_INIT].p.init.panel; struct widget_t *temppanel; init_wid(xuni->gui->widget, xuni->gui->widget, @@ -49,14 +47,19 @@ void init_game(void *vdata, struct xuni_t *xuni, widget_nameid_access(panel, WID_MENU_BACK), SDLK_ESCAPE, KMOD_NONE); /*add_widget_accelerator(temppanel, widget_nameid_access(panel, WID_MENU_BACK), SDLK_F10, KMOD_NONE);*/ + + return 0; } -void start_game(void *vdata, struct xuni_t *xuni) { +int game_start(struct xuni_t *xuni, struct panel_data_t *data) { set_caption("Explore the Universe"); + + return 0; } -int game_event(void *vdata, struct xuni_t *xuni, panel_type_t *mode, - SDL_Event *event) { +int game_event(struct xuni_t *xuni, struct panel_data_t *data) { + panel_type_t *mode = data->event[PANEL_EVENT_EVENT].p.event.mode; + SDL_Event *event = data->event[PANEL_EVENT_EVENT].p.event.event; switch(event->type) { case SDL_KEYDOWN: @@ -81,10 +84,10 @@ int game_event(void *vdata, struct xuni_t *xuni, panel_type_t *mode, return 0; } -void paint_game(void *vdata, panel_type_t mode, struct xuni_t *xuni) { - /*struct game_data_t *data = vdata;*/ +int game_paint(struct xuni_t *xuni, struct panel_data_t *data) { struct widget_t *panel = widget_nameid_access(xuni->gui->widget, PANEL_GAME); + panel_type_t mode = data->event[PANEL_EVENT_PAINT].p.paint.mode; clear_screen(xuni->smode->screen); @@ -103,12 +106,13 @@ void paint_game(void *vdata, panel_type_t mode, struct xuni_t *xuni) { } update_screen(xuni); + + return 0; } -int game_perform_click(struct widget_t *widget, panel_type_t *mode, - void *vdata, struct xuni_t *xuni) { - - /*struct game_data_t *data = vdata;*/ +int game_click(struct xuni_t *xuni, struct panel_data_t *data) { + panel_type_t *mode = data->event[PANEL_EVENT_CLICK].p.click.mode; + struct widget_t *widget = data->event[PANEL_EVENT_CLICK].p.click.widget; switch(widget->id) { case WID_MENU: @@ -129,6 +133,6 @@ int game_perform_click(struct widget_t *widget, panel_type_t *mode, return 0; /* ignored */ } -void free_game(void *vdata, struct xuni_t *xuni) { - +int game_free(struct xuni_t *xuni, struct panel_data_t *data) { + return 0; } diff --git a/src/test/game.h b/src/test/game.h dissimilarity index 72% index 5ce7f5c..30e0856 100644 --- a/src/test/game.h +++ b/src/test/game.h @@ -1,26 +1,23 @@ -/*! \file game.h - -*/ - -#ifndef XUNI_GUARD_GAME_H -#define XUNI_GUARD_GAME_H - -#include "graphics.h" -#include "gui.h" -#include "loop.h" - -struct game_data_t { - int unused; -}; - -void init_game(void *vdata, struct xuni_t *xuni, struct widget_t *panel, - struct resource_t *settings); -void start_game(void *vdata, struct xuni_t *xuni); -int game_event(void *vdata, struct xuni_t *xuni, panel_type_t *mode, - SDL_Event *event); -void paint_game(void *vdata, panel_type_t mode, struct xuni_t *xuni); -int game_perform_click(struct widget_t *widget, panel_type_t *mode, - void *vdata, struct xuni_t *xuni); -void free_game(void *vdata, struct xuni_t *xuni); - -#endif +/*! \file game.h + +*/ + +#ifndef XUNI_GUARD_GAME_H +#define XUNI_GUARD_GAME_H + +#include "graphics.h" +#include "gui.h" +#include "loop.h" + +struct game_data_t { + int unused; +}; + +int game_init(struct xuni_t *xuni, struct panel_data_t *data); +int game_start(struct xuni_t *xuni, struct panel_data_t *data); +int game_event(struct xuni_t *xuni, struct panel_data_t *data); +int game_paint(struct xuni_t *xuni, struct panel_data_t *data); +int game_click(struct xuni_t *xuni, struct panel_data_t *data); +int game_free(struct xuni_t *xuni, struct panel_data_t *data); + +#endif diff --git a/src/test/menu.c b/src/test/menu.c index cc8b3f3..0d8f702 100644 --- a/src/test/menu.c +++ b/src/test/menu.c @@ -98,8 +98,8 @@ void switch_main_menu_background(struct widget_t *panel, const char *name) { } } -void init_menu(void *vdata, struct xuni_t *xuni, - struct widget_t *panel, struct resource_t *settings) { +int menu_init(struct xuni_t *xuni, struct panel_data_t *data) { + struct widget_t *panel = data->event[PANEL_EVENT_INIT].p.init.panel; init_wid(xuni->gui->widget, xuni->gui->widget, PANEL_MAIN_MENU, "main menu"); @@ -157,15 +157,19 @@ void init_menu(void *vdata, struct xuni_t *xuni, data->quality.y0 = 500.0; data->quality.x1 = 600.0; data->quality.y1 = 200.0;*/ + + return 0; } -void start_menu(void *vdata, struct xuni_t *xuni) { +int menu_start(struct xuni_t *xuni, struct panel_data_t *data) { set_caption("Main menu"); + + return 0; } -int menu_event(void *vdata, struct xuni_t *xuni, panel_type_t *mode, - SDL_Event *event) { - +int menu_event(struct xuni_t *xuni, struct panel_data_t *data) { + panel_type_t *mode = data->event[PANEL_EVENT_EVENT].p.event.mode; + SDL_Event *event = data->event[PANEL_EVENT_EVENT].p.event.event; int repaint = 0; switch(event->type) { @@ -191,8 +195,9 @@ int menu_event(void *vdata, struct xuni_t *xuni, panel_type_t *mode, return repaint; } -int menu_perform_click(struct widget_t *widget, panel_type_t *mode, - void *vdata, struct xuni_t *xuni) { +int menu_click(struct xuni_t *xuni, struct panel_data_t *data) { + panel_type_t *mode = data->event[PANEL_EVENT_CLICK].p.click.mode; + struct widget_t *widget = data->event[PANEL_EVENT_CLICK].p.click.widget; switch(widget->id) { case WID_NEWGAME: @@ -228,7 +233,7 @@ static void set_hover_visible(struct gui_t *gui, struct widget_t *menu) { } } -void paint_menu(void *vdata, panel_type_t mode, struct xuni_t *xuni) { +int menu_paint(struct xuni_t *xuni, struct panel_data_t *data) { struct widget_t *panel = widget_nameid_access(xuni->gui->widget, PANEL_MAIN_MENU); @@ -247,6 +252,8 @@ void paint_menu(void *vdata, panel_type_t mode, struct xuni_t *xuni) { } update_screen(xuni); + + return 0; } void paint_menu_fps(struct xuni_t *xuni, size_t font) { @@ -276,6 +283,6 @@ void paint_menu_fps(struct xuni_t *xuni, size_t font) { free_surface(text); } -void free_main_menu(void *vdata, struct xuni_t *xuni) { - +int menu_free(struct xuni_t *xuni, struct panel_data_t *data) { + return 0; } diff --git a/src/test/menu.h b/src/test/menu.h dissimilarity index 62% index e69eb5a..391c5c4 100644 --- a/src/test/menu.h +++ b/src/test/menu.h @@ -1,27 +1,25 @@ -/*! \file menu.h - -*/ - -#ifndef XUNI_GUARD_MENU_H -#define XUNI_GUARD_MENU_H - -#include "graphics.h" -#include "gui.h" - -struct menu_data_t { - int unused; -}; - -void switch_main_menu_background(struct widget_t *panel, const char *name); -void init_menu(void *vdata, struct xuni_t *xuni, - struct widget_t *panel, struct resource_t *settings); -void start_menu(void *vdata, struct xuni_t *xuni); -int menu_event(void *vdata, struct xuni_t *xuni, panel_type_t *mode, - SDL_Event *event); -int menu_perform_click(struct widget_t *widget, panel_type_t *mode, - void *vdata, struct xuni_t *xuni); -void paint_menu(void *vdata, panel_type_t mode, struct xuni_t *xuni); -void paint_menu_fps(struct xuni_t *xuni, size_t font); -void free_main_menu(void *vdata, struct xuni_t *xuni); - -#endif +/*! \file menu.h + +*/ + +#ifndef XUNI_GUARD_MENU_H +#define XUNI_GUARD_MENU_H + +#include "graphics.h" +#include "gui.h" + +struct menu_data_t { + int unused; +}; + +void switch_main_menu_background(struct widget_t *panel, const char *name); +void paint_menu_fps(struct xuni_t *xuni, size_t font); + +int menu_init(struct xuni_t *xuni, struct panel_data_t *data); +int menu_start(struct xuni_t *xuni, struct panel_data_t *data); +int menu_event(struct xuni_t *xuni, struct panel_data_t *data); +int menu_click(struct xuni_t *xuni, struct panel_data_t *data); +int menu_paint(struct xuni_t *xuni, struct panel_data_t *data); +int menu_free(struct xuni_t *xuni, struct panel_data_t *data); + +#endif diff --git a/src/test/options.c b/src/test/options.c index 3a30a4e..96549fe 100644 --- a/src/test/options.c +++ b/src/test/options.c @@ -57,9 +57,8 @@ static void get_names_rec(struct xuni_t *xuni, struct widget_t *area, struct widget_t *widget, enum widget_type_t type); static void screen_mode_from_string(struct xuni_t *xuni, const char *mode); -void init_options(void *vdata, struct xuni_t *xuni, - struct widget_t *panel, struct resource_t *settings) { - +int options_init(struct xuni_t *xuni, struct panel_data_t *data) { + struct widget_t *panel = data->event[PANEL_EVENT_INIT].p.init.panel; struct widget_t *temppanel; init_wid(xuni->gui->widget, xuni->gui->widget, @@ -122,6 +121,8 @@ void init_options(void *vdata, struct xuni_t *xuni, get_font_names(xuni, panel); get_main_menu_backgrounds(xuni, panel); get_font_samples(xuni, panel); + + return 0; } static void get_screen_modes(struct xuni_t *xuni, struct widget_t *widget) { @@ -253,15 +254,15 @@ static void get_names_rec(struct xuni_t *xuni, struct widget_t *area, } } -void start_options(void *vdata, struct xuni_t *xuni) { +int options_start(struct xuni_t *xuni, struct panel_data_t *data) { set_caption("Options and preferences"); + + return 0; } -int options_event(void *vdata, struct xuni_t *xuni, panel_type_t *mode, - SDL_Event *event) { - - /*struct options_data_t *data = vdata;*/ - +int options_event(struct xuni_t *xuni, struct panel_data_t *data) { + panel_type_t *mode = data->event[PANEL_EVENT_EVENT].p.event.mode; + SDL_Event *event = data->event[PANEL_EVENT_EVENT].p.event.event; int repaint = 0; switch(event->type) { @@ -275,10 +276,9 @@ int options_event(void *vdata, struct xuni_t *xuni, panel_type_t *mode, return repaint; } -int options_perform_click(struct widget_t *widget, panel_type_t *mode, - void *vdata, struct xuni_t *xuni) { - - /*struct options_data_t *data = vdata;*/ +int options_click(struct xuni_t *xuni, struct panel_data_t *data) { + panel_type_t *mode = data->event[PANEL_EVENT_CLICK].p.click.mode; + struct widget_t *widget = data->event[PANEL_EVENT_CLICK].p.click.widget; struct widget_t *w; switch(widget->id) { @@ -395,8 +395,11 @@ static void screen_mode_from_string(struct xuni_t *xuni, const char *mode) { use_screen_mode(xuni, w, h); } -void options_graphics_deactivate(void *vdata, struct xuni_t *xuni, - struct widget_t *widget) { +int options_graphics_deactivate(struct xuni_t *xuni, + struct panel_data_t *data) { + + struct widget_t *widget + = data->event[PANEL_EVENT_DEACTIVATE].p.deactivate.widget; switch(widget->id) { case WID_GRAPHICS_TEXTSCREENMODE: @@ -404,10 +407,15 @@ void options_graphics_deactivate(void *vdata, struct xuni_t *xuni, break; } + + return 0; } -void options_theme_deactivate(void *vdata, struct xuni_t *xuni, - struct widget_t *widget) { +int options_theme_deactivate(struct xuni_t *xuni, + struct panel_data_t *data) { + + struct widget_t *widget + = data->event[PANEL_EVENT_DEACTIVATE].p.deactivate.widget; switch(widget->id) { case WID_THEME_FONT_FILENAME: @@ -444,9 +452,12 @@ void options_theme_deactivate(void *vdata, struct xuni_t *xuni, widget_nameid_access(xuni->gui->widget, PANEL_OPTIONS)); } } + + return 0; } -void paint_options(void *vdata, panel_type_t mode, struct xuni_t *xuni) { +int options_paint(struct xuni_t *xuni, struct panel_data_t *data) { + panel_type_t mode = data->event[PANEL_EVENT_PAINT].p.paint.mode; /*struct options_data_t *data = vdata;*/ /*static int fullscreen = -1;*/ @@ -491,8 +502,10 @@ void paint_options(void *vdata, panel_type_t mode, struct xuni_t *xuni) { paint_menu_fps(xuni, THEME_FONT_MONO); update_screen(xuni); + + return 0; } -void free_options(void *vdata, struct xuni_t *xuni) { - +int options_free(struct xuni_t *xuni, struct panel_data_t *data) { + return 0; } diff --git a/src/test/options.h b/src/test/options.h dissimilarity index 80% index d8dfaf5..50516b8 100644 --- a/src/test/options.h +++ b/src/test/options.h @@ -1,32 +1,26 @@ -/*! \file options.h - -*/ - -#ifndef XUNI_GUARD_OPTIONS_H -#define XUNI_GUARD_OPTIONS_H - -#include "graphics.h" -#include "gui.h" -#include "loop.h" - -struct options_data_t { - int unused; -}; - -void init_options(void *vdata, struct xuni_t *xuni, - struct widget_t *panel, struct resource_t *settings); -void start_options(void *vdata, struct xuni_t *xuni); -int options_event(void *vdata, struct xuni_t *xuni, panel_type_t *mode, - SDL_Event *event); -int options_perform_click(struct widget_t *widget, panel_type_t *mode, - void *vdata, struct xuni_t *xuni); -void perform_back_click(struct widget_t *widget, - panel_type_t *mode, void *vdata, struct xuni_t *xuni); -void paint_options(void *vdata, panel_type_t mode, struct xuni_t *xuni); -void options_graphics_deactivate(void *vdata, struct xuni_t *xuni, - struct widget_t *widget); -void options_theme_deactivate(void *vdata, struct xuni_t *xuni, - struct widget_t *widget); -void free_options(void *vdata, struct xuni_t *xuni); - -#endif +/*! \file options.h + +*/ + +#ifndef XUNI_GUARD_OPTIONS_H +#define XUNI_GUARD_OPTIONS_H + +#include "graphics.h" +#include "gui.h" +#include "loop.h" + +struct options_data_t { + int unused; +}; + +int options_init(struct xuni_t *xuni, struct panel_data_t *data); +int options_start(struct xuni_t *xuni, struct panel_data_t *data); +int options_event(struct xuni_t *xuni, struct panel_data_t *data); +int options_click(struct xuni_t *xuni, struct panel_data_t *data); +int options_paint(struct xuni_t *xuni, struct panel_data_t *data); +int options_graphics_deactivate(struct xuni_t *xuni, + struct panel_data_t *data); +int options_theme_deactivate(struct xuni_t *xuni, struct panel_data_t *data); +int options_free(struct xuni_t *xuni, struct panel_data_t *data); + +#endif diff --git a/src/widget/panel.c b/src/widget/panel.c index f10f327..fac442e 100644 --- a/src/widget/panel.c +++ b/src/widget/panel.c @@ -33,19 +33,18 @@ void panel_widget_event(struct xuni_t *xuni, struct widget_t *widget, } void init_panel(struct widget_t *widget) { + enum panel_event_type_t x; + widget->type = WIDGET_PANEL; widget->p.panel = xuni_memory_allocate(sizeof(*widget->p.panel)); widget->p.panel->data = 0; widget->p.panel->frameupdate = 0; - widget->p.panel->init_func = 0; - widget->p.panel->start_func = 0; - widget->p.panel->event_func = 0; - widget->p.panel->set_widget_sel_func = 0; - widget->p.panel->perform_click_func = 0; - widget->p.panel->deactivate_func = 0; - widget->p.panel->paint_func = 0; - widget->p.panel->free_func = 0; + + for(x = 0; x < PANEL_EVENTS; x ++) { + widget->p.panel->event[x].type = x; + widget->p.panel->event[x].handler = 0; + } widget->p.panel->nameid = 0; @@ -67,8 +66,7 @@ static func_point_t panel_load_handler(void *object, } /* !!! */ -int set_default_widget_sel(struct xuni_t *xuni, panel_type_t mode, - int xp, int yp, int click, void *vdata); +int default_panel_sel(struct xuni_t *xuni, struct panel_data_t *data); void init_panel_from_resource(struct xuni_t *xuni, struct widget_t *widget, struct resource_list_t *list) { @@ -82,23 +80,24 @@ void init_panel_from_resource(struct xuni_t *xuni, struct widget_t *widget, widget->p.panel->data = 0; widget->p.panel->frameupdate = 0; - widget->p.panel->init_func - = (init_func_t)panel_load_handler(object, handler, "init"); - widget->p.panel->start_func - = (start_func_t)panel_load_handler(object, handler, "start"); - widget->p.panel->event_func - = (event_func_t)panel_load_handler(object, handler, "event"); - widget->p.panel->set_widget_sel_func = set_default_widget_sel; - widget->p.panel->perform_click_func - = (perform_click_func_t)panel_load_handler(object, handler, - "perform-click"); - widget->p.panel->deactivate_func - = (deactivate_func_t)panel_load_handler(object, handler, + widget->p.panel->event[PANEL_EVENT_INIT].handler + = (panel_event_func_t)panel_load_handler(object, handler, "init"); + widget->p.panel->event[PANEL_EVENT_START].handler + = (panel_event_func_t)panel_load_handler(object, handler, "start"); + widget->p.panel->event[PANEL_EVENT_EVENT].handler + = (panel_event_func_t)panel_load_handler(object, handler, "event"); + widget->p.panel->event[PANEL_EVENT_SEL].handler + = (panel_event_func_t)default_panel_sel; + widget->p.panel->event[PANEL_EVENT_CLICK].handler + = (panel_event_func_t)panel_load_handler(object, handler, + "click"); + widget->p.panel->event[PANEL_EVENT_DEACTIVATE].handler + = (panel_event_func_t)panel_load_handler(object, handler, "deactivate"); - widget->p.panel->paint_func - = (paint_func_t)panel_load_handler(object, handler, "paint"); - widget->p.panel->free_func - = (free_func_t)panel_load_handler(object, handler, "free"); + widget->p.panel->event[PANEL_EVENT_PAINT].handler + = (panel_event_func_t)panel_load_handler(object, handler, "paint"); + widget->p.panel->event[PANEL_EVENT_FREE].handler + = (panel_event_func_t)panel_load_handler(object, handler, "free"); widget->p.panel->nameid = 0; diff --git a/src/widget/widgets.h b/src/widget/widgets.h index 134c051..5ba4498 100644 --- a/src/widget/widgets.h +++ b/src/widget/widgets.h @@ -143,19 +143,81 @@ struct wtype_array_t { size_t types; }; -/* not used */ -enum panel_event_t { +/* !!! needs alphabetical ordering */ +enum panel_event_type_t { + PANEL_EVENT_NONE = -1, PANEL_EVENT_FREE, PANEL_EVENT_EVENT, PANEL_EVENT_INIT, PANEL_EVENT_PAINT, - PANEL_EVENT_SWITCH, - PANEL_EVENT_WCLICK, - PANEL_EVENT_WDATA, - PANEL_EVENT_WSEL, + PANEL_EVENT_START, + PANEL_EVENT_CLICK, + PANEL_EVENT_DEACTIVATE, + PANEL_EVENT_SEL, PANEL_EVENTS }; +struct panel_event_init_t { + struct widget_t *panel; + struct resource_t *settings; +}; + +struct panel_event_start_t { + int unused; +}; + +struct panel_event_event_t { + panel_type_t *mode; + SDL_Event *event; + struct widget_t *widget; +}; + +struct panel_event_sel_t { + panel_type_t mode; + int xp, yp; + int click; +}; + +struct panel_event_click_t { + struct widget_t *widget; + panel_type_t *mode; +}; + +struct panel_event_deactivate_t { + struct widget_t *widget; +}; + +struct panel_event_paint_t { + panel_type_t mode; +}; + +struct panel_event_free_t { + int unused; +}; + +struct panel_data_t; + +typedef int (*panel_event_func_t)(struct xuni_t *xuni, + struct panel_data_t *data); + +struct panel_event_t { + enum panel_event_type_t type; + + union { + struct panel_event_init_t init; + struct panel_event_start_t start; + struct panel_event_event_t event; + struct panel_event_sel_t sel; + struct panel_event_click_t click; + struct panel_event_deactivate_t deactivate; + struct panel_event_paint_t paint; + struct panel_event_free_t free; + } p; + + panel_event_func_t handler; +}; + +#if 0 typedef void (*init_func_t)(void *vdata, struct xuni_t *xuni, struct widget_t *panel, struct resource_t *settings); typedef void (*start_func_t)(void *vdata, struct xuni_t *xuni); @@ -170,19 +232,13 @@ typedef void (*deactivate_func_t)(void *vdata, struct xuni_t *xuni, typedef void (*paint_func_t)(void *vdata, panel_type_t mode, struct xuni_t *xuni); typedef void (*free_func_t)(void *vdata, struct xuni_t *xuni); +#endif struct panel_data_t { void *data; int frameupdate; - init_func_t init_func; - start_func_t start_func; - event_func_t event_func; - set_widget_sel_func_t set_widget_sel_func; - perform_click_func_t perform_click_func; - deactivate_func_t deactivate_func; - paint_func_t paint_func; - free_func_t free_func; + struct panel_event_t event[PANEL_EVENTS]; struct nameid_t *nameid; -- 2.11.4.GIT