Antkeeper  0.0.1
mapping.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/mapping.hpp>
25 
26 namespace input {
27 
30  axis{axis},
31  direction{direction}
32 {}
33 
36  button{button}
37 {}
38 
42  repeat{repeat},
43  modifiers{modifiers}
44 {}
45 
47  mouse{mouse},
48  button{button}
49 {}
50 
52  mouse{mouse},
53  axis{axis},
54  direction{direction}
55 {}
56 
58  mouse{mouse},
59  axis{axis},
60  direction{direction}
61 {}
62 
63 } // namespace input
64 
74 template <>
76 {
77  ctx.write8(reinterpret_cast<const std::byte*>(&mapping.axis), 1);
78  const std::uint8_t direction = mapping.direction;
79  ctx.write8(reinterpret_cast<const std::byte*>(&direction), 1);
80 }
81 
82 template <>
84 {
85  ctx.write8(reinterpret_cast<const std::byte*>(&mapping.button), 1);
86 }
87 
88 template <>
90 {
91  ctx.write16<std::endian::big>(reinterpret_cast<const std::byte*>(&mapping.scancode), 1);
92  ctx.write16<std::endian::big>(reinterpret_cast<const std::byte*>(&mapping.modifiers), 1);
93  const std::uint8_t repeat = mapping.repeat;
94  ctx.write8(reinterpret_cast<const std::byte*>(&repeat), 1);
95 }
96 
97 template <>
99 {
100  ctx.write8(reinterpret_cast<const std::byte*>(&mapping.button), 1);
101 }
102 
103 template <>
105 {
106  ctx.write8(reinterpret_cast<const std::byte*>(&mapping.axis), 1);
107  const std::uint8_t direction = mapping.direction;
108  ctx.write8(reinterpret_cast<const std::byte*>(&direction), 1);
109 }
110 
111 template <>
113 {
114  ctx.write8(reinterpret_cast<const std::byte*>(&mapping.axis), 1);
115  const std::uint8_t direction = mapping.direction;
116  ctx.write8(reinterpret_cast<const std::byte*>(&direction), 1);
117 }
119 
129 template <>
131 {
132  mapping.gamepad = nullptr;
133 
134  ctx.read8(reinterpret_cast<std::byte*>(&mapping.axis), 1);
135  std::uint8_t direction = 0;
136  ctx.read8(reinterpret_cast<std::byte*>(&direction), 1);
137  mapping.direction = direction;
138 }
139 
140 template <>
142 {
143  mapping.gamepad = nullptr;
144 
145  ctx.read8(reinterpret_cast<std::byte*>(&mapping.button), 1);
146 }
147 
148 template <>
150 {
151  mapping.keyboard = nullptr;
152 
153  ctx.read16<std::endian::big>(reinterpret_cast<std::byte*>(&mapping.scancode), 1);
154  ctx.read16<std::endian::big>(reinterpret_cast<std::byte*>(&mapping.modifiers), 1);
155  std::uint8_t repeat = 0;
156  ctx.read8(reinterpret_cast<std::byte*>(&repeat), 1);
157  mapping.repeat = repeat;
158 }
159 
160 template <>
162 {
163  mapping.mouse = nullptr;
164 
165  ctx.read8(reinterpret_cast<std::byte*>(&mapping.button), 1);
166 }
167 
168 template <>
170 {
171  mapping.mouse = nullptr;
172 
173  ctx.read8(reinterpret_cast<std::byte*>(&mapping.axis), 1);
174  std::uint8_t direction = 0;
175  ctx.read8(reinterpret_cast<std::byte*>(&direction), 1);
176  mapping.direction = direction;
177 }
178 
179 template <>
181 {
182  mapping.mouse = nullptr;
183 
184  ctx.read8(reinterpret_cast<std::byte*>(&mapping.axis), 1);
185  std::uint8_t direction = 0;
186  ctx.read8(reinterpret_cast<std::byte*>(&direction), 1);
187  mapping.direction = direction;
188 }
Maps a direction along a gamepad axis to a control input value.
Definition: mapping.hpp:61
input::gamepad * gamepad
Pointer to the mapped gamepad, or nullptr if input from any gamepad is accepted.
Definition: mapping.hpp:82
bool direction
Sign bit of the mapped direction.
Definition: mapping.hpp:88
gamepad_axis axis
Mapped gamepad axis.
Definition: mapping.hpp:85
gamepad_axis_mapping()=default
Constructs a gamepad axis mapping.
Maps a gamepad button to a control input value.
Definition: mapping.hpp:95
gamepad_button_mapping()=default
Constructs a gamepad button mapping.
input::gamepad * gamepad
Pointer to the mapped gamepad, or nullptr if input from any gamepad is accepted.
Definition: mapping.hpp:115
gamepad_button button
Mapped gamepad button.
Definition: mapping.hpp:118
A virtual gamepad which generates gamepad-related input events.
Definition: gamepad.hpp:49
Maps a keyboard key to a control input value.
Definition: mapping.hpp:125
bool repeat
false if the mapping ignores key repeats, true otherwise.
Definition: mapping.hpp:156
key_mapping()=default
Constructs a key mapping.
scancode scancode
Scancode of the mapped key.
Definition: mapping.hpp:150
input::keyboard * keyboard
Pointer to the mapped keyboard, or nullptr if input from any keyboard is accepted.
Definition: mapping.hpp:147
std::uint16_t modifiers
Modifier keys bitbask.
Definition: mapping.hpp:153
A virtual keyboard which generates keyboard-related input events.
Definition: keyboard.hpp:35
Maps a mouse button to a control input value.
Definition: mapping.hpp:163
input::mouse * mouse
Pointer to the mapped mouse, or nullptr if input from any mouse is accepted.
Definition: mapping.hpp:183
mouse_button_mapping()=default
Constructs a mouse button mapping.
mouse_button button
Mapped mouse button.
Definition: mapping.hpp:186
Maps a direction along a mouse motion axis to a control input value.
Definition: mapping.hpp:193
mouse_motion_axis axis
Mapped mouse motion axis.
Definition: mapping.hpp:217
bool direction
Sign bit of the mapped direction.
Definition: mapping.hpp:220
mouse_motion_mapping()=default
Constructs a mouse motion mapping.
input::mouse * mouse
Pointer to the mapped mouse, or nullptr if input from any mouse is accepted.
Definition: mapping.hpp:214
Maps a direction along a mouse scroll axis to a control input value.
Definition: mapping.hpp:227
mouse_scroll_axis axis
Mapped mouse scroll axis.
Definition: mapping.hpp:252
mouse_scroll_mapping()=default
Constructs a mouse scroll mapping.
bool direction
Sign bit of the mapped direction.
Definition: mapping.hpp:255
input::mouse * mouse
Pointer to the mapped mouse, or nullptr if input from any mouse is accepted.
Definition: mapping.hpp:249
A virtual mouse which generates mouse-related input events.
Definition: mouse.hpp:36
Input devices, events, and mapping.
mouse_motion_axis
Mouse motion axes.
mouse_scroll_axis
Mouse scroll axes.
gamepad_button
Gamepad buttons.
scancode
Keyboard scancodes.
Definition: scancode.hpp:33
mouse_button
Mouse buttons.
gamepad_axis
Gamepad axes.
Provides access to a deserialization state.
std::size_t read16(std::byte *data, std::size_t count) noexcept(false)
Reads 16-bit (word) data.
virtual std::size_t read8(std::byte *data, std::size_t count) noexcept(false)=0
Reads 8-bit (byte) data.
void deserialize(T &value, deserialize_context &ctx)
Deserializes a value.
Provides access to a serialization state.
std::size_t write16(const std::byte *data, std::size_t count) noexcept(false)
Writes 16-bit (word) data.
virtual std::size_t write8(const std::byte *data, std::size_t count) noexcept(false)=0
Writes 8-bit (byte) data.
void serialize(const T &value, serialize_context &ctx)
Serializes a value.