| 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/input_manager.hpp" |
| 19 | |
| 20 | #include "control/game_controller_manager.hpp" |
| 21 | #include "control/joystick_config.hpp" |
| 22 | #include "control/joystick_manager.hpp" |
| 23 | #include "control/keyboard_manager.hpp" |
| 24 | #include "util/log.hpp" |
| 25 | |
| 26 | InputManager::InputManager(KeyboardConfig& keyboard_config, |
| 27 | JoystickConfig& joystick_config) : |
| 28 | controller(new Controller), |
| 29 | m_use_game_controller(joystick_config.m_use_game_controller), |
| 30 | keyboard_manager(new KeyboardManager(this, keyboard_config)), |
| 31 | joystick_manager(new JoystickManager(this, joystick_config)), |
| 32 | game_controller_manager(new GameControllerManager(this)) |
| 33 | { |
| 34 | } |
| 35 | |
| 36 | InputManager::~InputManager() |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | const Controller& |
| 41 | InputManager::get_controller() const |
| 42 | { |
| 43 | return *controller; |
| 44 | } |
| 45 | |
| 46 | Controller& |
| 47 | InputManager::get_controller() |
| 48 | { |
| 49 | return *controller; |
| 50 | } |
| 51 | |
| 52 | void |
| 53 | InputManager::use_game_controller(bool v) |
| 54 | { |
| 55 | m_use_game_controller = v; |
| 56 | } |
| 57 | |
| 58 | void |
| 59 | InputManager::update() |
| 60 | { |
| 61 | controller->update(); |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | InputManager::reset() |
| 66 | { |
| 67 | controller->reset(); |
| 68 | } |
| 69 | |
| 70 | void |
| 71 | InputManager::process_event(const SDL_Event& event) |
| 72 | { |
| 73 | switch (event.type) { |
| 74 | case SDL_TEXTINPUT: |
| 75 | keyboard_manager->process_text_input_event(event.text); |
| 76 | break; |
| 77 | |
| 78 | case SDL_KEYUP: |
| 79 | case SDL_KEYDOWN: |
| 80 | keyboard_manager->process_key_event(event.key); |
| 81 | break; |
| 82 | |
| 83 | case SDL_JOYAXISMOTION: |
| 84 | if (!m_use_game_controller) joystick_manager->process_axis_event(event.jaxis); |
| 85 | break; |
| 86 | |
| 87 | case SDL_JOYHATMOTION: |
| 88 | if (!m_use_game_controller) joystick_manager->process_hat_event(event.jhat); |
| 89 | break; |
| 90 | |
| 91 | case SDL_JOYBUTTONDOWN: |
| 92 | case SDL_JOYBUTTONUP: |
| 93 | if (!m_use_game_controller) joystick_manager->process_button_event(event.jbutton); |
| 94 | break; |
| 95 | |
| 96 | case SDL_JOYDEVICEADDED: |
| 97 | joystick_manager->on_joystick_added(event.jdevice.which); |
| 98 | break; |
| 99 | |
| 100 | case SDL_JOYDEVICEREMOVED: |
| 101 | joystick_manager->on_joystick_removed(event.jdevice.which); |
| 102 | break; |
| 103 | |
| 104 | case SDL_CONTROLLERAXISMOTION: |
| 105 | if (m_use_game_controller) game_controller_manager->process_axis_event(event.caxis); |
| 106 | break; |
| 107 | |
| 108 | case SDL_CONTROLLERBUTTONDOWN: |
| 109 | if (m_use_game_controller) game_controller_manager->process_button_event(event.cbutton); |
| 110 | break; |
| 111 | |
| 112 | case SDL_CONTROLLERBUTTONUP: |
| 113 | if (m_use_game_controller) game_controller_manager->process_button_event(event.cbutton); |
| 114 | break; |
| 115 | |
| 116 | case SDL_CONTROLLERDEVICEADDED: |
| 117 | log_debug << "SDL_CONTROLLERDEVICEADDED" << std::endl; |
| 118 | game_controller_manager->on_controller_added(event.cdevice.which); |
| 119 | break; |
| 120 | |
| 121 | case SDL_CONTROLLERDEVICEREMOVED: |
| 122 | log_debug << "SDL_CONTROLLERDEVICEREMOVED" << std::endl; |
| 123 | game_controller_manager->on_controller_removed(event.cdevice.which); |
| 124 | break; |
| 125 | |
| 126 | case SDL_CONTROLLERDEVICEREMAPPED: |
| 127 | log_debug << "SDL_CONTROLLERDEVICEREMAPPED" << std::endl; |
| 128 | break; |
| 129 | |
| 130 | default: |
| 131 | break; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /* EOF */ |
| 136 | |