arcdps_imgui\input/
keyboard.rs

1use crate::sys;
2use crate::Ui;
3
4/// A key identifier
5#[repr(u32)]
6#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
7pub enum Key {
8    Tab = sys::ImGuiKey_Tab,
9    LeftArrow = sys::ImGuiKey_LeftArrow,
10    RightArrow = sys::ImGuiKey_RightArrow,
11    UpArrow = sys::ImGuiKey_UpArrow,
12    DownArrow = sys::ImGuiKey_DownArrow,
13    PageUp = sys::ImGuiKey_PageUp,
14    PageDown = sys::ImGuiKey_PageDown,
15    Home = sys::ImGuiKey_Home,
16    End = sys::ImGuiKey_End,
17    Insert = sys::ImGuiKey_Insert,
18    Delete = sys::ImGuiKey_Delete,
19    Backspace = sys::ImGuiKey_Backspace,
20    Space = sys::ImGuiKey_Space,
21    Enter = sys::ImGuiKey_Enter,
22    Escape = sys::ImGuiKey_Escape,
23    KeyPadEnter = sys::ImGuiKey_KeyPadEnter,
24    A = sys::ImGuiKey_A,
25    C = sys::ImGuiKey_C,
26    V = sys::ImGuiKey_V,
27    X = sys::ImGuiKey_X,
28    Y = sys::ImGuiKey_Y,
29    Z = sys::ImGuiKey_Z,
30}
31
32impl Key {
33    /// All possible `Key` variants
34    pub const VARIANTS: [Key; Key::COUNT] = [
35        Key::Tab,
36        Key::LeftArrow,
37        Key::RightArrow,
38        Key::UpArrow,
39        Key::DownArrow,
40        Key::PageUp,
41        Key::PageDown,
42        Key::Home,
43        Key::End,
44        Key::Insert,
45        Key::Delete,
46        Key::Backspace,
47        Key::Space,
48        Key::Enter,
49        Key::Escape,
50        Key::KeyPadEnter,
51        Key::A,
52        Key::C,
53        Key::V,
54        Key::X,
55        Key::Y,
56        Key::Z,
57    ];
58    /// Total count of `Key` variants
59    pub const COUNT: usize = sys::ImGuiKey_COUNT as usize;
60}
61
62#[test]
63fn test_key_variants() {
64    for (idx, &value) in Key::VARIANTS.iter().enumerate() {
65        assert_eq!(idx, value as usize);
66    }
67}
68
69/// Target widget selection for keyboard focus
70#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
71pub enum FocusedWidget {
72    /// Previous widget
73    Previous,
74    /// Next widget
75    Next,
76    /// Widget using a relative positive offset (0 is the next widget).
77    ///
78    /// Use this to access sub components of a multiple component widget.
79    Offset(u32),
80}
81
82impl FocusedWidget {
83    #[inline]
84    fn as_offset(self) -> i32 {
85        match self {
86            FocusedWidget::Previous => -1,
87            FocusedWidget::Next => 0,
88            FocusedWidget::Offset(offset) => offset as i32,
89        }
90    }
91}
92
93/// # Input: Keyboard
94impl<'ui> Ui<'ui> {
95    /// Returns the key index of the given key identifier.
96    ///
97    /// Equivalent to indexing the Io struct `key_map` field: `ui.io().key_map[key]`
98    #[inline]
99    #[doc(alias = "GetKeyIndex")]
100    fn key_index(&self, key: Key) -> i32 {
101        unsafe { sys::igGetKeyIndex(key as i32) }
102    }
103    /// Returns true if the key is being held.
104    ///
105    /// Equivalent to indexing the Io struct `keys_down` field: `ui.io().keys_down[key_index]`
106    #[inline]
107    #[doc(alias = "IsKeyDown")]
108    pub fn is_key_down(&self, key: Key) -> bool {
109        let key_index = self.key_index(key);
110        self.is_key_index_down(key_index)
111    }
112
113    /// Same as [`is_key_down`](Self::is_key_down) but takes a key index. The meaning of
114    /// index is defined by your backend implementation.
115    #[inline]
116    #[doc(alias = "IsKeyDown")]
117    pub fn is_key_index_down(&self, key_index: i32) -> bool {
118        unsafe { sys::igIsKeyDown(key_index) }
119    }
120
121    /// Returns true if the key was pressed (went from !down to down).
122    ///
123    /// Affected by key repeat settings (`io.key_repeat_delay`, `io.key_repeat_rate`)
124    #[inline]
125    #[doc(alias = "IsKeyPressed")]
126    pub fn is_key_pressed(&self, key: Key) -> bool {
127        let key_index = self.key_index(key);
128        self.is_key_index_pressed(key_index)
129    }
130
131    /// Same as [`is_key_pressed`](Self::is_key_pressed) but takes a key index.
132    ///
133    /// The meaning of index is defined by your backend
134    /// implementation.
135    #[inline]
136    #[doc(alias = "IsKeyPressed")]
137    pub fn is_key_index_pressed(&self, key_index: i32) -> bool {
138        unsafe { sys::igIsKeyPressed(key_index, true) }
139    }
140
141    /// Returns true if the key was pressed (went from !down to down).
142    ///
143    /// Is **not** affected by key repeat settings (`io.key_repeat_delay`, `io.key_repeat_rate`)
144    #[inline]
145    #[doc(alias = "IsKeyPressed")]
146    pub fn is_key_pressed_no_repeat(&self, key: Key) -> bool {
147        let key_index = self.key_index(key);
148        self.is_key_index_pressed_no_repeat(key_index)
149    }
150
151    /// Same as [`is_key_pressed_no_repeat`](Self::is_key_pressed_no_repeat)
152    /// but takes a key index.
153    ///
154    /// The meaning of index is defined by your backend
155    /// implementation.
156    #[inline]
157    #[doc(alias = "IsKeyPressed")]
158    pub fn is_key_index_pressed_no_repeat(&self, key_index: i32) -> bool {
159        unsafe { sys::igIsKeyPressed(key_index, false) }
160    }
161
162    /// Returns true if the key was released (went from down to !down)
163    #[inline]
164    #[doc(alias = "IsKeyReleased")]
165    pub fn is_key_released(&self, key: Key) -> bool {
166        let key_index = self.key_index(key);
167        self.is_key_index_released(key_index)
168    }
169
170    /// Same as [`is_key_released`](Self::is_key_released) but takes a key index.
171    ///
172    /// The meaning of index is defined by your backend
173    /// implementation.
174    #[inline]
175    #[doc(alias = "IsKeyReleased")]
176    pub fn is_key_index_released(&self, key_index: i32) -> bool {
177        unsafe { sys::igIsKeyReleased(key_index) }
178    }
179
180    /// Returns a count of key presses using the given repeat rate/delay settings.
181    ///
182    /// Usually returns 0 or 1, but might be >1 if `rate` is small enough that `io.delta_time` >
183    /// `rate`.
184    #[inline]
185    #[doc(alias = "GetKeyPressedAmount")]
186    pub fn key_pressed_amount(&self, key: Key, repeat_delay: f32, rate: f32) -> u32 {
187        let key_index = self.key_index(key);
188        self.key_index_pressed_amount(key_index, repeat_delay, rate)
189    }
190
191    #[inline]
192    #[doc(alias = "GetKeyPressedAmount")]
193    pub fn key_index_pressed_amount(&self, key_index: i32, repeat_delay: f32, rate: f32) -> u32 {
194        unsafe { sys::igGetKeyPressedAmount(key_index, repeat_delay, rate) as u32 }
195    }
196
197    /// Focuses keyboard on the next widget.
198    ///
199    /// This is the equivalent to [set_keyboard_focus_here_with_offset](Self::set_keyboard_focus_here_with_offset)
200    /// with `target_widget` set to `FocusedWidget::Next`.
201    #[inline]
202    #[doc(alias = "SetKeyboardFocusHere")]
203    pub fn set_keyboard_focus_here(&self) {
204        self.set_keyboard_focus_here_with_offset(FocusedWidget::Next);
205    }
206
207    /// Focuses keyboard on a widget relative to current position.
208    #[inline]
209    #[doc(alias = "SetKeyboardFocusHere")]
210    pub fn set_keyboard_focus_here_with_offset(&self, target_widget: FocusedWidget) {
211        unsafe {
212            sys::igSetKeyboardFocusHere(target_widget.as_offset());
213        }
214    }
215}