[Add] Add player name on new game. Some hacks, but it works. So ship it.

This commit is contained in:
Allanis 2013-03-22 00:23:14 +00:00
parent 053aab14c9
commit 7ea7bf46a7
2 changed files with 59 additions and 11 deletions

View File

@ -145,6 +145,7 @@ static void player_nameClose(char* str) {
unsigned int wid; unsigned int wid;
wid = window_get("Player Name"); wid = window_get("Player Name");
if(player_name) free(player_name);
player_name = strdup(window_getInput(wid, "inpName")); player_name = strdup(window_getInput(wid, "inpName"));
window_destroy(wid); window_destroy(wid);

View File

@ -113,6 +113,7 @@ static void widget_cleanup(Widget* widget);
static Window* window_wget(const unsigned int wid); static Window* window_wget(const unsigned int wid);
static Widget* window_getwgt(const unsigned int wid, char* name); static Widget* window_getwgt(const unsigned int wid, char* name);
// Input. // Input.
static int toolkit_inputInput(Uint8 type, Widget* inp, SDLKey key);
static void toolkit_mouseEvent(SDL_Event* event); static void toolkit_mouseEvent(SDL_Event* event);
static int toolkit_keyEvent(SDL_Event* event); static int toolkit_keyEvent(SDL_Event* event);
// Focus. // Focus.
@ -907,7 +908,7 @@ static void toolkit_renderCust(Widget* cst, double bx, double by) {
// Render an input widget. // Render an input widget.
static void toolkit_renderInput(Widget* inp, double bx, double by) { static void toolkit_renderInput(Widget* inp, double bx, double by) {
double x, y; double x, y, ty;
x = bx + inp->x; x = bx + inp->x;
y = by + inp->y; y = by + inp->y;
@ -915,8 +916,12 @@ static void toolkit_renderInput(Widget* inp, double bx, double by) {
// Main background. // Main background.
toolkit_drawRect(x, y, inp->w, inp->h, &cWhite, NULL); toolkit_drawRect(x, y, inp->w, inp->h, &cWhite, NULL);
// Center vertically.
if(inp->dat.inp.oneline) ty = y - (inp->h - gl_smallFont.h)/2.;
gl_printText(&gl_smallFont, inp->w-10., inp->h, gl_printText(&gl_smallFont, inp->w-10., inp->h,
x+5., y, &cBlack, inp->dat.inp.input); x+5. + gl_screen.w/2., ty + gl_screen.h/2.,
&cBlack, inp->dat.inp.input);
// Inner outline. // Inner outline.
toolkit_drawOutline(x, y, inp->w, inp->h, 0., toolkit_drawOutline(x, y, inp->w, inp->h, 0.,
@ -926,6 +931,37 @@ static void toolkit_renderInput(Widget* inp, double bx, double by) {
toolkit_colDark, NULL); toolkit_colDark, NULL);
} }
// Handle input for input widget.
static int toolkit_inputInput(Uint8 type, Widget* inp, SDLKey key) {
SDLMod mods;
if(inp->type != WIDGET_INPUT) return 0;
mods = SDL_GetModState();
if(inp->dat.inp.oneline && isascii(key)) {
// Backspace -> delete text.
if((type == SDL_KEYDOWN) && (key == '\b') && (inp->dat.inp.pos > 0))
inp->dat.inp.input[--inp->dat.inp.pos] = '\0';
else if((type == SDL_KEYDOWN) && (inp->dat.inp.pos < inp->dat.inp.max)) {
if((key == SDLK_RETURN) && !inp->dat.inp.oneline)
inp->dat.inp.input[inp->dat.inp.pos++] = '\n';
// Uppder case characters.
else if(isalpha(key) && (mods & (KMOD_LSHIFT | KMOD_RSHIFT)))
inp->dat.inp.input[inp->dat.inp.pos++] = toupper(key);
// Rest.
else if(!iscntrl(key))
inp->dat.inp.input[inp->dat.inp.pos++] = key;
// Didn't get a useful key.
else return 0;
}
return 1;
}
return 0;
}
// Render the window. // Render the window.
void toolkit_render(void) { void toolkit_render(void) {
int i; int i;
@ -1068,15 +1104,16 @@ static int toolkit_keyEvent(SDL_Event* event) {
wgt = (wdw->focus >= 0) ? &wdw->widgets[wdw->focus] : NULL; wgt = (wdw->focus >= 0) ? &wdw->widgets[wdw->focus] : NULL;
key = event->key.keysym.sym; key = event->key.keysym.sym;
if(wgt && (wgt->type == WIDGET_INPUT)) { // Hack to simulate key repetition.
if((key == SDLK_BACKSPACE) || isalnum(key)) {
if(event->type == SDL_KEYDOWN) toolkit_regKey(key);
else if(event->type == SDL_KEYUP) toolkit_unregKey(key);
}
// Handle input widgets.
if(wgt && (wgt->type == WIDGET_INPUT))
// Grab all the events it wants. // Grab all the events it wants.
if(wgt->dat.inp.oneline && isalnum(key)) { if(toolkit_inputInput(event->type, wgt, key)) return 1;
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:
@ -1110,6 +1147,11 @@ static int toolkit_keyEvent(SDL_Event* event) {
void toolkit_update(void) { void toolkit_update(void) {
unsigned int t; unsigned int t;
Window* wdw;
Widget* wgt;
wdw = &windows[nwindows-1];
wgt = (wdw->focus >= 0) ? &wdw->widgets[wdw->focus] : NULL;
t = SDL_GetTicks(); t = SDL_GetTicks();
@ -1119,6 +1161,11 @@ void toolkit_update(void) {
return; return;
input_keyCounter++; input_keyCounter++;
if(wgt && (wgt->type == WIDGET_INPUT) &&
(input_key == SDLK_BACKSPACE || isalnum(input_key)))
toolkit_inputInput(SDL_KEYDOWN, wgt, input_key);
switch(input_key) { switch(input_key) {
case SDLK_UP: case SDLK_UP:
toolkit_listScroll(toolkit_getFocus(), +1); toolkit_listScroll(toolkit_getFocus(), +1);