Moved back button to an old dir.
[supertux.git] / src / menu.cpp
index 896591b..9d9cb32 100644 (file)
@@ -1,23 +1,32 @@
-/*
-  menu.c
-  
-  Super Tux - Menu
-  
-  by Tobias Glaesser
-  tobi.web@gmx.de
-  http://www.newbreedsoftware.com/supertux/
-  
-  December 20, 2003 - March 15, 2004
-*/
+//  $Id$
+// 
+//  SuperTux
+//  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
+//
+//  This program is free software; you can redistribute it and/or
+//  modify it under the terms of the GNU General Public License
+//  as published by the Free Software Foundation; either version 2
+//  of the License, or (at your option) any later version.
+//
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//  GNU General Public License for more details.
+// 
+//  You should have received a copy of the GNU General Public License
+//  along with this program; if not, write to the Free Software
+//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 #ifndef WIN32
 #include <sys/types.h>
 #include <ctype.h>
 #endif
 
+#include <iostream>
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#include <assert.h>
 
 #include "defines.h"
 #include "globals.h"
 #include "timer.h"
 #include "high_scores.h"
 
-/* (global) menu variables */
-MenuAction menuaction = MENU_ACTION_NONE;
-bool show_menu;
-bool menu_change;
+#define FLICK_CURSOR_TIME 500
 
 Surface* checkbox;
 Surface* checkbox_checked;
@@ -45,39 +51,95 @@ 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;
 Menu* contrib_menu   = 0;
 Menu* contrib_subset_menu   = 0;
 
-Menu* current_menu = 0;
+std::vector<Menu*> Menu::last_menus;
+Menu* Menu::current_ = 0;
 
-/* input implementation variables */
-int delete_character;
-char mn_input_char;
+/* 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)
+{
+  if (current_)
+    last_menus.push_back(current_);
+  
+  current_ = pmenu;
+  current_->effect.start(500);
+}
 
-/* Set the current menu */
 void
-Menu::set_current(Menu* pmenu)
+Menu::pop_current()
 {
-  if(pmenu != current_menu)
+  if (!last_menus.empty())
     {
-      menu_change  = true;
-      Menu* tmp = current_menu;
-      current_menu = pmenu;
-      if(tmp)
-        if(tmp->last_menu != pmenu)
-          current_menu->last_menu = tmp;
-
-      pmenu->effect.start(500);
+      current_ = last_menus.back();
+      current_->effect.start(500);
+
+      last_menus.pop_back();
     }
+  else
+    {
+      current_ = 0;
+    }
+}
+
+void
+Menu::set_current(Menu* menu)
+{
+  last_menus.clear();
+
+  if (menu)
+    menu->effect.start(500);
+  
+  current_ = menu;
 }
 
 /* Return a pointer to a new menu item */
 MenuItem*
-MenuItem::create(MenuItemKind kind_, const char *text_, int init_toggle_, Menu* target_menu_)
+MenuItem::create(MenuItemKind kind_, const char *text_, int init_toggle_, Menu* target_menu_, int id, int* int_p_)
 {
   MenuItem *pnew_item = new MenuItem;
   
@@ -101,6 +163,14 @@ 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;
 }
 
@@ -126,6 +196,84 @@ MenuItem::change_input(const  char *text_)
     }
 }
 
+std::string MenuItem::get_input_with_symbol(bool active_item)
+{
+if(!active_item)
+  input_flickering = true;
+else
+  {
+  if(input_flickering_timer.get_left() < 0)
+    {
+    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 */
 Menu::~Menu()
 {
@@ -140,40 +288,37 @@ Menu::~Menu()
     }
 }
 
+
 Menu::Menu()
 {
+  hit_item = -1;
+  menuaction = MENU_ACTION_NONE;
+  delete_character = 0;
+  mn_input_char = '\0';
+  
   pos_x        = screen->w/2;
   pos_y        = screen->h/2;
-  has_backitem = false;
-  last_menu    = 0;
   arrange_left = 0;
   active_item  = 0;
-  last_menu    = 0;
   effect.init(false);
 }
 
 void Menu::set_pos(int x, int y, float rw, float rh)
 {
-  pos_x = x + (int)((float)width() * rw);
-  pos_y = y + (int)((float)height() * rh);
+  pos_x = x + (int)((float)get_width() * rw);
+  pos_y = y + (int)((float)get_height() * rh);
 }
 
 void
-Menu::additem(MenuItemKind kind_, const std::string& text_, int toggle_, Menu* menu_)
+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_));
+  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;
 }
