From 053aab14c93ec574ec7b74a47cc22b8b2cf8527a Mon Sep 17 00:00:00 2001 From: Allanis Date: Thu, 21 Mar 2013 23:51:58 +0000 Subject: [PATCH] [Add] Input boxes in toolkit. Need to hack fonts up before it works. --- src/land.c | 6 +-- src/player.c | 29 ++++++++++++- src/space.c | 2 + src/toolkit.c | 117 +++++++++++++++++++++++++++++++++++++++++++++----- src/toolkit.h | 12 ++++-- 5 files changed, 147 insertions(+), 19 deletions(-) diff --git a/src/land.c b/src/land.c index 860af8d..8d2a545 100644 --- a/src/land.c +++ b/src/land.c @@ -203,7 +203,7 @@ static void outfits(void) { // Fancy 128x128 image. window_addRect(secondary_wid, -20, -50, 128, 128, "rctImage", &cBlack, 0); - window_addImage(secondary_wid, -20-128, -50-128, "imgOutfit", NULL); + window_addImage(secondary_wid, -20-128, -50-128, "imgOutfit", NULL, 1); window_addCust(secondary_wid, -40-BUTTON_WIDTH, 60+2*BUTTON_HEIGHT, BUTTON_WIDTH, BUTTON_HEIGHT, @@ -391,7 +391,7 @@ static void shipyard(void) { 128, 96, "rctTarget", &cBlack, 0); window_addImage(secondary_wid, -40-128, -50-96, - "imgTarget", NULL); + "imgTarget", NULL, 1); window_addText(secondary_wid, 40+200+40, -55, 80, 96, 0, "txtSDesc", &gl_smallFont, &cDConsole, @@ -529,7 +529,7 @@ void land(Planet* p) { land_wid = window_create(p->name, -1, -1, LAND_WIDTH, LAND_HEIGHT); // Pretty display. - window_addImage(land_wid, 20, -40, "imgPlanet", p->gfx_exterior); + window_addImage(land_wid, 20, -40, "imgPlanet", p->gfx_exterior, 1); window_addText(land_wid, 440, 80, LAND_WIDTH-460, 460, 0, "txtPlanetDesc", &gl_smallFont, &cBlack, p->description); // Buttons. diff --git a/src/player.c b/src/player.c index 7054e48..69bd882 100644 --- a/src/player.c +++ b/src/player.c @@ -115,6 +115,8 @@ extern void weapon_minimap(const double res, extern void planets_minimap(const double res, const double w, const double h, const RadarShape shape); // space.c // Internal. +static void player_nameClose(char* str); +static void player_newMake(void); static void rect_parse(const xmlNodePtr parent, double* x, double* y, double* w, double* h); static int gui_parse(const xmlNodePtr parent, const char* name); @@ -124,8 +126,33 @@ static void gui_renderBar(const glColour* c, const Rect* r, const double w); void player_dead(void); void player_destroyed(void); -// Create a new player. +// Prompt player name. void player_new(void) { + unsigned int wid; + + player_setFlag(PLAYER_DESTROYED); + + wid = window_create("Player Name", -1, -1, 240, 140); + + window_addText(wid, 30, -30, 180, 20, 0, "txtInfo", + &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); +} + +static void player_nameClose(char* str) { + (void)str; + unsigned int wid; + + wid = window_get("Player Name"); + player_name = strdup(window_getInput(wid, "inpName")); + window_destroy(wid); + + player_newMake(); +} + +// Create a new player. +static void player_newMake(void) { Ship* ship; char system[20]; uint32_t bufsize; diff --git a/src/space.c b/src/space.c index d12dd68..0914a0b 100644 --- a/src/space.c +++ b/src/space.c @@ -702,6 +702,8 @@ void space_render(double dt) { // Render the planets. void planets_render(void) { + if(cur_system == NULL) return; + int i; for(i = 0; i < cur_system->nplanets; i++) gl_blitSprite(cur_system->planets[i].gfx_space, diff --git a/src/toolkit.c b/src/toolkit.c index 2ae6cea..94e9195 100644 --- a/src/toolkit.c +++ b/src/toolkit.c @@ -14,7 +14,8 @@ typedef enum WidgetType_ { WIDGET_IMAGE, WIDGET_LIST, WIDGET_RECT, - WIDGET_CUST + WIDGET_CUST, + WIDGET_INPUT } WidgetType; typedef enum WidgetStatus_ { @@ -48,6 +49,7 @@ typedef struct Widget_ { struct { // Widget image. glTexture* image; + int border; } img; struct { // Widget list. @@ -68,6 +70,13 @@ typedef struct Widget_ { void(*render) (double bx, double by, double bw, double bh); void(*mouse) (SDL_Event* event, double bx, double by); } cst; + struct { + // Widget input. + char* input; // Input buffer. + int max; // Max length. + int oneline; // Is it a one-line? '\n' and co. if no. + int view, pos; // View and cursor position. + } inp; } dat; } Widget; @@ -121,6 +130,7 @@ static void toolkit_renderImage(Widget* img, double bx, double by); static void toolkit_renderList(Widget* lst, double bx, double by); static void toolkit_renderRect(Widget* rct, double bx, double by); static void toolkit_renderCust(Widget* cst, double bx, double by); +static void toolkit_renderInput(Widget* inp, double bx, double by); static void toolkit_drawOutline(double x, double y, double w, double h, double b, glColour* c, glColour* lc); static void toolkit_drawRect(double x, double y, double w, double h, @@ -183,7 +193,7 @@ void window_addText(const unsigned int wid, const int x, const int y, // Add a graphic to the window. void window_addImage(const unsigned int wid, const int x, const int y, - char* name, glTexture* image) { + char* name, glTexture* image, int border) { Window* wdw = window_wget(wid); Widget* wgt = window_newWidget(wdw); @@ -192,7 +202,8 @@ void window_addImage(const unsigned int wid, const int x, const int y, wgt->name = strdup(name); // Set the properties. - wgt->dat.img.image = image; + wgt->dat.img.image = image; + wgt->dat.img.border = border; wgt->w = (image == NULL) ? 0 : wgt->dat.img.image->sw; wgt->h = (image == NULL) ? 0 : wgt->dat.img.image->sh; if(x < 0) wgt->x = wdw->w - wgt->w + x; @@ -274,6 +285,36 @@ void window_addCust(const unsigned int wid, const int x, const int y, else wgt->y = (double) y; } +// Add an input widget. +void window_addInput(const unsigned int wid, + const int x, const int y, + const int w, const int h, + char* name, const int max, const int oneline) { + + Window* wdw = window_wget(wid); + Widget* wgt = window_newWidget(wdw); + + // Generic. + wgt->type = WIDGET_INPUT; + wgt->name = strdup(name); + + // Specific. + wgt->dat.inp.max = max; + wgt->dat.inp.oneline = oneline; + wgt->dat.inp.pos = 0; + wgt->dat.inp.view = 0; + wgt->dat.inp.input = malloc(sizeof(char)*max); + memset(wgt->dat.inp.input, 0, max*sizeof(char)); + + // Position/Size. + wgt->w = (double)w; + wgt->h = (double)h; + if(x < 0) wgt->x = wdw->w - wgt->w + x; + else wgt->x = (double)x; + if(y < 0) wgt->y = wdw->h - wgt->h + y; + else wgt->y = (double)y; +} + // Return pointer to newly allocated widget. static Widget* window_newWidget(Window* w) { Widget* wgt = NULL; @@ -325,6 +366,12 @@ glTexture* window_getImage(const unsigned int wid, char* name) { return (wgt) ? wgt->dat.img.image : NULL; } +// Get the input from an input widget. +char* window_getInput(const unsigned int wid, char* name) { + Widget* wgt = window_getwgt(wid, name); + return(wgt) ? wgt->dat.inp.input : NULL; +} + // Check if a window exists. int window_exists(const char* wdwname) { int i; @@ -412,6 +459,9 @@ static void widget_cleanup(Widget* widget) { free(widget->dat.lst.options); } break; + case WIDGET_INPUT: + free(widget->dat.inp.input); // Free the input buffer. + break; default: break; } @@ -680,6 +730,9 @@ static void window_render(Window* w) { case WIDGET_CUST: toolkit_renderCust(&w->widgets[i], x, y); break; + case WIDGET_INPUT: + toolkit_renderInput(&w->widgets[i], x, y); + break; } } // Focus widget. @@ -769,12 +822,15 @@ static void toolkit_renderImage(Widget* img, double bx, double by) { x + (double)gl_screen.w/2., y + (double)gl_screen.h/2., NULL); - // Inner outline (outwards). - toolkit_drawOutline(x, y+1, img->dat.img.image->sw-1, - img->dat.img.image->sh-1, 1., toolkit_colLight, toolkit_col); - // Outter outline. - toolkit_drawOutline(x, y+1, img->dat.img.image->sw-1, - img->dat.img.image->sh-1, 2., toolkit_colDark, NULL); + if(img->dat.img.border) { + // Inner outline (outwards). + toolkit_drawOutline(x, y+1, img->dat.img.image->sw-1, + img->dat.img.image->sh-1, 1., toolkit_colLight, + toolkit_col); + // Outter outline. + toolkit_drawOutline(x, y+1, img->dat.img.image->sw-1, + img->dat.img.image->sh-1, 2., toolkit_colDark, NULL); + } } // Render the list. @@ -831,7 +887,7 @@ static void toolkit_renderRect(Widget* rct, double bx, double by) { } } -// Render a customg widget. +// Render a custom widget. static void toolkit_renderCust(Widget* cst, double bx, double by) { double x, y; @@ -849,6 +905,27 @@ static void toolkit_renderCust(Widget* cst, double bx, double by) { (*cst->dat.cst.render) (x, y, cst->w, cst->h); } +// Render an input widget. +static void toolkit_renderInput(Widget* inp, double bx, double by) { + double x, y; + + x = bx + inp->x; + y = by + inp->y; + + // Main background. + toolkit_drawRect(x, y, inp->w, inp->h, &cWhite, NULL); + + gl_printText(&gl_smallFont, inp->w-10., inp->h, + x+5., y, &cBlack, inp->dat.inp.input); + + // Inner outline. + toolkit_drawOutline(x, y, inp->w, inp->h, 0., + toolkit_colLight, toolkit_col); + // Outter outline. + toolkit_drawOutline(x, y, inp->w, inp->h, 1., + toolkit_colDark, NULL); +} + // Render the window. void toolkit_render(void) { int i; @@ -983,7 +1060,24 @@ static void toolkit_unregKey(SDLKey key) { } static int toolkit_keyEvent(SDL_Event* event) { - SDLKey key = event->key.keysym.sym; + Window* wdw; + Widget* wgt; + SDLKey key; + + wdw = &windows[nwindows-1]; + wgt = (wdw->focus >= 0) ? &wdw->widgets[wdw->focus] : NULL; + key = event->key.keysym.sym; + + if(wgt && (wgt->type == WIDGET_INPUT)) { + // Grab all the events it wants. + if(wgt->dat.inp.oneline && isalnum(key)) { + if((event->type == SDL_KEYDOWN) && (wgt->dat.inp.pos < wgt->dat.inp.max)) + wgt->dat.inp.input[wgt->dat.inp.pos++] = key; + DEBUG("%s", wgt->dat.inp.input); + return 1; + } + } + switch(key) { case SDLK_TAB: if(event->type == SDL_KEYDOWN) @@ -1058,6 +1152,7 @@ static int toolkit_isFocusable(Widget* wgt) { switch(wgt->type) { case WIDGET_BUTTON: case WIDGET_LIST: + case WIDGET_INPUT: return 1; default: return 0; diff --git a/src/toolkit.h b/src/toolkit.h index 9318bb9..2eb9748 100644 --- a/src/toolkit.h +++ b/src/toolkit.h @@ -18,7 +18,7 @@ void window_addText(const unsigned int wid, const int x, const int y, glFont* font, glColour* colour, char* string); void window_addImage(const unsigned int wid, const int x, const int y, - char* name, glTexture* image); + char* name, glTexture* image, int border); void window_addList(const unsigned int wid, const int x, const int y, // Position. @@ -38,6 +38,11 @@ void window_addCust(const unsigned int wid, void(*render) (double x, double y, double w, double h), void(*mouse) (SDL_Event* event, double x, double y)); +void window_addInput(const unsigned int wid, + const int x, const int y, + const int w, const int h, + char* name, const int max, const int oneline); + // Popups and alerts. void toolkit_alert(const char* fmt, ...); @@ -45,13 +50,12 @@ void toolkit_alert(const char* fmt, ...); void window_modifyText(const unsigned int wid, char* name, char* newstring); void window_modifyImage(const unsigned int wid, char* name, glTexture* image); -// Get! -glTexture* window_getImage(const unsigned int wid, char* name); - // Get the window by name. int window_exists(const char* wdwname); unsigned int window_get(const char* wdwname); char* toolkit_getList(const unsigned int wid, char* name); +glTexture* window_getImage(const unsigned int wid, char* name); +char* window_getInput(const unsigned int wid, char* name); // Destroy window. void window_destroy(const unsigned int wid);