[Add] Input boxes in toolkit. Need to hack fonts up before it works.

This commit is contained in:
Allanis 2013-03-21 23:51:58 +00:00
parent 88b2f1b714
commit 053aab14c9
5 changed files with 147 additions and 19 deletions

View File

@ -203,7 +203,7 @@ static void outfits(void) {
// Fancy 128x128 image. // Fancy 128x128 image.
window_addRect(secondary_wid, -20, -50, 128, 128, "rctImage", &cBlack, 0); 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, window_addCust(secondary_wid, -40-BUTTON_WIDTH, 60+2*BUTTON_HEIGHT,
BUTTON_WIDTH, BUTTON_HEIGHT, BUTTON_WIDTH, BUTTON_HEIGHT,
@ -391,7 +391,7 @@ static void shipyard(void) {
128, 96, "rctTarget", &cBlack, 0); 128, 96, "rctTarget", &cBlack, 0);
window_addImage(secondary_wid, -40-128, -50-96, window_addImage(secondary_wid, -40-128, -50-96,
"imgTarget", NULL); "imgTarget", NULL, 1);
window_addText(secondary_wid, 40+200+40, -55, window_addText(secondary_wid, 40+200+40, -55,
80, 96, 0, "txtSDesc", &gl_smallFont, &cDConsole, 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); land_wid = window_create(p->name, -1, -1, LAND_WIDTH, LAND_HEIGHT);
// Pretty display. // 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, window_addText(land_wid, 440, 80, LAND_WIDTH-460, 460, 0,
"txtPlanetDesc", &gl_smallFont, &cBlack, p->description); "txtPlanetDesc", &gl_smallFont, &cBlack, p->description);
// Buttons. // Buttons.

View File

