From: Ingo Ruhnke Date: Wed, 6 Aug 2014 21:22:51 +0000 (+0200) Subject: Added option to disable gamecontroller use and fall back to old joystick code X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=e5a1dac5c50a5e409c7108299fc9e0d852913fdd;p=supertux.git Added option to disable gamecontroller use and fall back to old joystick code --- diff --git a/src/control/input_manager.cpp b/src/control/input_manager.cpp index f9d87d15b..6d4ab1104 100644 --- a/src/control/input_manager.cpp +++ b/src/control/input_manager.cpp @@ -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: diff --git a/src/control/input_manager.hpp b/src/control/input_manager.hpp index 7e5e377f6..ba1df178e 100644 --- a/src/control/input_manager.hpp +++ b/src/control/input_manager.hpp @@ -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: diff --git a/src/control/joystick_manager.cpp b/src/control/joystick_manager.cpp index 169cad593..8fb665523 100644 --- a/src/control/joystick_manager.cpp +++ b/src/control/joystick_manager.cpp @@ -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(), diff --git a/src/supertux/menu/joystick_menu.cpp b/src/supertux/menu/joystick_menu.cpp index 96c9e03ab..ecb0de133 100644 --- a/src/supertux/menu/joystick_menu.cpp +++ b/src/supertux/menu/joystick_menu.cpp @@ -22,41 +22,67 @@ #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(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(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(id)).change_input(name); + } + else + { + get_item_by_id(static_cast(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; } } diff --git a/src/supertux/menu/joystick_menu.hpp b/src/supertux/menu/joystick_menu.hpp index b7498c04c..b4e7fd043 100644 --- a/src/supertux/menu/joystick_menu.hpp +++ b/src/supertux/menu/joystick_menu.hpp @@ -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&);