@@ -188,6 +333,7 @@ Menu::clear()
 void
 Menu::action()
 {
+  hit_item = -1;
   if(item.size() != 0)
     {
       switch(menuaction)
@@ -230,30 +376,32 @@ Menu::action()
 
         case MENU_ACTION_HIT:
           {
+            hit_item = active_item;
             switch (item[active_item].kind)
               {
               case MN_GOTO:
                 if (item[active_item].target_menu != NULL)
-                  Menu::set_current(item[active_item].target_menu);
+                  Menu::push_current(item[active_item].target_menu);
                 else
                   puts("NULLL");
                 break;
 
               case MN_TOGGLE:
                 item[active_item].toggled = !item[active_item].toggled;
-                menu_change = true;
                 break;
 
               case MN_ACTION:
+                Menu::set_current(0); 
+                item[active_item].toggled = true;
+                break;
               case MN_TEXTFIELD:
               case MN_NUMFIELD:
-              case MN_CONTROLFIELD:
-                item[active_item].toggled = true;
+                menuaction = MENU_ACTION_DOWN;
+                action();
                 break;
 
               case MN_BACK:
-                if(last_menu != NULL)
-                  Menu::set_current(last_menu);
+                Menu::pop_current();
                 break;
               default:
                 break;
@@ -296,7 +444,6 @@ Menu::action()
                   item[active_item].input[1] = '\0';
                 }
             }
-          break;
 
         case MENU_ACTION_NONE:
           break;
@@ -309,35 +456,21 @@ Menu::action()
       || new_item.kind == MN_HL)
     {
       // Skip the horzontal line item
-      if(menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
+      if (menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
         menuaction = MENU_ACTION_DOWN;
 
-      if(item.size() > 1)
+      if (item.size() > 1)
         action();
     }
+
+  menuaction = MENU_ACTION_NONE;
 }
 
 int
 Menu::check()
 {
-  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;
-          show_menu = 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;
 }
@@ -347,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;
@@ -364,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;
 
@@ -421,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);
 
@@ -488,7 +632,7 @@ Menu::draw_item(int index, // Position of the current item in the menu
     }
 }
 
-int Menu::width()
+int Menu::get_width() const
 {
   /* The width of the menu has to be more than the width of the text
      with the most characters */
@@ -507,7 +651,7 @@ int Menu::width()
   return (menu_width * 16 + 24);
 }
 
-int Menu::height()
+int Menu::get_height() const
 {
   return item.size() * 24;
 }
@@ -516,8 +660,8 @@ int Menu::height()
 void
 Menu::draw()
 {
-  int menu_height = height();
-  int menu_width width();
+  int menu_height = get_height();
+  int menu_width  = get_width();
 
   /* Draw a transparent background */
   fillrect(pos_x - menu_width/2,
@@ -531,43 +675,34 @@ Menu::draw()
     }
 }
 
-/* Reset/Set global defaults */
-void menu_reset(void)
+MenuItem&
+Menu::get_item_by_id(int id)
 {
-  menu_change  = false;
-  show_menu    = false;
-  menuaction   = MENU_ACTION_NONE;
-  current_menu = NULL;
+  for(std::vector<MenuItem>::iterator i = item.begin(); i != item.end(); ++i) {
+    if(i->id == id)
+      return *i;
+  }
 
-  delete_character = 0;
-  mn_input_char    = '\0';
+  assert(false);
+  static MenuItem dummyitem;
+  return dummyitem;
 }
 
-/* --- MENU --- */
-/* Draw the current menu and execute the (menu)events */
-void menu_process_current(void)
+int Menu::get_active_item_id()
 {
-  if(!show_menu)
-    return;
-
-  menu_change = false;
-
-  if(current_menu != NULL)
-    {
-      current_menu->action();
-      current_menu->draw();
-    }
+return item[active_item].id;
+}
 
