Skip to main content

forge/
input.rs

1pub use sys::input::{Button, Key};
2
3/// Provides access to controller, keyboard, and touchscreen input.
4pub struct Input;
5
6impl Input {
7    /// Returns `true` if `button` is currently held down.
8    pub fn is_button_down(button: Button) -> bool {
9        unsafe { sys::input::forge_input_isDown(button) }
10    }
11
12    /// Returns `true` if `button` was pressed this frame.
13    pub fn is_button_pressed(button: Button) -> bool {
14        unsafe { sys::input::forge_input_isPressed(button) }
15    }
16
17    /// Returns `true` if `button` was released this frame.
18    pub fn is_button_released(button: Button) -> bool {
19        unsafe { sys::input::forge_input_isReleased(button) }
20    }
21
22    /// Returns the left analog stick position as `(x, y)` in the range `[-1.0, 1.0]`.
23    pub fn get_stick_l() -> (f32, f32) {
24        let mut x = 0.0;
25        let mut y = 0.0;
26        unsafe { sys::input::forge_input_getStickL(&mut x, &mut y) };
27        (x, y)
28    }
29
30    /// Returns the right analog stick position as `(x, y)` in the range `[-1.0, 1.0]`.
31    pub fn get_stick_r() -> (f32, f32) {
32        let mut x = 0.0;
33        let mut y = 0.0;
34        unsafe { sys::input::forge_input_getStickR(&mut x, &mut y) };
35        (x, y)
36    }
37
38    /// Returns `true` if a controller is connected.
39    pub fn is_connected() -> bool {
40        unsafe { sys::input::forge_input_isConnected() }
41    }
42
43    /// Returns the current touch position as `Some((x, y))`, or `None` if the screen is not touched.
44    pub fn get_touch() -> Option<(f32, f32)> {
45        let mut x = 0.0;
46        let mut y = 0.0;
47        if unsafe { sys::input::forge_input_getTouch(&mut x, &mut y) } {
48            Some((x, y))
49        } else {
50            None
51        }
52    }
53
54    /// Returns `true` if `key` is currently held down.
55    pub fn is_key_down(key: Key) -> bool {
56        unsafe { sys::input::forge_input_isKeyDown(key) }
57    }
58
59    /// Returns `true` if `key` was pressed this frame.
60    pub fn is_key_pressed(key: Key) -> bool {
61        unsafe { sys::input::forge_input_isKeyPressed(key) }
62    }
63
64    /// Returns `true` if `key` was released this frame.
65    pub fn is_key_released(key: Key) -> bool {
66        unsafe { sys::input::forge_input_isKeyReleased(key) }
67    }
68
69    /// Returns `true` if either Shift key is held down.
70    pub fn is_shift_down() -> bool {
71        unsafe { sys::input::forge_input_isShiftDown() }
72    }
73
74    /// Returns `true` if either Ctrl key is held down.
75    pub fn is_ctrl_down() -> bool {
76        unsafe { sys::input::forge_input_isCtrlDown() }
77    }
78
79    /// Returns `true` if either Alt key is held down.
80    pub fn is_alt_down() -> bool {
81        unsafe { sys::input::forge_input_isAltDown() }
82    }
83}