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