platform/input.rs
1// SPDX-FileCopyrightText: 2024 Jens Pitkänen <jens.pitkanen@helsinki.fi>
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4
5/// An input event sent by the platform to the engine for handling.
6pub enum Event {
7 /// Emitted when a digital input (a button, or a key, but not a thumbstick)
8 /// is pressed down.
9 DigitalInputPressed(InputDevice, Button),
10 /// Emitted when a digital input (a button, or a key, but not a thumbstick)
11 /// is pressed released.
12 DigitalInputReleased(InputDevice, Button),
13}
14
15/// A button or key on a specific input device.
16#[derive(Debug, Clone, Copy, PartialEq)]
17pub struct Button(u64);
18
19impl Button {
20 /// Creates a new [`Button`]. Should only be created in the platform
21 /// implementation, which also knows how the inner value is going to be
22 /// used.
23 pub fn new(id: u64) -> Button {
24 Button(id)
25 }
26
27 /// Returns the inner value passed into [`Button::new`]. Generally only
28 /// relevant to the platform implementation.
29 pub fn inner(self) -> u64 {
30 self.0
31 }
32}
33
34/// A specific input device.
35#[derive(Debug, Clone, Copy, PartialEq)]
36pub struct InputDevice(u64);
37
38impl InputDevice {
39 /// Creates a new [`InputDevice`]. Should only be created in the platform
40 /// implementation, which also knows how the inner value is going to be
41 /// used.
42 pub fn new(id: u64) -> InputDevice {
43 InputDevice(id)
44 }
45
46 /// Returns the inner value passed into [`InputDevice::new`]. Generally only
47 /// relevant to the platform implementation.
48 pub fn inner(self) -> u64 {
49 self.0
50 }
51}
52
53/// Generic action categories for which default buttons are provided. Can be
54/// used by games to set up their default mappings for any input device.
55/// Different categories may map to the same buttons, so making inputs
56/// inputs context-sensitive are recommended.
57#[allow(missing_docs)] // the enum variant names are all there is to them
58pub enum ActionCategory {
59 Up,
60 Down,
61 Right,
62 Left,
63 Accept,
64 Cancel,
65 Jump,
66 Run,
67 ActPrimary,
68 ActSecondary,
69 Pause,
70}