arcdps_imgui\widget/
misc.rs

1use bitflags::bitflags;
2use std::ops::{BitAnd, BitAndAssign, BitOrAssign, Not};
3
4use crate::sys;
5use crate::{Direction, Ui};
6
7bitflags!(
8    /// Flags for invisible buttons
9    #[repr(transparent)]
10    pub struct ButtonFlags: u32 {
11        /// React on left mouse button
12        const MOUSE_BUTTON_LEFT = sys::ImGuiButtonFlags_MouseButtonLeft;
13        /// React on right mouse button
14        const MOUSE_BUTTON_RIGHT = sys::ImGuiButtonFlags_MouseButtonRight;
15        /// React on middle mouse button
16        const MOUSE_BUTTON_MIDDLE = sys::ImGuiButtonFlags_MouseButtonMiddle;
17    }
18);
19
20/// # Widgets: Miscellaneous
21impl<'ui> Ui<'ui> {
22    /// Renders a clickable button.
23    ///
24    /// Returns true if this button was clicked.
25    ///
26    /// This is the equivalent of [button_with_size](Self::button_with_size)
27    /// with `size` set to `[0.0, 0.0]`, which will size the button to the
28    /// label's width in the current style.
29    /// the current style.
30    #[doc(alias = "Button")]
31    pub fn button(&self, label: impl AsRef<str>) -> bool {
32        self.button_with_size(label, [0.0, 0.0])
33    }
34
35    /// Renders a clickable button.
36    ///
37    /// Returns true if this button was clicked.
38    ///
39    /// Setting `size` as `[0.0, 0.0]` will size the button to the label's width in
40    /// the current style.
41    #[doc(alias = "Button")]
42    pub fn button_with_size(&self, label: impl AsRef<str>, size: [f32; 2]) -> bool {
43        unsafe { sys::igButton(self.scratch_txt(label), size.into()) }
44    }
45    /// Renders a small clickable button that is easy to embed in text.
46    ///
47    /// Returns true if this button was clicked.
48    #[doc(alias = "SmallButton")]
49    pub fn small_button(&self, label: impl AsRef<str>) -> bool {
50        unsafe { sys::igSmallButton(self.scratch_txt(label)) }
51    }
52    /// Renders a widget with button behaviour without the visual look.
53    ///
54    /// Returns true if this button was clicked.
55    #[doc(alias = "InvisibleButton")]
56    pub fn invisible_button(&self, id: impl AsRef<str>, size: [f32; 2]) -> bool {
57        unsafe { sys::igInvisibleButton(self.scratch_txt(id), size.into(), 0) }
58    }
59    /// Renders a widget with button behaviour without the visual look.
60    ///
61    /// Returns true if this button was clicked.
62    #[doc(alias = "InvisibleButton")]
63    pub fn invisible_button_flags(
64        &self,
65        id: impl AsRef<str>,
66        size: [f32; 2],
67        flags: ButtonFlags,
68    ) -> bool {
69        unsafe { sys::igInvisibleButton(self.scratch_txt(id), size.into(), flags.bits() as i32) }
70    }
71    /// Renders a square button with an arrow shape.
72    ///
73    /// Returns true if this button was clicked.
74    #[doc(alias = "ArrowButton")]
75    pub fn arrow_button(&self, id: impl AsRef<str>, direction: Direction) -> bool {
76        unsafe { sys::igArrowButton(self.scratch_txt(id), direction as i32) }
77    }
78    /// Renders a simple checkbox.
79    ///
80    /// Returns true if this checkbox was clicked.
81    #[doc(alias = "Checkbox")]
82    pub fn checkbox(&self, label: impl AsRef<str>, value: &mut bool) -> bool {
83        unsafe { sys::igCheckbox(self.scratch_txt(label), value as *mut bool) }
84    }
85    /// Renders a checkbox suitable for toggling bit flags using a mask.
86    ///
87    /// Returns true if this checkbox was clicked.
88    pub fn checkbox_flags<T>(&self, label: impl AsRef<str>, flags: &mut T, mask: T) -> bool
89    where
90        T: Copy + PartialEq + BitOrAssign + BitAndAssign + BitAnd<Output = T> + Not<Output = T>,
91    {
92        let mut value = *flags & mask == mask;
93        let pressed = self.checkbox(label, &mut value);
94        if pressed {
95            if value {
96                *flags |= mask;
97            } else {
98                *flags &= !mask;
99            }
100        }
101        pressed
102    }
103    /// Renders a simple radio button.
104    ///
105    /// Returns true if this radio button was clicked.
106    #[doc(alias = "RadioButtonBool")]
107    pub fn radio_button_bool(&self, label: impl AsRef<str>, active: bool) -> bool {
108        unsafe { sys::igRadioButton_Bool(self.scratch_txt(label), active) }
109    }
110    /// Renders a radio button suitable for choosing an arbitrary value.
111    ///
112    /// Returns true if this radio button was clicked.
113    #[doc(alias = "RadioButtonBool")]
114    pub fn radio_button<T>(&self, label: impl AsRef<str>, value: &mut T, button_value: T) -> bool
115    where
116        T: Copy + PartialEq,
117    {
118        let pressed = self.radio_button_bool(label, *value == button_value);
119        if pressed {
120            *value = button_value;
121        }
122        pressed
123    }
124    /// Renders a small circle and keeps the cursor on the same line
125    #[doc(alias = "Bullet")]
126    pub fn bullet(&self) {
127        unsafe { sys::igBullet() };
128    }
129}