- fixed an ugly hack I was using before for the JS config menu
[supertux.git] / src / menu.cpp
index 5b567c5..8b16164 100644 (file)
 #endif
 
 #include <iostream>
+#include <sstream>
 #include <stdlib.h>
 #include <stdio.h>
-#include <string.h>
+#include <string>
 #include <assert.h>
 
 #include "defines.h"
@@ -239,7 +240,7 @@ std::string MenuItem::get_input_with_symbol(bool active_item)
   return string;
 }
 
-/* Set ControlField a key */
+/* Set ControlField for keyboard key */
 void Menu::get_controlfield_key_into_input(MenuItem *item)
 {
   switch(*item->int_p)
@@ -290,6 +291,14 @@ void Menu::get_controlfield_key_into_input(MenuItem *item)
   }
 }
 
+/* Set ControlField for joystick button */
+void Menu::get_controlfield_js_into_input(MenuItem *item)
+{
+  std::ostringstream oss;
+  oss << "Button " << *item->int_p;
+  item->change_input(oss.str().c_str());
+}
+
 /* Free a menu and all its items */
 Menu::~Menu()
 {
@@ -481,8 +490,8 @@ Menu::action()
 
   menuaction = MENU_ACTION_NONE;
 
-  if (active_item >= item.size()) 
-    active_item = int(item.size()) - 1;
+  //if (active_item >= int(item.size()))
+  //  active_item = int(item.size()) - 1;
 }
 
 int
@@ -561,7 +570,8 @@ Menu::draw_item(int index, // Position of the current item in the menu
     }
   case MN_TEXTFIELD:
   case MN_NUMFIELD:
-  case MN_CONTROLFIELD:
+  case MN_CONTROLFIELD_KB:
+  case MN_CONTROLFIELD_JS:
     {
       int input_pos = input_width/2;
       int text_pos  = (text_width + font_width)/2;
@@ -573,8 +583,10 @@ Menu::draw_item(int index, // Position of the current item in the menu
                input_width + font_width, 18,
                0,0,0,128);
 
-      if(pitem.kind == MN_CONTROLFIELD)
+      if(pitem.kind == MN_CONTROLFIELD_KB)
         get_controlfield_key_into_input(&pitem);
+      else if (pitem.kind == MN_CONTROLFIELD_JS)
+        get_controlfield_js_into_input(&pitem);
 
       if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
       {
@@ -745,7 +757,7 @@ Menu::event(SDL_Event& event)
       /* An International Character. */
     }
 
-    if(item[active_item].kind == MN_CONTROLFIELD)
+    if(item[active_item].kind == MN_CONTROLFIELD_KB)
     {
       if(key == SDLK_ESCAPE)
       {
@@ -813,6 +825,11 @@ Menu::event(SDL_Event& event)
     }
     break;
   case  SDL_JOYBUTTONDOWN:
+    if (item[active_item].kind == MN_CONTROLFIELD_JS)
+    {
+      *item[active_item].int_p = key;
+      menuaction = MENU_ACTION_DOWN;
+    }
     menuaction = MENU_ACTION_HIT;
     break;
   case SDL_MOUSEBUTTONDOWN: