Skip to main content

dear_imgui_rs/utils/
input.rs

1use super::counts::non_negative_count_from_i32;
2use super::validation::assert_finite_f32;
3use crate::input::{Key, MouseButton};
4use crate::sys;
5
6impl crate::ui::Ui {
7    /// Returns the number of times the key was pressed in the current frame
8    #[doc(alias = "GetKeyPressedAmount")]
9    pub fn get_key_pressed_amount(&self, key: Key, repeat_delay: f32, rate: f32) -> usize {
10        assert_finite_f32("Ui::get_key_pressed_amount()", "repeat_delay", repeat_delay);
11        assert_finite_f32("Ui::get_key_pressed_amount()", "rate", rate);
12        non_negative_count_from_i32("Ui::get_key_pressed_amount()", unsafe {
13            sys::igGetKeyPressedAmount(key.into(), repeat_delay, rate)
14        })
15    }
16
17    /// Returns the name of a key
18    #[doc(alias = "GetKeyName")]
19    pub fn get_key_name(&self, key: Key) -> &str {
20        unsafe {
21            let name_ptr = sys::igGetKeyName(key.into());
22            if name_ptr.is_null() {
23                return "Unknown";
24            }
25            let c_str = std::ffi::CStr::from_ptr(name_ptr);
26            c_str.to_str().unwrap_or("Unknown")
27        }
28    }
29
30    /// Returns the number of times the mouse button was clicked in the current frame
31    #[doc(alias = "GetMouseClickedCount")]
32    pub fn get_mouse_clicked_count(&self, button: MouseButton) -> usize {
33        non_negative_count_from_i32("Ui::get_mouse_clicked_count()", unsafe {
34            sys::igGetMouseClickedCount(button.into())
35        })
36    }
37
38    /// Returns the mouse position in screen coordinates
39    #[doc(alias = "GetMousePos")]
40    pub fn get_mouse_pos(&self) -> [f32; 2] {
41        let pos = unsafe { sys::igGetMousePos() };
42        [pos.x, pos.y]
43    }
44
45    /// Returns the mouse position when the button was clicked
46    #[doc(alias = "GetMousePosOnOpeningCurrentPopup")]
47    pub fn get_mouse_pos_on_opening_current_popup(&self) -> [f32; 2] {
48        let pos = unsafe { sys::igGetMousePosOnOpeningCurrentPopup() };
49        [pos.x, pos.y]
50    }
51
52    /// Returns the mouse drag delta
53    #[doc(alias = "GetMouseDragDelta")]
54    pub fn get_mouse_drag_delta(&self, button: MouseButton, lock_threshold: f32) -> [f32; 2] {
55        assert_finite_f32(
56            "Ui::get_mouse_drag_delta()",
57            "lock_threshold",
58            lock_threshold,
59        );
60        let delta = unsafe { sys::igGetMouseDragDelta(button.into(), lock_threshold) };
61        [delta.x, delta.y]
62    }
63
64    /// Returns the mouse wheel delta
65    #[doc(alias = "GetIO")]
66    pub fn get_mouse_wheel(&self) -> f32 {
67        self.io().mouse_wheel()
68    }
69
70    /// Returns the horizontal mouse wheel delta
71    #[doc(alias = "GetIO")]
72    pub fn get_mouse_wheel_h(&self) -> f32 {
73        self.io().mouse_wheel_h()
74    }
75
76    /// Returns `true` if any mouse button is down
77    #[doc(alias = "IsAnyMouseDown")]
78    pub fn is_any_mouse_down(&self) -> bool {
79        unsafe { sys::igIsAnyMouseDown() }
80    }
81}