2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>,
3 // 2007,2014 Ingo Ruhnke <grumbel@gmail.com>
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "supertux/menu/joystick_menu.hpp"
22 #include "control/joystick_manager.hpp"
23 #include "supertux/gameconfig.hpp"
24 #include "util/gettext.hpp"
29 MNID_JUMP_WITH_UP = Controller::CONTROLCOUNT,
31 MNID_AUTO_JOYSTICK_CFG
36 JoystickMenu::JoystickMenu(InputManager& input_manager) :
37 m_input_manager(input_manager),
38 m_joysticks_available(false)
43 JoystickMenu::~JoystickMenu()
47 JoystickMenu::recreate_menu()
50 add_label(_("Setup Joystick"));
53 add_toggle(MNID_AUTO_JOYSTICK_CFG, _("Manual Configuration"),
54 !m_input_manager.use_game_controller())
55 ->set_help(_("Use manual configuration instead of SDL2's automatic GameController support"));
57 if (m_input_manager.use_game_controller())
59 m_joysticks_available = false;
63 if (m_input_manager.joystick_manager->joysticks.size() > 0)
65 m_joysticks_available = true;
67 add_controlfield(Controller::UP, _("Up"));
68 add_controlfield(Controller::DOWN, _("Down"));
69 add_controlfield(Controller::LEFT, _("Left"));
70 add_controlfield(Controller::RIGHT, _("Right"));
71 add_controlfield(Controller::JUMP, _("Jump"));
72 add_controlfield(Controller::ACTION, _("Action"));
73 add_controlfield(Controller::PAUSE_MENU, _("Pause/Menu"));
74 add_controlfield(Controller::PEEK_LEFT, _("Peek Left"));
75 add_controlfield(Controller::PEEK_RIGHT, _("Peek Right"));
76 add_controlfield(Controller::PEEK_UP, _("Peek Up"));
77 add_controlfield(Controller::PEEK_DOWN, _("Peek Down"));
79 add_toggle(MNID_JUMP_WITH_UP, _("Jump with Up"), g_config->joystick_config.jump_with_up_joy);
83 m_joysticks_available = false;
85 add_inactive(-1, _("No Joysticks found"));
86 add_entry(MNID_SCAN_JOYSTICKS, _("Scan for Joysticks"));
96 JoystickMenu::get_button_name(int button)
104 std::ostringstream name;
105 name << "Button " << button;
111 JoystickMenu::menu_action(MenuItem* item)
113 if (0 <= item->id && item->id < Controller::CONTROLCOUNT)
115 item->change_input(_("Press Button"));
116 m_input_manager.joystick_manager->wait_for_joystick = item->id;
118 else if (item->id == MNID_JUMP_WITH_UP)
120 g_config->joystick_config.jump_with_up_joy = item->toggled;
122 else if (item->id == MNID_AUTO_JOYSTICK_CFG)
124 m_input_manager.use_game_controller(!item->toggled);
125 m_input_manager.reset();
128 else if(item->id == MNID_SCAN_JOYSTICKS)
130 m_input_manager.reset();
136 JoystickMenu::refresh_menu_item(Controller::Control id)
138 int button = g_config->joystick_config.reversemap_joybutton(id);
139 int axis = g_config->joystick_config.reversemap_joyaxis(id);
140 int hat_dir = g_config->joystick_config.reversemap_joyhat(id);
144 get_item_by_id(static_cast<int>(id)).change_input(get_button_name(button));
148 std::ostringstream name;
159 else if (abs(axis) == 2)
161 else if (abs(axis) == 2)
163 else if (abs(axis) == 3)
168 get_item_by_id(static_cast<int>(id)).change_input(name.str());
170 else if (hat_dir != -1)
193 name = "Unknown hat_dir";
197 get_item_by_id(static_cast<int>(id)).change_input(name);
201 get_item_by_id(static_cast<int>(id)).change_input("None");
206 JoystickMenu::refresh()
208 if (m_joysticks_available)
210 refresh_menu_item(Controller::UP);
211 refresh_menu_item(Controller::DOWN);
212 refresh_menu_item(Controller::LEFT);
213 refresh_menu_item(Controller::RIGHT);
215 refresh_menu_item(Controller::JUMP);
216 refresh_menu_item(Controller::ACTION);
217 refresh_menu_item(Controller::PAUSE_MENU);
218 refresh_menu_item(Controller::PEEK_LEFT);
219 refresh_menu_item(Controller::PEEK_RIGHT);
220 refresh_menu_item(Controller::PEEK_UP);
221 refresh_menu_item(Controller::PEEK_DOWN);