- removed joystick setup menu, since it won't work anyway
[supertux.git] / src / menu.cpp
index 079e85d..b8c41e9 100644 (file)
@@ -26,6 +26,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#include <assert.h>
 
 #include "defines.h"
 #include "globals.h"
@@ -48,7 +49,8 @@ Menu* main_menu      = 0;
 Menu* game_menu      = 0;
 Menu* worldmap_menu  = 0;
 Menu* options_menu   = 0;
-Menu* options_controls_menu   = 0;
+Menu* options_keys_menu     = 0;
+Menu* options_joystick_menu = 0;
 Menu* highscore_menu = 0;
 Menu* load_game_menu = 0;
 Menu* save_game_menu = 0;
@@ -97,7 +99,7 @@ Menu::set_current(Menu* menu)
 
 /* Return a pointer to a new menu item */
 MenuItem*
-MenuItem::create(MenuItemKind kind_, const char *text_, int init_toggle_, Menu* target_menu_, int* int_p_)
+MenuItem::create(MenuItemKind kind_, const char *text_, int init_toggle_, Menu* target_menu_, int id, int* int_p_)
 {
   MenuItem *pnew_item = new MenuItem;
   
@@ -122,6 +124,7 @@ MenuItem::create(MenuItemKind kind_, const char *text_, int init_toggle_, Menu*
   else
     pnew_item->list = NULL;
 
+  pnew_item->id = id;
   pnew_item->int_p = int_p_;
 
   return pnew_item;
@@ -149,6 +152,57 @@ MenuItem::change_input(const  char *text_)
     }
 }
 
+/* Set ControlField a key */
+void Menu::get_controlfield_key_into_input(MenuItem *item)
+{
+  switch(*item->int_p)
+  {
+  case SDLK_UP:
+    item->change_input("Up cursor");
+    break;
+  case SDLK_DOWN:
+    item->change_input("Down cursor");
+    break;
+  case SDLK_LEFT:
+    item->change_input("Left cursor");
+    break;
+  case SDLK_RIGHT:
+    item->change_input("Right cursor");
+    break;
+  case SDLK_RETURN:
+    item->change_input("Return");
+    break;
+  case SDLK_SPACE:
+    item->change_input("Space");
+    break;
+  case SDLK_RSHIFT:
+    item->change_input("Right Shift");
+    break;
+  case SDLK_LSHIFT:
+    item->change_input("Left Shift");
+    break;
+  case SDLK_RCTRL:
+    item->change_input("Right Control");
+    break;
+  case SDLK_LCTRL:
+    item->change_input("Left Control");
+    break;
+  case SDLK_RALT:
+    item->change_input("Right Alt");
+    break;
+  case SDLK_LALT:
+    item->change_input("Left Alt");
+    break;
+  default:
+    {
+      char tmp[64];
+      snprintf(tmp, 64, "%d", *item->int_p);
+      item->change_input(tmp);
+    }
+    break;
+  }
+}
+
 /* Free a menu and all its items */
 Menu::~Menu()
 {
@@ -173,7 +227,6 @@ Menu::Menu()
   
   pos_x        = screen->w/2;
   pos_y        = screen->h/2;
-  has_backitem = false;
   arrange_left = 0;
   active_item  = 0;
   effect.init(false);
@@ -186,21 +239,15 @@ void Menu::set_pos(int x, int y, float rw, float rh)
 }
 
 void
-Menu::additem(MenuItemKind kind_, const std::string& text_, int toggle_, Menu* menu_, int* int_p)
+Menu::additem(MenuItemKind kind_, const std::string& text_, int toggle_, Menu* menu_, int id, int* int_p)
 {
-  if(kind_ == MN_BACK)
-    has_backitem = true;
-
-  additem(MenuItem::create(kind_, text_.c_str(), toggle_, menu_, int_p));
+  additem(MenuItem::create(kind_, text_.c_str(), toggle_, menu_, id, int_p));
 }
 
 /* Add an item to a menu */
 void
 Menu::additem(MenuItem* pmenu_item)
 {
-  if(pmenu_item->kind == MN_BACK)
-    has_backitem = true;
-
   item.push_back(*pmenu_item);
   delete pmenu_item;
 }
@@ -278,8 +325,6 @@ Menu::action()
                 Menu::set_current(0); 
                 item[active_item].toggled = true;
                 break;
-              case MN_CONTROLFIELD:
-                break;
 
               case MN_BACK:
                 Menu::pop_current();
