Allow creation of Tilemap and Door objects by feeding them with info.
[supertux.git] / src / menu.cpp
index 1de8a7f..a4e7ceb 100644 (file)
 
 #include <iostream>
 #include <sstream>
-#include <stdlib.h>
-#include <stdio.h>
+#include <cstdlib>
+#include <cstdio>
 #include <string>
-#include <assert.h>
+#include <cassert>
 
 #include "defines.h"
 #include "globals.h"
 #include "menu.h"
-#include "screen.h"
+#include "screen/screen.h"
+#include "screen/drawing_context.h"
 #include "setup.h"
 #include "sound.h"
 #include "scene.h"
 #include "leveleditor.h"
 #include "timer.h"
 #include "high_scores.h"
+#include "gettext.h"
 
 #define FLICK_CURSOR_TIME 500
 
@@ -64,19 +66,21 @@ 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(std::string text)
+bool confirm_dialog(Surface *background, std::string text)
 {
-  Surface* cap_screen = Surface::CaptureScreen();
+  //Surface* cap_screen = Surface::CaptureScreen();
   
   Menu* dialog = new Menu;
   dialog->additem(MN_DEACTIVE, text,0,0);
   dialog->additem(MN_HL,"",0,0);
-  dialog->additem(MN_ACTION,"Yes",0,0,true);
-  dialog->additem(MN_ACTION,"No",0,0,false);
+  dialog->additem(MN_ACTION,_("Yes"),0,0,true);
+  dialog->additem(MN_ACTION,_("No"),0,0,false);
   dialog->additem(MN_HL,"",0,0);
 
   Menu::set_current(dialog);
 
+  DrawingContext context;
+
   while(true)
   {
     SDL_Event event;
@@ -86,21 +90,24 @@ bool confirm_dialog(std::string text)
       dialog->event(event);
     }
 
-    cap_screen->draw(0,0);
+    if(background == NULL)
+      context.draw_gradient(Color(200,240,220), Color(200,200,220), LAYER_BACKGROUND0);
+    else
+      context.draw_surface(background, Vector(0,0), LAYER_BACKGROUND0);
 
-    dialog->draw();
+    dialog->draw(context);
     dialog->action();
 
     switch (dialog->check())
     {
     case true:
-      delete cap_screen;
+      //delete cap_screen;
       Menu::set_current(0);
       delete dialog;
       return true;
       break;
     case false:
-      delete cap_screen;
+      //delete cap_screen;
       Menu::set_current(0);
       delete dialog;
       return false;
@@ -109,12 +116,12 @@ bool confirm_dialog(std::string text)
       break;
     }
 
-    mouse_cursor->draw();
-    flipscreen();
+    mouse_cursor->draw(context);
+    context.do_drawing();
     SDL_Delay(25);
   }
 
-
+  return false;
 }
 
 void
@@ -231,9 +238,9 @@ std::string MenuItem::get_input_with_symbol(bool active_item)
 
   char str[1024];
   if(input_flickering)
-    sprintf(str,"%s_",input);
-  else
     sprintf(str,"%s ",input);
+  else
+    sprintf(str,"%s_",input);
 
   std::string string = str;
 
@@ -246,40 +253,40 @@ void Menu::get_controlfield_key_into_input(MenuItem *item)
   switch(*item->int_p)
   {
   case SDLK_UP:
-    item->change_input("Up cursor");
+    item->change_input(_("Up cursor"));
     break;
   case SDLK_DOWN:
-    item->change_input("Down cursor");
+    item->change_input(_("Down cursor"));
     break;
   case SDLK_LEFT:
-    item->change_input("Left cursor");
+    item->change_input(_("Left cursor"));
     break;
   case SDLK_RIGHT:
-    item->change_input("Right cursor");
+    item->change_input(_("Right cursor"));
     break;
   case SDLK_RETURN:
-    item->change_input("Return");
+    item->change_input(_("Return"));
     break;
   case SDLK_SPACE:
-    item->change_input("Space");
+    item->change_input(_("Space"));
     break;
   case SDLK_RSHIFT:
-    item->change_input("Right Shift");
+    item->change_input(_("Right Shift"));
     break;
   case SDLK_LSHIFT:
-    item->change_input("Left Shift");
+    item->change_input(_("Left Shift"));
     break;
   case SDLK_RCTRL:
-    item->change_input("Right Control");
+    item->change_input(_("Right Control"));
     break;
   case SDLK_LCTRL:
-    item->change_input("Left Control");
+    item->change_input(_("Left Control"));
     break;
   case SDLK_RALT:
-    item->change_input("Right Alt");
+    item->change_input(_("Right Alt"));
     break;
   case SDLK_LALT:
-    item->change_input("Left Alt");
+    item->change_input(_("Left Alt"));
     break;
   default:
     {
@@ -326,6 +333,8 @@ Menu::Menu()
   arrange_left = 0;
   active_item  = 0;
   effect.init(false);
+
+  joystick_timer.init(true);
 }
 
 void Menu::set_pos(int x, int y, float rw, float rh)
@@ -504,13 +513,12 @@ Menu::check()
 }
 
 void
