Removed made public variables private in JoystickManager and KeyboardManager
[supertux.git] / src / control / keyboard_manager.cpp
index 5b3d0c2..ca4e461 100644 (file)
@@ -1,6 +1,6 @@
 //  SuperTux
 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>,
-//                2007-2014 Ingo Ruhnke <grumbel@gmx.de>
+//                2007-2014 Ingo Ruhnke <grumbel@gmail.com>
 //
 //  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
@@ -19,6 +19,7 @@
 
 #include "control/controller.hpp"
 #include "control/joystick_manager.hpp"
+#include "control/keyboard_config.hpp"
 #include "gui/menu_manager.hpp"
 #include "lisp/list_iterator.hpp"
 #include "supertux/console.hpp"
 #include "supertux/menu/menu_storage.hpp"
 #include "util/writer.hpp"
 
-KeyboardManager::KeyboardManager(InputManager* parent) :
+KeyboardManager::KeyboardManager(InputManager* parent,
+                                 KeyboardConfig& keyboard_config) :
   m_parent(parent),
-  keymap(),
-  jump_with_up_kbd(false),
+  m_keyboard_config(keyboard_config),
   wait_for_key(-1)
 {
-  // initialize default keyboard map
-  keymap[SDLK_LEFT]     = Controller::LEFT;
-  keymap[SDLK_RIGHT]    = Controller::RIGHT;
-  keymap[SDLK_UP]       = Controller::UP;
-  keymap[SDLK_DOWN]     = Controller::DOWN;
-  keymap[SDLK_SPACE]    = Controller::JUMP;
-  keymap[SDLK_LCTRL]    = Controller::ACTION;
-  keymap[SDLK_LALT]     = Controller::ACTION;
-  keymap[SDLK_ESCAPE]   = Controller::PAUSE_MENU;
-  keymap[SDLK_p]        = Controller::PAUSE_MENU;
-  keymap[SDLK_PAUSE]    = Controller::PAUSE_MENU;
-  keymap[SDLK_RETURN]   = Controller::MENU_SELECT;
-  keymap[SDLK_KP_ENTER] = Controller::MENU_SELECT;
-  keymap[SDLK_CARET]    = Controller::CONSOLE;
-  keymap[SDLK_DELETE]   = Controller::PEEK_LEFT;
-  keymap[SDLK_PAGEDOWN] = Controller::PEEK_RIGHT;
-  keymap[SDLK_HOME]     = Controller::PEEK_UP;
-  keymap[SDLK_END]      = Controller::PEEK_DOWN;
 }
 
 KeyboardManager::~KeyboardManager()
