When Enter is pressed in an input field, instead of hidding the menu, go to the next...
[supertux.git] / src / menu.cpp
index b8c41e9..c299975 100644 (file)
@@ -39,6 +39,8 @@
 #include "timer.h"
 #include "high_scores.h"
 
+#define FLICK_CURSOR_TIME 500
+
 Surface* checkbox;
 Surface* checkbox_checked;
 Surface* back;
@@ -60,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)
 {
@@ -127,6 +167,10 @@ MenuItem::create(MenuItemKind kind_, const char *text_, int init_toggle_, Menu*
   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;
 }
 
@@ -152,6 +196,31 @@ MenuItem::change_input(const  char *text_)
     }
 }
 
+char* 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);
+
+return (char*)str;
+}
+
 /* Set ControlField a key */
 void Menu::get_controlfield_key_into_input(MenuItem *item)
 {
@@ -320,11 +389,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();
@@ -423,7 +495,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;
 
@@ -483,7 +555,15 @@ Menu::draw_item(int index, // Position of the current item in the menu
         if(pitem.kind == MN_CONTROLFIELD)
           get_controlfield_key_into_input(&pitem);
 
-        gold_text->draw_align(pitem.input,
+        if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
+          {
+          if(active_item == index)
+            gold_text->draw_align(pitem.get_input_with_symbol(true), x_pos + text_pos, y_pos, A_HMIDDLE, A_VMIDDLE, 2);
+          else
+            gold_text->draw_align(pitem.get_input_with_symbol(false), 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);
 
@@ -606,6 +686,11 @@ Menu::get_item_by_id(int id)
   return dummyitem;
 }
 
+int Menu::get_active_item_id()
+{
+return item[active_item].id;
+}
+
 bool
 Menu::isToggled(int id)
 {