-Menu::draw_item(int index, // Position of the current item in the menu
-                int menu_width,
-                int menu_height)
+Menu::draw_item(DrawingContext& context,
+    int index, // Position of the current item in the menu
+    int menu_width, int menu_height)
 {
   MenuItem& pitem = item[index];
 
-  int font_width  = 16;
   int effect_offset = 0;
   {
     int effect_time = 0;
@@ -521,13 +529,14 @@ Menu::draw_item(int index, // Position of the current item in the menu
     effect_offset = (index % 2) ? effect_time : -effect_time;
   }
 
+  Font* text_font = white_text;
   int x_pos       = pos_x;
   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)+ 1) * font_width;
-  int list_width  = strlen(string_list_active(pitem.list)) * font_width;
-  Text* text_font = white_text;
+  int text_width  = int(text_font->get_text_width(pitem.text));
+  int input_width = int(text_font->get_text_width(pitem.input) + 10);
+  int list_width  =
+    int(text_font->get_text_width(string_list_active(pitem.list)));
 
   if (arrange_left)
     x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
@@ -542,30 +551,29 @@ Menu::draw_item(int index, // Position of the current item in the menu
   {
   case MN_DEACTIVE:
     {
-      black_text->draw_align(pitem.text,
-                             x_pos, y_pos,
-                             A_HMIDDLE, A_VMIDDLE, 2);
+      context.draw_text_center(gray_text, pitem.text,
+          Vector(0, y_pos - int(blue_text->get_height()/2)),
+          LAYER_GUI);
       break;
     }
 
   case MN_HL:
     {
+      // TODO
       int x = pos_x - menu_width/2;
       int y = y_pos - 12 - effect_offset;
       /* Draw a horizontal line with a little 3d effect */
-      fillrect(x, y + 6,
-               menu_width, 4,
-               150,200,255,225);
-      fillrect(x, y + 6,
-               menu_width, 2,
-               255,255,255,255);
+      context.draw_filled_rect(Vector(x, y + 6),
+          Vector(menu_width, 4), Color(150,200,255,225), LAYER_GUI);
+      context.draw_filled_rect(Vector(x, y + 6),
+          Vector(menu_width, 2), Color(255,255,255,255), LAYER_GUI);
       break;
     }
   case MN_LABEL:
     {
-      white_big_text->draw_align(pitem.text,
-                                 x_pos, y_pos,
-                                 A_HMIDDLE, A_VMIDDLE, 2);
+      context.draw_text_center(white_big_text,
+          pitem.text, Vector(0, y_pos - int(white_big_text->get_height()/2)),
+          LAYER_GUI);
       break;
     }
   case MN_TEXTFIELD:
@@ -573,15 +581,18 @@ Menu::draw_item(int index, // Position of the current item in the menu
   case MN_CONTROLFIELD_KB:
   case MN_CONTROLFIELD_JS:
     {
-      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);
+      int width = text_width + input_width + 5;
+      int text_pos = screen->w/2 - width/2;
+      int input_pos = text_pos + text_width + 10;
+
+      context.draw_filled_rect(
+          Vector(input_pos - 5, y_pos - 10),
+          Vector(input_width + 10, 20),
+          Color(255,255,255,255), LAYER_GUI-5);
+      context.draw_filled_rect(
+          Vector(input_pos - 4, y_pos - 9),
+          Vector(input_width + 8, 18),
+          Color(0,0,0,128), LAYER_GUI-4);
 
       if(pitem.kind == MN_CONTROLFIELD_KB)
         get_controlfield_key_into_input(&pitem);
@@ -591,74 +602,95 @@ Menu::draw_item(int index, // Position of the current item in the menu
       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);
+          context.draw_text(gold_text,
+              pitem.get_input_with_symbol(true),
+              Vector(input_pos, y_pos - int(gold_text->get_height()/2)),
+              LAYER_GUI);
         else
-          gold_text->draw_align((pitem.get_input_with_symbol(false)).c_str(), x_pos + text_pos, y_pos, A_HMIDDLE, A_VMIDDLE, 2);
+          context.draw_text(gold_text,
+              pitem.get_input_with_symbol(false),
+              Vector(input_pos, y_pos - int(gold_text->get_height()/2)),
+              LAYER_GUI);
       }
       else
-        gold_text->draw_align(pitem.input,
-                              x_pos + text_pos, y_pos,
-                              A_HMIDDLE, A_VMIDDLE, 2);
+        context.draw_text(gold_text, pitem.input,
+            Vector(input_pos, y_pos - int(gold_text->get_height()/2)),
+            LAYER_GUI);
 
-      text_font->draw_align(pitem.text,
-                            x_pos - (input_width + font_width)/2, y_pos,
-                            A_HMIDDLE, A_VMIDDLE, shadow_size);
+      context.draw_text(text_font, pitem.text,
+          Vector(text_pos, y_pos - int(text_font->get_height()/2)),
+          LAYER_GUI);
       break;
     }
   case MN_STRINGSELECT:
     {
-      int list_pos_2 = list_width + font_width;
+      int list_pos_2 = list_width + 16;
       int list_pos   = list_width/2;
-      int text_pos   = (text_width + font_width)/2;
+      int text_pos   = (text_width + 16)/2;
 
       /* Draw arrows */
-      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);
+      context.draw_surface(arrow_left,
+          Vector(x_pos - list_pos + text_pos - 17, y_pos - 8),
+          LAYER_GUI);
+      context.draw_surface(arrow_right,
+          Vector(x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8),
+          LAYER_GUI);
 
       /* Draw input background */
-      fillrect(x_pos - list_pos + text_pos - 1, y_pos - 10,
-               list_pos_2 + 2, 20,
-               255,255,255,255);
-      fillrect(x_pos - list_pos + text_pos, y_pos - 9,
-               list_pos_2, 18,
-               0,0,0,128);
-
-      gold_text->draw_align(string_list_active(pitem.list),
-                            x_pos + text_pos, y_pos,
-                            A_HMIDDLE, A_VMIDDLE,2);
-
-      text_font->draw_align(pitem.text,
-                            x_pos - list_pos_2/2, y_pos,
-                            A_HMIDDLE, A_VMIDDLE, shadow_size);
+      context.draw_filled_rect(
+          Vector(x_pos - list_pos + text_pos - 1, y_pos - 10),
+          Vector(list_pos_2 + 2, 20),
+          Color(255,255,255,255), LAYER_GUI - 4);
+      context.draw_filled_rect(
+          Vector(x_pos - list_pos + text_pos, y_pos - 9),
+          Vector(list_pos_2, 18),
+          Color(0,0,0,128), LAYER_GUI - 5);
+
+      context.draw_text_center(text_font, string_list_active(pitem.list),
+          Vector(text_pos, y_pos - int(text_font->get_height()/2)),
+          LAYER_GUI);
+      context.draw_text_center(text_font, pitem.text,
+          Vector(list_pos_2/2, y_pos - int(text_font->get_height()/2)),
+          LAYER_GUI);
       break;
     }
   case MN_BACK:
     {
-      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);
+      context.draw_text_center(text_font, pitem.text,
+          Vector(0, y_pos - int(text_font->get_height()/2)),
+          LAYER_GUI);
+      context.draw_surface(back,
+          Vector(x_pos + text_width/2  + 16, y_pos - 8),
+          LAYER_GUI);
       break;
     }
 
   case MN_TOGGLE:
     {
-      text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
+      context.draw_text_center(text_font, pitem.text,
+          Vector(0, y_pos - (text_font->get_height()/2)),
+          LAYER_GUI);
 
       if(pitem.toggled)
-        checkbox_checked->draw(
-          x_pos + (text_width+font_width)/2,
-          y_pos - 8);
+        context.draw_surface(checkbox_checked,
+            Vector(x_pos + (text_width+16)/2, y_pos - 8),
+            LAYER_GUI + 1);
       else
-        checkbox->draw(
-          x_pos + (text_width+font_width)/2,
-          y_pos - 8);
+        context.draw_surface(checkbox,
+            Vector(x_pos + (text_width+16)/2, y_pos - 8),
+            LAYER_GUI + 1);                                      
       break;
     }
   case MN_ACTION:
