79 lines
2.4 KiB
Rust
79 lines
2.4 KiB
Rust
// file: crates/engines/engine-v1-common/src/input.rs
|
|
// version: 3
|
|
|
|
/// Platform-independent snapshot of logical input actions for one update.
|
|
#[derive(Clone, Copy, Debug, Default, PartialEq)]
|
|
pub struct InputState {
|
|
left: bool,
|
|
right: bool,
|
|
up: bool,
|
|
down: bool,
|
|
primary: bool,
|
|
secondary: bool,
|
|
pause: bool,
|
|
pointer: crate::PointerState,
|
|
}
|
|
|
|
impl InputState {
|
|
/// Creates a snapshot with no active action.
|
|
#[must_use]
|
|
pub const fn none() -> Self {
|
|
return Self {
|
|
left: false,
|
|
right: false,
|
|
up: false,
|
|
down: false,
|
|
primary: false,
|
|
secondary: false,
|
|
pause: false,
|
|
pointer: crate::PointerState::inactive(),
|
|
};
|
|
}
|
|
|
|
/// Returns a copy with the selected logical action set to the requested state.
|
|
#[must_use]
|
|
pub const fn with_action(mut self, action: crate::GameAction, active: bool) -> Self {
|
|
match action {
|
|
crate::GameAction::Left => self.left = active,
|
|
crate::GameAction::Right => self.right = active,
|
|
crate::GameAction::Up => self.up = active,
|
|
crate::GameAction::Down => self.down = active,
|
|
crate::GameAction::Primary => self.primary = active,
|
|
crate::GameAction::Secondary => self.secondary = active,
|
|
crate::GameAction::Pause => self.pause = active,
|
|
}
|
|
return self;
|
|
}
|
|
|
|
/// Returns a copy carrying the selected normalized primary pointer state.
|
|
#[must_use]
|
|
pub const fn with_pointer(mut self, pointer: crate::PointerState) -> Self {
|
|
self.pointer = pointer;
|
|
return self;
|
|
}
|
|
|
|
/// Reports whether the selected logical action is active.
|
|
#[must_use]
|
|
pub const fn is_active(self, action: crate::GameAction) -> bool {
|
|
return match action {
|
|
crate::GameAction::Left => self.left,
|
|
crate::GameAction::Right => self.right,
|
|
crate::GameAction::Up => self.up,
|
|
crate::GameAction::Down => self.down,
|
|
crate::GameAction::Primary => self.primary,
|
|
crate::GameAction::Secondary => self.secondary,
|
|
crate::GameAction::Pause => self.pause,
|
|
};
|
|
}
|
|
|
|
/// Returns the primary pointer snapshot.
|
|
#[must_use]
|
|
pub const fn pointer(self) -> crate::PointerState {
|
|
return self.pointer;
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[path = "../unit_tests/input.rs"]
|
|
mod tests;
|