@@ -350,30 +395,10 @@ Menu::action()
 int
 Menu::check()
 {
-  return hit_item;
-  /*
-  if (item.size() != 0)
-    {
-      if((item[active_item].kind == MN_ACTION
-          || item[active_item].kind == MN_TEXTFIELD
-          || item[active_item].kind == MN_NUMFIELD)
-          && item[active_item].toggled)
-        { 
-          item[active_item].toggled = false;
-          Menu::set_current(0);
-          return active_item;
-        }
-      else if(item[active_item].kind == MN_TOGGLE 
-              || item[active_item].kind == MN_GOTO)
-        {
-          return active_item;
-        }
-      else
-        return -1;
-    }
+  if (hit_item != -1)
+    return item[hit_item].id;
   else
     return -1;
-  */
 }
 
 void
@@ -381,7 +406,7 @@ Menu::draw_item(int index, // Position of the current item in the menu
                 int menu_width,
                 int menu_height)
 {
-  const MenuItem& pitem = item[index];
+  MenuItem& pitem = item[index];
 
   int font_width  = 16;
   int effect_offset = 0;
@@ -455,6 +480,9 @@ 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)
+          get_controlfield_key_into_input(&pitem);
+
         gold_text->draw_align(pitem.input,
                               x_pos + text_pos, y_pos,
                               A_HMIDDLE, A_VMIDDLE, 2);
@@ -464,32 +492,6 @@ Menu::draw_item(int index, // Position of the current item in the menu
                               A_HMIDDLE, A_VMIDDLE, shadow_size);
         break;
       }
-//    case MN_CONTROLFIELD:
-//      {
-        /* display key */  // FIXME: the key number is not that obvious to the user :P
-/*        char str[12];
-        sprintf(str, "%i", *pitem.int_p);
-        input_width = strlen(str) * font_width;
-
-        int input_pos = input_width/2;
-        int text_pos  = (text_width + font_width)/2;
-
-        fillrect(x_pos - input_pos + text_pos - 1, y_pos - 10,
-                 input_width + font_width + 2, 20,
-                 255,255,255,255);
-        fillrect(x_pos - input_pos + text_pos, y_pos - 9,
-                 input_width + font_width, 18,
-                 0,0,0,128);
-
-        gold_text->draw_align(str,
-                              x_pos + text_pos, y_pos,
-                              A_HMIDDLE, A_VMIDDLE, 2);
-
-        text_font->draw_align(pitem.text,
-                              x_pos - (input_width + font_width)/2, y_pos,
-                              A_HMIDDLE, A_VMIDDLE, shadow_size);
-        break;
-      }*/
     case MN_STRINGSELECT:
       {
         int list_pos_2 = list_width + font_width;
@@ -591,6 +593,25 @@ Menu::draw()
     }
 }
 
+MenuItem&
+Menu::get_item_by_id(int id)
+{
+  for(std::vector<MenuItem>::iterator i = item.begin(); i != item.end(); ++i) {
+    if(i->id == id)
+      return *i;
+  }
+
+  assert(false);
+  static MenuItem dummyitem;
+  return dummyitem;
+}
+
+bool
+Menu::isToggled(int id)
+{
+  return get_item_by_id(id).toggled;
+}
+
 /* Check for menu event */
 void
 Menu::event(SDL_Event& event)
@@ -619,53 +640,12 @@ Menu::event(SDL_Event& event)
 
       if(item[active_item].kind == MN_CONTROLFIELD)
         {
-        *item[active_item].int_p = event.key.keysym.unicode;
-        if(ch[0] != '\0')
-          strcpy(item[active_item].input, ch);
-        else
-          switch(key)
-           {
-           case SDLK_UP:
-             strcpy(item[active_item].input, "Up cursor");
-             break;
-           case SDLK_DOWN:
-             strcpy(item[active_item].input, "Down cursor");
-             break;
-           case SDLK_LEFT:
-             strcpy(item[active_item].input, "Left cursor");
-             break;
-           case SDLK_RIGHT:
-             strcpy(item[active_item].input, "Right cursor");
-             break;
-           case SDLK_RETURN:
-             strcpy(item[active_item].input, "Return");
-             break;
-           case SDLK_SPACE:
-             strcpy(item[active_item].input, "Space");
-             break;
-           case SDLK_RSHIFT:
-             strcpy(item[active_item].input, "Right Shift");
-             break;
-           case SDLK_LSHIFT:
-             strcpy(item[active_item].input, "Left Shift");
-             break;
-           case SDLK_RCTRL:
-             strcpy(item[active_item].input, "Right Control");
-             break;
-           case SDLK_LCTRL:
-             strcpy(item[active_item].input, "Left Control");
-             break;
-           case SDLK_RALT:
-             strcpy(item[active_item].input, "Right Alt");
-             break;
-           case SDLK_LALT:
-             strcpy(item[active_item].input, "Left Alt");
-             break;
-           default:
-             strcpy(item[active_item].input, "?");
-             break;
-           }
-        
+        if(key == SDLK_ESCAPE)
+          {
+          Menu::pop_current();
+          return;
+          }
+        *item[active_item].int_p = key;
         menuaction = MENU_ACTION_DOWN;
         return;
         }