-    text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
+    context.draw_text_center(text_font, pitem.text,
+        Vector(0, y_pos - int(text_font->get_height()/2)),
+        LAYER_GUI);
     break;
 
   case MN_GOTO:
-    text_font->draw_align(pitem.text, x_pos, y_pos, A_HMIDDLE, A_VMIDDLE, shadow_size);
+    context.draw_text_center(text_font, pitem.text,
+        Vector(0, y_pos - int(text_font->get_height()/2)),
+        LAYER_GUI);
     break;
   }
 }
@@ -689,20 +721,20 @@ int Menu::get_height() const
 
 /* Draw the current menu. */
 void
-Menu::draw()
+Menu::draw(DrawingContext& context)
 {
   int menu_height = get_height();
   int menu_width  = get_width();
 
   /* Draw a transparent background */
-  fillrect(pos_x - menu_width/2,
-           pos_y - 24*item.size()/2 - 10,
-           menu_width,menu_height + 20,
-           150,180,200,125);
+  context.draw_filled_rect(
+      Vector(pos_x - menu_width/2, pos_y - 24*item.size()/2 - 10),
+      Vector(menu_width,menu_height + 20),
+      Color(150,180,200,125), LAYER_GUI-10);
 
   for(unsigned int i = 0; i < item.size(); ++i)
   {
-    draw_item(i, menu_width, menu_height);
+    draw_item(context, i, menu_width, menu_height);
   }
 }
 
