From 7ea7bf46a747fed571edce92646ff4d4ff43e12f Mon Sep 17 00:00:00 2001
From: Allanis <allanis@saracraft.net>
Date: Fri, 22 Mar 2013 00:23:14 +0000
Subject: [PATCH] [Add] Add player name on new game. Some hacks, but it works.
 So ship it.

---
 src/player.c  |  1 +
 src/toolkit.c | 69 +++++++++++++++++++++++++++++++++++++++++++--------
 2 files changed, 59 insertions(+), 11 deletions(-)

diff --git a/src/player.c b/src/player.c
index 69bd882..28c28be 100644
--- a/src/player.c
+++ b/src/player.c
@@ -145,6 +145,7 @@ static void player_nameClose(char* str) {
   unsigned int wid;
 
   wid = window_get("Player Name");
+  if(player_name) free(player_name);
   player_name = strdup(window_getInput(wid, "inpName"));
   window_destroy(wid);
 
diff --git a/src/toolkit.c b/src/toolkit.c
index 94e9195..73ea46b 100644
--- a/src/toolkit.c
+++ b/src/toolkit.c
@@ -113,8 +113,9 @@ static void widget_cleanup(Widget* widget);
 static Window* window_wget(const unsigned int wid);
 static Widget* window_getwgt(const unsigned int wid, char* name);
 // Input.
+static int  toolkit_inputInput(Uint8 type, Widget* inp, SDLKey key);
 static void toolkit_mouseEvent(SDL_Event* event);
-static int toolkit_keyEvent(SDL_Event* event);
+static int  toolkit_keyEvent(SDL_Event* event);
 // Focus.
 static void toolkit_nextFocus(void);
 static int toolkit_isFocusable(Widget* wgt);
@@ -907,7 +908,7 @@ static void toolkit_renderCust(Widget* cst, double bx, double by) {
 
 // Render an input widget.
 static void toolkit_renderInput(Widget* inp, double bx, double by) {
-  double x, y;
+  double x, y, ty;
 
   x = bx + inp->x;
   y = by + inp->y;
@@ -915,8 +916,12 @@ static void toolkit_renderInput(Widget* inp, double bx, double by) {
   // Main background.
   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,
-               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.
   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);
 }
 
+// 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.
 void toolkit_render(void) {
   int i;
@@ -1068,16 +1104,17 @@ static int toolkit_keyEvent(SDL_Event* event) {
   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;
-    }
+  // 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.
+    if(toolkit_inputInput(event->type, wgt, key)) return 1;
+
   switch(key) {
   case SDLK_TAB:
     if(event->type == SDL_KEYDOWN)
@@ -1110,6 +1147,11 @@ static int toolkit_keyEvent(SDL_Event* event) {
 
 void toolkit_update(void) {
   unsigned int t;
+  Window* wdw;
+  Widget* wgt;
+
+  wdw = &windows[nwindows-1];
+  wgt = (wdw->focus >= 0) ? &wdw->widgets[wdw->focus] : NULL;
 
   t = SDL_GetTicks();
 
@@ -1119,6 +1161,11 @@ void toolkit_update(void) {
     return;
 
   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) {
   case SDLK_UP:
     toolkit_listScroll(toolkit_getFocus(), +1);