supertux
joystick_manager.hpp
1 // SuperTux
2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 // 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 #ifndef HEADER_SUPERTUX_CONTROL_JOYSTICK_MANAGER_HPP
19 #define HEADER_SUPERTUX_CONTROL_JOYSTICK_MANAGER_HPP
20 
21 #include <SDL.h>
22 #include <vector>
23 #include <unordered_map>
24 
25 #include "control/controller.hpp"
26 
27 class InputManager;
28 class JoystickConfig;
29 
30 
36 class JoystickManager final
37 {
38  friend class KeyboardManager;
39 
40 public:
41  JoystickManager(InputManager* parent, JoystickConfig& joystick_config);
42  ~JoystickManager();
43 
44  void process_hat_event(const SDL_JoyHatEvent& jhat);
45  void process_axis_event(const SDL_JoyAxisEvent& jaxis);
46  void process_button_event(const SDL_JoyButtonEvent& jbutton);
47 
48  void bind_next_event_to(Control id);
49 
50  void set_joy_controls(SDL_JoystickID joystick, Control id, bool value);
51 
52  void on_joystick_added(int joystick_index);
53  void on_joystick_removed(int instance_id);
54 
55  int get_num_joysticks() const { return static_cast<int>(joysticks.size()); }
56 
57  void on_player_removed(int player_id);
58  bool has_corresponding_joystick(int player_id) const;
59 
61  int rumble(SDL_Joystick* joystick) const;
62 
63  void bind_joystick(SDL_Joystick* joystick, int player_id);
64 
65  std::unordered_map<SDL_Joystick*, int>& get_joystick_mapping() { return joysticks; }
66 
67 private:
68  InputManager* parent;
69  JoystickConfig& m_joystick_config;
70 
72  int min_joybuttons;
73 
75  int max_joybuttons;
76 
77  int max_joyaxis;
78  int max_joyhats;
79 
80  Uint8 hat_state;
81 
82  int wait_for_joystick;
83 
84  std::unordered_map<SDL_Joystick*, int> joysticks;
85 
86 private:
87  JoystickManager(const JoystickManager&) = delete;
88  JoystickManager& operator=(const JoystickManager&) = delete;
89 };
90 
91 #endif
92 
93 /* EOF */
Manages Joysticks.
Definition: joystick_manager.hpp:36
Definition: input_manager.hpp:39
Definition: joystick_config.hpp:28
Definition: keyboard_manager.hpp:30
int rumble(SDL_Joystick *joystick) const
Definition: joystick_manager.cpp:330