supertux
input_manager.hpp
1 // SuperTux
2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef HEADER_SUPERTUX_CONTROL_INPUT_MANAGER_HPP
18 #define HEADER_SUPERTUX_CONTROL_INPUT_MANAGER_HPP
19 
20 #include "control/controller.hpp"
21 
22 #include <SDL.h>
23 #include <map>
24 #include <string>
25 #include <vector>
26 #include <memory>
27 #include <unordered_map>
28 
29 #include "util/currenton.hpp"
30 
32 class JoystickManager;
33 class JoystickMenu;
34 class KeyboardManager;
35 class KeyboardMenu;
36 class KeyboardConfig;
37 class JoystickConfig;
38 
39 class InputManager final : public Currenton<InputManager>
40 {
41 private:
42  friend class KeyboardMenu;
43  friend class JoystickMenu;
44 
45 public:
46  InputManager(KeyboardConfig& keyboard_config,
47  JoystickConfig& joystick_config);
48  ~InputManager() override;
49 
50  void process_event(const SDL_Event& event);
51 
52  void update();
53  void reset();
54 
55  void use_game_controller(bool v);
56  bool use_game_controller() const { return m_use_game_controller; }
57 
58  const Controller& get_controller(int player_id = 0) const;
59  Controller& get_controller(int player_id = 0);
60 
61  int get_num_users() const { return static_cast<int>(m_controllers.size()); }
62 
63  void push_user();
64  void pop_user();
65 
66  void on_player_removed(int player_id);
67 
68  bool has_corresponsing_controller(int player_id) const;
69 
70 private:
71  std::vector<std::unique_ptr<Controller>> m_controllers;
72 
73 public:
74  bool& m_use_game_controller;
75  std::unique_ptr<KeyboardManager> keyboard_manager;
76  std::unique_ptr<JoystickManager> joystick_manager;
77  std::unique_ptr<GameControllerManager> game_controller_manager;
78 
80  std::unordered_map<int, bool> m_uses_keyboard;
81 
82 private:
83  InputManager(const InputManager&) = delete;
84  InputManager& operator=(const InputManager&) = delete;
85 };
86 
87 #endif
88 
89 /* EOF */
Definition: controller.hpp:57
Manages Joysticks.
Definition: joystick_manager.hpp:36
std::unordered_map< int, bool > m_uses_keyboard
True if the given player is on the keyboard and plays without a controller.
Definition: input_manager.hpp:80
Definition: input_manager.hpp:39
Definition: joystick_config.hpp:28
Definition: keyboard_config.hpp:29
A &#39;Currenton&#39; allows access to the currently active instance of a class via the static current() func...
Definition: currenton.hpp:30
Definition: keyboard_manager.hpp:30
Manages GameControllers.
Definition: game_controller_manager.hpp:37
Definition: keyboard_menu.hpp:26
Definition: joystick_menu.hpp:24