Skip to content

Commit

Permalink
Add more convenient methods to Input
Browse files Browse the repository at this point in the history
  • Loading branch information
Shatur committed Oct 14, 2024
1 parent 3370862 commit 306faa5
Showing 1 changed file with 37 additions and 4 deletions.
41 changes: 37 additions & 4 deletions src/input_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,16 +343,49 @@ pub enum Input {

impl Input {
/// Returns [`Input::MouseMotion`] without keyboard modifiers.
pub fn mouse_motion() -> Self {
#[must_use]
pub const fn mouse_motion() -> Self {
Self::MouseMotion {
modifiers: Default::default(),
modifiers: Modifiers::empty(),
}
}

/// Returns [`Input::MouseWheel`] without keyboard modifiers.
pub fn mouse_wheel() -> Self {
#[must_use]
pub const fn mouse_wheel() -> Self {
Self::MouseWheel {
modifiers: Default::default(),
modifiers: Modifiers::empty(),
}
}

/// Returns new instance without any keyboard modifiers.
///
/// # Panics
///
/// Panics when called on [`Self::GamepadButton`] or [`Self::GamepadAxis`].
#[must_use]
pub const fn without_modifiers(self) -> Self {
self.with_modifiers(Modifiers::empty())
}

/// Returns new instance with the replaced keyboard modifiers.
///
/// # Panics
///
/// Panics when called on [`Self::GamepadButton`] or [`Self::GamepadAxis`].
#[must_use]
pub const fn with_modifiers(self, modifiers: Modifiers) -> Self {
match self {
Input::Keyboard { key_code, .. } => Self::Keyboard {
key_code,
modifiers,
},
Input::MouseButton { button, .. } => Self::MouseButton { button, modifiers },
Input::MouseMotion { .. } => Self::MouseMotion { modifiers },
Input::MouseWheel { .. } => Self::MouseWheel { modifiers },
Input::GamepadButton { .. } | Input::GamepadAxis { .. } => {
panic!("keyboard modifiers can't be applied to gamepads")
}
}
}
}
Expand Down

0 comments on commit 306faa5

Please sign in to comment.