- fixed problem with last_menu not being able to handle menues deeper than two submenues
[supertux.git] / src / menu.cpp
index c70b699..044d94a 100644 (file)
@@ -1,20 +1,28 @@
-/*
-  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 "timer.h"
 #include "high_scores.h"
 
-/* (global) menu variables */
-MenuAction menuaction = MENU_ACTION_NONE;
-bool show_menu;
-bool menu_change;
-texture_type checkbox, checkbox_checked, back, arrow_left, arrow_right;
+Surface* checkbox;
+Surface* checkbox_checked;
+Surface* back;
+Surface* arrow_left;
+Surface* arrow_right;
 
 Menu* main_menu      = 0;
 Menu* game_menu      = 0;
+Menu* worldmap_menu  = 0;
 Menu* options_menu   = 0;
 Menu* options_controls_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::stack<Menu*> Menu::last_menus;
+Menu* Menu::current_ = 0;
 
-/* input implementation variables */
-int delete_character;
-char mn_input_char;
-
-/* Set the current menu */
 void
-Menu::set_current(Menu* pmenu)
+Menu::set_current(Menu* menu)
 {
-  if(pmenu != current_menu)
+  if (menu)
     {
-      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);
+      if (last_menus.empty() || menu != last_menus.top())
+        last_menus.push(current_);
+
+      menu->effect.start(500);
     }
+  
+  current_ = menu;
 }
 
 /* Return a pointer to a new menu item */
 MenuItem*
-MenuItem::create(MenuItemKind kind_, char *text_, int init_toggle_, Menu* target_menu_)
+MenuItem::create(MenuItemKind kind_, const char *text_, int init_toggle_, Menu* target_menu_)
 {
   MenuItem *pnew_item = new MenuItem;
   
@@ -133,14 +137,18 @@ Menu::~Menu()
     }
 }
 
+
 Menu::Menu()
 {
+  menuaction = MENU_ACTION_NONE;
+  delete_character = 0;
+  mn_input_char = '\0';
+  
   pos_x        = screen->w/2;
   pos_y        = screen->h/2;
-  last_menu    = 0;
+  has_backitem = false;
   arrange_left = 0;
   active_item  = 0;
-  last_menu    = 0;
   effect.init(false);
 }
 
@@ -151,19 +159,31 @@ void Menu::set_pos(int x, int y, float rw, float rh)
 }
 
 void
-Menu::additem(MenuItemKind kind_, char *text_, int toggle_, Menu* menu_)
+Menu::additem(MenuItemKind kind_, const std::string& text_, int toggle_, Menu* menu_)
 {
-  additem(MenuItem::create(kind_, text_, toggle_, menu_));
+  if(kind_ == MN_BACK)
+    has_backitem = true;
+
+  additem(MenuItem::create(kind_, text_.c_str(), toggle_, menu_));
 }
 
 /* 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;
 }
 
+void
+Menu::clear()
+{
+  item.clear();
+}
+
 /* Process actions done on the menu */
 void
 Menu::action()
@@ -221,7 +241,6 @@ Menu::action()
 
               case MN_TOGGLE:
                 item[active_item].toggled = !item[active_item].toggled;
-                menu_change = true;
                 break;
 
               case MN_ACTION:
@@ -232,8 +251,11 @@ Menu::action()
                 break;
 
               case MN_BACK:
-                if(last_menu != NULL)
-                  Menu::set_current(last_menu);
+                if (!last_menus.empty())
+                  {
+                    Menu::set_current(last_menus.top());
+                    last_menus.pop();
+                  }
                 break;
               default:
                 break;
@@ -289,12 +311,14 @@ 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
@@ -308,7 +332,7 @@ Menu::check()
           && item[active_item].toggled)
         {
           item[active_item].toggled = false;
-          show_menu = 0;
+          Menu::set_current(0);
           return active_item;
         }
       else if(item[active_item].kind == MN_TOGGLE || item[active_item].kind == MN_GOTO)
