Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / games / voxels / src / main / rust / src / input.rs
input.rs26 lines · 1.7 KiBRaw
| 1 | use std::sync::{Mutex, OnceLock}; |
| 2 | #[derive(Debug, Clone, Copy, Default)] |
| 3 | pub struct InputState { |
| 4 | pub move_forward: f32, pub move_right: f32, |
| 5 | // Look is a rate (normalized stick displacement, -1..1) applied every frame, not a one-shot delta. |
| 6 | pub look_yaw_rate: f32, pub look_pitch_rate: f32, |
| 7 | pub jump_held: bool, // top button held: jump when walking, ascend when flying |
| 8 | pub down_held: bool, // bottom button held while flying: descend |
| 9 | pub sneak: bool, // walking sneak mode (toggle); disables jumping and prevents walking off edges |
| 10 | pub sprint: bool, |
| 11 | pub toggle_fly: bool, // edge-triggered |
| 12 | } |
| 13 | static INPUT: OnceLock<Mutex<InputState>> = OnceLock::new(); |
| 14 | fn mutex() -> &'static Mutex<InputState> { INPUT.get_or_init(|| Mutex::new(InputState::default())) } |
| 15 | pub fn set_move(x: f32, y: f32) { if let Ok(mut g)=mutex().lock() { g.move_right=x.clamp(-1.0,1.0); g.move_forward=y.clamp(-1.0,1.0); } } |
| 16 | pub fn set_look_rate(rx: f32, ry: f32) { if let Ok(mut g)=mutex().lock() { g.look_yaw_rate=rx.clamp(-1.0,1.0); g.look_pitch_rate=ry.clamp(-1.0,1.0); } } |
| 17 | pub fn set_jump(held: bool) { if let Ok(mut g)=mutex().lock() { g.jump_held=held; } } |
| 18 | pub fn set_down(held: bool) { if let Ok(mut g)=mutex().lock() { g.down_held=held; } } |
| 19 | pub fn set_sneak(on: bool) { if let Ok(mut g)=mutex().lock() { g.sneak=on; } } |
| 20 | pub fn request_toggle_fly() { if let Ok(mut g)=mutex().lock() { g.toggle_fly=true; } } |
| 21 | pub fn snapshot_and_clear_look() -> InputState { |
| 22 | // Held/persistent values (move, look, jump, down, sneak) stay set until the UI reports a change; |
| 23 | // only the edge-triggered fly toggle is consumed here. |
| 24 | if let Ok(mut g)=mutex().lock() { let c=*g; g.toggle_fly=false; return c; } |
| 25 | InputState::default() |
| 26 | } |