@@ -735,133 +767,152 @@ Menu::isToggled(int id)
 void
 Menu::event(SDL_Event& event)
 {
-  SDLKey key;
   switch(event.type)
-  {
-  case SDL_KEYDOWN:
-    key = event.key.keysym.sym;
-    SDLMod keymod;
-    char ch[2];
-    keymod = SDL_GetModState();
-    int x,y;
-
-    /* If the current unicode character is an ASCII character,
-       assign it to ch. */
-    if ( (event.key.keysym.unicode & 0xFF80) == 0 )
     {
-      ch[0] = event.key.keysym.unicode & 0x7F;
-      ch[1] = '\0';
-    }
-    else
-    {
-      /* An International Character. */
-    }
-
-    if(item[active_item].kind == MN_CONTROLFIELD_KB)
-    {
-      if(key == SDLK_ESCAPE)
+    case SDL_KEYDOWN:
       {
-        Menu::pop_current();
-        return;
-      }
-      *item[active_item].int_p = key;
-      menuaction = MENU_ACTION_DOWN;
-      return;
-    }
+        SDLKey key = key = event.key.keysym.sym;
+        SDLMod keymod;
+        char ch[2];
+        keymod = SDL_GetModState();
+
+        /* If the current unicode character is an ASCII character,
+           assign it to ch. */
+        if ( (event.key.keysym.unicode & 0xFF80) == 0 )
+          {
+            ch[0] = event.key.keysym.unicode & 0x7F;
+            ch[1] = '\0';
+          }
+        else
+          {
+            /* An International Character. */
+          }
 
+        if(item[active_item].kind == MN_CONTROLFIELD_KB)
+          {
+            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;
-      break;
-    case SDLK_DOWN:            /* Menu Down */
-      menuaction = MENU_ACTION_DOWN;
-      break;
-    case SDLK_LEFT:            /* Menu Up */
-      menuaction = MENU_ACTION_LEFT;
-      break;
-    case SDLK_RIGHT:           /* Menu Down */
-      menuaction = MENU_ACTION_RIGHT;
-      break;
-    case SDLK_SPACE:
-      if(item[active_item].kind == MN_TEXTFIELD)
-      {
-        menuaction = MENU_ACTION_INPUT;
-        mn_input_char = ' ';
-        break;
+
+        switch(key)
+          {
+          case SDLK_UP:                /* Menu Up */
+            menuaction = MENU_ACTION_UP;
+            break;
+          case SDLK_DOWN:              /* Menu Down */
+            menuaction = MENU_ACTION_DOWN;
+            break;
+          case SDLK_LEFT:              /* Menu Up */
+            menuaction = MENU_ACTION_LEFT;
+            break;
+          case SDLK_RIGHT:             /* Menu Down */
+            menuaction = MENU_ACTION_RIGHT;
+            break;
+          case SDLK_SPACE:
+            if(item[active_item].kind == MN_TEXTFIELD)
+              {
+                menuaction = MENU_ACTION_INPUT;
+                mn_input_char = ' ';
+                break;
+              }
+          case SDLK_RETURN: /* Menu Hit */
+            menuaction = MENU_ACTION_HIT;
+            break;
+          case SDLK_DELETE:
+          case SDLK_BACKSPACE:
+            menuaction = MENU_ACTION_REMOVE;
+            delete_character++;
+            break;
+          case SDLK_ESCAPE:
+            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;
+                mn_input_char = *ch;
+              }
+            else
+              {
+                mn_input_char = '\0';
+              }
+            break;
+          }
       }
-    case SDLK_RETURN: /* Menu Hit */
-      menuaction = MENU_ACTION_HIT;
       break;
-    case SDLK_DELETE:
-    case SDLK_BACKSPACE:
-      menuaction = MENU_ACTION_REMOVE;
-      delete_character++;
+      
+    case  SDL_JOYAXISMOTION:
+      if(event.jaxis.axis == joystick_keymap.y_axis)
+        {
+          if (event.jaxis.value > joystick_keymap.dead_zone && !joystick_timer.started())
+            {
+              menuaction = MENU_ACTION_DOWN;
+              joystick_timer.start(JOYSTICK_MENU_DELAY);
+            }
+          else if (event.jaxis.value < -joystick_keymap.dead_zone && !joystick_timer.started())
+            {
+              menuaction = MENU_ACTION_UP;
+              joystick_timer.start(JOYSTICK_MENU_DELAY);
+            }
+          else
+            joystick_timer.stop();
+        }
       break;
-    case SDLK_ESCAPE:
-      Menu::pop_current();
+    case  SDL_JOYBUTTONDOWN:
+      if (item[active_item].kind == MN_CONTROLFIELD_JS)
+        {
+          // FIXME: This next line does nothing useable, right? 
+          // *item[active_item].int_p = key;
+          menuaction = MENU_ACTION_DOWN;
+        }
+      menuaction = MENU_ACTION_HIT;
       break;
-    default:
-      if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
+
+    case SDL_MOUSEBUTTONDOWN:
       {
-        menuaction = MENU_ACTION_INPUT;
-        mn_input_char = *ch;
+        int x = event.motion.x;
+        int y = event.motion.y;
+
+        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;
+          }
       }
-      else
+      break;
+
+    case SDL_MOUSEMOTION:
       {
-        mn_input_char = '\0';
+        int x = event.motion.x;
+        int y = event.motion.y;
+
+        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 - get_height()/2)) / 24;
+            mouse_cursor->set_state(MC_LINK);
+          }
+        else
+          {
+            mouse_cursor->set_state(MC_NORMAL);
+          }
       }
       break;
+
+    default:
+      break;
     }
-    break;
-  case  SDL_JOYAXISMOTION:
-    if(event.jaxis.axis == joystick_keymap.y_axis)
-    {
-      if (event.jaxis.value > 1024)
-        menuaction = MENU_ACTION_DOWN;
-      else if (event.jaxis.value < -1024)
-        menuaction = MENU_ACTION_UP;
-    }
-    break;
-  case  SDL_JOYBUTTONDOWN:
-    if (item[active_item].kind == MN_CONTROLFIELD_JS)
-    {
-      *item[active_item].int_p = key;
-      menuaction = MENU_ACTION_DOWN;
-    }
-    menuaction = MENU_ACTION_HIT;
-    break;
-  case SDL_MOUSEBUTTONDOWN:
-    x = event.motion.x;
-    y = event.motion.y;
-    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;
-    }
-    break;
-  case SDL_MOUSEMOTION:
-    x = event.motion.x;
-    y = event.motion.y;
-    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 - get_height()/2)) / 24;
-      mouse_cursor->set_state(MC_LINK);
-    }
-    else
-    {
-      mouse_cursor->set_state(MC_NORMAL);
-    }
-    break;
-  default:
-    break;
-  }
 }