Merge branch 'feature/sdl2'
[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   //Show Joysticks currently activated: //edit by giby
63   SDL_Joystick *joy;
64   if (SDL_NumJoysticks() > 0) {
65     joy = SDL_JoystickOpen(0);
66   }
67
68   for(std::vector<SDL_Joystick*>::iterator i = controller->joysticks.begin();
69       i != controller->joysticks.end(); ++i) {
70     if(*i != 0)
71       add_inactive(-1, SDL_JoystickName(joy) );
72   }
73
74   add_hl();
75   add_back(_("Back"));
76   update();
77 }
78
79 std::string
80 JoystickMenu::get_button_name(int button)
81 {
82   if(button < 0)
83     return _("None");
84
85   std::ostringstream name;
86   name << "Button " << button;
87   return name.str();
88 }
89
90 void
91 JoystickMenu::menu_action(MenuItem* item)
92 {
93   if (item->id >= 0 && item->id < Controller::CONTROLCOUNT) {
94     item->change_input(_("Press Button"));
95     controller->wait_for_joystick = item->id;
96   } else if (item->id == Controller::CONTROLCOUNT) {
97     controller->jump_with_up_joy = item->toggled;
98   } else if( item->id == SCAN_JOYSTICKS) {
99     controller->updateAvailableJoysticks();
100     recreateMenu();
101   }
102 }
103
104 void
105 JoystickMenu::update_menu_item(Controller::Control id)
106 {
107   int button  = controller->reversemap_joybutton(id);
108   int axis    = controller->reversemap_joyaxis(id);
109   int hat_dir = controller->reversemap_joyhat(id);
110
111   if (button != -1) {
112     get_item_by_id((int)id).change_input(get_button_name(button));
113   } else if (axis != 0) {
114     std::ostringstream name;
115
116     name << "Axis ";
117
118     if (axis < 0)
119       name << "-";
120     else
121       name << "+";
122
123     if (abs(axis) == 1)
124       name << "X";
125     else if (abs(axis) == 2)
126       name << "Y";
127     else if (abs(axis) == 2)
128       name << "X2";
129     else if (abs(axis) == 3)
130       name << "Y2";
131     else
132       name << abs(axis);
133
134     get_item_by_id((int)id).change_input(name.str());
135   } else if (hat_dir != -1) {
136     std::string name;
137
138     switch (hat_dir)
139     {
140       case SDL_HAT_UP:
141         name = "Hat Up";
142         break;
143
144       case SDL_HAT_DOWN:
145         name = "Hat Down";
146         break;
147
148       case SDL_HAT_LEFT:
149         name = "Hat Left";
150         break;
151
152       case SDL_HAT_RIGHT:
153         name = "Hat Right";
154         break;
155
156       default:
157         name = "Unknown hat_dir";
158         break;
159     }
160
161     get_item_by_id((int)id).change_input(name);
162   } else {
163     get_item_by_id((int)id).change_input("None");
164   }
165 }
166
167 void
168 JoystickMenu::update()
169 {
170   if(controller->joysticks.size() == 0)
171     return;
172
173   update_menu_item(Controller::UP);
174   update_menu_item(Controller::DOWN);
175   update_menu_item(Controller::LEFT);
176   update_menu_item(Controller::RIGHT);
177
178   update_menu_item(Controller::JUMP);
179   update_menu_item(Controller::ACTION);
180   update_menu_item(Controller::PAUSE_MENU);
181   update_menu_item(Controller::PEEK_LEFT);
182   update_menu_item(Controller::PEEK_RIGHT);
183   update_menu_item(Controller::PEEK_UP);
184   update_menu_item(Controller::PEEK_DOWN);
185
186   get_item_by_id(Controller::CONTROLCOUNT).toggled = controller->jump_with_up_joy;
187 }
188
189 /* EOF */