dear_imgui/
input.rs

1#![allow(
2    clippy::cast_possible_truncation,
3    clippy::cast_sign_loss,
4    clippy::as_conversions
5)]
6use crate::sys;
7use bitflags::bitflags;
8
9/// Mouse button identifier
10#[repr(i32)]
11#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
12pub enum MouseButton {
13    /// Left mouse button
14    Left = sys::ImGuiMouseButton_Left as i32,
15    /// Right mouse button
16    Right = sys::ImGuiMouseButton_Right as i32,
17    /// Middle mouse button
18    Middle = sys::ImGuiMouseButton_Middle as i32,
19    /// Extra mouse button 1 (typically Back)
20    Extra1 = 3,
21    /// Extra mouse button 2 (typically Forward)
22    Extra2 = 4,
23}
24
25/// Mouse cursor types
26#[repr(i32)]
27#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
28pub enum MouseCursor {
29    /// No cursor
30    None = sys::ImGuiMouseCursor_None,
31    /// Arrow cursor
32    Arrow = sys::ImGuiMouseCursor_Arrow,
33    /// Text input I-beam cursor
34    TextInput = sys::ImGuiMouseCursor_TextInput,
35    /// Resize all directions cursor
36    ResizeAll = sys::ImGuiMouseCursor_ResizeAll,
37    /// Resize north-south cursor
38    ResizeNS = sys::ImGuiMouseCursor_ResizeNS,
39    /// Resize east-west cursor
40    ResizeEW = sys::ImGuiMouseCursor_ResizeEW,
41    /// Resize northeast-southwest cursor
42    ResizeNESW = sys::ImGuiMouseCursor_ResizeNESW,
43    /// Resize northwest-southeast cursor
44    ResizeNWSE = sys::ImGuiMouseCursor_ResizeNWSE,
45    /// Hand cursor
46    Hand = sys::ImGuiMouseCursor_Hand,
47    /// Not allowed cursor
48    NotAllowed = sys::ImGuiMouseCursor_NotAllowed,
49}
50
51/// Key identifier for keyboard input
52#[repr(i32)]
53#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
54pub enum Key {
55    /// No key
56    None = sys::ImGuiKey_None as i32,
57    /// Tab key
58    Tab = sys::ImGuiKey_Tab as i32,
59    /// Left arrow key
60    LeftArrow = sys::ImGuiKey_LeftArrow as i32,
61    /// Right arrow key
62    RightArrow = sys::ImGuiKey_RightArrow as i32,
63    /// Up arrow key
64    UpArrow = sys::ImGuiKey_UpArrow as i32,
65    /// Down arrow key
66    DownArrow = sys::ImGuiKey_DownArrow as i32,
67    /// Page up key
68    PageUp = sys::ImGuiKey_PageUp as i32,
69    /// Page down key
70    PageDown = sys::ImGuiKey_PageDown as i32,
71    /// Home key
72    Home = sys::ImGuiKey_Home as i32,
73    /// End key
74    End = sys::ImGuiKey_End as i32,
75    /// Insert key
76    Insert = sys::ImGuiKey_Insert as i32,
77    /// Delete key
78    Delete = sys::ImGuiKey_Delete as i32,
79    /// Backspace key
80    Backspace = sys::ImGuiKey_Backspace as i32,
81    /// Space key
82    Space = sys::ImGuiKey_Space as i32,
83    /// Enter key
84    Enter = sys::ImGuiKey_Enter as i32,
85    /// Escape key
86    Escape = sys::ImGuiKey_Escape as i32,
87    /// Left Ctrl key
88    LeftCtrl = sys::ImGuiKey_LeftCtrl as i32,
89    /// Left Shift key
90    LeftShift = sys::ImGuiKey_LeftShift as i32,
91    /// Left Alt key
92    LeftAlt = sys::ImGuiKey_LeftAlt as i32,
93    /// Left Super key
94    LeftSuper = sys::ImGuiKey_LeftSuper as i32,
95    /// Right Ctrl key
96    RightCtrl = sys::ImGuiKey_RightCtrl as i32,
97    /// Right Shift key
98    RightShift = sys::ImGuiKey_RightShift as i32,
99    /// Right Alt key
100    RightAlt = sys::ImGuiKey_RightAlt as i32,
101    /// Right Super key
102    RightSuper = sys::ImGuiKey_RightSuper as i32,
103    /// Menu key
104    Menu = sys::ImGuiKey_Menu as i32,
105    /// 0 key
106    Key0 = sys::ImGuiKey_0 as i32,
107    /// 1 key
108    Key1 = sys::ImGuiKey_1 as i32,
109    /// 2 key
110    Key2 = sys::ImGuiKey_2 as i32,
111    /// 3 key
112    Key3 = sys::ImGuiKey_3 as i32,
113    /// 4 key
114    Key4 = sys::ImGuiKey_4 as i32,
115    /// 5 key
116    Key5 = sys::ImGuiKey_5 as i32,
117    /// 6 key
118    Key6 = sys::ImGuiKey_6 as i32,
119    /// 7 key
120    Key7 = sys::ImGuiKey_7 as i32,
121    /// 8 key
122    Key8 = sys::ImGuiKey_8 as i32,
123    /// 9 key
124    Key9 = sys::ImGuiKey_9 as i32,
125    /// A key
126    A = sys::ImGuiKey_A as i32,
127    /// B key
128    B = sys::ImGuiKey_B as i32,
129    /// C key
130    C = sys::ImGuiKey_C as i32,
131    /// D key
132    D = sys::ImGuiKey_D as i32,
133    /// E key
134    E = sys::ImGuiKey_E as i32,
135    /// F key
136    F = sys::ImGuiKey_F as i32,
137    /// G key
138    G = sys::ImGuiKey_G as i32,
139    /// H key
140    H = sys::ImGuiKey_H as i32,
141    /// I key
142    I = sys::ImGuiKey_I as i32,
143    /// J key
144    J = sys::ImGuiKey_J as i32,
145    /// K key
146    K = sys::ImGuiKey_K as i32,
147    /// L key
148    L = sys::ImGuiKey_L as i32,
149    /// M key
150    M = sys::ImGuiKey_M as i32,
151    /// N key
152    N = sys::ImGuiKey_N as i32,
153    /// O key
154    O = sys::ImGuiKey_O as i32,
155    /// P key
156    P = sys::ImGuiKey_P as i32,
157    /// Q key
158    Q = sys::ImGuiKey_Q as i32,
159    /// R key
160    R = sys::ImGuiKey_R as i32,
161    /// S key
162    S = sys::ImGuiKey_S as i32,
163    /// T key
164    T = sys::ImGuiKey_T as i32,
165    /// U key
166    U = sys::ImGuiKey_U as i32,
167    /// V key
168    V = sys::ImGuiKey_V as i32,
169    /// W key
170    W = sys::ImGuiKey_W as i32,
171    /// X key
172    X = sys::ImGuiKey_X as i32,
173    /// Y key
174    Y = sys::ImGuiKey_Y as i32,
175    /// Z key
176    Z = sys::ImGuiKey_Z as i32,
177    /// F1 key
178    F1 = sys::ImGuiKey_F1 as i32,
179    /// F2 key
180    F2 = sys::ImGuiKey_F2 as i32,
181    /// F3 key
182    F3 = sys::ImGuiKey_F3 as i32,
183    /// F4 key
184    F4 = sys::ImGuiKey_F4 as i32,
185    /// F5 key
186    F5 = sys::ImGuiKey_F5 as i32,
187    /// F6 key
188    F6 = sys::ImGuiKey_F6 as i32,
189    /// F7 key
190    F7 = sys::ImGuiKey_F7 as i32,
191    /// F8 key
192    F8 = sys::ImGuiKey_F8 as i32,
193    /// F9 key
194    F9 = sys::ImGuiKey_F9 as i32,
195    /// F10 key
196    F10 = sys::ImGuiKey_F10 as i32,
197    /// F11 key
198    F11 = sys::ImGuiKey_F11 as i32,
199    /// F12 key
200    F12 = sys::ImGuiKey_F12 as i32,
201
202    // --- Punctuation and extra named keys ---
203    /// Apostrophe (') key
204    Apostrophe = sys::ImGuiKey_Apostrophe as i32,
205    /// Comma (,) key
206    Comma = sys::ImGuiKey_Comma as i32,
207    /// Minus (-) key
208    Minus = sys::ImGuiKey_Minus as i32,
209    /// Period (.) key
210    Period = sys::ImGuiKey_Period as i32,
211    /// Slash (/) key
212    Slash = sys::ImGuiKey_Slash as i32,
213    /// Semicolon (;) key
214    Semicolon = sys::ImGuiKey_Semicolon as i32,
215    /// Equal (=) key
216    Equal = sys::ImGuiKey_Equal as i32,
217    /// Left bracket ([) key
218    LeftBracket = sys::ImGuiKey_LeftBracket as i32,
219    /// Backslash (\) key
220    Backslash = sys::ImGuiKey_Backslash as i32,
221    /// Right bracket (]) key
222    RightBracket = sys::ImGuiKey_RightBracket as i32,
223    /// Grave accent (`) key
224    GraveAccent = sys::ImGuiKey_GraveAccent as i32,
225    /// CapsLock key
226    CapsLock = sys::ImGuiKey_CapsLock as i32,
227    /// ScrollLock key
228    ScrollLock = sys::ImGuiKey_ScrollLock as i32,
229    /// NumLock key
230    NumLock = sys::ImGuiKey_NumLock as i32,
231    /// PrintScreen key
232    PrintScreen = sys::ImGuiKey_PrintScreen as i32,
233    /// Pause key
234    Pause = sys::ImGuiKey_Pause as i32,
235
236    // --- Keypad ---
237    /// Numpad 0
238    Keypad0 = sys::ImGuiKey_Keypad0 as i32,
239    /// Numpad 1
240    Keypad1 = sys::ImGuiKey_Keypad1 as i32,
241    /// Numpad 2
242    Keypad2 = sys::ImGuiKey_Keypad2 as i32,
243    /// Numpad 3
244    Keypad3 = sys::ImGuiKey_Keypad3 as i32,
245    /// Numpad 4
246    Keypad4 = sys::ImGuiKey_Keypad4 as i32,
247    /// Numpad 5
248    Keypad5 = sys::ImGuiKey_Keypad5 as i32,
249    /// Numpad 6
250    Keypad6 = sys::ImGuiKey_Keypad6 as i32,
251    /// Numpad 7
252    Keypad7 = sys::ImGuiKey_Keypad7 as i32,
253    /// Numpad 8
254    Keypad8 = sys::ImGuiKey_Keypad8 as i32,
255    /// Numpad 9
256    Keypad9 = sys::ImGuiKey_Keypad9 as i32,
257    /// Numpad decimal
258    KeypadDecimal = sys::ImGuiKey_KeypadDecimal as i32,
259    /// Numpad divide
260    KeypadDivide = sys::ImGuiKey_KeypadDivide as i32,
261    /// Numpad multiply
262    KeypadMultiply = sys::ImGuiKey_KeypadMultiply as i32,
263    /// Numpad subtract
264    KeypadSubtract = sys::ImGuiKey_KeypadSubtract as i32,
265    /// Numpad add
266    KeypadAdd = sys::ImGuiKey_KeypadAdd as i32,
267    /// Numpad enter
268    KeypadEnter = sys::ImGuiKey_KeypadEnter as i32,
269    /// Numpad equal
270    KeypadEqual = sys::ImGuiKey_KeypadEqual as i32,
271
272    /// OEM 102 key (ISO < > |)
273    Oem102 = sys::ImGuiKey_Oem102 as i32,
274}
275
276impl From<MouseButton> for sys::ImGuiMouseButton {
277    #[inline]
278    fn from(value: MouseButton) -> sys::ImGuiMouseButton {
279        value as sys::ImGuiMouseButton
280    }
281}
282
283impl From<Key> for sys::ImGuiKey {
284    #[inline]
285    fn from(value: Key) -> sys::ImGuiKey {
286        value as sys::ImGuiKey
287    }
288}
289
290// Key modifier flags are available via io.KeyCtrl/KeyShift/KeyAlt/KeySuper.
291
292bitflags! {
293    /// Input text flags for text input widgets
294    #[repr(transparent)]
295    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
296    pub struct InputTextFlags: i32 {
297        /// No flags
298        const NONE = sys::ImGuiInputTextFlags_None as i32;
299        /// Allow 0123456789.+-*/
300        const CHARS_DECIMAL = sys::ImGuiInputTextFlags_CharsDecimal as i32;
301        /// Allow 0123456789ABCDEFabcdef
302        const CHARS_HEXADECIMAL = sys::ImGuiInputTextFlags_CharsHexadecimal as i32;
303        /// Turn a..z into A..Z
304        const CHARS_UPPERCASE = sys::ImGuiInputTextFlags_CharsUppercase as i32;
305        /// Filter out spaces, tabs
306        const CHARS_NO_BLANK = sys::ImGuiInputTextFlags_CharsNoBlank as i32;
307        /// Select entire text when first taking mouse focus
308        const AUTO_SELECT_ALL = sys::ImGuiInputTextFlags_AutoSelectAll as i32;
309        /// Return 'true' when Enter is pressed (as opposed to every time the value was modified)
310        const ENTER_RETURNS_TRUE = sys::ImGuiInputTextFlags_EnterReturnsTrue as i32;
311        /// Callback on pressing TAB (for completion handling)
312        const CALLBACK_COMPLETION = sys::ImGuiInputTextFlags_CallbackCompletion as i32;
313        /// Callback on pressing Up/Down arrows (for history handling)
314        const CALLBACK_HISTORY = sys::ImGuiInputTextFlags_CallbackHistory as i32;
315        /// Callback on each iteration (user can query cursor and modify text)
316        const CALLBACK_ALWAYS = sys::ImGuiInputTextFlags_CallbackAlways as i32;
317        /// Callback on character inputs to replace or discard them
318        const CALLBACK_CHAR_FILTER = sys::ImGuiInputTextFlags_CallbackCharFilter as i32;
319        /// Pressing TAB input a '\t' character into the text field
320        const ALLOW_TAB_INPUT = sys::ImGuiInputTextFlags_AllowTabInput as i32;
321        /// In multi-line mode, unfocus with Enter, add new line with Ctrl+Enter
322        const CTRL_ENTER_FOR_NEW_LINE = sys::ImGuiInputTextFlags_CtrlEnterForNewLine as i32;
323        /// Disable following the cursor horizontally
324        const NO_HORIZONTAL_SCROLL = sys::ImGuiInputTextFlags_NoHorizontalScroll as i32;
325        /// Overwrite mode
326        const ALWAYS_OVERWRITE = sys::ImGuiInputTextFlags_AlwaysOverwrite as i32;
327        /// Read-only mode
328        const READ_ONLY = sys::ImGuiInputTextFlags_ReadOnly as i32;
329        /// Password mode, display all characters as '*'
330        const PASSWORD = sys::ImGuiInputTextFlags_Password as i32;
331        /// Disable undo/redo
332        const NO_UNDO_REDO = sys::ImGuiInputTextFlags_NoUndoRedo as i32;
333        /// Allow 0123456789.+-*/eE (Scientific notation input)
334        const CHARS_SCIENTIFIC = sys::ImGuiInputTextFlags_CharsScientific as i32;
335        /// Callback on buffer capacity changes request
336        const CALLBACK_RESIZE = sys::ImGuiInputTextFlags_CallbackResize as i32;
337        /// Callback on any edit (note that InputText() already returns true on edit)
338        const CALLBACK_EDIT = sys::ImGuiInputTextFlags_CallbackEdit as i32;
339    }
340}
341
342// TODO: Add NavInput enum once we have proper constants in sys crate
343
344impl crate::Ui {
345    /// Check if a key is being held down
346    #[doc(alias = "IsKeyDown")]
347    pub fn is_key_down(&self, key: Key) -> bool {
348        unsafe { sys::igIsKeyDown_Nil(key as sys::ImGuiKey) }
349    }
350
351    /// Check if a key was pressed (went from !Down to Down)
352    #[doc(alias = "IsKeyPressed")]
353    pub fn is_key_pressed(&self, key: Key) -> bool {
354        unsafe { sys::igIsKeyPressed_Bool(key as sys::ImGuiKey, true) }
355    }
356
357    /// Check if a key was pressed (went from !Down to Down), with repeat
358    #[doc(alias = "IsKeyPressed")]
359    pub fn is_key_pressed_with_repeat(&self, key: Key, repeat: bool) -> bool {
360        unsafe { sys::igIsKeyPressed_Bool(key as sys::ImGuiKey, repeat) }
361    }
362
363    /// Check if a key was released (went from Down to !Down)
364    #[doc(alias = "IsKeyReleased")]
365    pub fn is_key_released(&self, key: Key) -> bool {
366        unsafe { sys::igIsKeyReleased_Nil(key as sys::ImGuiKey) }
367    }
368
369    /// Check if a mouse button is being held down
370    #[doc(alias = "IsMouseDown")]
371    pub fn is_mouse_down(&self, button: MouseButton) -> bool {
372        unsafe { sys::igIsMouseDown_Nil(button.into()) }
373    }
374
375    /// Check if a mouse button was clicked (went from !Down to Down)
376    #[doc(alias = "IsMouseClicked")]
377    pub fn is_mouse_clicked(&self, button: MouseButton) -> bool {
378        unsafe { sys::igIsMouseClicked_Bool(button.into(), false) }
379    }
380
381    /// Check if a mouse button was clicked, with repeat
382    #[doc(alias = "IsMouseClicked")]
383    pub fn is_mouse_clicked_with_repeat(&self, button: MouseButton, repeat: bool) -> bool {
384        unsafe { sys::igIsMouseClicked_Bool(button.into(), repeat) }
385    }
386
387    /// Check if a mouse button was released (went from Down to !Down)
388    #[doc(alias = "IsMouseReleased")]
389    pub fn is_mouse_released(&self, button: MouseButton) -> bool {
390        unsafe { sys::igIsMouseReleased_Nil(button.into()) }
391    }
392
393    /// Check if a mouse button was double-clicked
394    #[doc(alias = "IsMouseDoubleClicked")]
395    pub fn is_mouse_double_clicked(&self, button: MouseButton) -> bool {
396        unsafe { sys::igIsMouseDoubleClicked_Nil(button.into()) }
397    }
398
399    /// Get mouse position in screen coordinates
400    #[doc(alias = "GetMousePos")]
401    pub fn mouse_pos(&self) -> [f32; 2] {
402        let mut pos = sys::ImVec2 { x: 0.0, y: 0.0 };
403        unsafe { sys::igGetMousePos(&mut pos) };
404        [pos.x, pos.y]
405    }
406
407    /// Get mouse position when a specific button was clicked
408    #[doc(alias = "GetMousePosOnOpeningCurrentPopup")]
409    pub fn mouse_pos_on_opening_current_popup(&self) -> [f32; 2] {
410        let mut pos = sys::ImVec2 { x: 0.0, y: 0.0 };
411        unsafe { sys::igGetMousePosOnOpeningCurrentPopup(&mut pos) };
412        [pos.x, pos.y]
413    }
414
415    /// Check if mouse is hovering given rectangle
416    #[doc(alias = "IsMouseHoveringRect")]
417    pub fn is_mouse_hovering_rect(&self, r_min: [f32; 2], r_max: [f32; 2]) -> bool {
418        unsafe {
419            sys::igIsMouseHoveringRect(
420                sys::ImVec2::new(r_min[0], r_min[1]),
421                sys::ImVec2::new(r_max[0], r_max[1]),
422                true,
423            )
424        }
425    }
426
427    /// Check if mouse is hovering given rectangle (with clipping test)
428    #[doc(alias = "IsMouseHoveringRect")]
429    pub fn is_mouse_hovering_rect_with_clip(
430        &self,
431        r_min: [f32; 2],
432        r_max: [f32; 2],
433        clip: bool,
434    ) -> bool {
435        unsafe {
436            sys::igIsMouseHoveringRect(
437                sys::ImVec2::new(r_min[0], r_min[1]),
438                sys::ImVec2::new(r_max[0], r_max[1]),
439                clip,
440            )
441        }
442    }
443
444    /// Check if mouse is dragging
445    #[doc(alias = "IsMouseDragging")]
446    pub fn is_mouse_dragging(&self, button: MouseButton) -> bool {
447        unsafe { sys::igIsMouseDragging(button as i32, -1.0) }
448    }
449
450    /// Check if mouse is dragging with threshold
451    #[doc(alias = "IsMouseDragging")]
452    pub fn is_mouse_dragging_with_threshold(
453        &self,
454        button: MouseButton,
455        lock_threshold: f32,
456    ) -> bool {
457        unsafe { sys::igIsMouseDragging(button as i32, lock_threshold) }
458    }
459
460    /// Get mouse drag delta
461    #[doc(alias = "GetMouseDragDelta")]
462    pub fn mouse_drag_delta(&self, button: MouseButton) -> [f32; 2] {
463        let mut delta = sys::ImVec2 { x: 0.0, y: 0.0 };
464        unsafe { sys::igGetMouseDragDelta(&mut delta, button as i32, -1.0) };
465        [delta.x, delta.y]
466    }
467
468    /// Get mouse drag delta with threshold
469    #[doc(alias = "GetMouseDragDelta")]
470    pub fn mouse_drag_delta_with_threshold(
471        &self,
472        button: MouseButton,
473        lock_threshold: f32,
474    ) -> [f32; 2] {
475        let mut delta = sys::ImVec2 { x: 0.0, y: 0.0 };
476        unsafe { sys::igGetMouseDragDelta(&mut delta, button as i32, lock_threshold) };
477        [delta.x, delta.y]
478    }
479
480    /// Reset mouse drag delta for a specific button
481    #[doc(alias = "ResetMouseDragDelta")]
482    pub fn reset_mouse_drag_delta(&self, button: MouseButton) {
483        unsafe { sys::igResetMouseDragDelta(button as i32) }
484    }
485}