1use super::*;
2#[derive(Clone, Copy, Debug, PartialEq, Eq)]
3pub enum Preset {
4 Platformer,
5 Shooter,
6 Explore,
7 HoverRacer,
8 Racer,
9 Flight,
10 TopDown,
11 Pinball,
12 Turret,
13 Strategy,
14 Lab2,
15 Lab3,
16}
17fn b(code: InputCode) -> Binding {
18 Binding::digital(code)
19}
20fn key(code: KeyCode) -> Binding {
21 b(InputCode::Key(code as u16))
22}
23impl PlayerInput {
24 pub fn preset(owner: &str, preset: Preset) -> Self {
25 let mut p = Self::new(owner);
26 let mut add =
27 |name: &str, label: &str, kind: ActionKind, bindings: Vec<Binding>, global: bool| {
28 if global {
29 p.global_action(name, label, kind, bindings)
30 } else {
31 p.action(name, label, kind, bindings)
32 }
33 .expect("valid built-in input profile");
34 };
35 let directional = matches!(
36 preset,
37 Preset::Platformer
38 | Preset::Shooter
39 | Preset::Explore
40 | Preset::HoverRacer
41 | Preset::Racer
42 | Preset::Flight
43 | Preset::TopDown
44 | Preset::Lab3
45 | Preset::Turret
46 | Preset::Strategy
47 | Preset::Lab2
48 );
49 if directional {
50 for (name, label, a, z, pad) in [
51 ("move_left", "Left", KeyCode::A, KeyCode::Left, 14),
52 ("move_right", "Right", KeyCode::D, KeyCode::Right, 15),
53 ] {
54 add(
55 name,
56 label,
57 ActionKind::Axis,
58 vec![
59 key(a),
60 key(z),
61 b(InputCode::PadButton(pad)),
62 b(InputCode::Touch(if name == "move_left" { 12 } else { 13 })),
63 ],
64 false,
65 );
66 }
67 add(
68 "move_x",
69 "Move / steer X",
70 ActionKind::Axis,
71 vec![b(InputCode::PadAxis(0)), b(InputCode::Touch(0))],
72 false,
73 );
74 }
75 if directional && preset != Preset::Platformer {
76 for (name, label, a, z, pad) in [
77 ("move_forward", "Forward / up", KeyCode::W, KeyCode::Up, 12),
78 ("move_back", "Back / down", KeyCode::S, KeyCode::Down, 13),
79 ] {
80 let mut bindings = vec![
81 key(a),
82 key(z),
83 b(InputCode::PadButton(pad)),
84 b(InputCode::Touch(if name == "move_forward" {
85 14
86 } else {
87 15
88 })),
89 ];
90 if matches!(preset, Preset::HoverRacer | Preset::Racer) && name == "move_forward" {
91 bindings.push(b(InputCode::PadButton(7)));
92 }
93 add(name, label, ActionKind::Axis, bindings, false);
94 }
95 add(
96 "move_y",
97 "Vertical axis",
98 ActionKind::Axis,
99 vec![b(InputCode::PadAxis(1)), b(InputCode::Touch(1))],
100 false,
101 );
102 }
103 if matches!(
104 preset,
105 Preset::Shooter | Preset::Explore | Preset::Lab3 | Preset::Turret
106 ) {
107 add(
108 "look_x",
109 "Look right",
110 ActionKind::Delta,
111 vec![b(InputCode::MouseDelta(0)), b(InputCode::TouchDelta(0))],
112 false,
113 );
114 add(
115 "look_y",
116 "Look down",
117 ActionKind::Delta,
118 vec![b(InputCode::MouseDelta(1)), b(InputCode::TouchDelta(1))],
119 false,
120 );
121 add(
122 "look_rate_x",
123 "Look X stick",
124 ActionKind::Axis,
125 vec![b(InputCode::PadAxis(2))],
126 false,
127 );
128 add(
129 "look_rate_y",
130 "Look Y stick",
131 ActionKind::Axis,
132 vec![b(InputCode::PadAxis(3))],
133 false,
134 );
135 }
136 if matches!(preset, Preset::Platformer | Preset::Shooter) {
137 let mut bindings = vec![
138 key(KeyCode::Space),
139 b(InputCode::PadButton(0)),
140 b(InputCode::Touch(3)),
141 ];
142 if preset == Preset::Platformer {
143 bindings.extend([key(KeyCode::W), key(KeyCode::Up)]);
144 }
145 add("jump", "Jump", ActionKind::Button, bindings, false);
146 }
147 if preset == Preset::Shooter {
148 add(
149 "reload",
150 "Reload",
151 ActionKind::Button,
152 vec![
153 key(KeyCode::R),
154 b(InputCode::PadButton(3)),
155 b(InputCode::Touch(5)),
156 ],
157 false,
158 );
159 }
160 if matches!(preset, Preset::Shooter | Preset::TopDown | Preset::Turret) {
161 let mut bindings = vec![b(InputCode::PadButton(7)), b(InputCode::Touch(2))];
162 if preset == Preset::Turret {
163 bindings.push(key(KeyCode::Space));
164 } else {
165 bindings.push(b(InputCode::Mouse(1)));
166 }
167 add("fire", "Fire", ActionKind::Button, bindings, false);
168 }
169 if preset == Preset::Explore {
170 add(
171 "interact",
172 "Interact",
173 ActionKind::Button,
174 vec![
175 key(KeyCode::E),
176 b(InputCode::PadButton(2)),
177 b(InputCode::Touch(4)),
178 ],
179 false,
180 );
181 }
182 if matches!(preset, Preset::Racer | Preset::Flight) {
183 let mut bindings = vec![
184 key(if preset == Preset::Flight {
185 KeyCode::LeftShift
186 } else {
187 KeyCode::Space
188 }),
189 b(InputCode::PadButton(6)),
190 b(InputCode::Touch(6)),
191 ];
192 if preset == Preset::Flight {
193 bindings.push(key(KeyCode::RightShift));
194 }
195 add("brake", "Brake / slow", ActionKind::Axis, bindings, false);
196 }
197 if matches!(
198 preset,
199 Preset::HoverRacer | Preset::Flight | Preset::TopDown
200 ) {
201 let mut bindings = vec![
202 key(KeyCode::Space),
203 b(InputCode::PadButton(0)),
204 b(InputCode::Touch(7)),
205 ];
206 if preset != Preset::Flight {
207 bindings.extend([key(KeyCode::LeftShift), key(KeyCode::RightShift)]);
208 }
209 add("boost", "Boost / dash", ActionKind::Button, bindings, false);
210 }
211 if preset == Preset::Pinball {
212 for (name, label, a, z, pad, touch) in [
213 (
214 "flipper_left",
215 "Left flipper",
216 KeyCode::A,
217 KeyCode::Left,
218 4,
219 0,
220 ),
221 (
222 "flipper_right",
223 "Right flipper",
224 KeyCode::D,
225 KeyCode::Right,
226 5,
227 1,
228 ),
229 ] {
230 add(
231 name,
232 label,
233 ActionKind::Button,
234 vec![
235 key(a),
236 key(z),
237 b(InputCode::PadButton(pad)),
238 b(InputCode::Touch(touch)),
239 ],
240 false,
241 );
242 }
243 add(
244 "launch",
245 "Launch",
246 ActionKind::Button,
247 vec![
248 key(KeyCode::Space),
249 b(InputCode::PadButton(0)),
250 b(InputCode::Touch(8)),
251 ],
252 false,
253 );
254 }
255 if matches!(preset, Preset::Strategy | Preset::Lab2) {
256 add(
257 "primary",
258 "Point / select",
259 ActionKind::Button,
260 vec![b(InputCode::Mouse(1)), b(InputCode::PadButton(0))],
261 false,
262 );
263 }
264 if preset == Preset::Lab2 {
265 add(
266 "secondary",
267 "Spawn crate",
268 ActionKind::Button,
269 vec![b(InputCode::Mouse(3)), b(InputCode::PadButton(1))],
270 false,
271 );
272 }
273 if matches!(preset, Preset::Strategy | Preset::Lab2 | Preset::Lab3) {
274 add(
275 "select_one",
276 if preset == Preset::Strategy {
277 "Build seed"
278 } else if preset == Preset::Lab3 {
279 "Drop crate"
280 } else {
281 "Choose ball"
282 },
283 ActionKind::Button,
284 vec![
285 key(KeyCode::Key1),
286 b(InputCode::PadButton(if preset == Preset::Lab3 {
287 0
288 } else {
289 2
290 })),
291 ],
292 false,
293 );
294 add(
295 "select_two",
296 if preset == Preset::Strategy {
297 "Build frost"
298 } else if preset == Preset::Lab3 {
299 "Drop ball"
300 } else {
301 "Choose crate"
302 },
303 ActionKind::Button,
304 vec![
305 key(KeyCode::Key2),
306 b(InputCode::PadButton(if preset == Preset::Lab3 {
307 1
308 } else {
309 3
310 })),
311 ],
312 false,
313 );
314 }
315 if matches!(preset, Preset::Lab2 | Preset::Lab3) {
316 add(
317 "pulse",
318 "Impulse pulse",
319 ActionKind::Button,
320 vec![key(KeyCode::F), b(InputCode::PadButton(4))],
321 false,
322 );
323 }
324 if preset == Preset::Strategy {
325 add(
326 "upgrade",
327 "Upgrade",
328 ActionKind::Button,
329 vec![key(KeyCode::U), b(InputCode::PadButton(5))],
330 false,
331 );
332 }
333 if preset == Preset::TopDown {
334 for (name, label, axis) in [("aim_x", "Aim stick X", 2), ("aim_y", "Aim stick Y", 3)] {
335 add(
336 name,
337 label,
338 ActionKind::Axis,
339 vec![b(InputCode::PadAxis(axis))],
340 false,
341 );
342 }
343 }
344 if preset == Preset::Lab3 {
345 add(
346 "zoom",
347 "Zoom",
348 ActionKind::Delta,
349 vec![b(InputCode::Wheel(1))],
350 false,
351 );
352 }
353 if preset == Preset::Lab3 {
354 add(
355 "zoom_rate",
356 "Zoom stick triggers",
357 ActionKind::Axis,
358 vec![
359 b(InputCode::PadButton(6)),
360 Binding {
361 code: InputCode::PadButton(7),
362 scale: -1.,
363 deadzone: 0.,
364 },
365 ],
366 false,
367 );
368 }
369 if preset == Preset::Strategy {
370 add(
371 "wave",
372 "Start wave",
373 ActionKind::Button,
374 vec![key(KeyCode::Space), b(InputCode::PadButton(4))],
375 false,
376 );
377 }
378 if preset != Preset::Shooter {
379 add(
380 "restart",
381 "Restart",
382 ActionKind::Button,
383 vec![
384 key(KeyCode::R),
385 b(InputCode::PadButton(8)),
386 b(InputCode::Touch(9)),
387 ],
388 true,
389 );
390 }
391 if !matches!(preset, Preset::Lab2 | Preset::Lab3) {
392 add(
393 "confirm",
394 "Play / confirm",
395 ActionKind::Button,
396 vec![
397 key(KeyCode::Enter),
398 b(InputCode::PadButton(0)),
399 b(InputCode::Touch(10)),
400 ],
401 true,
402 );
403 }
404 let pause_key = if matches!(preset, Preset::Lab2 | Preset::Lab3) {
405 KeyCode::Space
406 } else {
407 KeyCode::Escape
408 };
409 add(
410 "pause",
411 "Pause / menu",
412 ActionKind::Button,
413 vec![
414 key(pause_key),
415 b(InputCode::PadButton(9)),
416 b(InputCode::Touch(11)),
417 ],
418 true,
419 );
420 p
421 }
422}