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