pub struct InputState {
pub keyboard: KeyboardState,
pub mouse: MouseState,
/* private fields */
}Expand description
Unified input state, accessible via ctx.input.
Fields§
§keyboard: KeyboardStateKeyboard state.
mouse: MouseStateMouse state.
Implementations§
Source§impl InputState
impl InputState
Sourcepub fn register_action(&mut self, action: InputAction)
pub fn register_action(&mut self, action: InputAction)
Register a new input action.
Sourcepub fn action(&self, name: &str) -> Option<&InputAction>
pub fn action(&self, name: &str) -> Option<&InputAction>
Find an action by name.
Sourcepub fn is_action_down(&self, name: &str) -> bool
pub fn is_action_down(&self, name: &str) -> bool
Check if a named action is currently active.
Sourcepub fn is_action_pressed(&self, name: &str) -> bool
pub fn is_action_pressed(&self, name: &str) -> bool
Check if a named action was just pressed.
Examples found in repository?
examples/basics.rs (line 83)
44 fn update(&mut self, ctx: &mut Context) {
45 let dt = ctx.time.delta() as f32;
46
47 // --- Ball physics ---
48 self.ball.velocity.y += 600.0 * dt; // Gravity
49 self.ball.position += self.ball.velocity * dt;
50
51 // Bounce off walls
52 let w = ctx.screen_width();
53 let h = ctx.screen_height();
54
55 if self.ball.position.x - self.ball.radius < 0.0 {
56 self.ball.position.x = self.ball.radius;
57 self.ball.velocity.x = self.ball.velocity.x.abs();
58 }
59 if self.ball.position.x + self.ball.radius > w {
60 self.ball.position.x = w - self.ball.radius;
61 self.ball.velocity.x = -self.ball.velocity.x.abs();
62 }
63 if self.ball.position.y - self.ball.radius < 0.0 {
64 self.ball.position.y = self.ball.radius;
65 self.ball.velocity.y = self.ball.velocity.y.abs();
66 }
67 if self.ball.position.y + self.ball.radius > h {
68 self.ball.position.y = h - self.ball.radius;
69 self.ball.velocity.y = -self.ball.velocity.y.abs() * 0.95; // Damping
70 }
71
72 // Update trail
73 self.ball.trail.push((self.ball.position, 1.0));
74 if self.ball.trail.len() > 60 {
75 self.ball.trail.remove(0);
76 }
77 for (_, alpha) in &mut self.ball.trail {
78 *alpha -= dt * 2.0;
79 }
80 self.ball.trail.retain(|(_, a)| *a > 0.0);
81
82 // --- Input ---
83 if ctx.input.is_action_pressed("shake") {
84 ctx.graphics.camera.shake(10.0, 0.3);
85 }
86
87 if ctx.input.mouse.is_pressed(MouseButton::Left) {
88 self.click_count += 1;
89 // Change ball color
90 self.hue = (self.hue + 30.0) % 360.0;
91 self.ball.color = Color::from_hsla(self.hue, 0.8, 0.6, 1.0);
92 // Boost ball toward click
93 let dir = ctx.input.mouse.position - self.ball.position;
94 self.ball.velocity += dir.normalize_or_zero() * 200.0;
95 }
96
97 // Zoom with scroll
98 let zoom = ctx.graphics.camera.zoom + ctx.input.mouse.scroll.y * 0.1;
99 ctx.graphics.camera.set_zoom(zoom);
100
101 // Escape to quit
102 if ctx.input.keyboard.is_pressed(KeyCode::Escape) {
103 ctx.quit();
104 }
105
106 // Update camera follow target
107 ctx.graphics.camera.follow(self.ball.position, 0.05);
108 }Trait Implementations§
Source§impl Clone for InputState
impl Clone for InputState
Source§fn clone(&self) -> InputState
fn clone(&self) -> InputState
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for InputState
impl Debug for InputState
Source§impl Default for InputState
impl Default for InputState
Source§fn default() -> InputState
fn default() -> InputState
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for InputState
impl RefUnwindSafe for InputState
impl Send for InputState
impl Sync for InputState
impl Unpin for InputState
impl UnsafeUnpin for InputState
impl UnwindSafe for InputState
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more