Antkeeper  0.0.1
mapper.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 
20 #include <engine/input/mapper.hpp>
21 #include <cmath>
22 
23 namespace input {
24 
26 {
27  subscriptions.emplace_back(dispatcher.subscribe<gamepad_axis_moved_event>(std::bind_front(&mapper::handle_gamepad_axis_moved, this)));
28  subscriptions.emplace_back(dispatcher.subscribe<gamepad_button_pressed_event>(std::bind_front(&mapper::handle_gamepad_button_pressed, this)));
29  subscriptions.emplace_back(dispatcher.subscribe<key_pressed_event>(std::bind_front(&mapper::handle_key_pressed, this)));
30  subscriptions.emplace_back(dispatcher.subscribe<mouse_button_pressed_event>(std::bind_front(&mapper::handle_mouse_button_pressed, this)));
31  subscriptions.emplace_back(dispatcher.subscribe<mouse_moved_event>(std::bind_front(&mapper::handle_mouse_moved, this)));
32  subscriptions.emplace_back(dispatcher.subscribe<mouse_scrolled_event>(std::bind_front(&mapper::handle_mouse_scrolled, this)));
33 }
34 
36 {
37  subscriptions.clear();
38 }
39 
40 void mapper::handle_gamepad_axis_moved(const gamepad_axis_moved_event& event)
41 {
42  if (std::abs(event.position) > 0.5f)
43  {
44  gamepad_axis_mapped_publisher.publish({gamepad_axis_mapping(event.gamepad, event.axis, std::signbit(event.position))});
45  }
46 }
47 
48 void mapper::handle_gamepad_button_pressed(const gamepad_button_pressed_event& event)
49 {
50  gamepad_button_mapped_publisher.publish({gamepad_button_mapping(event.gamepad, event.button)});
51 }
52 
53 void mapper::handle_key_pressed(const key_pressed_event& event)
54 {
55  if (!event.repeat)
56  {
57  key_mapped_publisher.publish({key_mapping(event.keyboard, event.scancode)});
58  }
59 }
60 
61 void mapper::handle_mouse_button_pressed(const mouse_button_pressed_event& event)
62 {
63  mouse_button_mapped_publisher.publish({mouse_button_mapping(event.mouse, event.button)});
64 }
65 
66 void mapper::handle_mouse_moved(const mouse_moved_event& event)
67 {
68  if (event.difference.x())
69  {
70  mouse_motion_mapped_publisher.publish({mouse_motion_mapping(event.mouse, mouse_motion_axis::x, std::signbit(static_cast<float>(event.difference.x())))});
71  }
72 
73  if (event.difference.y())
74  {
75  mouse_motion_mapped_publisher.publish({mouse_motion_mapping(event.mouse, mouse_motion_axis::y, std::signbit(static_cast<float>(event.difference.y())))});
76  }
77 }
78 
79 void mapper::handle_mouse_scrolled(const mouse_scrolled_event& event)
80 {
81  if (event.velocity.x())
82  {
83  mouse_scroll_mapped_publisher.publish({mouse_scroll_mapping(event.mouse, mouse_scroll_axis::x, std::signbit(event.velocity.x()))});
84  }
85 
86  if (event.velocity.y())
87  {
88  mouse_scroll_mapped_publisher.publish({mouse_scroll_mapping(event.mouse, mouse_scroll_axis::y, std::signbit(event.velocity.y()))});
89  }
90 }
91 
92 } // namespace input
Forwards messages from publishers to subscribers.
Definition: dispatcher.hpp:37
std::shared_ptr< subscription > subscribe(subscriber< T > &&subscriber)
Subscribes a function object to messages dispatched by this dispatcher.
Definition: dispatcher.hpp:50
Maps a direction along a gamepad axis to a control input value.
Definition: mapping.hpp:61
void disconnect()
Disconnects all input event signals from the mapper.
Definition: mapper.cpp:35
void connect(::event::dispatcher &dispatcher)
Connects the input event signals of an event dispatcher to the mapper.
Definition: mapper.cpp:25
Publish-subscribe messaging.
Definition: channel.hpp:32
Input devices, events, and mapping.
constexpr vector< T, N > abs(const vector< T, N > &x)
Returns the absolute values of each element.
Definition: vector.hpp:985
Event generated when a gamepad axis has been moved.
Event generated when a gamepad button has been pressed.
Event generated when a keyboard key has been pressed.
Event generated when a mouse button has been pressed.
Event generated when a mouse has been moved.
Event generated when a mouse has been scrolled.