leafwing_input_manager/buttonlike.rs
1//! Tools for working with button-like user inputs (mouse clicks, gamepad button, keyboard inputs and so on)
2
3use bevy::reflect::Reflect;
4use serde::{Deserialize, Serialize};
5
6/// Current values of a button.
7#[derive(Debug, Default, Clone, Copy, PartialEq, Reflect, Serialize, Deserialize)]
8pub struct ButtonValue {
9 /// Is the button currently pressed?
10 pub pressed: bool,
11
12 /// How far has the button been pressed,
13 /// ranging from 0.0 (not pressed) to 1.0 (fully pressed).
14 pub value: f32,
15}
16
17impl ButtonValue {
18 /// Create a new [`ButtonValue`] with the given `pressed` state and `value`.
19 #[inline]
20 pub fn new(pressed: bool, value: f32) -> Self {
21 Self { pressed, value }
22 }
23
24 /// Create a new [`ButtonValue`] with the given `pressed` state.
25 ///
26 /// The value will set to 1.0 if `pressed` is true, and 0.0 otherwise
27 #[inline]
28 pub fn from_pressed(pressed: bool) -> Self {
29 Self::new(pressed, f32::from(pressed))
30 }
31}
32
33impl From<ButtonState> for ButtonValue {
34 fn from(value: ButtonState) -> Self {
35 Self::from_pressed(value.pressed())
36 }
37}
38
39impl From<bevy::input::ButtonState> for ButtonValue {
40 fn from(value: bevy::input::ButtonState) -> Self {
41 Self::from_pressed(value.is_pressed())
42 }
43}
44
45/// The current state of a particular button,
46/// usually corresponding to a single [`Actionlike`](crate::Actionlike) action.
47///
48/// By default, buttons are [`ButtonState::Released`].
49#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Reflect, Default)]
50pub enum ButtonState {
51 /// The button has been pressed since the most recent tick
52 JustPressed,
53 /// This button is currently pressed (and was pressed before the most recent tick)
54 Pressed,
55 /// The button has been released since the most recent tick
56 JustReleased,
57 /// This button is currently released (and was released before the most recent tick)
58 #[default]
59 Released,
60}
61
62impl ButtonState {
63 /// Causes [`just_pressed`](ButtonState::just_pressed) and [`just_released`](ButtonState::just_released) to become false
64 ///
65 /// [`JustPressed`](ButtonState::JustPressed) becomes [`Pressed`](ButtonState::Pressed) and
66 /// [`JustReleased`](ButtonState::JustReleased) becomes [`Released`](ButtonState::Released)
67 pub fn tick(&mut self) {
68 use ButtonState::*;
69 *self = match self {
70 JustPressed => Pressed,
71 Pressed => Pressed,
72 JustReleased => Released,
73 Released => Released,
74 }
75 }
76
77 /// Presses the button
78 ///
79 /// It will be [`JustPressed`](ButtonState::JustPressed), unless it was already [`Pressed`](ButtonState::Pressed)
80 #[inline]
81 pub fn press(&mut self) {
82 if *self != ButtonState::Pressed {
83 *self = ButtonState::JustPressed;
84 }
85 }
86
87 /// Releases the button
88 ///
89 /// It will be [`JustReleased`](ButtonState::JustReleased), unless it was already [`Released`](ButtonState::Released)
90 #[inline]
91 pub fn release(&mut self) {
92 if *self != ButtonState::Released {
93 *self = ButtonState::JustReleased;
94 }
95 }
96
97 /// Is the button currently pressed?
98 #[inline]
99 #[must_use]
100 pub fn pressed(&self) -> bool {
101 *self == ButtonState::Pressed || *self == ButtonState::JustPressed
102 }
103
104 /// Is the button currently released?
105 #[inline]
106 #[must_use]
107 pub fn released(&self) -> bool {
108 *self == ButtonState::Released || *self == ButtonState::JustReleased
109 }
110
111 /// Has the button been pressed since the last time [`ActionState::update`](crate::action_state::ActionState::update) was called?
112 #[inline]
113 #[must_use]
114 pub fn just_pressed(&self) -> bool {
115 *self == ButtonState::JustPressed
116 }
117
118 /// Has the button been released since the last time [`ActionState::update`](crate::action_state::ActionState::update) was called?
119 #[inline]
120 #[must_use]
121 pub fn just_released(&self) -> bool {
122 *self == ButtonState::JustReleased
123 }
124}