@@ -60,38 +43,38 @@ KeyboardManager::~KeyboardManager()
 void
 KeyboardManager::process_key_event(const SDL_KeyboardEvent& event)
 {
-  KeyMap::iterator key_mapping = keymap.find(event.keysym.sym);
+  KeyboardConfig::KeyMap::iterator key_mapping = m_keyboard_config.keymap.find(event.keysym.sym);
 
   // if console key was pressed: toggle console
-  if (key_mapping != keymap.end() && 
+  if (key_mapping != m_keyboard_config.keymap.end() &&
       key_mapping->second == Controller::CONSOLE)
   {
     if (event.type == SDL_KEYDOWN)
     {
-      Console::instance->toggle();
+      Console::current()->toggle();
     }
-  } 
-  else if (Console::instance->hasFocus()) 
+  }
+  else if (Console::current()->hasFocus())
   {
     // if console is open: send key there
     process_console_key_event(event);
   }
-  else if (MenuManager::instance().current())
+  else if (MenuManager::instance().is_active())
   {
     // if menu mode: send key there
     process_menu_key_event(event);
   }
-  else if (key_mapping == keymap.end()) 
+  else if (key_mapping == m_keyboard_config.keymap.end())
   {
     // default action: update controls
     //log_debug << "Key " << event.key.SDL_Keycode.sym << " is unbound" << std::endl;
   }
-  else 
+  else
   {
     auto control = key_mapping->second;
     bool value = (event.type == SDL_KEYDOWN);
     m_parent->get_controller()->set_control(control, value);
-    if (jump_with_up_kbd && control == Controller::UP)
+    if (m_keyboard_config.jump_with_up_kbd && control == Controller::UP)
     {
       m_parent->get_controller()->set_control(Controller::JUMP, value);
     }
@@ -101,10 +84,10 @@ KeyboardManager::process_key_event(const SDL_KeyboardEvent& event)
 void
 KeyboardManager::process_text_input_event(const SDL_TextInputEvent& event)
 {
-  if (Console::instance->hasFocus()) {
+  if (Console::current()->hasFocus()) {
     for(int i = 0; event.text[i] != '\0'; ++i)
     {
-      Console::instance->input(event.text[i]);
+      Console::current()->input(event.text[i]);
     }
   }
 }
@@ -116,37 +99,37 @@ KeyboardManager::process_console_key_event(const SDL_KeyboardEvent& event)
 
   switch (event.keysym.sym) {
     case SDLK_RETURN:
-      Console::instance->enter();
+      Console::current()->enter();
       break;
     case SDLK_BACKSPACE:
-      Console::instance->backspace();
+      Console::current()->backspace();
       break;
     case SDLK_TAB:
-      Console::instance->autocomplete();
+      Console::current()->autocomplete();
       break;
     case SDLK_PAGEUP:
-      Console::instance->scroll(-1);
+      Console::current()->scroll(-1);
       break;
     case SDLK_PAGEDOWN:
-      Console::instance->scroll(+1);
+      Console::current()->scroll(+1);
       break;
     case SDLK_HOME:
-      Console::instance->move_cursor(-65535);
+      Console::current()->move_cursor(-65535);
       break;
     case SDLK_END:
-      Console::instance->move_cursor(+65535);
+      Console::current()->move_cursor(+65535);
       break;
     case SDLK_UP:
-      Console::instance->show_history(-1);
+      Console::current()->show_history(-1);
       break;
     case SDLK_DOWN:
-      Console::instance->show_history(+1);
+      Console::current()->show_history(+1);
       break;
     case SDLK_LEFT:
-      Console::instance->move_cursor(-1);
+      Console::current()->move_cursor(-1);
       break;
     case SDLK_RIGHT:
-      Console::instance->move_cursor(+1);
+      Console::current()->move_cursor(+1);
       break;
     default:
       break;
@@ -157,28 +140,28 @@ void
 KeyboardManager::process_menu_key_event(const SDL_KeyboardEvent& event)
 {
   // wait for key mode?
-  if (wait_for_key >= 0) 
+  if (wait_for_key >= 0)
   {
     if (event.type == SDL_KEYUP)
       return;
 
-    if (event.keysym.sym != SDLK_ESCAPE && 
-        event.keysym.sym != SDLK_PAUSE) 
+    if (event.keysym.sym != SDLK_ESCAPE &&
+        event.keysym.sym != SDLK_PAUSE)
     {
-      bind_key(event.keysym.sym, static_cast<Controller::Control>(wait_for_key));
+      m_keyboard_config.bind_key(event.keysym.sym, static_cast<Controller::Control>(wait_for_key));
     }
     m_parent->reset();
-    MenuStorage::get_key_options_menu()->update();
+    MenuManager::instance().refresh();
     wait_for_key = -1;
     return;
   }
-  
-  if (m_parent->joystick_manager->wait_for_joystick >= 0) 
+
+  if (m_parent->joystick_manager->wait_for_joystick >= 0)
   {
-    if (event.keysym.sym == SDLK_ESCAPE) 
+    if (event.keysym.sym == SDLK_ESCAPE)
     {
       m_parent->reset();
-      MenuStorage::get_joystick_options_menu()->update();
+      MenuManager::instance().refresh();
       m_parent->joystick_manager->wait_for_joystick = -1;
     }
     return;
@@ -207,8 +190,10 @@ KeyboardManager::process_menu_key_event(const SDL_KeyboardEvent& event)
       control = Controller::MENU_SELECT;
       break;
     case SDLK_ESCAPE:
+      control = Controller::ESCAPE;
+      break;
     case SDLK_PAUSE:
-      control = Controller::PAUSE_MENU;
+      control = Controller::START;
       break;
     default:
       return;
@@ -219,90 +204,9 @@ KeyboardManager::process_menu_key_event(const SDL_KeyboardEvent& event)
 }
 
 void
-KeyboardManager::bind_key(SDL_Keycode key, Controller::Control control)
+KeyboardManager::bind_next_event_to(Controller::Control id)
 {
-  // remove all previous mappings for that control and for that key
-  for(KeyMap::iterator i = keymap.begin();
-      i != keymap.end(); /* no ++i */) {
-    if (i->second == control) {
-      KeyMap::iterator e = i;
-      ++i;
-      keymap.erase(e);
-    } else {
-      ++i;
-    }
-  }
-
-  KeyMap::iterator i = keymap.find(key);
-  if (i != keymap.end())
-    keymap.erase(i);
-
-  // add new mapping
-  keymap[key] = control;
-}
-
-SDL_Keycode
-KeyboardManager::reversemap_key(Controller::Control c)
-{
-  for(KeyMap::iterator i = keymap.begin(); i != keymap.end(); ++i) 
-  {
-    if (i->second == c)
-    {
-      return i->first;
-    }
-  }
-
-  return SDLK_UNKNOWN;
-}
-
-void
-KeyboardManager::read(const lisp::Lisp* keymap_lisp)
-{
-  // keycode values changed between SDL1 and SDL2, so we skip old SDL1
-  // based values and use the defaults instead on the first read of
-  // the config file
-  bool config_is_sdl2 = false;
-  keymap_lisp->get("sdl2", config_is_sdl2);
-  if (config_is_sdl2)
-  {
-    keymap.clear();
-    keymap_lisp->get("jump-with-up", jump_with_up_kbd);
-    lisp::ListIterator iter(keymap_lisp);
-    while(iter.next()) {
-      if (iter.item() == "map") {
-        int key = -1;
-        std::string control;
-        const lisp::Lisp* map = iter.lisp();
-        map->get("key", key);
-
-        map->get("control", control);
-
-        int i = 0;
-        for(i = 0; Controller::controlNames[i] != 0; ++i) {
-          if (control == Controller::controlNames[i])
-            break;
-        }
-        if (Controller::controlNames[i] == 0) {
-          log_info << "Invalid control '" << control << "' in keymap" << std::endl;
-          continue;
-        }
-        keymap[static_cast<SDL_Keycode>(key)] = static_cast<Controller::Control>(i);
-      }
-    }
-  }
-}
-
-void
-KeyboardManager::write(Writer& writer)
-{
-  writer.write("sdl2", true);
-  writer.write("jump-with-up", jump_with_up_kbd);
-  for(KeyMap::iterator i = keymap.begin(); i != keymap.end(); ++i) {
-    writer.start_list("map");
-    writer.write("key", (int) i->first);
-    writer.write("control", Controller::controlNames[i->second]);
-    writer.end_list("map");
-  }
+  wait_for_key = id;
 }
 
 /* EOF */