Added option to disable gamecontroller use and fall back to old joystick code
authorIngo Ruhnke <grumbel@gmail.com>
Wed, 6 Aug 2014 21:22:51 +0000 (23:22 +0200)
committerIngo Ruhnke <grumbel@gmail.com>
Wed, 6 Aug 2014 21:22:51 +0000 (23:22 +0200)
src/control/input_manager.cpp
src/control/input_manager.hpp
src/control/joystick_manager.cpp
src/supertux/menu/joystick_menu.cpp
src/supertux/menu/joystick_menu.hpp

index f9d87d1..6d4ab11 100644 (file)
@@ -48,6 +48,12 @@ InputManager::get_controller()
 }
 
 void
+InputManager::use_game_controller(bool v)
+{
+  m_use_game_controller = v;
+}
+
+void
 InputManager::read(const Reader& lisp)
 {
   const lisp::Lisp* keymap_lisp = lisp.get_lisp("keymap");
@@ -114,11 +120,11 @@ InputManager::process_event(const SDL_Event& event)
       break;
 
     case SDL_JOYDEVICEADDED:
-      if (!m_use_game_controller) joystick_manager->on_joystick_added(event.jdevice.which);
+      joystick_manager->on_joystick_added(event.jdevice.which);
       break;
 
     case SDL_JOYDEVICEREMOVED:
-      if (!m_use_game_controller) joystick_manager->on_joystick_removed(event.jdevice.which);
+      joystick_manager->on_joystick_removed(event.jdevice.which);
       break;
 
     case SDL_CONTROLLERAXISMOTION:
@@ -135,12 +141,12 @@ InputManager::process_event(const SDL_Event& event)
 
     case SDL_CONTROLLERDEVICEADDED:
       std::cout << "SDL_CONTROLLERDEVICEADDED" << std::endl;
-      if (m_use_game_controller) game_controller_manager->on_controller_added(event.cdevice.which);
+      game_controller_manager->on_controller_added(event.cdevice.which);
       break;
 
     case SDL_CONTROLLERDEVICEREMOVED:
       std::cout << "SDL_CONTROLLERDEVICEREMOVED" << std::endl;
-      if (m_use_game_controller) game_controller_manager->on_controller_removed(event.cdevice.which);
+      game_controller_manager->on_controller_removed(event.cdevice.which);
       break;
 
     case SDL_CONTROLLERDEVICEREMAPPED:
index 7e5e377..ba1df17 100644 (file)
@@ -55,6 +55,9 @@ public:
   void update();
   void reset();
 
+  void use_game_controller(bool v);
+  bool use_game_controller() const { return m_use_game_controller; }
+
   Controller* get_controller();
 
 private:
index 169cad5..8fb6655 100644 (file)
@@ -32,7 +32,7 @@ JoystickManager::JoystickManager(InputManager* parent) :
   joy_button_map(),
   joy_axis_map(),
   joy_hat_map(),
-  dead_zone(4000),
+  dead_zone(8000),
   min_joybuttons(),
   max_joybuttons(),
   max_joyaxis(),
index 96c9e03..ecb0de1 100644 (file)
 #include "control/joystick_manager.hpp"
 #include "util/gettext.hpp"
 
-JoystickMenu::JoystickMenu(InputManager* _controller) :
-  controller(_controller),
-  joysticks_available(false)
+namespace {
+
+enum {
+  MNID_JUMP_WITH_UP = Controller::CONTROLCOUNT,
+  MNID_SCAN_JOYSTICKS,
+  MNID_AUTO_JOYSTICK_CFG
+};
+
+} // namespace
+
+JoystickMenu::JoystickMenu(InputManager* input_manager) :
+  m_input_manager(input_manager),
+  m_joysticks_available(false)
 {
-  recreateMenu();
+  recreate_menu();
 }
 
 JoystickMenu::~JoystickMenu()
 {}
 
 void
-JoystickMenu::recreateMenu()
+JoystickMenu::recreate_menu()
 {
   clear();
   add_label(_("Setup Joystick"));
   add_hl();
-  if(controller->joystick_manager->joysticks.size() > 0) {
-    joysticks_available = true;
-
-    add_controlfield(Controller::UP,          _("Up"));
-    add_controlfield(Controller::DOWN,        _("Down"));
-    add_controlfield(Controller::LEFT,        _("Left"));
-    add_controlfield(Controller::RIGHT,       _("Right"));
-    add_controlfield(Controller::JUMP,        _("Jump"));
-    add_controlfield(Controller::ACTION,      _("Action"));
-    add_controlfield(Controller::PAUSE_MENU,  _("Pause/Menu"));
-    add_controlfield(Controller::PEEK_LEFT,   _("Peek Left"));
-    add_controlfield(Controller::PEEK_RIGHT,  _("Peek Right"));
-    add_controlfield(Controller::PEEK_UP,     _("Peek Up"));
-    add_controlfield(Controller::PEEK_DOWN,   _("Peek Down"));
-
-    add_toggle(Controller::CONTROLCOUNT, _("Jump with Up"), controller->joystick_manager->jump_with_up_joy);
-  } else {
-    add_inactive(-1, _("No Joysticks found"));
-    joysticks_available = false;
+
+  add_toggle(MNID_AUTO_JOYSTICK_CFG, _("Manual Joystick Configuration"),
+             !m_input_manager->use_game_controller());
+
+  if (m_input_manager->use_game_controller())
+  {
+    m_joysticks_available = false;
+  }
+  else
+  {
+    if (m_input_manager->joystick_manager->joysticks.size() > 0)
+    {
+      m_joysticks_available = true;
+
+      add_controlfield(Controller::UP,          _("Up"));
+      add_controlfield(Controller::DOWN,        _("Down"));
+      add_controlfield(Controller::LEFT,        _("Left"));
+      add_controlfield(Controller::RIGHT,       _("Right"));
+      add_controlfield(Controller::JUMP,        _("Jump"));
+      add_controlfield(Controller::ACTION,      _("Action"));
+      add_controlfield(Controller::PAUSE_MENU,  _("Pause/Menu"));
+      add_controlfield(Controller::PEEK_LEFT,   _("Peek Left"));
+      add_controlfield(Controller::PEEK_RIGHT,  _("Peek Right"));
+      add_controlfield(Controller::PEEK_UP,     _("Peek Up"));
+      add_controlfield(Controller::PEEK_DOWN,   _("Peek Down"));
+
+      add_toggle(MNID_JUMP_WITH_UP, _("Jump with Up"), m_input_manager->joystick_manager->jump_with_up_joy);
+    }
+    else
+    {
+      m_joysticks_available = false;
+
+      add_inactive(-1, _("No Joysticks found"));
+      add_entry(MNID_SCAN_JOYSTICKS, _("Scan for Joysticks"));
+    }
   }
 
   add_hl();
@@ -68,34 +94,55 @@ std::string
 JoystickMenu::get_button_name(int button)
 {
   if(button < 0)
+  {
     return _("None");
-
-  std::ostringstream name;
-  name << "Button " << button;
-  return name.str();
+  }
+  else
+  {
+    std::ostringstream name;
+    name << "Button " << button;
+    return name.str();
+  }
 }
 
 void
 JoystickMenu::menu_action(MenuItem* item)
 {
-  if (item->id >= 0 && item->id < Controller::CONTROLCOUNT) {
+  if (0 <= item->id && item->id < Controller::CONTROLCOUNT)
+  {
     item->change_input(_("Press Button"));
-    controller->joystick_manager->wait_for_joystick = item->id;
-  } else if (item->id == Controller::CONTROLCOUNT) {
-    controller->joystick_manager->jump_with_up_joy = item->toggled;
+    m_input_manager->joystick_manager->wait_for_joystick = item->id;
+  }
+  else if (item->id == MNID_JUMP_WITH_UP)
+  {
+    m_input_manager->joystick_manager->jump_with_up_joy = item->toggled;
+  }
+  else if (item->id == MNID_AUTO_JOYSTICK_CFG)
+  {
+    m_input_manager->use_game_controller(!item->toggled);
+    m_input_manager->reset();
+    recreate_menu();
+  }
+  else if(item->id == MNID_SCAN_JOYSTICKS)
+  {
+    m_input_manager->reset();
+    recreate_menu();
   }
 }
 
 void
 JoystickMenu::update_menu_item(Controller::Control id)
 {
-  int button  = controller->joystick_manager->reversemap_joybutton(id);
-  int axis    = controller->joystick_manager->reversemap_joyaxis(id);
-  int hat_dir = controller->joystick_manager->reversemap_joyhat(id);
+  int button  = m_input_manager->joystick_manager->reversemap_joybutton(id);
+  int axis    = m_input_manager->joystick_manager->reversemap_joyaxis(id);
+  int hat_dir = m_input_manager->joystick_manager->reversemap_joyhat(id);
 
-  if (button != -1) {
-    get_item_by_id((int)id).change_input(get_button_name(button));
-  } else if (axis != 0) {
+  if (button != -1)
+  {
+    get_item_by_id(static_cast<int>(id)).change_input(get_button_name(button));
+  }
+  else if (axis != 0)
+  {
     std::ostringstream name;
 
     name << "Axis ";
@@ -116,8 +163,10 @@ JoystickMenu::update_menu_item(Controller::Control id)
     else
       name << abs(axis);
 
-    get_item_by_id((int)id).change_input(name.str());
-  } else if (hat_dir != -1) {
+    get_item_by_id(static_cast<int>(id)).change_input(name.str());
+  }
+  else if (hat_dir != -1)
+  {
     std::string name;
 
     switch (hat_dir)
@@ -143,28 +192,19 @@ JoystickMenu::update_menu_item(Controller::Control id)
         break;
     }
 
-    get_item_by_id((int)id).change_input(name);
-  } else {
-    get_item_by_id((int)id).change_input("None");
+    get_item_by_id(static_cast<int>(id)).change_input(name);
+  }
+  else
+  {
+    get_item_by_id(static_cast<int>(id)).change_input("None");
   }
 }
 
 void
 JoystickMenu::update()
 {
-  log_info << controller->joystick_manager->joysticks.size() << std::endl;
-
-  if(true) //controller->joysticks.size() == 0)
+  if (m_joysticks_available)
   {
-    // do nothing
-  }
-  else
-  {
-    if (!joysticks_available)
-    {
-      recreateMenu();
-    }
-
     update_menu_item(Controller::UP);
     update_menu_item(Controller::DOWN);
     update_menu_item(Controller::LEFT);
@@ -177,8 +217,6 @@ JoystickMenu::update()
     update_menu_item(Controller::PEEK_RIGHT);
     update_menu_item(Controller::PEEK_UP);
     update_menu_item(Controller::PEEK_DOWN);
-
-    //get_item_by_id(Controller::CONTROLCOUNT).toggled = controller->jump_with_up_joy;
   }
 }
 
index b7498c0..b4e7fd0 100644 (file)
@@ -24,7 +24,7 @@
 class JoystickMenu : public Menu
 {
 public:
-  JoystickMenu(InputManager* controller);
+  JoystickMenu(InputManager* input_manager);
   virtual ~JoystickMenu();
 
   void update();
@@ -34,11 +34,11 @@ public:
   void check_menu() {}
 
 private:
-  void recreateMenu();
+  void recreate_menu();
 
 private:
-  InputManager* controller;
-  bool joysticks_available;
+  InputManager* m_input_manager;
+  bool m_joysticks_available;
 
 private:
   JoystickMenu(const JoystickMenu&);