0.1.0-0-pre.8
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
// file: crates/engines/engine-v1-common/src/input.rs
|
||||
// version: 2
|
||||
// version: 3
|
||||
|
||||
/// Platform-independent snapshot of logical input actions for one update.
|
||||
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq)]
|
||||
pub struct InputState {
|
||||
left: bool,
|
||||
right: bool,
|
||||
@@ -11,13 +11,23 @@ pub struct InputState {
|
||||
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 };
|
||||
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.
|
||||
@@ -35,6 +45,13 @@ impl InputState {
|
||||
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 {
|
||||
@@ -48,6 +65,12 @@ impl InputState {
|
||||
crate::GameAction::Pause => self.pause,
|
||||
};
|
||||
}
|
||||
|
||||
/// Returns the primary pointer snapshot.
|
||||
#[must_use]
|
||||
pub const fn pointer(self) -> crate::PointerState {
|
||||
return self.pointer;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// file: crates/engines/engine-v1-common/src/lib.rs
|
||||
// version: 2
|
||||
// version: 3
|
||||
|
||||
#![warn(missing_docs)]
|
||||
#![deny(unreachable_pub)]
|
||||
@@ -11,6 +11,7 @@ mod action;
|
||||
mod frame;
|
||||
mod game_loop;
|
||||
mod input;
|
||||
mod pointer;
|
||||
|
||||
/// Re-export of the canonical logical input action used by engine V1 games.
|
||||
pub use self::action::GameAction;
|
||||
@@ -22,3 +23,5 @@ pub use self::game_loop::EngineGame;
|
||||
pub use self::game_loop::FixedStepRunner;
|
||||
/// Re-export of the platform-independent logical input snapshot.
|
||||
pub use self::input::InputState;
|
||||
/// Re-export of the normalized primary pointer snapshot.
|
||||
pub use self::pointer::PointerState;
|
||||
|
||||
52
crates/engines/engine-v1-common/src/pointer.rs
Normal file
52
crates/engines/engine-v1-common/src/pointer.rs
Normal file
@@ -0,0 +1,52 @@
|
||||
// file: crates/engines/engine-v1-common/src/pointer.rs
|
||||
// version: 1
|
||||
|
||||
/// Platform-independent normalized primary pointer state for one engine update.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct PointerState {
|
||||
active: bool,
|
||||
x: f32,
|
||||
y: f32,
|
||||
}
|
||||
|
||||
impl PointerState {
|
||||
/// Creates an inactive pointer at the normalized origin.
|
||||
#[must_use]
|
||||
pub const fn inactive() -> Self {
|
||||
return Self { active: false, x: 0.0, y: 0.0 };
|
||||
}
|
||||
|
||||
/// Creates a pointer state from normalized coordinates.
|
||||
#[must_use]
|
||||
pub fn normalized(active: bool, x: f32, y: f32) -> Self {
|
||||
return Self { active, x: x.clamp(0.0, 1.0), y: y.clamp(0.0, 1.0) };
|
||||
}
|
||||
|
||||
/// Reports whether the pointer is currently active.
|
||||
#[must_use]
|
||||
pub const fn active(self) -> bool {
|
||||
return self.active;
|
||||
}
|
||||
|
||||
/// Returns the normalized horizontal coordinate.
|
||||
#[must_use]
|
||||
pub const fn x(self) -> f32 {
|
||||
return self.x;
|
||||
}
|
||||
|
||||
/// Returns the normalized vertical coordinate.
|
||||
#[must_use]
|
||||
pub const fn y(self) -> f32 {
|
||||
return self.y;
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for PointerState {
|
||||
fn default() -> Self {
|
||||
return Self::inactive();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "../unit_tests/pointer.rs"]
|
||||
mod tests;
|
||||
@@ -1,5 +1,5 @@
|
||||
// file: crates/engines/engine-v1-common/unit_tests/input.rs
|
||||
// version: 1
|
||||
// version: 2
|
||||
|
||||
#[test]
|
||||
fn action_state_is_independent() {
|
||||
@@ -10,3 +10,11 @@ fn action_state_is_independent() {
|
||||
assert!(!input.is_active(crate::GameAction::Left));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pointer_state_is_carried_independently_from_actions() {
|
||||
let pointer = crate::PointerState::normalized(true, 0.25, 0.75);
|
||||
let input = crate::InputState::none().with_pointer(pointer);
|
||||
assert_eq!(input.pointer(), pointer);
|
||||
assert!(!input.is_active(crate::GameAction::Primary));
|
||||
}
|
||||
|
||||
10
crates/engines/engine-v1-common/unit_tests/pointer.rs
Normal file
10
crates/engines/engine-v1-common/unit_tests/pointer.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
// file: crates/engines/engine-v1-common/unit_tests/pointer.rs
|
||||
// version: 1
|
||||
|
||||
#[test]
|
||||
fn normalized_pointer_clamps_coordinates() {
|
||||
let pointer = crate::PointerState::normalized(true, -0.5, 1.5);
|
||||
assert!(pointer.active());
|
||||
assert_eq!(pointer.x(), 0.0);
|
||||
assert_eq!(pointer.y(), 1.0);
|
||||
}
|
||||
Reference in New Issue
Block a user