Removed made public variables private in JoystickManager and KeyboardManager
[supertux.git] / src / control / keyboard_manager.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 "control/keyboard_manager.hpp"
19
20 #include "control/controller.hpp"
21 #include "control/joystick_manager.hpp"
22 #include "control/keyboard_config.hpp"
23 #include "gui/menu_manager.hpp"
24 #include "lisp/list_iterator.hpp"
25 #include "supertux/console.hpp"
26 #include "supertux/menu/joystick_menu.hpp"
27 #include "supertux/menu/keyboard_menu.hpp"
28 #include "supertux/menu/menu_storage.hpp"
29 #include "util/writer.hpp"
30
31 KeyboardManager::KeyboardManager(InputManager* parent,
32                                  KeyboardConfig& keyboard_config) :
33   m_parent(parent),
34   m_keyboard_config(keyboard_config),
35   wait_for_key(-1)
36 {
37 }
38
39 KeyboardManager::~KeyboardManager()
40 {
41 }
42
43 void
44 KeyboardManager::process_key_event(const SDL_KeyboardEvent& event)
45 {
46   KeyboardConfig::KeyMap::iterator key_mapping = m_keyboard_config.keymap.find(event.keysym.sym);
47
48   // if console key was pressed: toggle console
49   if (key_mapping != m_keyboard_config.keymap.end() &&
50       key_mapping->second == Controller::CONSOLE)
51   {
52     if (event.type == SDL_KEYDOWN)
53     {
54       Console::current()->toggle();
55     }
56   }
57   else if (Console::current()->hasFocus())
58   {
59     // if console is open: send key there
60     process_console_key_event(event);
61   }
62   else if (MenuManager::instance().is_active())
63   {
64     // if menu mode: send key there
65     process_menu_key_event(event);
66   }
67   else if (key_mapping == m_keyboard_config.keymap.end())
68   {
69     // default action: update controls
70     //log_debug << "Key " << event.key.SDL_Keycode.sym << " is unbound" << std::endl;
71   }
72   else
73   {
74     auto control = key_mapping->second;
75     bool value = (event.type == SDL_KEYDOWN);
76     m_parent->get_controller()->set_control(control, value);
77     if (m_keyboard_config.jump_with_up_kbd && control == Controller::UP)
78     {
79       m_parent->get_controller()->set_control(Controller::JUMP, value);
80     }
81   }
82 }
83
84 void
85 KeyboardManager::process_text_input_event(const SDL_TextInputEvent& event)
86 {
87   if (Console::current()->hasFocus()) {
88     for(int i = 0; event.text[i] != '\0'; ++i)
89     {
90       Console::current()->input(event.text[i]);
91     }
92   }
93 }
94
95 void
96 KeyboardManager::process_console_key_event(const SDL_KeyboardEvent& event)
97 {
98   if (event.type != SDL_KEYDOWN) return;
99
100   switch (event.keysym.sym) {
101     case SDLK_RETURN:
102       Console::current()->enter();
103       break;
104     case SDLK_BACKSPACE:
105       Console::current()->backspace();
106       break;
107     case SDLK_TAB:
108       Console::current()->autocomplete();
109       break;
110     case SDLK_PAGEUP:
111       Console::current()->scroll(-1);
112       break;
113     case SDLK_PAGEDOWN:
114       Console::current()->scroll(+1);
115       break;
116     case SDLK_HOME:
117       Console::current()->move_cursor(-65535);
118       break;
119     case SDLK_END:
120       Console::current()->move_cursor(+65535);
121       break;
122     case SDLK_UP:
123       Console::current()->show_history(-1);
124       break;
125     case SDLK_DOWN:
126       Console::current()->show_history(+1);
127       break;
128     case SDLK_LEFT:
129       Console::current()->move_cursor(-1);
130       break;
131     case SDLK_RIGHT:
132       Console::current()->move_cursor(+1);
133       break;
134     default:
135       break;
136   }
137 }
138
139 void
140 KeyboardManager::process_menu_key_event(const SDL_KeyboardEvent& event)
141 {
142   // wait for key mode?
143   if (wait_for_key >= 0)
144   {
145     if (event.type == SDL_KEYUP)
146       return;
147
148     if (event.keysym.sym != SDLK_ESCAPE &&
149         event.keysym.sym != SDLK_PAUSE)
150     {
151       m_keyboard_config.bind_key(event.keysym.sym, static_cast<Controller::Control>(wait_for_key));
152     }
153     m_parent->reset();
154     MenuManager::instance().refresh();
155     wait_for_key = -1;
156     return;
157   }
158
159   if (m_parent->joystick_manager->wait_for_joystick >= 0)
160   {
161     if (event.keysym.sym == SDLK_ESCAPE)
162     {
163       m_parent->reset();
164       MenuManager::instance().refresh();
165       m_parent->joystick_manager->wait_for_joystick = -1;
166     }
167     return;
168   }
169
170   Controller::Control control;
171   /* we use default keys when the menu is open (to avoid problems when
172    * redefining keys to invalid settings
173    */
174   switch(event.keysym.sym) {
175     case SDLK_UP:
176       control = Controller::UP;
177       break;
178     case SDLK_DOWN:
179       control = Controller::DOWN;
180       break;
181     case SDLK_LEFT:
182       control = Controller::LEFT;
183       break;
184     case SDLK_RIGHT:
185       control = Controller::RIGHT;
186       break;
187     case SDLK_SPACE:
188     case SDLK_RETURN:
189     case SDLK_KP_ENTER:
190       control = Controller::MENU_SELECT;
191       break;
192     case SDLK_ESCAPE:
193       control = Controller::ESCAPE;
194       break;
195     case SDLK_PAUSE:
196       control = Controller::START;
197       break;
198     default:
199       return;
200       break;
201   }
202
203   m_parent->get_controller()->set_control(control, (event.type == SDL_KEYDOWN));
204 }
205
206 void
207 KeyboardManager::bind_next_event_to(Controller::Control id)
208 {
209   wait_for_key = id;
210 }
211
212 /* EOF */