Skip to main content

dear_imgui_rs/layout/
cursor.rs

1use super::validation::{assert_finite_f32, assert_finite_vec2};
2use crate::Ui;
3use crate::sys;
4
5impl Ui {
6    /// Returns the cursor position (in window coordinates)
7    #[doc(alias = "GetCursorPos")]
8    pub fn cursor_pos(&self) -> [f32; 2] {
9        let pos = unsafe { sys::igGetCursorPos() };
10        [pos.x, pos.y]
11    }
12
13    /// Returns the cursor position (in absolute screen coordinates)
14    #[doc(alias = "GetCursorScreenPos")]
15    pub fn cursor_screen_pos(&self) -> [f32; 2] {
16        let pos = unsafe { sys::igGetCursorScreenPos() };
17        [pos.x, pos.y]
18    }
19
20    /// Sets the cursor position (in window coordinates)
21    #[doc(alias = "SetCursorPos")]
22    pub fn set_cursor_pos(&self, pos: impl Into<[f32; 2]>) {
23        let pos_array = pos.into();
24        assert_finite_vec2("Ui::set_cursor_pos()", "position", pos_array);
25        let pos_vec = sys::ImVec2 {
26            x: pos_array[0],
27            y: pos_array[1],
28        };
29        unsafe { sys::igSetCursorPos(pos_vec) };
30    }
31
32    /// Sets the cursor position (in absolute screen coordinates)
33    #[doc(alias = "SetCursorScreenPos")]
34    pub fn set_cursor_screen_pos(&self, pos: impl Into<[f32; 2]>) {
35        let pos_array = pos.into();
36        assert_finite_vec2("Ui::set_cursor_screen_pos()", "position", pos_array);
37        let pos_vec = sys::ImVec2 {
38            x: pos_array[0],
39            y: pos_array[1],
40        };
41        unsafe { sys::igSetCursorScreenPos(pos_vec) };
42    }
43
44    /// Returns the X cursor position (in window coordinates)
45    #[doc(alias = "GetCursorPosX")]
46    pub fn cursor_pos_x(&self) -> f32 {
47        unsafe { sys::igGetCursorPosX() }
48    }
49
50    /// Returns the Y cursor position (in window coordinates)
51    #[doc(alias = "GetCursorPosY")]
52    pub fn cursor_pos_y(&self) -> f32 {
53        unsafe { sys::igGetCursorPosY() }
54    }
55
56    /// Sets the X cursor position (in window coordinates)
57    #[doc(alias = "SetCursorPosX")]
58    pub fn set_cursor_pos_x(&self, x: f32) {
59        assert_finite_f32("Ui::set_cursor_pos_x()", "x", x);
60        unsafe { sys::igSetCursorPosX(x) };
61    }
62
63    /// Sets the Y cursor position (in window coordinates)
64    #[doc(alias = "SetCursorPosY")]
65    pub fn set_cursor_pos_y(&self, y: f32) {
66        assert_finite_f32("Ui::set_cursor_pos_y()", "y", y);
67        unsafe { sys::igSetCursorPosY(y) };
68    }
69
70    /// Returns the initial cursor position (in window coordinates)
71    #[doc(alias = "GetCursorStartPos")]
72    pub fn cursor_start_pos(&self) -> [f32; 2] {
73        let pos = unsafe { sys::igGetCursorStartPos() };
74        [pos.x, pos.y]
75    }
76}