2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>,
3 // 2007-2014 Ingo Ruhnke <grumbel@gmail.com>
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.
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.
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/>.
18 #include "control/keyboard_manager.hpp"
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"
31 KeyboardManager::KeyboardManager(InputManager* parent,
32 KeyboardConfig& keyboard_config) :
34 m_keyboard_config(keyboard_config),
39 KeyboardManager::~KeyboardManager()
44 KeyboardManager::process_key_event(const SDL_KeyboardEvent& event)
46 KeyboardConfig::KeyMap::iterator key_mapping = m_keyboard_config.keymap.find(event.keysym.sym);
48 // if console key was pressed: toggle console
49 if (key_mapping != m_keyboard_config.keymap.end() &&
50 key_mapping->second == Controller::CONSOLE)
52 if (event.type == SDL_KEYDOWN)
54 Console::current()->toggle();
57 else if (Console::current()->hasFocus())
59 // if console is open: send key there
60 process_console_key_event(event);
62 else if (MenuManager::instance().is_active())
64 // if menu mode: send key there
65 process_menu_key_event(event);
67 else if (key_mapping == m_keyboard_config.keymap.end())
69 // default action: update controls
70 //log_debug << "Key " << event.key.SDL_Keycode.sym << " is unbound" << std::endl;
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)
79 m_parent->get_controller()->set_control(Controller::JUMP, value);
85 KeyboardManager::process_text_input_event(const SDL_TextInputEvent& event)
87 if (Console::current()->hasFocus()) {
88 for(int i = 0; event.text[i] != '\0'; ++i)
90 Console::current()->input(event.text[i]);
96 KeyboardManager::process_console_key_event(const SDL_KeyboardEvent& event)
98 if (event.type != SDL_KEYDOWN) return;
100 switch (event.keysym.sym) {
102 Console::current()->enter();
105 Console::current()->backspace();
108 Console::current()->autocomplete();
111 Console::current()->scroll(-1);
114 Console::current()->scroll(+1);
117 Console::current()->move_cursor(-65535);
120 Console::current()->move_cursor(+65535);
123 Console::current()->show_history(-1);
126 Console::current()->show_history(+1);
129 Console::current()->move_cursor(-1);
132 Console::current()->move_cursor(+1);
140 KeyboardManager::process_menu_key_event(const SDL_KeyboardEvent& event)
142 // wait for key mode?
143 if (wait_for_key >= 0)
145 if (event.type == SDL_KEYUP)
148 if (event.keysym.sym != SDLK_ESCAPE &&
149 event.keysym.sym != SDLK_PAUSE)
151 m_keyboard_config.bind_key(event.keysym.sym, static_cast<Controller::Control>(wait_for_key));
154 MenuManager::instance().refresh();
159 if (m_parent->joystick_manager->wait_for_joystick >= 0)
161 if (event.keysym.sym == SDLK_ESCAPE)
164 MenuManager::instance().refresh();
165 m_parent->joystick_manager->wait_for_joystick = -1;
170 Controller::Control control;
171 /* we use default keys when the menu is open (to avoid problems when
172 * redefining keys to invalid settings
174 switch(event.keysym.sym) {
176 control = Controller::UP;
179 control = Controller::DOWN;
182 control = Controller::LEFT;
185 control = Controller::RIGHT;
190 control = Controller::MENU_SELECT;
193 control = Controller::ESCAPE;
196 control = Controller::START;
203 m_parent->get_controller()->set_control(control, (event.type == SDL_KEYDOWN));
207 KeyboardManager::bind_next_event_to(Controller::Control id)