Moved keyboard code into KeyboardManager class
authorIngo Ruhnke <grumbel@gmail.com>
Wed, 6 Aug 2014 07:34:49 +0000 (09:34 +0200)
committerIngo Ruhnke <grumbel@gmail.com>
Wed, 6 Aug 2014 07:34:49 +0000 (09:34 +0200)
15 files changed:
src/control/game_controller_manager.cpp
src/control/joystick_manager.cpp
src/control/joystick_manager.hpp
src/control/joystickkeyboardcontroller.cpp
src/control/joystickkeyboardcontroller.hpp
src/control/keyboard_manager.cpp [new file with mode: 0644]
src/control/keyboard_manager.hpp [new file with mode: 0644]
src/gui/menu.cpp
src/object/player.cpp
src/supertux/game_session.cpp
src/supertux/levelintro.cpp
src/supertux/menu/keyboard_menu.cpp
src/supertux/textscroller.cpp
src/worldmap/tux.cpp
src/worldmap/worldmap.cpp

index b76d41f..a9478ce 100644 (file)
@@ -40,7 +40,7 @@ void
 GameControllerManager::process_button_event(const SDL_ControllerButtonEvent& ev)
 {
   //log_info << "button event: " << static_cast<int>(ev.button) << " " << static_cast<int>(ev.state) << std::endl;
-  auto controller = m_parent->get_main_controller();
+  auto controller = m_parent->get_controller();
   switch(ev.button)
   {
     case SDL_CONTROLLER_BUTTON_A:
@@ -112,7 +112,7 @@ GameControllerManager::process_axis_event(const SDL_ControllerAxisEvent& ev)
   // to OR the values together
 
   //log_info << "axis event: " << static_cast<int>(ev.axis) << " " << ev.value << std::endl;
-  auto controller = m_parent->get_main_controller();
+  auto controller = m_parent->get_controller();
   auto axis2button = [this, &controller](int value,
                                          Controller::Control control_left, Controller::Control control_right)
     {
index 4d2ba41..a9230b4 100644 (file)
@@ -452,10 +452,10 @@ JoystickManager::set_joy_controls(Controller::Control id, bool value)
 {
   if (jump_with_up_joy && id == Controller::UP)
   {
-    parent->get_main_controller()->set_control(Controller::JUMP, value);
+    parent->get_controller()->set_control(Controller::JUMP, value);
   }
 
-  parent->get_main_controller()->set_control(id, value);
+  parent->get_controller()->set_control(id, value);
 }
 
 /* EOF */
index f899ffc..e30fdc4 100644 (file)
@@ -29,7 +29,7 @@
 
 class JoystickKeyboardController;
 
-class JoystickManager
+class JoystickManager final
 {
 private:
   typedef Uint8 JoyId;
index 50de739..ea2d9ea 100644 (file)
 
 #include <iostream>
 
+#include "control/keyboard_manager.hpp"
 #include "control/joystick_manager.hpp"
 #include "control/game_controller_manager.hpp"
 #include "gui/menu_manager.hpp"
 #include "lisp/list_iterator.hpp"
-#include "supertux/console.hpp"
 #include "supertux/gameconfig.hpp"
-#include "supertux/menu/joystick_menu.hpp"
-#include "supertux/menu/keyboard_menu.hpp"
-#include "supertux/menu/menu_storage.hpp"
 #include "util/gettext.hpp"
 #include "util/writer.hpp"
 
 JoystickKeyboardController::JoystickKeyboardController() :
   controller(new Controller),
   m_use_game_controller(true),
+  keyboard_manager(new KeyboardManager(this)),
   joystick_manager(new JoystickManager(this)),
-  game_controller_manager(new GameControllerManager(this)),
-  keymap(),
-  jump_with_up_kbd(),
-  wait_for_key(-1)
+  game_controller_manager(new GameControllerManager(this))
 {
-  // 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;
-
-  jump_with_up_kbd = false;
 }
 
 JoystickKeyboardController::~JoystickKeyboardController()
@@ -67,7 +42,7 @@ JoystickKeyboardController::~JoystickKeyboardController()
 }
 
 Controller*
-JoystickKeyboardController::get_main_controller()
+JoystickKeyboardController::get_controller()
 {
   return controller.get();
 }
@@ -76,30 +51,9 @@ void
 JoystickKeyboardController::read(const Reader& lisp)
 {
   const lisp::Lisp* keymap_lisp = lisp.get_lisp("keymap");
-  if (keymap_lisp) {
-    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[SDL_Keycode(key)] = Control(i);
-      }
-    }
+  if (keymap_lisp) 
+  {
+    keyboard_manager->read(keymap_lisp);
   }
 
   const lisp::Lisp* joystick_lisp = lisp.get_lisp(_("joystick"));
@@ -113,13 +67,7 @@ void
 JoystickKeyboardController::write(Writer& writer)
 {
   writer.start_list("keymap");
-  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");
-  }
+  keyboard_manager->write(writer);
   writer.end_list("keymap");
 
   writer.start_list("joystick");
@@ -144,12 +92,12 @@ JoystickKeyboardController::process_event(const SDL_Event& event)
 {
   switch(event.type) {
     case SDL_TEXTINPUT:
-      process_text_input_event(event.text);
+      keyboard_manager->process_text_input_event(event.text);
       break;
 
     case SDL_KEYUP:
     case SDL_KEYDOWN:
-      process_key_event(event.key);
+      keyboard_manager->process_key_event(event.key);
       break;
 
     case SDL_JOYAXISMOTION:
@@ -204,183 +152,4 @@ JoystickKeyboardController::process_event(const SDL_Event& event)
   }
 }
 
-void
-JoystickKeyboardController::process_text_input_event(const SDL_TextInputEvent& event)
-{
-  if (Console::instance->hasFocus()) {
-    for(int i = 0; event.text[i] != '\0'; ++i)
-    {
-      Console::instance->input(event.text[i]);
-    }
-  }
-}
-
-void
-JoystickKeyboardController::process_key_event(const SDL_KeyboardEvent& event)
-{
-  KeyMap::iterator key_mapping = keymap.find(event.keysym.sym);
-
-  // if console key was pressed: toggle console
-  if ((key_mapping != keymap.end()) && (key_mapping->second == Controller::CONSOLE)) {
-    if (event.type == SDL_KEYDOWN) 
-      Console::instance->toggle();
-  } else {
-    if (Console::instance->hasFocus()) {
-      // if console is open: send key there
-      process_console_key_event(event);
-    } else if (MenuManager::current()) {
-      // if menu mode: send key there
-      process_menu_key_event(event);
-    } else if (key_mapping == keymap.end()) {
-      // default action: update controls
-      //log_debug << "Key " << event.key.SDL_Keycode.sym << " is unbound" << std::endl;
-    } else {
-      Control control = key_mapping->second;
-      bool value = (event.type == SDL_KEYDOWN);
-      controller->set_control(control, value);
-      if (jump_with_up_kbd && control == Controller::UP){
-        controller->set_control(Controller::JUMP, value);
-      }
-    }
-  }
-}
-
-void
-JoystickKeyboardController::process_console_key_event(const SDL_KeyboardEvent& event)
-{
-  if (event.type != SDL_KEYDOWN) return;
-
-  switch (event.keysym.sym) {
-    case SDLK_RETURN:
-      Console::instance->enter();
-      break;
-    case SDLK_BACKSPACE:
-      Console::instance->backspace();
-      break;
-    case SDLK_TAB:
-      Console::instance->autocomplete();
-      break;
-    case SDLK_PAGEUP:
-      Console::instance->scroll(-1);
-      break;
-    case SDLK_PAGEDOWN:
-      Console::instance->scroll(+1);
-      break;
-    case SDLK_HOME:
-      Console::instance->move_cursor(-65535);
-      break;
-    case SDLK_END:
-      Console::instance->move_cursor(+65535);
-      break;
-    case SDLK_UP:
-      Console::instance->show_history(-1);
-      break;
-    case SDLK_DOWN:
-      Console::instance->show_history(+1);
-      break;
-    case SDLK_LEFT:
-      Console::instance->move_cursor(-1);
-      break;
-    case SDLK_RIGHT:
-      Console::instance->move_cursor(+1);
-      break;
-    default:
-      break;
-  }
-}
-
-void
-JoystickKeyboardController::process_menu_key_event(const SDL_KeyboardEvent& event)
-{
-  // wait for key mode?
-  if (wait_for_key >= 0) {
-    if (event.type == SDL_KEYUP)
-      return;
-
-    if (event.keysym.sym != SDLK_ESCAPE
-       && event.keysym.sym != SDLK_PAUSE) {
-      bind_key(event.keysym.sym, Control(wait_for_key));
-    }
-    reset();
-    MenuStorage::get_key_options_menu()->update();
-    wait_for_key = -1;
-    return;
-  }
-  if (joystick_manager->wait_for_joystick >= 0) {
-    if (event.keysym.sym == SDLK_ESCAPE) {
-      reset();
-      MenuStorage::get_joystick_options_menu()->update();
-      joystick_manager->wait_for_joystick = -1;
-    }
-    return;
-  }
-
-  Control control;
-  /* we use default keys when the menu is open (to avoid problems when
-   * redefining keys to invalid settings
-   */
-  switch(event.keysym.sym) {
-    case SDLK_UP:
-      control = Controller::UP;
-      break;
-    case SDLK_DOWN:
-      control = Controller::DOWN;
-      break;
-    case SDLK_LEFT:
-      control = Controller::LEFT;
-      break;
-    case SDLK_RIGHT:
-      control = Controller::RIGHT;
-      break;
-    case SDLK_SPACE:
-    case SDLK_RETURN:
-    case SDLK_KP_ENTER:
-      control = Controller::MENU_SELECT;
-      break;
-    case SDLK_ESCAPE:
-    case SDLK_PAUSE:
-      control = Controller::PAUSE_MENU;
-      break;
-    default:
-      return;
-      break;
-  }
-
-  controller->set_control(control, (event.type == SDL_KEYDOWN));
-}
-
-void
-JoystickKeyboardController::bind_key(SDL_Keycode key, Control control)
-{
-  // 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
-JoystickKeyboardController::reversemap_key(Control c)
-{
-  for(KeyMap::iterator i = keymap.begin(); i != keymap.end(); ++i) {
-    if (i->second == c)
-      return i->first;
-  }
-
-  return SDLK_UNKNOWN;
-}
-
 /* EOF */
index 411000b..24adfc4 100644 (file)
@@ -32,10 +32,11 @@ class Controller;
 class GameControllerManager;
 class JoystickManager;
 class JoystickMenu;
+class KeyboardManager;
 class KeyboardMenu;
 class Menu;
 
-class JoystickKeyboardController
+class JoystickKeyboardController final
 {
 private:
   friend class KeyboardMenu;
@@ -43,14 +44,10 @@ private:
 
   typedef Controller::Control Control;
 
-  typedef std::map<SDL_Keycode, Control> KeyMap;
-
 public:
   JoystickKeyboardController();
   virtual ~JoystickKeyboardController();
 
-  /** Process an SDL Event and return true if the event has been used
-   */
   void process_event(const SDL_Event& event);
 
   void write(Writer& writer);
@@ -58,34 +55,18 @@ public:
   void update();
   void reset();
 
-  void updateAvailableJoysticks();
-
-  Controller *get_main_controller();
-
-private:
-  void process_text_input_event(const SDL_TextInputEvent& event);
-  void process_key_event(const SDL_KeyboardEvent& event);
-  void process_console_key_event(const SDL_KeyboardEvent& event);
-  void process_menu_key_event(const SDL_KeyboardEvent& event);
-
-  SDL_Keycode reversemap_key(Control c);
-  void bind_key(SDL_Keycode key, Control c);
+  Controller* get_controller();
 
 private:
   std::unique_ptr<Controller> controller;
+
 public:
   bool m_use_game_controller;
-  std::unique_ptr<JoystickManager> joystick_manager;
+  std::unique_ptr<KeyboardManager> keyboard_manager;
+  std::unique_ptr<JoystickManager> joystick_manager; 
   std::unique_ptr<GameControllerManager> game_controller_manager;
 
 private:
-  KeyMap keymap;
-
-  bool jump_with_up_kbd; // Keyboard up jumps
-
-  int wait_for_key;
-
-private:
   JoystickKeyboardController(const JoystickKeyboardController&);
   JoystickKeyboardController& operator=(const JoystickKeyboardController&);
 };
diff --git a/src/control/keyboard_manager.cpp b/src/control/keyboard_manager.cpp
new file mode 100644 (file)
index 0000000..929e106
--- /dev/null
@@ -0,0 +1,298 @@
+//  SuperTux
+//  Copyright (C) 2006 Matthias Braun <matze@braunis.de>,
+//                2007-2014 Ingo Ruhnke <grumbel@gmx.de>
+//
+//  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
+//  the Free Software Foundation, either version 3 of the License, or
+//  (at your option) any later version.
+//
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//  GNU General Public License for more details.
+//
+//  You should have received a copy of the GNU General Public License
+//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+#include "control/keyboard_manager.hpp"
+
+#include "control/controller.hpp"
+#include "control/joystick_manager.hpp"
+#include "gui/menu_manager.hpp"
+#include "lisp/list_iterator.hpp"
+#include "supertux/console.hpp"
+#include "supertux/menu/joystick_menu.hpp"
+#include "supertux/menu/keyboard_menu.hpp"
+#include "supertux/menu/menu_storage.hpp"
+#include "util/writer.hpp"
+
+KeyboardManager::KeyboardManager(JoystickKeyboardController* parent) :
+  m_parent(parent),
+  keymap(),
+  jump_with_up_kbd(false),
+  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()
+{
+}
+
+void
+KeyboardManager::process_key_event(const SDL_KeyboardEvent& event)
+{
+  KeyMap::iterator key_mapping = keymap.find(event.keysym.sym);
+
+  // if console key was pressed: toggle console
+  if (key_mapping != keymap.end() && 
+      key_mapping->second == Controller::CONSOLE)
+  {
+    if (event.type == SDL_KEYDOWN)
+    {
+      Console::instance->toggle();
+    }
+  } 
+  else if (Console::instance->hasFocus()) 
+  {
+    // if console is open: send key there
+    process_console_key_event(event);
+  }
+  else if (MenuManager::current()) 
+  {
+    // if menu mode: send key there
+    process_menu_key_event(event);
+  }
+  else if (key_mapping == keymap.end()) 
+  {
+    // default action: update controls
+    //log_debug << "Key " << event.key.SDL_Keycode.sym << " is unbound" << std::endl;
+  }
+  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)
+    {
+      m_parent->get_controller()->set_control(Controller::JUMP, value);
+    }
+  }
+}
+
+void
+KeyboardManager::process_text_input_event(const SDL_TextInputEvent& event)
+{
+  if (Console::instance->hasFocus()) {
+    for(int i = 0; event.text[i] != '\0'; ++i)
+    {
+      Console::instance->input(event.text[i]);
+    }
+  }
+}
+
+void
+KeyboardManager::process_console_key_event(const SDL_KeyboardEvent& event)
+{
+  if (event.type != SDL_KEYDOWN) return;
+
+  switch (event.keysym.sym) {
+    case SDLK_RETURN:
+      Console::instance->enter();
+      break;
+    case SDLK_BACKSPACE:
+      Console::instance->backspace();
+      break;
+    case SDLK_TAB:
+      Console::instance->autocomplete();
+      break;
+    case SDLK_PAGEUP:
+      Console::instance->scroll(-1);
+      break;
+    case SDLK_PAGEDOWN:
+      Console::instance->scroll(+1);
+      break;
+    case SDLK_HOME:
+      Console::instance->move_cursor(-65535);
+      break;
+    case SDLK_END:
+      Console::instance->move_cursor(+65535);
+      break;
+    case SDLK_UP:
+      Console::instance->show_history(-1);
+      break;
+    case SDLK_DOWN:
+      Console::instance->show_history(+1);
+      break;
+    case SDLK_LEFT:
+      Console::instance->move_cursor(-1);
+      break;
+    case SDLK_RIGHT:
+      Console::instance->move_cursor(+1);
+      break;
+    default:
+      break;
+  }
+}
+
+void
+KeyboardManager::process_menu_key_event(const SDL_KeyboardEvent& event)
+{
+  // wait for key mode?
+  if (wait_for_key >= 0) 
+  {
+    if (event.type == SDL_KEYUP)
+      return;
+
+    if (event.keysym.sym != SDLK_ESCAPE && 
+        event.keysym.sym != SDLK_PAUSE) 
+    {
+      bind_key(event.keysym.sym, static_cast<Controller::Control>(wait_for_key));
+    }
+    m_parent->reset();
+    MenuStorage::get_key_options_menu()->update();
+    wait_for_key = -1;
+    return;
+  }
+  
+  if (m_parent->joystick_manager->wait_for_joystick >= 0) 
+  {
+    if (event.keysym.sym == SDLK_ESCAPE) 
+    {
+      m_parent->reset();
+      MenuStorage::get_joystick_options_menu()->update();
+      m_parent->joystick_manager->wait_for_joystick = -1;
+    }
+    return;
+  }
+
+  Controller::Control control;
+  /* we use default keys when the menu is open (to avoid problems when
+   * redefining keys to invalid settings
+   */
+  switch(event.keysym.sym) {
+    case SDLK_UP:
+      control = Controller::UP;
+      break;
+    case SDLK_DOWN:
+      control = Controller::DOWN;
+      break;
+    case SDLK_LEFT:
+      control = Controller::LEFT;
+      break;
+    case SDLK_RIGHT:
+      control = Controller::RIGHT;
+      break;
+    case SDLK_SPACE:
+    case SDLK_RETURN:
+    case SDLK_KP_ENTER:
+      control = Controller::MENU_SELECT;
+      break;
+    case SDLK_ESCAPE:
+    case SDLK_PAUSE:
+      control = Controller::PAUSE_MENU;
+      break;
+    default:
+      return;
+      break;
+  }
+
+  m_parent->get_controller()->set_control(control, (event.type == SDL_KEYDOWN));
+}
+
+void
+KeyboardManager::bind_key(SDL_Keycode key, Controller::Control control)
+{
+  // 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)
+{
+  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("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");
+  }
+}
+
+/* EOF */
diff --git a/src/control/keyboard_manager.hpp b/src/control/keyboard_manager.hpp
new file mode 100644 (file)
index 0000000..15d8043
--- /dev/null
@@ -0,0 +1,65 @@
+//  SuperTux
+//  Copyright (C) 2006 Matthias Braun <matze@braunis.de>,
+//                2007-2014 Ingo Ruhnke <grumbel@gmx.de>
+//
+//  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
+//  the Free Software Foundation, either version 3 of the License, or
+//  (at your option) any later version.
+//
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//  GNU General Public License for more details.
+//
+//  You should have received a copy of the GNU General Public License
+//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+#ifndef HEADER_SUPERTUX_CONTROL_KEYBOARD_MANAGER_HPP
+#define HEADER_SUPERTUX_CONTROL_KEYBOARD_MANAGER_HPP
+
+#include <map>
+
+#include "SDL.h"
+
+#include "control/controller.hpp"
+#include "util/reader_fwd.hpp"
+#include "util/writer_fwd.hpp"
+
+class JoystickKeyboardController;
+
+class KeyboardManager final
+{
+private:
+  friend class KeyboardMenu;
+  typedef std::map<SDL_Keycode, Controller::Control> KeyMap;
+
+public:
+  KeyboardManager(JoystickKeyboardController* parent);
+  ~KeyboardManager();
+
+  void process_key_event(const SDL_KeyboardEvent& event);
+  void process_text_input_event(const SDL_TextInputEvent& event);
+  void process_console_key_event(const SDL_KeyboardEvent& event);
+  void process_menu_key_event(const SDL_KeyboardEvent& event);
+
+  SDL_Keycode reversemap_key(Controller::Control c);
+  void bind_key(SDL_Keycode key, Controller::Control c);
+
+  void read(const lisp::Lisp* keymap_lisp);
+  void write(Writer& writer);
+
+private:
+  JoystickKeyboardController* m_parent;
+  KeyMap keymap;
+  bool jump_with_up_kbd;
+  int wait_for_key;
+
+private:
+  KeyboardManager(const KeyboardManager&) = delete;
+  KeyboardManager& operator=(const KeyboardManager&) = delete;
+};
+
+#endif
+
+/* EOF */
index c2917e7..55415ee 100644 (file)
@@ -227,7 +227,7 @@ Menu::update()
     effect_progress = 0.0f;
   }
 
-  Controller *controller = g_jk_controller->get_main_controller();
+  Controller* controller = g_jk_controller->get_controller();
   /** check main input controller... */
   if(controller->pressed(Controller::UP)) {
     menuaction = MENU_ACTION_UP;
index 46c4423..ab6c519 100644 (file)
@@ -159,7 +159,7 @@ Player::Player(PlayerStatus* _player_status, const std::string& name) :
   climbing(0)
 {
   this->name = name;
-  controller = g_jk_controller->get_main_controller();
+  controller = g_jk_controller->get_controller();
   scripting_controller.reset(new CodeController());
   // if/when we have complete penny gfx, we can
   // load those instead of Tux's sprite in the
index 393d6e7..2e76fa4 100644 (file)
@@ -349,7 +349,7 @@ GameSession::process_events()
 
   // save input for demo?
   if(capture_demo_stream != 0) {
-    Controller *controller = g_jk_controller->get_main_controller();
+    Controller *controller = g_jk_controller->get_controller();
     capture_demo_stream ->put(controller->hold(Controller::LEFT));
     capture_demo_stream ->put(controller->hold(Controller::RIGHT));
     capture_demo_stream ->put(controller->hold(Controller::UP));
@@ -436,7 +436,7 @@ void
 GameSession::update(float elapsed_time)
 {
   // handle controller
-  if(g_jk_controller->get_main_controller()->pressed(Controller::PAUSE_MENU))
+  if(g_jk_controller->get_controller()->pressed(Controller::PAUSE_MENU))
     on_escape_press();
 
   process_events();
index f81d653..8b3d3ed 100644 (file)
@@ -53,7 +53,7 @@ LevelIntro::setup()
 void
 LevelIntro::update(float elapsed_time)
 {
-  Controller *controller = g_jk_controller->get_main_controller();
+  Controller *controller = g_jk_controller->get_controller();
 
   // Check if it's time to exit the screen
   if(controller->pressed(Controller::JUMP)
index bbdd1cf..e75b59d 100644 (file)
 
 #include "supertux/menu/keyboard_menu.hpp"
 
-#include "util/gettext.hpp"
+#include "control/keyboard_manager.hpp"
 #include "supertux/gameconfig.hpp"
 #include "supertux/globals.hpp"
+#include "util/gettext.hpp"
 
 KeyboardMenu::KeyboardMenu(JoystickKeyboardController* _controller) :
   controller(_controller)
@@ -39,7 +40,7 @@ KeyboardMenu::KeyboardMenu(JoystickKeyboardController* _controller) :
   if (g_config->console_enabled) {
     add_controlfield(Controller::CONSOLE, _("Console"));
   }
-  add_toggle(Controller::CONTROLCOUNT, _("Jump with Up"), controller->jump_with_up_kbd);
+  add_toggle(Controller::CONTROLCOUNT, _("Jump with Up"), controller->keyboard_manager->jump_with_up_kbd);
   add_hl();
   add_back(_("Back"));
   update();
@@ -88,9 +89,9 @@ KeyboardMenu::menu_action(MenuItem* item)
 {
   if(item->id >= 0 && item->id < Controller::CONTROLCOUNT){
     item->change_input(_("Press Key"));
-    controller->wait_for_key = item->id;
+    controller->keyboard_manager->wait_for_key = item->id;
   } else if( item->id == Controller::CONTROLCOUNT) {
-    controller->jump_with_up_kbd = item->toggled;
+    controller->keyboard_manager->jump_with_up_kbd = item->toggled;
   } 
 }
 
@@ -98,31 +99,34 @@ void
 KeyboardMenu::update()
 {
   // update menu
+
+  auto& kbd_mgr = controller->keyboard_manager;
+
   get_item_by_id((int) Controller::UP).change_input(get_key_name(
-                                                      controller->reversemap_key(Controller::UP)));
+                                                      kbd_mgr->reversemap_key(Controller::UP)));
   get_item_by_id((int) Controller::DOWN).change_input(get_key_name(
-                                                        controller->reversemap_key(Controller::DOWN)));
+                                                        kbd_mgr->reversemap_key(Controller::DOWN)));
   get_item_by_id((int) Controller::LEFT).change_input(get_key_name(
-                                                        controller->reversemap_key(Controller::LEFT)));
+                                                        kbd_mgr->reversemap_key(Controller::LEFT)));
   get_item_by_id((int) Controller::RIGHT).change_input(get_key_name(
-                                                         controller->reversemap_key(Controller::RIGHT)));
+                                                         kbd_mgr->reversemap_key(Controller::RIGHT)));
   get_item_by_id((int) Controller::JUMP).change_input(get_key_name(
-                                                        controller->reversemap_key(Controller::JUMP)));
+                                                        kbd_mgr->reversemap_key(Controller::JUMP)));
   get_item_by_id((int) Controller::ACTION).change_input(get_key_name(
-                                                          controller->reversemap_key(Controller::ACTION)));
+                                                          kbd_mgr->reversemap_key(Controller::ACTION)));
   get_item_by_id((int) Controller::PEEK_LEFT).change_input(get_key_name(
-                                                             controller->reversemap_key(Controller::PEEK_LEFT)));
+                                                             kbd_mgr->reversemap_key(Controller::PEEK_LEFT)));
   get_item_by_id((int) Controller::PEEK_RIGHT).change_input(get_key_name(
-                                                              controller->reversemap_key(Controller::PEEK_RIGHT)));
+                                                              kbd_mgr->reversemap_key(Controller::PEEK_RIGHT)));
   get_item_by_id((int) Controller::PEEK_UP).change_input(get_key_name(
-                                                           controller->reversemap_key(Controller::PEEK_UP)));
+                                                           kbd_mgr->reversemap_key(Controller::PEEK_UP)));
   get_item_by_id((int) Controller::PEEK_DOWN).change_input(get_key_name(
-                                                             controller->reversemap_key(Controller::PEEK_DOWN)));
+                                                             kbd_mgr->reversemap_key(Controller::PEEK_DOWN)));
   if (g_config->console_enabled) {
     get_item_by_id((int) Controller::CONSOLE).change_input(get_key_name(
-                                                             controller->reversemap_key(Controller::CONSOLE)));
+                                                             kbd_mgr->reversemap_key(Controller::CONSOLE)));
   }
