Antkeeper  0.0.1
input-manager.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Christopher J. Howard
3  *
4  * This file is part of Antkeeper source code.
5  *
6  * Antkeeper source code is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Antkeeper source code is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef ANTKEEPER_APP_INPUT_MANAGER_HPP
21 #define ANTKEEPER_APP_INPUT_MANAGER_HPP
22 
23 #include <engine/input/device.hpp>
24 #include <engine/input/gamepad.hpp>
26 #include <engine/input/mouse.hpp>
28 #include <map>
29 #include <memory>
30 #include <unordered_set>
31 
32 namespace app {
33 
38 {
39 public:
43  static std::unique_ptr<input_manager> instance();
44 
46  virtual ~input_manager() = default;
47 
51  virtual void update() = 0;
52 
58  virtual void set_cursor_visible(bool visible) = 0;
59 
65  virtual void set_relative_mouse_mode(bool enabled) = 0;
66 
71  [[nodiscard]] inline const ::event::dispatcher& get_event_dispatcher() const noexcept
72  {
73  return m_event_dispatcher;
74  }
75  [[nodiscard]] inline ::event::dispatcher& get_event_dispatcher() noexcept
76  {
77  return m_event_dispatcher;
78  }
80 
82  [[nodiscard]] inline const std::unordered_set<input::gamepad*>& get_gamepads() noexcept
83  {
84  return m_gamepads;
85  }
86 
88  [[nodiscard]] inline const std::unordered_set<input::keyboard*>& get_keyboards() noexcept
89  {
90  return m_keyboards;
91  }
92 
94  [[nodiscard]] inline const std::unordered_set<input::mouse*>& get_mice() noexcept
95  {
96  return m_mice;
97  }
98 
99 protected:
106  void register_device(input::device& device);
107  void register_gamepad(input::gamepad& device);
108  void register_keyboard(input::keyboard& device);
109  void register_mouse(input::mouse& device);
111 
118  void unregister_device(input::device& device);
119  void unregister_gamepad(input::gamepad& device);
120  void unregister_keyboard(input::keyboard& device);
121  void unregister_mouse(input::mouse& device);
123 
125 
126 private:
127  std::multimap<input::device*, std::shared_ptr<::event::subscription>> m_subscriptions;
128  std::unordered_set<input::gamepad*> m_gamepads;
129  std::unordered_set<input::keyboard*> m_keyboards;
130  std::unordered_set<input::mouse*> m_mice;
131 };
132 
133 } // namespace app
134 
135 #endif // ANTKEEPER_APP_INPUT_MANAGER_HPP
Manages virtual input devices.
void unregister_device(input::device &device)
Unregisters an input device.
const std::unordered_set< input::keyboard * > & get_keyboards() noexcept
Returns the set of registered keyboards.
void register_device(input::device &device)
Registers an input device.
virtual void set_cursor_visible(bool visible)=0
Shows or hides the cursor.
virtual void update()=0
Processes input events.
const std::unordered_set< input::gamepad * > & get_gamepads() noexcept
Returns the set of registered gamepads.
void unregister_mouse(input::mouse &device)
Unregisters an input device.
static std::unique_ptr< input_manager > instance()
Allocates and returns an input manager.
virtual void set_relative_mouse_mode(bool enabled)=0
Enables or disables relative mouse mode.
void unregister_keyboard(input::keyboard &device)
Unregisters an input device.
const std::unordered_set< input::mouse * > & get_mice() noexcept
Returns the set of registered mice.
void register_mouse(input::mouse &device)
Registers an input device.
virtual ~input_manager()=default
Destructs an input manager.
void register_keyboard(input::keyboard &device)
Registers an input device.
::event::dispatcher & get_event_dispatcher() noexcept
Returns the event dispatcher associated with registered input devices.
::event::dispatcher m_event_dispatcher
void unregister_gamepad(input::gamepad &device)
Unregisters an input device.
const ::event::dispatcher & get_event_dispatcher() const noexcept
Returns the event dispatcher associated with registered input devices.
void register_gamepad(input::gamepad &device)
Registers an input device.
Forwards messages from publishers to subscribers.
Definition: dispatcher.hpp:37
Abstract base class for virtual devices that generate input events.
Definition: device.hpp:36
A virtual gamepad which generates gamepad-related input events.
Definition: gamepad.hpp:49
A virtual keyboard which generates keyboard-related input events.
Definition: keyboard.hpp:35
A virtual mouse which generates mouse-related input events.
Definition: mouse.hpp:36