Renamed JoystickKeyboardController to InputManager
[supertux.git] / src / control / input_manager.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 "control/input_manager.hpp"
19
20 #include <iostream>
21
22 #include "control/keyboard_manager.hpp"
23 #include "control/joystick_manager.hpp"
24 #include "control/game_controller_manager.hpp"
25 #include "gui/menu_manager.hpp"
26 #include "lisp/list_iterator.hpp"
27 #include "supertux/gameconfig.hpp"
28 #include "util/gettext.hpp"
29 #include "util/writer.hpp"
30
31 InputManager::InputManager() :
32   controller(new Controller),
33   m_use_game_controller(true),
34   keyboard_manager(new KeyboardManager(this)),
35   joystick_manager(new JoystickManager(this)),
36   game_controller_manager(new GameControllerManager(this))
37 {
38 }
39
40 InputManager::~InputManager()
41 {
42 }
43
44 Controller*
45 InputManager::get_controller()
46 {
47   return controller.get();
48 }
49
50 void
51 InputManager::read(const Reader& lisp)
52 {
53   const lisp::Lisp* keymap_lisp = lisp.get_lisp("keymap");
54   if (keymap_lisp) 
55   {
56     keyboard_manager->read(keymap_lisp);
57   }
58
59   const lisp::Lisp* joystick_lisp = lisp.get_lisp(_("joystick"));
60   if (joystick_lisp) 
61   {
62     joystick_manager->read(joystick_lisp);
63   }
64 }
65
66 void
67 InputManager::write(Writer& writer)
68 {
69   writer.start_list("keymap");
70   keyboard_manager->write(writer);
71   writer.end_list("keymap");
72
73   writer.start_list("joystick");
74   joystick_manager->write(writer);
75   writer.end_list("joystick");
76 }
77
78 void
79 InputManager::update()
80 {
81   controller->update();
82 }
83
84 void
85 InputManager::reset()
86 {
87   controller->reset();
88 }
89
90 void
91 InputManager::process_event(const SDL_Event& event)
92 {
93   switch(event.type) {
94     case SDL_TEXTINPUT:
95       keyboard_manager->process_text_input_event(event.text);
96       break;
97
98     case SDL_KEYUP:
99     case SDL_KEYDOWN:
100       keyboard_manager->process_key_event(event.key);
101       break;
102
103     case SDL_JOYAXISMOTION:
104       if (!m_use_game_controller) joystick_manager->process_axis_event(event.jaxis);
105       break;
106
107     case SDL_JOYHATMOTION:
108       if (!m_use_game_controller) joystick_manager->process_hat_event(event.jhat);
109       break;
110
111     case SDL_JOYBUTTONDOWN:
112     case SDL_JOYBUTTONUP:
113       if (!m_use_game_controller) joystick_manager->process_button_event(event.jbutton);
114       break;
115
116     case SDL_JOYDEVICEADDED:
117       if (!m_use_game_controller) joystick_manager->on_joystick_added(event.jdevice.which);
118       break;
119
120     case SDL_JOYDEVICEREMOVED:
121       if (!m_use_game_controller) joystick_manager->on_joystick_removed(event.jdevice.which);
122       break;
123
124     case SDL_CONTROLLERAXISMOTION:
125       if (m_use_game_controller) game_controller_manager->process_axis_event(event.caxis);
126       break;
127
128     case SDL_CONTROLLERBUTTONDOWN:
129       if (m_use_game_controller) game_controller_manager->process_button_event(event.cbutton);
130       break;
131
132     case SDL_CONTROLLERBUTTONUP:
133       if (m_use_game_controller) game_controller_manager->process_button_event(event.cbutton);
134       break;
135
136     case SDL_CONTROLLERDEVICEADDED:
137       std::cout << "SDL_CONTROLLERDEVICEADDED" << std::endl;
138       if (m_use_game_controller) game_controller_manager->on_controller_added(event.cdevice.which);
139       break;
140
141     case SDL_CONTROLLERDEVICEREMOVED:
142       std::cout << "SDL_CONTROLLERDEVICEREMOVED" << std::endl;
143       if (m_use_game_controller) game_controller_manager->on_controller_removed(event.cdevice.which);
144       break;
145
146     case SDL_CONTROLLERDEVICEREMAPPED:
147       std::cout << "SDL_CONTROLLERDEVICEREMAPPED" << std::endl;
148       break;
149
150     default:
151       break;
152   }
153 }
154
155 /* EOF */