Moved back button to an old dir.
[supertux.git] / src / menu.cpp
index 3fe7226..9d9cb32 100644 (file)
@@ -26,6 +26,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#include <assert.h>
 
 #include "defines.h"
 #include "globals.h"
@@ -38,6 +39,8 @@
 #include "timer.h"
 #include "high_scores.h"
 
+#define FLICK_CURSOR_TIME 500
+
 Surface* checkbox;
 Surface* checkbox_checked;
 Surface* back;
@@ -48,7 +51,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;
@@ -58,6 +62,44 @@ Menu* contrib_subset_menu   = 0;
 std::vector<Menu*> Menu::last_menus;
 Menu* Menu::current_ = 0;
 
+/* just displays a Yes/No text that can be used to confirm stuff */
+bool confirm_dialog(char *text)
+{
+white_text->drawf(text,0,0,A_HMIDDLE,A_VMIDDLE,2);
+red_text->drawf("(Y)es/(N)o",0,20,A_HMIDDLE,A_VMIDDLE,2);
+flipscreen();
+SDL_Event event;
+int done = 0;
+bool confirm = false;
+while(done == 0)
+  {
+  while(SDL_PollEvent(&event))
+    switch(event.type)
+      {
+      case SDL_KEYDOWN:                // key pressed
+        switch(event.key.keysym.sym)
+          {
+          case SDLK_y:
+            done = 1;
+            confirm = true;
+            break;
+          case SDLK_n:
+            done = 1;
+            break;
+          default:
+            break;
+          }
+        break;
+      case SDL_QUIT:           // quit signal
+        done = 1;
+      default:
+        break;
+      }
+  SDL_Delay(50);
+  }
+return confirm;
+}
+
 void
 Menu::push_current(Menu* pmenu)
 {
@@ -97,7 +139,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,8 +164,13 @@ 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_;
 
+  pnew_item->input_flickering = false;
+  pnew_item->input_flickering_timer.init(true);
+  pnew_item->input_flickering_timer.start(FLICK_CURSOR_TIME);
+
   return pnew_item;
 }
 
@@ -149,55 +196,82 @@ MenuItem::change_input(const  char *text_)
     }
 }
 
-/* Set ControlField a key */
-void MenuItem::set_controlfield_key(SDLKey key, char ch[])
+std::string MenuItem::get_input_with_symbol(bool active_item)
 {
-*int_p = key;
-if(ch[0] != '\0')
-  strcpy(input, ch);
+if(!active_item)
+  input_flickering = true;
 else
-  switch(key)
+  {
+  if(input_flickering_timer.get_left() < 0)
     {
-    case SDLK_UP:
-      strcpy(input, "Up cursor");
-      break;
-    case SDLK_DOWN:
-      strcpy(input, "Down cursor");
-      break;
-    case SDLK_LEFT:
-      strcpy(input, "Left cursor");
-      break;
-    case SDLK_RIGHT:
-      strcpy(input, "Right cursor");
-      break;
-    case SDLK_RETURN:
-      strcpy(input, "Return");
-      break;
-    case SDLK_SPACE:
-      strcpy(input, "Space");
-      break;
-    case SDLK_RSHIFT:
-      strcpy(input, "Right Shift");
-      break;
-    case SDLK_LSHIFT:
-      strcpy(input, "Left Shift");
-      break;
-    case SDLK_RCTRL:
-      strcpy(input, "Right Control");
-      break;
-    case SDLK_LCTRL:
-      strcpy(input, "Left Control");
-      break;
-    case SDLK_RALT:
-      strcpy(input, "Right Alt");
-      break;
-    case SDLK_LALT:
-      strcpy(input, "Left Alt");
-      break;
-    default:
-      strcpy(input, "?");
-      break;
+    if(input_flickering)
+      input_flickering = false;
+    else
+      input_flickering = true;
+    input_flickering_timer.start(FLICK_CURSOR_TIME);
+    }
+  }
+
+char str[1024];
+if(input_flickering)
+  sprintf(str,"%s_",input);
+else
+  sprintf(str,"%s ",input);
+
+std::string string = str;
+
+return string;
+}
+
+/* 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 */
@@ -224,7 +298,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);
@@ -237,21 +310,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;
 }
@@ -324,11 +391,14 @@ Menu::action()
                 break;
 
               case MN_ACTION:
-              case MN_TEXTFIELD:
-              case MN_NUMFIELD:
                 Menu::set_current(0); 
                 item[active_item].toggled = true;
                 break;
+              case MN_TEXTFIELD:
+              case MN_NUMFIELD:
+                menuaction = MENU_ACTION_DOWN;
+                action();
+                break;
 
               case MN_BACK:
                 Menu::pop_current();
@@ -399,30 +469,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
@@ -430,7 +480,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;
@@ -447,7 +497,7 @@ Menu::draw_item(int index, // Position of the current item in the menu
   int y_pos       = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
   int shadow_size = 2;
   int text_width  = strlen(pitem.text) * font_width;
-  int input_width = strlen(pitem.input) * font_width;
+  int input_width = (strlen(pitem.input)+ 1) * font_width;
   int list_width  = strlen(string_list_active(pitem.list)) * font_width;
   Text* text_font = white_text;
 
@@ -504,7 +554,18 @@ Menu::draw_item(int index, // Position of the current item in the menu
                  input_width + font_width, 18,
                  0,0,0,128);
 
-        gold_text->draw_align(pitem.input,
+        if(pitem.kind == MN_CONTROLFIELD)
+          get_controlfield_key_into_input(&pitem);
+
+        if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
+          {
+          if(active_item == index)
+            gold_text->draw_align((pitem.get_input_with_symbol(true)).c_str(), x_pos + text_pos, y_pos, A_HMIDDLE, A_VMIDDLE, 2);
+          else
+            gold_text->draw_align((pitem.get_input_with_symbol(false)).c_str(), x_pos + text_pos, y_pos, A_HMIDDLE, A_VMIDDLE, 2);
+          }
+        else
+          gold_text->draw_align(pitem.input,
                               x_pos + text_pos, y_pos,
                               A_HMIDDLE, A_VMIDDLE, 2);
 
@@ -614,6 +675,30 @@ 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;
+}
+
+int Menu::get_active_item_id()
+{
+return item[active_item].id;
+}
+
+bool
+Menu::isToggled(int id)
+{
+  return get_item_by_id(id).toggled;
+}
+
 /* Check for menu event */
 void
 Menu::event(SDL_Event& event)
@@ -642,7 +727,12 @@ Menu::event(SDL_Event& event)
 
       if(item[active_item].kind == MN_CONTROLFIELD)
         {
-        item[active_item].set_controlfield_key(key, ch);
+        if(key == SDLK_ESCAPE)
+          {
+          Menu::pop_current();
+          return;
+          }
+        *item[active_item].int_p = key;
         menuaction = MENU_ACTION_DOWN;
         return;
         }