@@ -333,6 +357,7 @@ Menu::draw_item(int index, // Position of the current item in the menu
   int effect_offset = 0;
   {
     int effect_time = 0;
+
     if(effect.check())
       effect_time = effect.get_left() / 4;
 
@@ -345,7 +370,7 @@ Menu::draw_item(int index, // Position of the current item in the menu
   int text_width  = strlen(pitem.text) * font_width;
   int input_width = strlen(pitem.input) * font_width;
   int list_width  = strlen(string_list_active(pitem.list)) * font_width;
-  text_type* text_font = &white_text;
+  Text* text_font = white_text;
 
   if (arrange_left)
     x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
@@ -353,16 +378,16 @@ Menu::draw_item(int index, // Position of the current item in the menu
   if(index == active_item)
     {
       shadow_size = 3;
-      text_font = &blue_text;
+      text_font = blue_text;
     }
 
   switch (pitem.kind)
     {
     case MN_DEACTIVE:
       {
-        text_draw_align(&black_text, pitem.text,
-                        x_pos, y_pos,
-                        A_HMIDDLE, A_VMIDDLE, 2);
+        black_text->draw_align(pitem.text,
+                               x_pos, y_pos,
+                               A_HMIDDLE, A_VMIDDLE, 2);
         break;
       }
 
@@ -381,9 +406,9 @@ Menu::draw_item(int index, // Position of the current item in the menu
       }
     case MN_LABEL:
       {
-        text_draw_align(&white_big_text, pitem.text,
-                        x_pos, y_pos,
-                        A_HMIDDLE, A_VMIDDLE, 2);
+        white_big_text->draw_align(pitem.text,
+                                   x_pos, y_pos,
+                                   A_HMIDDLE, A_VMIDDLE, 2);
         break;
       }
     case MN_TEXTFIELD:
@@ -400,13 +425,13 @@ Menu::draw_item(int index, // Position of the current item in the menu
                  input_width + font_width, 18,
                  0,0,0,128);
 
-        text_draw_align(&gold_text, pitem.input,
-                        x_pos + text_pos, y_pos,
-                        A_HMIDDLE, A_VMIDDLE, 2);
+        gold_text->draw_align(pitem.input,
+                              x_pos + text_pos, y_pos,
+                              A_HMIDDLE, A_VMIDDLE, 2);
 
-        text_draw_align(text_font, pitem.text,
-                        x_pos - (input_width + font_width)/2, y_pos,
-                        A_HMIDDLE, A_VMIDDLE, shadow_size);
+        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:
@@ -416,8 +441,8 @@ Menu::draw_item(int index, // Position of the current item in the menu
         int text_pos   = (text_width + font_width)/2;
 
         /* Draw arrows */
-        texture_draw(&arrow_left,  x_pos - list_pos + text_pos - 17, y_pos - 8);
-        texture_draw(&arrow_right, x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8);
+        arrow_left->draw(  x_pos - list_pos + text_pos - 17, y_pos - 8);
+        arrow_right->draw( x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8);
 
         /* Draw input background */
         fillrect(x_pos - list_pos + text_pos - 1, y_pos - 10,
@@ -427,42 +452,42 @@ Menu::draw_item(int index, // Position of the current item in the menu
                  list_pos_2, 18,
                  0,0,0,128);
 
-        text_draw_align(&gold_text, string_list_active(pitem.list),
+        gold_text->draw_align(string_list_active(pitem.list),
                         x_pos + text_pos, y_pos,
                         A_HMIDDLE, A_VMIDDLE,2);
 
-        text_draw_align(text_font, pitem.text,
+        text_font->draw_align(pitem.text,
                         x_pos - list_pos_2/2, y_pos,
                         A_HMIDDLE, A_VMIDDLE, shadow_size);
         break;
       }
     case MN_BACK:
       {
-        text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
-        texture_draw(&back, x_pos + text_width/2  + font_width, y_pos - 8);
+        text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
+        back->draw( x_pos + text_width/2  + font_width, y_pos - 8);
         break;
       }
 
     case MN_TOGGLE:
       {
-        text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
+        text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
 
         if(pitem.toggled)
-          texture_draw(&checkbox_checked,
+          checkbox_checked->draw(
                        x_pos + (text_width+font_width)/2,
                        y_pos - 8);
         else
-          texture_draw(&checkbox,
+          checkbox->draw(
                        x_pos + (text_width+font_width)/2,
                        y_pos - 8);
         break;
       }
     case MN_ACTION:
-      text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
+      text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
       break;
 
     case MN_GOTO:
-      text_draw_align(text_font, pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
+      text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
       break;
     }
 }
@@ -510,33 +535,6 @@ Menu::draw()
     }
 }
 
-/* Reset/Set global defaults */
-void menu_reset(void)
-{
-  menu_change  = false;
-  show_menu    = false;
-  menuaction   = MENU_ACTION_NONE;
-  current_menu = NULL;
-
-  delete_character = 0;
-  mn_input_char    = '\0';
-}
-
-/* --- MENU --- */
-/* Draw the current menu and execute the (menu)events */
-void menu_process_current(void)
-{
-  menu_change = false;
-
-  if(current_menu != NULL)
-    {
-      current_menu->action();
-      current_menu->draw();
-    }
-
-  menuaction = MENU_ACTION_NONE;
-}
-
 /* Check for menu event */
 void
 Menu::event(SDL_Event& event)
@@ -567,43 +565,46 @@ Menu::event(SDL_Event& event)
         {
         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(Menu::current())
+            {
+              if (has_backitem == true && !last_menus.empty())
+                {
+                  Menu::set_current(last_menus.top());
+                  last_menus.pop();
+                }
+              else
+                Menu::set_current(0);
+            }
         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
@@ -629,9 +630,9 @@ Menu::event(SDL_Event& event)
       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)
+         x < pos_x + width()/2 &&
+         y > pos_y - height()/2 &&
+         y < pos_y + height()/2)
         {
           menuaction = MENU_ACTION_HIT;
         }
@@ -640,15 +641,14 @@ Menu::event(SDL_Event& event)
       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)
+         x < pos_x + width()/2 &&
+         y > pos_y - height()/2 &&
+         y < pos_y + height()/2)
         {
           active_item = (y - (pos_y - height()/2)) / 24;
-          menu_change = true;
-         mouse_cursor->set_state(MC_LINK);
+          mouse_cursor->set_state(MC_LINK);
         }
-       else
+      else
        {
          mouse_cursor->set_state(MC_NORMAL);
        }