Renamed JoystickKeyboardController to InputManager
[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 "control/joystick_manager.hpp"
23 #include "util/gettext.hpp"
24
25 JoystickMenu::JoystickMenu(InputManager* _controller) :
26   controller(_controller),
27   joysticks_available(false)
28 {
29   recreateMenu();
30 }
31
32 JoystickMenu::~JoystickMenu()
33 {}
34
35 void
36 JoystickMenu::recreateMenu()
37 {
38   clear();
39   add_label(_("Setup Joystick"));
40   add_hl();
41   if(controller->joystick_manager->joysticks.size() > 0) {
42     joysticks_available = true;
43
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->joystick_manager->jump_with_up_joy);
57   } else {
58     add_inactive(-1, _("No Joysticks found"));
59     joysticks_available = false;
60   }
61
62   add_hl();
63   add_back(_("Back"));
64   update();
65 }
66
67 std::string
68 JoystickMenu::get_button_name(int button)
69 {
70   if(button < 0)
71     return _("None");
72
73   std::ostringstream name;
74   name << "Button " << button;
75   return name.str();
76 }
77
78 void
79 JoystickMenu::menu_action(MenuItem* item)
80 {
81   if (item->id >= 0 && item->id < Controller::CONTROLCOUNT) {
82     item->change_input(_("Press Button"));
83     controller->joystick_manager->wait_for_joystick = item->id;
84   } else if (item->id == Controller::CONTROLCOUNT) {
85     controller->joystick_manager->jump_with_up_joy = item->toggled;
86   }
87 }
88
89 void
90 JoystickMenu::update_menu_item(Controller::Control id)
91 {
92   int button  = controller->joystick_manager->reversemap_joybutton(id);
93   int axis    = controller->joystick_manager->reversemap_joyaxis(id);
94   int hat_dir = controller->joystick_manager->reversemap_joyhat(id);
95
96   if (button != -1) {
97     get_item_by_id((int)id).change_input(get_button_name(button));
98   } else if (axis != 0) {
99     std::ostringstream name;
100
101     name << "Axis ";
102
103     if (axis < 0)
104       name << "-";
105     else
106       name << "+";
107
108     if (abs(axis) == 1)
109       name << "X";
110     else if (abs(axis) == 2)
111       name << "Y";
112     else if (abs(axis) == 2)
113       name << "X2";
114     else if (abs(axis) == 3)
115       name << "Y2";
116     else
117       name << abs(axis);
118
119     get_item_by_id((int)id).change_input(name.str());
120   } else if (hat_dir != -1) {
121     std::string name;
122
123     switch (hat_dir)
124     {
125       case SDL_HAT_UP:
126         name = "Hat Up";
127         break;
128
129       case SDL_HAT_DOWN:
130         name = "Hat Down";
131         break;
132
133       case SDL_HAT_LEFT:
134         name = "Hat Left";
135         break;
136
137       case SDL_HAT_RIGHT:
138         name = "Hat Right";
139         break;
140
141       default:
142         name = "Unknown hat_dir";
143         break;
144     }
145
146     get_item_by_id((int)id).change_input(name);
147   } else {
148     get_item_by_id((int)id).change_input("None");
149   }
150 }
151
152 void
153 JoystickMenu::update()
154 {
155   log_info << controller->joystick_manager->joysticks.size() << std::endl;
156
157   if(true) //controller->joysticks.size() == 0)
158   {
159     // do nothing
160   }
161   else
162   {
163     if (!joysticks_available)
164     {
165       recreateMenu();
166     }
167
168     update_menu_item(Controller::UP);
169     update_menu_item(Controller::DOWN);
170     update_menu_item(Controller::LEFT);
171     update_menu_item(Controller::RIGHT);
172
173     update_menu_item(Controller::JUMP);
174     update_menu_item(Controller::ACTION);
175     update_menu_item(Controller::PAUSE_MENU);
176     update_menu_item(Controller::PEEK_LEFT);
177     update_menu_item(Controller::PEEK_RIGHT);
178     update_menu_item(Controller::PEEK_UP);
179     update_menu_item(Controller::PEEK_DOWN);
180
181     //get_item_by_id(Controller::CONTROLCOUNT).toggled = controller->jump_with_up_joy;
182   }
183 }
184
185 /* EOF */