Include optimizations
[supertux.git] / src / supertux / menu / joystick_menu.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>,
3 //                2007 Ingo Ruhnke <grumbel@gmx.de>
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 "util/gettext.hpp"
23
24 namespace{
25   const int SCAN_JOYSTICKS = Controller::CONTROLCOUNT + 1;
26 }
27
28 JoystickMenu::JoystickMenu(JoystickKeyboardController* _controller) :
29   controller(_controller)
30 {
31   recreateMenu();
32 }
33
34 JoystickMenu::~JoystickMenu()
35 {}
36
37 void
38 JoystickMenu::recreateMenu()
39 {
40   clear();
41   add_label(_("Setup Joystick"));
42   add_hl();
43   if(controller->joysticks.size() > 0) {
44     add_controlfield(Controller::UP,          _("Up"));
45     add_controlfield(Controller::DOWN,        _("Down"));
46     add_controlfield(Controller::LEFT,        _("Left"));
47     add_controlfield(Controller::RIGHT,       _("Right"));
48     add_controlfield(Controller::JUMP,        _("Jump"));
49     add_controlfield(Controller::ACTION,      _("Action"));
50     add_controlfield(Controller::PAUSE_MENU,  _("Pause/Menu"));
51     add_controlfield(Controller::PEEK_LEFT,   _("Peek Left"));
52     add_controlfield(Controller::PEEK_RIGHT,  _("Peek Right"));
53     add_controlfield(Controller::PEEK_UP,     _("Peek Up"));
54     add_controlfield(Controller::PEEK_DOWN,   _("Peek Down"));
55
56     add_toggle(Controller::CONTROLCOUNT, _("Jump with Up"), controller->jump_with_up_joy);
57   } else {
58     add_inactive(-1, _("No Joysticks found"));
59   }
60   add_inactive(-1,"");
61   add_entry(SCAN_JOYSTICKS, _("Scan for Joysticks"));
62
63   //Show Joysticks currently activated:
64   for(std::vector<SDL_Joystick*>::iterator i = controller->joysticks.begin();
65       i != controller->joysticks.end(); ++i) {
66     if(*i != 0)
67       add_inactive(-1, SDL_JoystickName(SDL_JoystickIndex(*i)) );
68   }
69
70   add_hl();
71   add_back(_("Back"));
72   update();
73 }
74
75 std::string
76 JoystickMenu::get_button_name(int button)
77 {
78   if(button < 0)
79     return _("None");
80
81   std::ostringstream name;
82   name << "Button " << button;
83   return name.str();
84 }
85
86 void
87 JoystickMenu::menu_action(MenuItem* item)
88 {
89   if (item->id >= 0 && item->id < Controller::CONTROLCOUNT) {
90     item->change_input(_("Press Button"));
91     controller->wait_for_joystick = item->id;
92   } else if (item->id == Controller::CONTROLCOUNT) {
93     controller->jump_with_up_joy = item->toggled;
94   } else if( item->id == SCAN_JOYSTICKS) {
95     controller->updateAvailableJoysticks();
96     recreateMenu();
97   }
98 }
99
100 void
101 JoystickMenu::update_menu_item(Controller::Control id)
102 {
103   int button  = controller->reversemap_joybutton(id);
104   int axis    = controller->reversemap_joyaxis(id);
105   int hat_dir = controller->reversemap_joyhat(id);
106
107   if (button != -1) {
108     get_item_by_id((int)id).change_input(get_button_name(button));
109   } else if (axis != 0) {
110     std::ostringstream name;
111
112     name << "Axis ";
113
114     if (axis < 0)
115       name << "-";
116     else
117       name << "+";
118
119     if (abs(axis) == 1)
120       name << "X";
121     else if (abs(axis) == 2)
122       name << "Y";
123     else if (abs(axis) == 2)
124       name << "X2";
125     else if (abs(axis) == 3)
126       name << "Y2";
127     else
128       name << abs(axis);
129
130     get_item_by_id((int)id).change_input(name.str());
131   } else if (hat_dir != -1) {
132     std::string name;
133
134     switch (hat_dir)
135     {
136       case SDL_HAT_UP:
137         name = "Hat Up";
138         break;
139
140       case SDL_HAT_DOWN:
141         name = "Hat Down";
142         break;
143
144       case SDL_HAT_LEFT:
145         name = "Hat Left";
146         break;
147
148       case SDL_HAT_RIGHT:
149         name = "Hat Right";
150         break;
151
152       default:
153         name = "Unknown hat_dir";
154         break;
155     }
156
157     get_item_by_id((int)id).change_input(name);
158   } else {
159     get_item_by_id((int)id).change_input("None");
160   }
161 }
162
163 void
164 JoystickMenu::update()
165 {
166   if(controller->joysticks.size() == 0)
167     return;
168
169   update_menu_item(Controller::UP);
170   update_menu_item(Controller::DOWN);
171   update_menu_item(Controller::LEFT);
172   update_menu_item(Controller::RIGHT);
173
174   update_menu_item(Controller::JUMP);
175   update_menu_item(Controller::ACTION);
176   update_menu_item(Controller::PAUSE_MENU);
177   update_menu_item(Controller::PEEK_LEFT);
178   update_menu_item(Controller::PEEK_RIGHT);
179   update_menu_item(Controller::PEEK_UP);
180   update_menu_item(Controller::PEEK_DOWN);
181
182   get_item_by_id(Controller::CONTROLCOUNT).toggled = controller->jump_with_up_joy;
183 }
184
185 /* EOF */