-renamed ViewPort to Camera
[supertux.git] / src / menu.cpp
index baa7032..f9fc8ae 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()
 {
@@ -317,6 +326,8 @@ Menu::Menu()
   arrange_left = 0;
   active_item  = 0;
   effect.init(false);
+
+  joystick_timer.init(true);
 }
 
 void Menu::set_pos(int x, int y, float rw, float rh)
@@ -480,6 +491,9 @@ Menu::action()
   }
 
   menuaction = MENU_ACTION_NONE;
+
+  if (active_item >= int(item.size()))
+    active_item = int(item.size()) - 1;
 }
 
 int
@@ -558,7 +572,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;
@@ -570,8 +585,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)
       {
@@ -742,7 +759,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)
       {
@@ -803,13 +820,26 @@ Menu::event(SDL_Event& event)
   case  SDL_JOYAXISMOTION:
     if(event.jaxis.axis == joystick_keymap.y_axis)
     {
-      if (event.jaxis.value > 1024)
+      if (event.jaxis.value > joystick_keymap.dead_zone && !joystick_timer.started())
+      {
         menuaction = MENU_ACTION_DOWN;
-      else if (event.jaxis.value < -1024)
+        joystick_timer.start(JOYSTICK_MENU_DELAY);
+      }
+      else if (event.jaxis.value < -joystick_keymap.dead_zone && !joystick_timer.started())
+      {
         menuaction = MENU_ACTION_UP;
+        joystick_timer.start(JOYSTICK_MENU_DELAY);
+      }
+      else
+        joystick_timer.stop();
     }
     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: