arcdps_imgui/
utils.rs

1#![allow(clippy::float_cmp)]
2use bitflags::bitflags;
3
4use crate::input::mouse::MouseButton;
5use crate::style::StyleColor;
6use crate::sys;
7use crate::Ui;
8
9bitflags! {
10    /// Item hover check option flags
11    #[repr(transparent)]
12    pub struct ItemHoveredFlags: u32 {
13        /// Return true even if a popup window is blocking access to this item
14        const ALLOW_WHEN_BLOCKED_BY_POPUP = sys::ImGuiHoveredFlags_AllowWhenBlockedByPopup;
15        /// Return true even if an active item is blocking access to this item
16        const ALLOW_WHEN_BLOCKED_BY_ACTIVE_ITEM = sys::ImGuiHoveredFlags_AllowWhenBlockedByActiveItem;
17        /// Return true even if the position is obstructed or overlapped by another window
18        const ALLOW_WHEN_OVERLAPPED = sys::ImGuiHoveredFlags_AllowWhenOverlapped;
19        /// Return true even if the item is disabled
20        const ALLOW_WHEN_DISABLED = sys::ImGuiHoveredFlags_AllowWhenDisabled;
21        const RECT_ONLY = sys::ImGuiHoveredFlags_RectOnly;
22    }
23}
24
25/// # Item/widget utilities
26impl<'ui> Ui<'ui> {
27    /// Returns `true` if the last item is hovered
28    #[doc(alias = "IsItemHovered")]
29    pub fn is_item_hovered(&self) -> bool {
30        unsafe { sys::igIsItemHovered(0) }
31    }
32    /// Returns `true` if the last item is hovered based on the given flags
33    #[doc(alias = "IsItemHovered")]
34    pub fn is_item_hovered_with_flags(&self, flags: ItemHoveredFlags) -> bool {
35        unsafe { sys::igIsItemHovered(flags.bits() as i32) }
36    }
37    /// Returns `true` if the last item is active
38    #[doc(alias = "IsItemActive")]
39    pub fn is_item_active(&self) -> bool {
40        unsafe { sys::igIsItemActive() }
41    }
42    #[doc(alias = "IsItemFocused")]
43    /// Returns `true` if the last item is focused for keyboard/gamepad navigation
44    pub fn is_item_focused(&self) -> bool {
45        unsafe { sys::igIsItemFocused() }
46    }
47    /// Returns `true` if the last item is being clicked by `MouseButton::Left`.
48    ///
49    /// This is the same as [is_item_clicked_with_button](Self::is_item_clicked_with_button)
50    /// with `button` set to `MouseButton::Left`.
51    #[doc(alias = "IsItemClicked")]
52    pub fn is_item_clicked(&self) -> bool {
53        self.is_item_clicked_with_button(MouseButton::Left)
54    }
55
56    /// Returns `true` if the last item is being clicked
57    #[doc(alias = "IsItemClicked")]
58    pub fn is_item_clicked_with_button(&self, button: MouseButton) -> bool {
59        unsafe { sys::igIsItemClicked(button as i32) }
60    }
61    /// Returns `true` if the last item is visible
62    #[doc(alias = "IsItemVisible")]
63    pub fn is_item_visible(&self) -> bool {
64        unsafe { sys::igIsItemVisible() }
65    }
66    /// Returns `true` if the last item modified its underlying value this frame or was pressed
67    #[doc(alias = "IsItemEdited")]
68    pub fn is_item_edited(&self) -> bool {
69        unsafe { sys::igIsItemEdited() }
70    }
71    /// Returns `true` if the last item was just made active
72    #[doc(alias = "IsItemActivated")]
73    pub fn is_item_activated(&self) -> bool {
74        unsafe { sys::igIsItemActivated() }
75    }
76    /// Returns `true` if the last item was just made inactive
77    #[doc(alias = "IsItemDeactivated")]
78    pub fn is_item_deactivated(&self) -> bool {
79        unsafe { sys::igIsItemDeactivated() }
80    }
81    /// Returns `true` if the last item was just made inactive and made a value change when it was
82    #[doc(alias = "IsItemDeactivatedAfterEdit")]
83    /// active
84    pub fn is_item_deactivated_after_edit(&self) -> bool {
85        unsafe { sys::igIsItemDeactivatedAfterEdit() }
86    }
87    /// Returns `true` if the last item open state was toggled
88    #[doc(alias = "IsItemToggledOpen")]
89    pub fn is_item_toggled_open(&self) -> bool {
90        unsafe { sys::igIsItemToggledOpen() }
91    }
92    /// Returns `true` if any item is hovered
93    #[doc(alias = "IsAnyItemHovered")]
94    pub fn is_any_item_hovered(&self) -> bool {
95        unsafe { sys::igIsAnyItemHovered() }
96    }
97    /// Returns `true` if any item is active
98    #[doc(alias = "IsAnyItemActive")]
99    pub fn is_any_item_active(&self) -> bool {
100        unsafe { sys::igIsAnyItemActive() }
101    }
102    /// Returns `true` if any item is focused
103    #[doc(alias = "IsAnyItemFocused")]
104    pub fn is_any_item_focused(&self) -> bool {
105        unsafe { sys::igIsAnyItemFocused() }
106    }
107    /// Returns the upper-left bounding rectangle of the last item (in screen coordinates)
108    #[doc(alias = "GetItemRectMin")]
109    pub fn item_rect_min(&self) -> [f32; 2] {
110        let mut out = sys::ImVec2::zero();
111        unsafe { sys::igGetItemRectMin(&mut out) }
112        out.into()
113    }
114    /// Returns the lower-right bounding rectangle of the last item (in screen coordinates)
115    #[doc(alias = "GetItemRectMax")]
116    pub fn item_rect_max(&self) -> [f32; 2] {
117        let mut out = sys::ImVec2::zero();
118        unsafe { sys::igGetItemRectMax(&mut out) }
119        out.into()
120    }
121    /// Returns the size of the last item
122    #[doc(alias = "GetItemRectSize")]
123    pub fn item_rect_size(&self) -> [f32; 2] {
124        let mut out = sys::ImVec2::zero();
125        unsafe { sys::igGetItemRectSize(&mut out) }
126        out.into()
127    }
128    /// Allows the last item to be overlapped by a subsequent item.
129    ///
130    /// Both may be activated during the same frame before the later one takes priority.
131    #[doc(alias = "SetItemAllowOverlap")]
132    pub fn set_item_allow_overlap(&self) {
133        unsafe { sys::igSetItemAllowOverlap() };
134    }
135    /// Makes the last item the default focused item of the window
136    #[doc(alias = "SetItemDefaultFocus")]
137    pub fn set_item_default_focus(&self) {
138        unsafe { sys::igSetItemDefaultFocus() };
139    }
140}
141
142/// # Miscellaneous utilities
143impl<'ui> Ui<'ui> {
144    /// Returns `true` if the rectangle (of given size, starting from cursor position) is visible
145    #[doc(alias = "IsRectVisibleNil")]
146    pub fn is_cursor_rect_visible(&self, size: [f32; 2]) -> bool {
147        unsafe { sys::igIsRectVisible_Nil(size.into()) }
148    }
149    /// Returns `true` if the rectangle (in screen coordinates) is visible
150    #[doc(alias = "IsRectVisibleNilVec2")]
151    pub fn is_rect_visible(&self, rect_min: [f32; 2], rect_max: [f32; 2]) -> bool {
152        unsafe { sys::igIsRectVisible_Vec2(rect_min.into(), rect_max.into()) }
153    }
154    /// Returns the global imgui-rs time.
155    ///
156    /// Incremented by Io::delta_time every frame.
157    #[doc(alias = "GetTime")]
158    pub fn time(&self) -> f64 {
159        unsafe { sys::igGetTime() }
160    }
161    /// Returns the global imgui-rs frame count.
162    ///
163    /// Incremented by 1 every frame.
164    #[doc(alias = "GetFrameCount")]
165    pub fn frame_count(&self) -> i32 {
166        unsafe { sys::igGetFrameCount() }
167    }
168    /// Returns a single style color from the user interface style.
169    ///
170    /// Use this function if you need to access the colors, but don't want to clone the entire
171    /// style object.
172    #[doc(alias = "GetStyle")]
173    pub fn style_color(&self, style_color: StyleColor) -> [f32; 4] {
174        self.ctx.style()[style_color]
175    }
176}