Antkeeper  0.0.1
input-manager.cpp
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 
22 
23 namespace app {
24 
25 std::unique_ptr<input_manager> input_manager::instance()
26 {
27  return std::make_unique<sdl_input_manager>();
28 }
29 
31 {
32  switch (device.get_device_type())
33  {
35  register_gamepad(static_cast<input::gamepad&>(device));
36  break;
37 
39  register_keyboard(static_cast<input::keyboard&>(device));
40  break;
41 
43  register_mouse(static_cast<input::mouse&>(device));
44  break;
45 
46  default:
47  //std::unreachable();
48  break;
49  }
50 }
51 
53 {
54  // Connect gamepad event signals to the event dispatcher
55  m_subscriptions.emplace(&device, device.get_connected_channel().subscribe(m_event_dispatcher));
56  m_subscriptions.emplace(&device, device.get_disconnected_channel().subscribe(m_event_dispatcher));
57  m_subscriptions.emplace(&device, device.get_axis_moved_channel().subscribe(m_event_dispatcher));
58  m_subscriptions.emplace(&device, device.get_button_pressed_channel().subscribe(m_event_dispatcher));
59  m_subscriptions.emplace(&device, device.get_button_released_channel().subscribe(m_event_dispatcher));
60 
61  // Add gamepad to list of gamepads
62  m_gamepads.emplace(&device);
63 }
64 
66 {
67  // Connect keyboard event signals to the event dispatcher
68  m_subscriptions.emplace(&device, device.get_connected_channel().subscribe(m_event_dispatcher));
69  m_subscriptions.emplace(&device, device.get_disconnected_channel().subscribe(m_event_dispatcher));
70  m_subscriptions.emplace(&device, device.get_key_pressed_channel().subscribe(m_event_dispatcher));
71  m_subscriptions.emplace(&device, device.get_key_released_channel().subscribe(m_event_dispatcher));
72 
73  // Add keyboard to list of keyboards
74  m_keyboards.emplace(&device);
75 }
76 
78 {
79  // Connect mouse event signals to the event dispatcher
80  m_subscriptions.emplace(&device, device.get_connected_channel().subscribe(m_event_dispatcher));
81  m_subscriptions.emplace(&device, device.get_disconnected_channel().subscribe(m_event_dispatcher));
82  m_subscriptions.emplace(&device, device.get_button_pressed_channel().subscribe(m_event_dispatcher));
83  m_subscriptions.emplace(&device, device.get_button_released_channel().subscribe(m_event_dispatcher));
84  m_subscriptions.emplace(&device, device.get_moved_channel().subscribe(m_event_dispatcher));
85  m_subscriptions.emplace(&device, device.get_scrolled_channel().subscribe(m_event_dispatcher));
86 
87  // Add mouse to list of mice
88  m_mice.emplace(&device);
89 }
90 
92 {
93  m_subscriptions.erase(&device);
94 
95  switch (device.get_device_type())
96  {
98  unregister_gamepad(static_cast<input::gamepad&>(device));
99  break;
100 
102  unregister_keyboard(static_cast<input::keyboard&>(device));
103  break;
104 
106  unregister_mouse(static_cast<input::mouse&>(device));
107  break;
108 
109  default:
110  //std::unreachable();
111  break;
112  }
113 }
114 
116 {
117  m_gamepads.erase(&gamepad);
118 }
119 
121 {
122  m_keyboards.erase(&keyboard);
123 }
124 
126 {
127  m_mice.erase(&mouse);
128 }
129 
130 } // namespace app
void unregister_device(input::device &device)
Unregisters an input device.
void register_device(input::device &device)
Registers an input device.
void unregister_mouse(input::mouse &device)
Unregisters an input device.
static std::unique_ptr< input_manager > instance()
Allocates and returns an input manager.
void unregister_keyboard(input::keyboard &device)
Unregisters an input device.
void register_mouse(input::mouse &device)
Registers an input device.
void register_keyboard(input::keyboard &device)
Registers an input device.
::event::dispatcher m_event_dispatcher
void unregister_gamepad(input::gamepad &device)
Unregisters an input device.
void register_gamepad(input::gamepad &device)
Registers an input device.
Abstract base class for virtual devices that generate input events.
Definition: device.hpp:36
virtual constexpr device_type get_device_type() const noexcept=0
Returns the input device type.
::event::channel< device_connected_event > & get_connected_channel() noexcept
Returns the channel through which device connected events are published.
Definition: device.hpp:70
::event::channel< device_disconnected_event > & get_disconnected_channel() noexcept
Returns the channel through which device disconnected events are published.
Definition: device.hpp:76
A virtual gamepad which generates gamepad-related input events.
Definition: gamepad.hpp:49
::event::channel< gamepad_button_pressed_event > & get_button_pressed_channel() noexcept
Returns the channel through which gamepad button pressed events are published.
Definition: gamepad.hpp:124
::event::channel< gamepad_button_released_event > & get_button_released_channel() noexcept
Returns the channel through which gamepad button released events are published.
Definition: gamepad.hpp:130
::event::channel< gamepad_axis_moved_event > & get_axis_moved_channel() noexcept
Returns the channel through which gamepad axis moved events are published.
Definition: gamepad.hpp:136
A virtual keyboard which generates keyboard-related input events.
Definition: keyboard.hpp:35
::event::channel< key_released_event > & get_key_released_channel() noexcept
Returns the channel through which key released events are published.
Definition: keyboard.hpp:61
::event::channel< key_pressed_event > & get_key_pressed_channel() noexcept
Returns the channel through which key pressed events are published.
Definition: keyboard.hpp:55
A virtual mouse which generates mouse-related input events.
Definition: mouse.hpp:36
::event::channel< mouse_scrolled_event > & get_scrolled_channel() noexcept
Returns the channel through which mouse scrolled events are published.
Definition: mouse.hpp:92
::event::channel< mouse_moved_event > & get_moved_channel() noexcept
Returns the channel through which mouse moved events are published.
Definition: mouse.hpp:86
::event::channel< mouse_button_pressed_event > & get_button_pressed_channel() noexcept
Returns the channel through which mouse button pressed events are published.
Definition: mouse.hpp:74
::event::channel< mouse_button_released_event > & get_button_released_channel() noexcept
Returns the channel through which mouse button released events are published.
Definition: mouse.hpp:80
@ keyboard
Keyboard input device.
@ mouse
Mouse input device.
@ gamepad
Gamepad input device.