Possible fix for expression that's always false
[supertux.git] / src / supertux / menu / options_menu.cpp
index 1bebe59..8e1ce9d 100644 (file)
 #include "supertux/menu/options_menu.hpp"
 
 #include "audio/sound_manager.hpp"
-#include "control/joystickkeyboardcontroller.hpp"
-#include "gui/menu.hpp"
-#include "gui/menu_item.hpp"
+#include "gui/menu_manager.hpp"
 #include "supertux/gameconfig.hpp"
-#include "supertux/globals.hpp"
-#include "supertux/main.hpp"
-#include "supertux/menu/profile_menu.hpp"
+#include "supertux/menu/joystick_menu.hpp"
+#include "supertux/menu/keyboard_menu.hpp"
 #include "supertux/menu/language_menu.hpp"
-#include "supertux/menu/menu_manager.hpp"
-#include "util/gettext.hpp"
+#include "supertux/menu/menu_storage.hpp"
+#include "supertux/menu/profile_menu.hpp"
+#include "util/string_util.hpp"
 #include "video/renderer.hpp"
 
+#include <algorithm>
+#include <sstream>
+#include <stdio.h>
+
 enum OptionsMenuIDs {
   MNID_FULLSCREEN,
   MNID_FULLSCREEN_RESOLUTION,
   MNID_MAGNIFICATION,
   MNID_ASPECTRATIO,
-  MNID_PROFILES,
   MNID_SOUND,
-  MNID_MUSIC
+  MNID_MUSIC,
+  MNID_DEVELOPER_MODE
 };
 
