Limitated the number of bullets to be shot at the same time.
[supertux.git] / src / menu.cpp
index 8c5bc22..079e85d 100644 (file)
@@ -55,16 +55,17 @@ Menu* save_game_menu = 0;
 Menu* contrib_menu   = 0;
 Menu* contrib_subset_menu   = 0;
 
-std::stack<Menu*> Menu::last_menus;
+std::vector<Menu*> Menu::last_menus;
 Menu* Menu::current_ = 0;
 
 void
 Menu::push_current(Menu* pmenu)
 {
   if (current_)
-    last_menus.push(current_);
+    last_menus.push_back(current_);
   
-  set_current(pmenu);
+  current_ = pmenu;
+  current_->effect.start(500);
 }
 
 void
@@ -72,18 +73,22 @@ Menu::pop_current()
 {
   if (!last_menus.empty())
     {
-      set_current(last_menus.top());
-      last_menus.pop();
+      current_ = last_menus.back();
+      current_->effect.start(500);
+
+      last_menus.pop_back();
     }
   else
     {
-      set_current(0);
+      current_ = 0;
     }
 }
 
 void
 Menu::set_current(Menu* menu)
 {
+  last_menus.clear();
+
   if (menu)
     menu->effect.start(500);
   
@@ -92,7 +97,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_)
+MenuItem::create(MenuItemKind kind_, const char *text_, int init_toggle_, Menu* target_menu_, int* int_p_)
 {
   MenuItem *pnew_item = new MenuItem;
   
@@ -116,6 +121,9 @@ MenuItem::create(MenuItemKind kind_, const char *text_, int init_toggle_, Menu*
     }
   else
     pnew_item->list = NULL;
+
+  pnew_item->int_p = int_p_;
+
   return pnew_item;
 }
 
@@ -158,6 +166,7 @@ Menu::~Menu()
 
 Menu::Menu()
 {
+  hit_item = -1;
   menuaction = MENU_ACTION_NONE;
   delete_character = 0;
   mn_input_char = '\0';
@@ -172,17 +181,17 @@ Menu::Menu()
 
 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* 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_, int_p));
 }
 
 /* Add an item to a menu */
@@ -206,6 +215,7 @@ Menu::clear()
 void
 Menu::action()
 {
+  hit_item = -1;
   if(item.size() != 0)
     {
       switch(menuaction)
@@ -248,6 +258,7 @@ Menu::action()
 
         case MENU_ACTION_HIT:
           {
+            hit_item = active_item;
             switch (item[active_item].kind)
               {
               case MN_GOTO:
@@ -264,9 +275,11 @@ Menu::action()
               case MN_ACTION:
               case MN_TEXTFIELD:
               case MN_NUMFIELD:
-              case MN_CONTROLFIELD:
+                Menu::set_current(0); 
                 item[active_item].toggled = true;
                 break;
+              case MN_CONTROLFIELD:
+                break;
 
               case MN_BACK:
                 Menu::pop_current();
@@ -312,7 +325,6 @@ Menu::action()
                   item[active_item].input[1] = '\0';
                 }
             }
-          break;
 
         case MENU_ACTION_NONE:
           break;
@@ -338,18 +350,21 @@ 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)
+      else if(item[active_item].kind == MN_TOGGLE 
+              || item[active_item].kind == MN_GOTO)
         {
           return active_item;
         }
@@ -358,6 +373,7 @@ Menu::check()
     }
   else
     return -1;
+  */
 }
 
 void
@@ -448,6 +464,32 @@ 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;
@@ -506,7 +548,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 */
@@ -525,7 +567,7 @@ int Menu::width()
   return (menu_width * 16 + 24);
 }
 
-int Menu::height()
+int Menu::get_height() const
 {
   return item.size() * 24;
 }
@@ -534,8 +576,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,
@@ -575,6 +617,60 @@ Menu::event(SDL_Event& event)
           /* An International Character. */
         }
 
+      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;
+           }
+        
+        menuaction = MENU_ACTION_DOWN;
+        return;
+        }
+
+
       switch(key)
         {
         case SDLK_UP:          /* Menu Up */
@@ -621,7 +717,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;
@@ -635,10 +731,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;
         }
@@ -646,12 +742,12 @@ 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;
+          active_item = (y - (pos_y - get_height()/2)) / 24;
           mouse_cursor->set_state(MC_LINK);
         }
       else