-  get_item_by_id(Controller::CONTROLCOUNT).toggled = controller->jump_with_up_kbd;
+  get_item_by_id(Controller::CONTROLCOUNT).toggled = kbd_mgr->jump_with_up_kbd;
 }
 
 /* EOF */
index be8fca5..2a023fb 100644 (file)
@@ -93,7 +93,7 @@ TextScroller::setup()
 void
 TextScroller::update(float elapsed_time)
 {
-  Controller *controller = g_jk_controller->get_main_controller();
+  Controller *controller = g_jk_controller->get_controller();
   if(controller->hold(Controller::UP)) {
     speed = -defaultspeed*5;
   } else if(controller->hold(Controller::DOWN)) {
index 1e6ba7b..85c4e78 100644 (file)
@@ -290,7 +290,7 @@ Tux::tryContinueWalking(float elapsed_time)
 void
 Tux::updateInputDirection()
 {
-  Controller *controller = g_jk_controller->get_main_controller();
+  Controller* controller = g_jk_controller->get_controller();
   if(controller->hold(Controller::UP))
     input_direction = D_NORTH;
   else if(controller->hold(Controller::DOWN))
index dc2203b..f30241b 100644 (file)
@@ -648,7 +648,7 @@ WorldMap::update(float delta)
     }
 
     // handle input
-    Controller *controller = g_jk_controller->get_main_controller();
+    Controller *controller = g_jk_controller->get_controller();
     bool enter_level = false;
     if(controller->pressed(Controller::ACTION)
        || controller->pressed(Controller::JUMP)