-OptionsMenu::OptionsMenu() :
-  language_menu()
+OptionsMenu::OptionsMenu(bool complete)
 {
-  language_menu.reset(new LanguageMenu());
-
   add_label(_("Options"));
   add_hl();
 
-  // Language change should only be possible in the main menu, since elsewhere it might not always work fully
-  // FIXME: Implement me: if (get_parent() == main_menu)
-  add_submenu(_("Select Language"), language_menu.get())
-    ->set_help(_("Select a different language to display text in"));
+  if (complete)
+  {
+    // Language and profile changes are only be possible in the
+    // main menu, since elsewhere it might not always work fully
+    add_submenu(_("Select Language"), MenuStorage::LANGUAGE_MENU)
+      ->set_help(_("Select a different language to display text in"));
 
-  add_submenu(_("Select Profile"), MenuManager::get_profile_menu())
-    ->set_help(_("Select a profile to play with"));
+    add_submenu(_("Select Profile"), MenuStorage::PROFILE_MENU)
+      ->set_help(_("Select a profile to play with"));
+  }
 
-  add_toggle(MNID_PROFILES, _("Profile on Startup"), g_config->sound_enabled)
-    ->set_help(_("Select your profile immediately after start-up"));
-  
   add_toggle(MNID_FULLSCREEN,_("Fullscreen"), g_config->use_fullscreen)
     ->set_help(_("Fill the entire screen"));
 
@@ -70,7 +69,7 @@ OptionsMenu::OptionsMenu() :
 
   // These values go from screen:640/projection:1600 to
   // screen:1600/projection:640 (i.e. 640, 800, 1024, 1280, 1600)
-  magnification->list.push_back("auto");
+  magnification->list.push_back(_("auto"));
   magnification->list.push_back("40%");
   magnification->list.push_back("50%");
   magnification->list.push_back("62.5%");
@@ -80,59 +79,104 @@ OptionsMenu::OptionsMenu() :
   magnification->list.push_back("160%");
   magnification->list.push_back("200%");
   magnification->list.push_back("250%");
+  if (g_config->magnification != 0.0f) //auto
+  {
+    std::ostringstream out;
+    out << (g_config->magnification*100) << "%";
+    std::string magn = out.str();
+    size_t count = 0;
+    for (std::vector<std::string>::iterator i = magnification->list.begin(); i != magnification->list.end(); ++i)
+    {
+      if (*i == magn)
+      {
+       magnification->selected = count;
+       magn.clear();
+       break;
+      }
 
-  SDL_Rect** modes = SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_OPENGL);
-
-  if (modes == (SDL_Rect **)0) 
-  { // No resolutions at all available, bad
+      ++count;
+    }
+    if (!magn.empty()) //magnification not in our list but accept anyway
+    {
+      magnification->selected = magnification->list.size();
+      magnification->list.push_back(magn);
+    }
+  }
 
+  int display_mode_count = SDL_GetNumDisplayModes(0);
+  std::string last_display_mode;
+  for(int i = 0; i < display_mode_count; ++i)
+  {
+    SDL_DisplayMode mode;
+    int ret = SDL_GetDisplayMode(0, i, &mode);
+    if (ret != 0)
+    {
+      log_warning << "failed to get display mode: " << SDL_GetError() << std::endl;
+    }
+    else
+    {
+      std::ostringstream out;
+      out << mode.w << "x" << mode.h << "@" << mode.refresh_rate;
+      if(last_display_mode == out.str())
+        continue;
+      last_display_mode = out.str();
+      fullscreen_res->list.push_back(out.str());
+    }
   }
-  else if(modes == (SDL_Rect **)-1) 
-  { // All resolutions should work, so we fall back to hardcoded defaults
-    fullscreen_res->list.push_back("640x480");
-    fullscreen_res->list.push_back("800x600");
-    fullscreen_res->list.push_back("1024x768");
-    fullscreen_res->list.push_back("1152x864");
-    fullscreen_res->list.push_back("1280x960");
-    fullscreen_res->list.push_back("1280x1024");
-    fullscreen_res->list.push_back("1440x900");
-    fullscreen_res->list.push_back("1680x1050");
-    fullscreen_res->list.push_back("1600x1200");
-    fullscreen_res->list.push_back("1920x1080");
-    fullscreen_res->list.push_back("1920x1200");
+  fullscreen_res->list.push_back("Desktop");
+
+  std::string fullscreen_size_str = "Desktop";
+  {
+    std::ostringstream out;
+    if (g_config->fullscreen_size != Size(0, 0))
+    {
+      out << g_config->fullscreen_size.width << "x" << g_config->fullscreen_size.height << "@" << g_config->fullscreen_refresh_rate;
+      fullscreen_size_str = out.str();
+    }
   }
-  else 
+
+  size_t cnt = 0;
+  for (std::vector<std::string>::iterator i = fullscreen_res->list.begin(); i != fullscreen_res->list.end(); ++i)
   {
-    for(int i = 0; modes[i]; ++i)
+    if (*i == fullscreen_size_str)
     {
-      std::ostringstream out;          
-      out << modes[i]->w << "x" << modes[i]->h;
-      fullscreen_res->list.push_back(out.str());
+      fullscreen_size_str.clear();
+      fullscreen_res->selected = cnt;
+      break;
     }
+    ++cnt;
+  }
+  if (!fullscreen_size_str.empty())
+  {
+    fullscreen_res->selected = fullscreen_res->list.size();
+    fullscreen_res->list.push_back(fullscreen_size_str);
   }
 
   MenuItem* aspect = add_string_select(MNID_ASPECTRATIO, _("Aspect Ratio"));
   aspect->set_help(_("Adjust the aspect ratio"));
-  
-  aspect->list.push_back("auto");
+
+  aspect->list.push_back(_("auto"));
   aspect->list.push_back("5:4");
   aspect->list.push_back("4:3");
   aspect->list.push_back("16:10");
   aspect->list.push_back("16:9");
   aspect->list.push_back("1368:768");
 
-  if (g_config->aspect_width != 0 && g_config->aspect_height != 0)
+  if (g_config->aspect_size != Size(0, 0))
   {
     std::ostringstream out;
-    out << g_config->aspect_width << ":" << g_config->aspect_height;
+    out << g_config->aspect_size.width << ":" << g_config->aspect_size.height;
     std::string aspect_ratio = out.str();
+    size_t cnt_ = 0;
     for(std::vector<std::string>::iterator i = aspect->list.begin(); i != aspect->list.end(); ++i)
     {
       if(*i == aspect_ratio)
       {
         aspect_ratio.clear();
+       aspect->selected = cnt_;
         break;
       }
+      ++cnt_;
     }
 
     if (!aspect_ratio.empty())
@@ -141,8 +185,8 @@ OptionsMenu::OptionsMenu() :
       aspect->list.push_back(aspect_ratio);
     }
   }
-  
-  if (sound_manager->is_audio_enabled()) {
+
+  if (SoundManager::current()->is_audio_enabled()) {
     add_toggle(MNID_SOUND, _("Sound"), g_config->sound_enabled)
       ->set_help(_("Disable all sound effects"));
     add_toggle(MNID_MUSIC, _("Music"), g_config->music_enabled)
@@ -151,12 +195,18 @@ OptionsMenu::OptionsMenu() :
     add_inactive(MNID_SOUND, _("Sound (disabled)"));
     add_inactive(MNID_MUSIC, _("Music (disabled)"));
   }
-  
-  add_submenu(_("Setup Keyboard"), g_main_controller->get_key_options_menu())
+
+  add_submenu(_("Setup Keyboard"), MenuStorage::KEYBOARD_MENU)
     ->set_help(_("Configure key-action mappings"));
 
-  add_submenu(_("Setup Joystick") ,g_main_controller->get_joystick_options_menu())
+  add_submenu(_("Setup Joystick"), MenuStorage::JOYSTICK_MENU)
     ->set_help(_("Configure joystick control-action mappings"));
+
+  if (g_config->developer_mode)
+  {
+    add_toggle(MNID_DEVELOPER_MODE, _("Developer Mode"), g_config->developer_mode);
+  }
+
   add_hl();
   add_back(_("Back"));
 }
@@ -170,51 +220,66 @@ OptionsMenu::menu_action(MenuItem* item)
 {
   switch (item->id) {
     case MNID_ASPECTRATIO:
-    { 
-      if (item->list[item->selected] == "auto")
-      {
-        g_config->aspect_width  = 0; // Magic values
-        g_config->aspect_height = 0;
-        Renderer::instance()->apply_config();
-        Menu::recalc_pos();
-      }
-      else if(sscanf(item->list[item->selected].c_str(), "%d:%d", &g_config->aspect_width, &g_config->aspect_height) == 2)
-      {
-        Renderer::instance()->apply_config();
-        Menu::recalc_pos();
-      }
-      else
       {
-        assert(!"This must not be reached");
+        if (item->list[item->selected] == _("auto"))
+        {
+          g_config->aspect_size = Size(0, 0); // Magic values
+          VideoSystem::current()->get_renderer().apply_config();
+          MenuManager::instance().on_window_resize();
+        }
+        else if (sscanf(item->list[item->selected].c_str(), "%d:%d",
+                        &g_config->aspect_size.width, &g_config->aspect_size.height) == 2)
+        {
+          VideoSystem::current()->get_renderer().apply_config();
+          MenuManager::instance().on_window_resize();
+        }
+        else
+        {
+          assert(!"This must not be reached");
+        }
       }
-    }
-    break;
+      break;
 
     case MNID_MAGNIFICATION:
-      if (item->list[item->selected] == "auto")
+      if (item->list[item->selected] == _("auto"))
       {
-        g_config->magnification = 0.0f; // Magic value 
+        g_config->magnification = 0.0f; // Magic value
       }
       else if(sscanf(item->list[item->selected].c_str(), "%f", &g_config->magnification) == 1)
       {
         g_config->magnification /= 100.0f;
       }
-      Renderer::instance()->apply_config();
-      Menu::recalc_pos();
+      VideoSystem::current()->get_renderer().apply_config();
+      MenuManager::instance().on_window_resize();
       break;
 
     case MNID_FULLSCREEN_RESOLUTION:
-      if(sscanf(item->list[item->selected].c_str(), "%dx%d", &g_config->fullscreen_width, &g_config->fullscreen_height) == 2)
       {
-        // do nothing, changes are only applied when toggling fullscreen mode
-      }      
+        int width;
+        int height;
+        int refresh_rate;
+        if (item->list[item->selected] == "Desktop")
+        {
+          g_config->fullscreen_size.width = 0;
+          g_config->fullscreen_size.height = 0;
+          g_config->fullscreen_refresh_rate = 0;
+        }
+        else if(sscanf(item->list[item->selected].c_str(), "%dx%d@%d",
+                  &width, &height, &refresh_rate) == 3)
+        {
+          // do nothing, changes are only applied when toggling fullscreen mode
+          g_config->fullscreen_size.width = width;
+          g_config->fullscreen_size.height = height;
+          g_config->fullscreen_refresh_rate = refresh_rate;
+        }
+      }
       break;
 
     case MNID_FULLSCREEN:
       if(g_config->use_fullscreen != is_toggled(MNID_FULLSCREEN)) {
         g_config->use_fullscreen = !g_config->use_fullscreen;
-        init_video(); // FIXME: Should call apply_config instead
-        Menu::recalc_pos();
+        VideoSystem::current()->get_renderer().apply_config();
+        MenuManager::instance().on_window_resize();
         g_config->save();
       }
       break;
@@ -222,7 +287,7 @@ OptionsMenu::menu_action(MenuItem* item)
     case MNID_SOUND:
       if(g_config->sound_enabled != is_toggled(MNID_SOUND)) {
         g_config->sound_enabled = !g_config->sound_enabled;
-        sound_manager->enable_sound(g_config->sound_enabled);
+        SoundManager::current()->enable_sound(g_config->sound_enabled);
         g_config->save();
       }
       break;
@@ -230,11 +295,16 @@ OptionsMenu::menu_action(MenuItem* item)
     case MNID_MUSIC:
       if(g_config->music_enabled != is_toggled(MNID_MUSIC)) {
         g_config->music_enabled = !g_config->music_enabled;
-        sound_manager->enable_music(g_config->music_enabled);
+        SoundManager::current()->enable_music(g_config->music_enabled);
         g_config->save();
       }
       break;
 
+    case MNID_DEVELOPER_MODE:
+      g_config->developer_mode = is_toggled(MNID_DEVELOPER_MODE);
+      log_info << "developer mode: " << g_config->developer_mode << std::endl;
+      break;
+
     default:
       break;
   }