coastline 0.0.4
Loading...
Searching...
No Matches
controls.h
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <cstddef>
5#include <string>
6#include <string_view>
7#include <vector>
8
9#include <SDL3/SDL.h>
10
11namespace coastline {
12
13class GameConfig;
14
52
55 "SteerLeft", "SteerRight", "Accelerate", "Brake", "Reverse",
56 "GearUp", "GearDown", "CockpitCamera", "BumperCamera", "ChaseCamera",
57 "PauseMenuUp", "PauseMenuDown", "PauseMenuSelect", "Pause", "StartOrRespawn",
58 "MenuA", "MenuB", "MenuZ", "MenuStart", "MenuL",
59 "MenuR", "MenuCUp", "MenuCDown", "MenuCLeft", "MenuCRight",
60 "MenuDUp", "MenuDDown", "MenuDLeft", "MenuDRight", "MenuStickLeft",
61 "MenuStickRight", "MenuStickUp", "MenuStickDown",
62};
63
65inline constexpr std::string_view CONTROL_PRESET_KEY = "Preset";
67inline constexpr std::string_view DEAD_ZONE_KEY = "DeadZone";
68
70inline constexpr std::array<std::string_view, 2> PLAYER_DEVICE_KEYS{"Player1", "Player2"};
71
77
79inline constexpr ControlPreset DEFAULT_CONTROL_PRESET{"Default",
80 {"leftx-,dpleft",
81 "leftx+,dpright",
82 "a,righttrigger",
83 "b,lefttrigger",
84 "x",
85 "rightshoulder",
86 "leftshoulder",
87 "righty-",
88 "rightx-",
89 "righty+,y",
90 "lefty-,dpup",
91 "lefty+,dpdown",
92 "a",
93 "start",
94 "back",
95 "a",
96 "b,x",
97 "lefttrigger",
98 "start",
99 "leftshoulder",
100 "rightshoulder,righttrigger",
101 "righty-",
102 "righty+",
103 "rightx-",
104 "rightx+",
105 "dpup",
106 "dpdown",
107 "dpleft",
108 "dpright",
109 "leftx-",
110 "leftx+",
111 "lefty-",
112 "lefty+"}};
113
116
122constexpr ControlPreset
126 preset.name = name;
127 for (std::size_t i = 0; i < race.size(); ++i) {
128 preset.values[i] = race[i];
129 }
130 return preset;
131}
132
146 {"Kart",
147 {"leftx-,dpleft",
148 "leftx+,dpright",
149 "b",
150 "a",
151 "x",
152 "rightshoulder,righttrigger",
153 "leftshoulder,lefttrigger",
154 "righty-",
155 "rightx-",
156 "righty+,y",
157 "lefty-,dpup",
158 "lefty+,dpdown",
159 "b",
160 "start",
161 "back",
162 "b",
163 "a,x",
164 "lefttrigger",
165 "start",
166 "leftshoulder",
167 "rightshoulder,righttrigger",
168 "righty-",
169 "righty+",
170 "rightx-",
171 "rightx+",
172 "dpup",
173 "dpdown",
174 "dpleft",
175 "dpright",
176 "leftx-",
177 "leftx+",
178 "lefty-",
179 "lefty+"}},
180 RaceControlPreset("TypeA",
181 {"leftx-",
182 "leftx+",
183 "a",
184 "b",
185 "lefty+",
186 "rightshoulder",
187 "lefttrigger",
188 "righty-",
189 "rightx-",
190 "righty+"}),
191 RaceControlPreset("TypeB",
192 {"leftx-",
193 "leftx+",
194 "a",
195 "b",
196 "lefty+",
197 "rightshoulder",
198 "lefttrigger",
199 "righty-",
200 "rightx-",
201 "rightx+"}),
202 RaceControlPreset("TypeC",
203 {"dpleft",
204 "dpright",
205 "a",
206 "b",
207 "dpdown",
208 "rightshoulder",
209 "leftshoulder",
210 "righty-",
211 "rightx-",
212 "righty+"}),
213 RaceControlPreset("TypeD",
214 {"leftx-",
215 "leftx+",
216 "lefty-",
217 "lefttrigger",
218 "lefty+",
219 "b,dpup",
220 "a,dpdown",
221 "righty-",
222 "rightx-",
223 "righty+"}),
224 RaceControlPreset("SteeringWheel",
225 {"leftx-",
226 "leftx+",
227 "a",
228 "b",
229 "lefty+",
230 "rightshoulder",
231 "leftshoulder",
232 "righty-",
233 "rightx-",
234 "righty+"}),
235}};
236
239 bool axis;
240 int index;
241 int sign;
242};
243
245inline constexpr int CONTROL_FULL = 32768;
246
249public:
255 explicit ControlBindings(const GameConfig &config);
262 void Load(const GameConfig &config);
272 bool IsAnalog(ControlAction action) const;
278 int Read(SDL_Gamepad *gamepad, ControlAction action) const;
279
280private:
282 std::array<std::string, PLAYER_DEVICE_KEYS.size()> m_playerDevices;
283 int m_deadZone = 0;
284};
285
294void LoadControls(const GameConfig &config);
295
302
308
314int ReadControl(SDL_Gamepad *gamepad, ControlAction action);
315
316} // namespace coastline
bool IsAnalog(ControlAction action) const
std::string_view PlayerDevice(std::size_t player) const
ControlBindings(const GameConfig &config)
Load configured assignments and preset defaults.
void Load(const GameConfig &config)
Replace assignments, logging and ignoring invalid sources.
int Read(SDL_Gamepad *gamepad, ControlAction action) const
The settings of BossRally.ini.
Definition config.h:18
Definition assetimport.h:11
constexpr std::size_t RACE_CONTROL_ACTIONS
The actions of a race, from CONTROL_STEER_LEFT to CONTROL_CHASE_CAMERA.
Definition controls.h:115
std::string_view PlayerDevice(std::size_t player)
constexpr std::array< std::string_view, CONTROL_ACTION_COUNT > CONTROL_KEYS
The keys of the Controls section, by ControlAction.
Definition controls.h:54
constexpr int CONTROL_FULL
The magnitude of a button held down or of an axis at its end.
Definition controls.h:245
constexpr std::string_view CONTROL_PRESET_KEY
The key of the Controls section that chooses a preset.
Definition controls.h:65
constexpr ControlPreset DEFAULT_CONTROL_PRESET
The preset of an absent or unknown Preset key.
Definition controls.h:79
constexpr std::array< std::string_view, 2 > PLAYER_DEVICE_KEYS
The keys of the Controls section that choose the input device of each player.
Definition controls.h:70
ControlAction
The actions of a gamepad the Controls section of BossRally.ini binds, in the order of keys.
Definition controls.h:16
@ CONTROL_MENU_STICK_RIGHT
Definition controls.h:47
@ CONTROL_MENU_C_UP
Definition controls.h:38
@ CONTROL_ACTION_COUNT
Definition controls.h:50
@ CONTROL_BRAKE
Definition controls.h:20
@ CONTROL_BUMPER_CAMERA
Definition controls.h:25
@ CONTROL_PAUSE
Definition controls.h:30
@ CONTROL_MENU_R
Definition controls.h:37
@ CONTROL_MENU_Z
Definition controls.h:34
@ CONTROL_MENU_D_LEFT
Definition controls.h:44
@ CONTROL_MENU_C_DOWN
Definition controls.h:39
@ CONTROL_COCKPIT_CAMERA
Definition controls.h:24
@ CONTROL_STEER_LEFT
Definition controls.h:17
@ CONTROL_MENU_D_DOWN
Definition controls.h:43
@ CONTROL_GEAR_DOWN
Definition controls.h:23
@ CONTROL_MENU_A
Definition controls.h:32
@ CONTROL_PAUSE_MENU_DOWN
Definition controls.h:28
@ CONTROL_REVERSE
Definition controls.h:21
@ CONTROL_PAUSE_MENU_SELECT
Definition controls.h:29
@ CONTROL_MENU_L
Definition controls.h:36
@ CONTROL_START_OR_RESPAWN
Definition controls.h:31
@ CONTROL_MENU_B
Definition controls.h:33
@ CONTROL_MENU_STICK_UP
Definition controls.h:48
@ CONTROL_GEAR_UP
Definition controls.h:22
@ CONTROL_MENU_D_UP
Definition controls.h:42
@ CONTROL_MENU_D_RIGHT
Definition controls.h:45
@ CONTROL_CHASE_CAMERA
Definition controls.h:26
@ CONTROL_MENU_C_LEFT
Definition controls.h:40
@ CONTROL_MENU_C_RIGHT
Definition controls.h:41
@ CONTROL_ACCELERATE
Definition controls.h:19
@ CONTROL_MENU_START
Definition controls.h:35
@ CONTROL_MENU_STICK_DOWN
Definition controls.h:49
@ CONTROL_PAUSE_MENU_UP
Definition controls.h:27
@ CONTROL_STEER_RIGHT
Definition controls.h:18
@ CONTROL_MENU_STICK_LEFT
Definition controls.h:46
int ReadControl(SDL_Gamepad *gamepad, ControlAction action)
constexpr std::string_view DEAD_ZONE_KEY
The key of the Controls section with the share of the travel of an axis that reads as 0.
Definition controls.h:67
constexpr std::array< ControlPreset, 7 > CONTROL_PRESETS
The presets the Preset key chooses from, the first the default.
Definition controls.h:144
void LoadControls(const GameConfig &config)
Read the Controls section of config: the preset of the Preset key, the default for an absent or unkno...
bool IsControlAnalog(ControlAction action)
constexpr ControlPreset RaceControlPreset(std::string_view name, const std::array< std::string_view, RACE_CONTROL_ACTIONS > &race)
Definition controls.h:123
T size(T... args)
A set of bindings of every action, in the syntax of the Controls section.
Definition controls.h:73
std::array< std::string_view, CONTROL_ACTION_COUNT > values
Definition controls.h:75
std::string_view name
Definition controls.h:74
A button or the half of an axis of a gamepad that drives an action.
Definition controls.h:238
bool axis
Definition controls.h:239
int sign
Definition controls.h:241
int index
Definition controls.h:240