Moved some joystick related messages into log_debug, added help message for manual...
[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 "util/gettext.hpp"
24
25 namespace {
26
27 enum {
28   MNID_JUMP_WITH_UP = Controller::CONTROLCOUNT,
29   MNID_SCAN_JOYSTICKS,
30   MNID_AUTO_JOYSTICK_CFG
31 };
32
33 } // namespace
34
35 JoystickMenu::JoystickMenu(InputManager* input_manager) :
36   m_input_manager(input_manager),
37   m_joysticks_available(false)
38 {
39   recreate_menu();
40 }
41
42 JoystickMenu::~JoystickMenu()
43 {}
44
45 void
46 JoystickMenu::recreate_menu()
47 {
48   clear();
49   add_label(_("Setup Joystick"));
50   add_hl();
51
52   add_toggle(MNID_AUTO_JOYSTICK_CFG, _("Manual Configuration"),
53              !m_input_manager->use_game_controller())
54     ->set_help(_("Use manual configuration instead of SDL2's automatic GameController support"));
55
56   if (m_input_manager->use_game_controller())
57   {
58     m_joysticks_available = false;
59   }
60   else
61   {
62     if (m_input_manager->joystick_manager->joysticks.size() > 0)
63     {
64       m_joysticks_available = true;
65
66       add_controlfield(Controller::UP,          _("Up"));
67       add_controlfield(Controller::DOWN,        _("Down"));
68       add_controlfield(Controller::LEFT,        _("Left"));
69       add_controlfield(Controller::RIGHT,       _("Right"));
70       add_controlfield(Controller::JUMP,        _("Jump"));
71       add_controlfield(Controller::ACTION,      _("Action"));
72       add_controlfield(Controller::PAUSE_MENU,  _("Pause/Menu"));
73       add_controlfield(Controller::PEEK_LEFT,   _("Peek Left"));
74       add_controlfield(Controller::PEEK_RIGHT,  _("Peek Right"));
75       add_controlfield(Controller::PEEK_UP,     _("Peek Up"));
76       add_controlfield(Controller::PEEK_DOWN,   _("Peek Down"));
77
78       add_toggle(MNID_JUMP_WITH_UP, _("Jump with Up"), m_input_manager->joystick_manager->jump_with_up_joy);
79     }
80     else
81     {
82       m_joysticks_available = false;
83
84       add_inactive(-1, _("No Joysticks found"));
85       add_entry(MNID_SCAN_JOYSTICKS, _("Scan for Joysticks"));
86     }
87   }
88
89   add_hl();
90   add_back(_("Back"));
91   refresh();
92 }
93
94 std::string
95 JoystickMenu::get_button_name(int button)
96 {
97   if(button < 0)
98   {
99     return _("None");
100   }
101   else
102   {
103     std::ostringstream name;
104     name << "Button " << button;
105     return name.str();
106   }
107 }
108
109 void
110 JoystickMenu::menu_action(MenuItem* item)
111 {
112   if (0 <= item->id && item->id < Controller::CONTROLCOUNT)
113   {
114     item->change_input(_("Press Button"));
115     m_input_manager->joystick_manager->wait_for_joystick = item->id;
116   }
117   else if (item->id == MNID_JUMP_WITH_UP)
118   {
119     m_input_manager->joystick_manager->jump_with_up_joy = item->toggled;
120   }
121   else if (item->id == MNID_AUTO_JOYSTICK_CFG)
122   {
123     m_input_manager->use_game_controller(!item->toggled);
124     m_input_manager->reset();
125     recreate_menu();
126   }
127   else if(item->id == MNID_SCAN_JOYSTICKS)
128   {
129     m_input_manager->reset();
130     recreate_menu();
131   }
132 }
133
134 void
135 JoystickMenu::refresh_menu_item(Controller::Control id)
136 {
137   int button  = m_input_manager->joystick_manager->reversemap_joybutton(id);
138   int axis    = m_input_manager->joystick_manager->reversemap_joyaxis(id);
139   int hat_dir = m_input_manager->joystick_manager->reversemap_joyhat(id);
140
141   if (button != -1)
142   {
143     get_item_by_id(static_cast<int>(id)).change_input(get_button_name(button));
144   }
145   else if (axis != 0)
146   {
147     std::ostringstream name;
148
149     name << "Axis ";
150
151     if (axis < 0)
152       name << "-";
153     else
154       name << "+";
155
156     if (abs(axis) == 1)
157       name << "X";
158     else if (abs(axis) == 2)
159       name << "Y";
160     else if (abs(axis) == 2)
161       name << "X2";
162     else if (abs(axis) == 3)
163       name << "Y2";
164     else
165       name << abs(axis);
166
167     get_item_by_id(static_cast<int>(id)).change_input(name.str());
168   }
169   else if (hat_dir != -1)
170   {
171     std::string name;
172
173     switch (hat_dir)
174     {
175       case SDL_HAT_UP:
176         name = "Hat Up";
177         break;
178
179       case SDL_HAT_DOWN:
180         name = "Hat Down";
181         break;
182
183       case SDL_HAT_LEFT:
184         name = "Hat Left";
185         break;
186
187       case SDL_HAT_RIGHT:
188         name = "Hat Right";
189         break;
190
191       default:
192         name = "Unknown hat_dir";
193         break;
194     }
195
196     get_item_by_id(static_cast<int>(id)).change_input(name);
197   }
198   else
199   {
200     get_item_by_id(static_cast<int>(id)).change_input("None");
201   }
202 }
203
204 void
205 JoystickMenu::refresh()
206 {
207   if (m_joysticks_available)
208   {
209     refresh_menu_item(Controller::UP);
210     refresh_menu_item(Controller::DOWN);
211     refresh_menu_item(Controller::LEFT);
212     refresh_menu_item(Controller::RIGHT);
213
214     refresh_menu_item(Controller::JUMP);
215     refresh_menu_item(Controller::ACTION);
216     refresh_menu_item(Controller::PAUSE_MENU);
217     refresh_menu_item(Controller::PEEK_LEFT);
218     refresh_menu_item(Controller::PEEK_RIGHT);
219     refresh_menu_item(Controller::PEEK_UP);
220     refresh_menu_item(Controller::PEEK_DOWN);
221   }
222 }
223
224 /* EOF */