Possible fix for expression that's always false
[supertux.git] / src / supertux / menu / joystick_menu.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>,
3 //           2007,2014 Ingo Ruhnke <grumbel@gmail.com>
4 //
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.
9 //
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.
14 //
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/>.
17
18 #include "supertux/menu/joystick_menu.hpp"
19
20 #include <sstream>
21
22 #include "control/joystick_manager.hpp"
23 #include "supertux/gameconfig.hpp"
24 #include "util/gettext.hpp"
25
26 namespace {
27
28 enum {
29   MNID_JUMP_WITH_UP = Controller::CONTROLCOUNT,
30   MNID_SCAN_JOYSTICKS,
31   MNID_AUTO_JOYSTICK_CFG
32 };
33
34 } // namespace
35
36 JoystickMenu::JoystickMenu(InputManager& input_manager) :
37   m_input_manager(input_manager),
38   m_joysticks_available(false)
39 {
40   recreate_menu();
41 }
42
43 JoystickMenu::~JoystickMenu()
44 {}
45
46 void
47 JoystickMenu::recreate_menu()
48 {
49   clear();
50   add_label(_("Setup Joystick"));
51   add_hl();
52
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"));
56
57   if (m_input_manager.use_game_controller())
58   {
59     m_joysticks_available = false;
60   }
61   else
62   {
63     if (m_input_manager.joystick_manager->get_num_joysticks() > 0)
64     {
65       m_joysticks_available = true;
66
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::START,       _("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"));
78       if (g_config->developer_mode || g_config->console_enabled) {
79         add_controlfield(Controller::CONSOLE, _("Console"));
80       }
81       if (g_config->developer_mode) {
82         add_controlfield(Controller::CHEAT_MENU, _("Cheat Menu"));
83       }
84       add_toggle(MNID_JUMP_WITH_UP, _("Jump with Up"), g_config->joystick_config.jump_with_up_joy);
85     }
86     else
87     {
88       m_joysticks_available = false;
89
90       add_inactive(-1, _("No Joysticks found"));
91       add_entry(MNID_SCAN_JOYSTICKS, _("Scan for Joysticks"));
92     }
93   }
94
95   add_hl();
96   add_back(_("Back"));
97   refresh();
98 }
99
100 std::string
101 JoystickMenu::get_button_name(int button)
102 {
103   if(button < 0)
104   {
105     return _("None");
106   }
107   else
108   {
109     std::ostringstream name;
110     name << "Button " << button;
111     return name.str();
112   }
113 }
114
115 void
116 JoystickMenu::menu_action(MenuItem* item)
117 {
118   if (0 <= item->id && item->id < Controller::CONTROLCOUNT)
119   {
120     item->change_input(_("Press Button"));
121     m_input_manager.joystick_manager->bind_next_event_to(static_cast<Controller::Control>(item->id));
122   }
123   else if (item->id == MNID_JUMP_WITH_UP)
124   {
125     g_config->joystick_config.jump_with_up_joy = item->toggled;
126   }
127   else if (item->id == MNID_AUTO_JOYSTICK_CFG)
128   {
129     m_input_manager.use_game_controller(!item->toggled);
130     m_input_manager.reset();
131     recreate_menu();
132   }
133   else if(item->id == MNID_SCAN_JOYSTICKS)
134   {
135     m_input_manager.reset();
136     recreate_menu();
137   }
138 }
139
140 void
141 JoystickMenu::refresh_menu_item(Controller::Control id)
142 {
143   int button  = g_config->joystick_config.reversemap_joybutton(id);
144   int axis    = g_config->joystick_config.reversemap_joyaxis(id);
145   int hat_dir = g_config->joystick_config.reversemap_joyhat(id);
146
147   if (button != -1)
148   {
149     get_item_by_id(static_cast<int>(id)).change_input(get_button_name(button));
150   }
151   else if (axis != 0)
152   {
153     std::ostringstream name;
154
155     name << "Axis ";
156
157     if (axis < 0)
158       name << "-";
159     else
160       name << "+";
161
162     if (abs(axis) == 1)
163       name << "X";
164     else if (abs(axis) == 2)
165       name << "Y";
166     else if (abs(axis) == 3)
167       name << "X2";
168     else if (abs(axis) == 4)
169       name << "Y2";
170     else
171       name << abs(axis);
172
173     get_item_by_id(static_cast<int>(id)).change_input(name.str());
174   }
175   else if (hat_dir != -1)
176   {
177     std::string name;
178
179     switch (hat_dir)
180     {
181       case SDL_HAT_UP:
182         name = "Hat Up";
183         break;
184
185       case SDL_HAT_DOWN:
186         name = "Hat Down";
187         break;
188
189       case SDL_HAT_LEFT:
190         name = "Hat Left";
191         break;
192
193       case SDL_HAT_RIGHT:
194         name = "Hat Right";
195         break;
196
197       default:
198         name = "Unknown hat_dir";
199         break;
200     }
201
202     get_item_by_id(static_cast<int>(id)).change_input(name);
203   }
204   else
205   {
206     get_item_by_id(static_cast<int>(id)).change_input("None");
207   }
208 }
209
210 void
211 JoystickMenu::refresh()
212 {
213   if (m_joysticks_available)
214   {
215     refresh_menu_item(Controller::UP);
216     refresh_menu_item(Controller::DOWN);
217     refresh_menu_item(Controller::LEFT);
218     refresh_menu_item(Controller::RIGHT);
219
220     refresh_menu_item(Controller::JUMP);
221     refresh_menu_item(Controller::ACTION);
222     refresh_menu_item(Controller::START);
223     refresh_menu_item(Controller::PEEK_LEFT);
224     refresh_menu_item(Controller::PEEK_RIGHT);
225     refresh_menu_item(Controller::PEEK_UP);
226     refresh_menu_item(Controller::PEEK_DOWN);
227
228     if (g_config->developer_mode || g_config->console_enabled) {
229       refresh_menu_item(Controller::CONSOLE);
230     }
231     if (g_config->developer_mode) {
232       refresh_menu_item(Controller::CHEAT_MENU);
233     }
234   }
235 }
236
237 /* EOF */