@ -115,6 +115,8 @@ extern void weapon_minimap(const double res,
extern void planets_minimap(const double res, extern void planets_minimap(const double res,
const double w, const double h, const RadarShape shape); // space.c const double w, const double h, const RadarShape shape); // space.c
// Internal. // Internal.
static void player_nameClose(char* str);
static void player_newMake(void);
static void rect_parse(const xmlNodePtr parent, static void rect_parse(const xmlNodePtr parent,
double* x, double* y, double* w, double* h); double* x, double* y, double* w, double* h);
static int gui_parse(const xmlNodePtr parent, const char* name); 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_dead(void);
void player_destroyed(void); void player_destroyed(void);
// Create a new player. // Prompt player name.
void player_new(void) { 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; Ship* ship;
char system[20]; char system[20];
uint32_t bufsize; uint32_t bufsize;

View File

@ -702,6 +702,8 @@ void space_render(double dt) {
// Render the planets. // Render the planets.
void planets_render(void) { void planets_render(void) {
if(cur_system == NULL) return;
int i; int i;
for(i = 0; i < cur_system->nplanets; i++) for(i = 0; i < cur_system->nplanets; i++)
gl_blitSprite(cur_system->planets[i].gfx_space, gl_blitSprite(cur_system->planets[i].gfx_space,

View File

@ -14,7 +14,8 @@ typedef enum WidgetType_ {
WIDGET_IMAGE, WIDGET_IMAGE,
WIDGET_LIST, WIDGET_LIST,
WIDGET_RECT, WIDGET_RECT,
WIDGET_CUST WIDGET_CUST,
WIDGET_INPUT
} WidgetType; } WidgetType;
typedef enum WidgetStatus_ { typedef enum WidgetStatus_ {
@ -48,6 +49,7 @@ typedef struct Widget_ {
struct { struct {
// Widget image. // Widget image.
glTexture* image; glTexture* image;
int border;
} img; } img;
struct { struct {
// Widget list. // Widget list.
@ -68,6 +70,13 @@ typedef struct Widget_ {
void(*render) (double bx, double by, double bw, double bh); void(*render) (double bx, double by, double bw, double bh);
void(*mouse) (SDL_Event* event, double bx, double by); void(*mouse) (SDL_Event* event, double bx, double by);
} cst; } 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; } dat;
} Widget; } 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_renderList(Widget* lst, double bx, double by);
static void toolkit_renderRect(Widget* rct, 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_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, static void toolkit_drawOutline(double x, double y, double w,
double h, double b, glColour* c, glColour* lc); double h, double b, glColour* c, glColour* lc);
static void toolkit_drawRect(double x, double y, double w, double h, 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. // Add a graphic to the window.
void window_addImage(const unsigned int wid, const int x, const int y, 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); Window* wdw = window_wget(wid);
Widget* wgt = window_newWidget(wdw); 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); wgt->name = strdup(name);
// Set the properties. // 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->w = (image == NULL) ? 0 : wgt->dat.img.image->sw;
wgt->h = (image == NULL) ? 0 : wgt->dat.img.image->sh; wgt->h = (image == NULL) ? 0 : wgt->dat.img.image->sh;
if(x < 0) wgt->x = wdw->w - wgt->w + x; 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; 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. // Return pointer to newly allocated widget.
static Widget* window_newWidget(Window* w) { static Widget* window_newWidget(Window* w) {
Widget* wgt = NULL; Widget* wgt = NULL;
@ -325,6 +366,12 @@ glTexture* window_getImage(const unsigned int wid, char* name) {
return (wgt) ? wgt->dat.img.image : NULL; 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. // Check if a window exists.
int window_exists(const char* wdwname) { int window_exists(const char* wdwname) {
int i; int i;
@ -412,6 +459,9 @@ static void widget_cleanup(Widget* widget) {
free(widget->dat.lst.options); free(widget->dat.lst.options);
} }
break; break;
case WIDGET_INPUT:
free(widget->dat.inp.input); // Free the input buffer.
break;
default: default:
break; break;
} }
@ -680,6 +730,9 @@ static void window_render(Window* w) {
case WIDGET_CUST: case WIDGET_CUST:
toolkit_renderCust(&w->widgets[i], x, y); toolkit_renderCust(&w->widgets[i], x, y);
break; break;
case WIDGET_INPUT:
toolkit_renderInput(&w->widgets[i], x, y);
break;
} }
} }
// Focus widget. // Focus widget.
@ -769,12 +822,15 @@ static void toolkit_renderImage(Widget* img, double bx, double by) {
x + (double)gl_screen.w/2., x + (double)gl_screen.w/2.,
y + (double)gl_screen.h/2., NULL); y + (double)gl_screen.h/2., NULL);
// Inner outline (outwards). if(img->dat.img.border) {
toolkit_drawOutline(x, y+1, img->dat.img.image->sw-1, // Inner outline (outwards).
img->dat.img.image->sh-1, 1., toolkit_colLight, toolkit_col); toolkit_drawOutline(x, y+1, img->dat.img.image->sw-1,
// Outter outline. img->dat.img.image->sh-1, 1., toolkit_colLight,
toolkit_drawOutline(x, y+1, img->dat.img.image->sw-1, toolkit_col);
img->dat.img.image->sh-1, 2., toolkit_colDark, NULL); // 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. // 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) { static void toolkit_renderCust(Widget* cst, double bx, double by) {
double x, y; 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); (*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. // Render the window.
void toolkit_render(void) { void toolkit_render(void) {
int i; int i;
@ -983,7 +1060,24 @@ static void toolkit_unregKey(SDLKey key) {
} }
static int toolkit_keyEvent(SDL_Event* event) { 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) { switch(key) {
case SDLK_TAB: case SDLK_TAB:
if(event->type == SDL_KEYDOWN) if(event->type == SDL_KEYDOWN)
@ -1058,6 +1152,7 @@ static int toolkit_isFocusable(Widget* wgt) {
switch(wgt->type) { switch(wgt->type) {
case WIDGET_BUTTON: case WIDGET_BUTTON:
case WIDGET_LIST: case WIDGET_LIST:
case WIDGET_INPUT:
return 1; return 1;
default: default:
return 0; return 0;

View File

@ -18,7 +18,7 @@ void window_addText(const unsigned int wid, const int x, const int y,
glFont* font, glColour* colour, char* string); glFont* font, glColour* colour, char* string);
void window_addImage(const unsigned int wid, const int x, const int y, 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, void window_addList(const unsigned int wid,
const int x, const int y, // Position. 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(*render) (double x, double y, double w, double h),
void(*mouse) (SDL_Event* event, double x, double y)); 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. // Popups and alerts.
void toolkit_alert(const char* fmt, ...); 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_modifyText(const unsigned int wid, char* name, char* newstring);
void window_modifyImage(const unsigned int wid, char* name, glTexture* image); 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. // Get the window by name.
int window_exists(const char* wdwname); int window_exists(const char* wdwname);
unsigned int window_get(const char* wdwname); unsigned int window_get(const char* wdwname);
char* toolkit_getList(const unsigned int wid, char* name); 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. // Destroy window.
void window_destroy(const unsigned int wid); void window_destroy(const unsigned int wid);