-  menuaction = MENU_ACTION_NONE;
+bool
+Menu::isToggled(int id)
+{
+  return get_item_by_id(id).toggled;
 }
 
 /* Check for menu event */
 void
 Menu::event(SDL_Event& event)
 {
-  if(show_menu == false && event.key.keysym.sym != SDLK_ESCAPE)
-    return;
-
   SDLKey key;
   switch(event.type)
     {
@@ -590,54 +725,55 @@ Menu::event(SDL_Event& event)
           /* An International Character. */
         }
 
+      if(item[active_item].kind == MN_CONTROLFIELD)
+        {
+        if(key == SDLK_ESCAPE)
+          {
+          Menu::pop_current();
+          return;
+          }
+        *item[active_item].int_p = key;
+        menuaction = MENU_ACTION_DOWN;
+        return;
+        }
+
+
       switch(key)
         {
         case SDLK_UP:          /* Menu Up */
           menuaction = MENU_ACTION_UP;
-          menu_change = true;
           break;
         case SDLK_DOWN:                /* Menu Down */
           menuaction = MENU_ACTION_DOWN;
-          menu_change = true;
           break;
         case SDLK_LEFT:                /* Menu Up */
           menuaction = MENU_ACTION_LEFT;
-          menu_change = true;
           break;
         case SDLK_RIGHT:               /* Menu Down */
           menuaction = MENU_ACTION_RIGHT;
-          menu_change = true;
           break;
         case SDLK_SPACE:
           if(item[active_item].kind == MN_TEXTFIELD)
             {
               menuaction = MENU_ACTION_INPUT;
-              menu_change = true;
               mn_input_char = ' ';
               break;
             }
         case SDLK_RETURN: /* Menu Hit */
           menuaction = MENU_ACTION_HIT;
-          menu_change = true;
           break;
         case SDLK_DELETE:
         case SDLK_BACKSPACE:
           menuaction = MENU_ACTION_REMOVE;
-          menu_change = true;
           delete_character++;
           break;
         case SDLK_ESCAPE:
-          if(show_menu && has_backitem == true && last_menu != NULL)
-            Menu::set_current(last_menu);
-          else if(show_menu)
-            show_menu = false;
-          else
-            show_menu = true;
+          Menu::pop_current();
+          break;
         default:
           if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
             {
               menuaction = MENU_ACTION_INPUT;
-              menu_change = true;
               mn_input_char = *ch;
             }
           else
@@ -648,7 +784,7 @@ Menu::event(SDL_Event& event)
         }
       break;
     case  SDL_JOYAXISMOTION:
-      if(event.jaxis.axis == JOY_Y)
+      if(event.jaxis.axis == joystick_keymap.y_axis)
         {
           if (event.jaxis.value > 1024)
             menuaction = MENU_ACTION_DOWN;
@@ -662,10 +798,10 @@ Menu::event(SDL_Event& event)
     case SDL_MOUSEBUTTONDOWN:
       x = event.motion.x;
       y = event.motion.y;
-      if(x > pos_x - width()/2 &&
-          x < pos_x + width()/2 &&
-          y > pos_y - height()/2 &&
-          y < pos_y + height()/2)
+      if(x > pos_x - get_width()/2 &&
+         x < pos_x + get_width()/2 &&
+         y > pos_y - get_height()/2 &&
+         y < pos_y + get_height()/2)
         {
           menuaction = MENU_ACTION_HIT;
         }
@@ -673,16 +809,15 @@ Menu::event(SDL_Event& event)
     case SDL_MOUSEMOTION:
       x = event.motion.x;
       y = event.motion.y;
-      if(x > pos_x - width()/2 &&
-          x < pos_x + width()/2 &&
-          y > pos_y - height()/2 &&
-          y < pos_y + height()/2)
+      if(x > pos_x - get_width()/2 &&
+         x < pos_x + get_width()/2 &&
+         y > pos_y - get_height()/2 &&
+         y < pos_y + get_height()/2)
         {
-          active_item = (y - (pos_y - height()/2)) / 24;
-          menu_change = true;
-         mouse_cursor->set_state(MC_LINK);
+          active_item = (y - (pos_y - get_height()/2)) / 24;
+          mouse_cursor->set_state(MC_LINK);
         }
-       else
+      else
        {
          mouse_cursor->set_state(MC_NORMAL);
        }