From 8da2cb1299b608d45d2a7dcb9e4c43a93871c1aa Mon Sep 17 00:00:00 2001 From: Allanis Date: Fri, 22 Mar 2013 20:31:42 +0000 Subject: [PATCH] [Add] You can specify default fptr for windows now. --- src/player.c | 4 ++++ src/toolkit.c | 48 +++++++++++++++++++++++++++++++++++------------- src/toolkit.h | 3 ++- 3 files changed, 41 insertions(+), 14 deletions(-) diff --git a/src/player.c b/src/player.c index ef81d40..2bd806e 100644 --- a/src/player.c +++ b/src/player.c @@ -158,6 +158,8 @@ void player_new(void) { &gl_smallFont, &cDConsole, "Please tell me your name:"); window_addInput(wid, 20, -50, 200, 20, "inpName", 20, 1); window_addButton(wid, -20, 20, 80, 30, "btnClose", "Done", player_nameClose); + + window_setFptr(wid, player_nameClose); } static void player_nameClose(char* str) { @@ -269,6 +271,8 @@ void player_newShip(Ship* ship, double px, double py, window_addInput(wid, 20, -50, 200, 20, "inpName", 20, 1); window_addButton(wid, -20, 20, 80, 30, "btnClose", "Done", player_nameShipClose); + + window_setFptr(wid, player_nameShipClose); } static void player_nameShipClose(char* str) { diff --git a/src/toolkit.c b/src/toolkit.c index ee223d1..4aa30e2 100644 --- a/src/toolkit.c +++ b/src/toolkit.c @@ -87,6 +87,10 @@ typedef struct Window_ { int hidden; // Is it hidden? int focus; // Which window is focused. + // Pointer to a function to run if user hits 'enter' and no button is focused + // nor any other input thiny that catches 'enter'. + void(*def_fptr)(char*); + double x,y; // Position. double w,h; // Dimensions. @@ -396,6 +400,9 @@ unsigned int window_get(const char* wdwname) { // Create a window. unsigned int window_create(char* name, const int x, const int y, const int w, const int h) { + + Window* wdw; + if(nwindows >= mwindows) { // We have reached our memory limit. windows = realloc(windows, sizeof(Window)*(++mwindows)); @@ -404,31 +411,35 @@ unsigned int window_create(char* name, const int x, const int y, const int wid = (++genwid); // Unique id - windows[nwindows].id = wid; - windows[nwindows].name = strdup(name); + wdw = &windows[nwindows]; - windows[nwindows].hidden = 0; - windows[nwindows].focus = -1; + wdw->id = wid; + wdw->name = strdup(name); + + wdw->hidden = 0; + wdw->focus = -1; + wdw->def_fptr = NULL; + + wdw->w = (double)w; + wdw->h = (double)h; - windows[nwindows].w = (double) w; - windows[nwindows].h = (double) h; // x pos. if(x == -1) // Center. - windows[nwindows].x = gl_screen.w/2. - windows[nwindows].w/2.; + wdw->x = gl_screen.w/2. - windows[nwindows].w/2.; else if(x < 0) - windows[nwindows].x = gl_screen.w - windows[nwindows].w + (double)x; + wdw->x = gl_screen.w - windows[nwindows].w + (double)x; else windows[nwindows].x = (double)x; // y pos. if(y == -1) // Center. - windows[nwindows].y = gl_screen.h/2. - windows[nwindows].h/2.; + wdw->y = gl_screen.h/2. - windows[nwindows].h/2.; else if(y < 0) - windows[nwindows].x = gl_screen.h - windows[nwindows].h + (double)y; - else windows[nwindows].y = (double)y; + wdw->x = gl_screen.h - windows[nwindows].h + (double)y; + else wdw->y = (double)y; - windows[nwindows].widgets = NULL; - windows[nwindows].nwidgets = 0; + wdw->widgets = NULL; + wdw->nwidgets = 0; nwindows++; @@ -441,6 +452,14 @@ unsigned int window_create(char* name, const int x, const int y, return wid; } +// Sets the window's default function. +void window_setFptr(const unsigned int wid, void(*fptr)(char*)) { + Window* wdw; + + wdw = window_wget(wid); + if(wdw != NULL) wdw->def_fptr = fptr; +} + // Destroy a widget. static void widget_cleanup(Widget* widget) { int i; @@ -1208,6 +1227,7 @@ static void toolkit_nextFocus(void) { toolkit_nextFocus(); } +// Return 1 if the window is focusable. static int toolkit_isFocusable(Widget* wgt) { if(wgt == NULL) return 0; @@ -1239,6 +1259,7 @@ static void toolkit_triggerFocus(void) { wgt->name, wdw->name); break; default: + if(wdw->def_fptr)(*wdw->def_fptr)(wgt->name); break; } } @@ -1272,6 +1293,7 @@ static void toolkit_listFocus(Widget* lst, double bx, double by) { } } +// Get what is selected currently in a list. char* toolkit_getList(const unsigned int wid, char* name) { Widget* wgt = window_getwgt(wid, name); diff --git a/src/toolkit.h b/src/toolkit.h index 2eb9748..39518f0 100644 --- a/src/toolkit.h +++ b/src/toolkit.h @@ -46,7 +46,8 @@ void window_addInput(const unsigned int wid, // Popups and alerts. void toolkit_alert(const char* fmt, ...); -// Modification +// Modification. +void window_setFptr(const unsigned int wid, void(*fptr)(char*)); void window_modifyText(const unsigned int wid, char* name, char* newstring); void window_modifyImage(const unsigned int wid, char* name, glTexture* image);