From 3dbfcbd85d5af1298b42b0f7880f26a55002c686 Mon Sep 17 00:00:00 2001 From: Joonas Javanainen Date: Mon, 1 Jul 2019 00:04:13 +0300 Subject: [PATCH] Pull misc widgets from 0.1-dev --- src/lib.rs | 63 ------------------------------------ src/widget/misc.rs | 80 ++++++++++++++++++++++++++++++++++++++++++++++ src/widget/mod.rs | 1 + 3 files changed, 81 insertions(+), 63 deletions(-) create mode 100644 src/widget/misc.rs diff --git a/src/lib.rs b/src/lib.rs index f939a92..3b97869 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -369,29 +369,6 @@ impl<'ui> Ui<'ui> { } } -// Widgets -impl<'ui> Ui<'ui> { - pub fn bullet(&self) { - unsafe { - sys::igBullet(); - } - } - pub fn button(&self, label: &ImStr, size: [f32; 2]) -> bool { - unsafe { sys::igButton(label.as_ptr(), size.into()) } - } - pub fn small_button<'p>(&self, label: &'p ImStr) -> bool { - unsafe { sys::igSmallButton(label.as_ptr()) } - } - /// Make a invisible event. Can be used to conveniently catch events when - /// mouse hovers or click the area covered by this invisible button. - pub fn invisible_button(&self, label: &ImStr, size: [f32; 2]) -> bool { - unsafe { sys::igInvisibleButton(label.as_ptr(), size.into()) } - } - pub fn checkbox<'p>(&self, label: &'p ImStr, value: &'p mut bool) -> bool { - unsafe { sys::igCheckbox(label.as_ptr(), value) } - } -} - // Widgets: Input impl<'ui> Ui<'ui> { pub fn input_text<'p>(&self, label: &'p ImStr, buf: &'p mut ImString) -> InputText<'ui, 'p> { @@ -801,46 +778,6 @@ impl<'ui> Ui<'ui> { } } -// Widgets: Radio -impl<'ui> Ui<'ui> { - /// Creates a radio button for selecting an integer value. - /// Returns true if pressed. - /// - /// # Example - /// ```rust,no_run - /// # use imgui::*; - /// # let mut imgui = Context::create(); - /// # let ui = imgui.frame(); - /// # let mut selected_radio_value = 2; - /// ui.radio_button(im_str!("Item 1"), &mut selected_radio_value, 1); - /// ui.radio_button(im_str!("Item 2"), &mut selected_radio_value, 2); - /// ui.radio_button(im_str!("Item 3"), &mut selected_radio_value, 3); - /// ``` - pub fn radio_button<'p>(&self, label: &'p ImStr, value: &'p mut i32, wanted: i32) -> bool { - unsafe { sys::igRadioButtonIntPtr(label.as_ptr(), value, wanted) } - } - - /// Creates a radio button that shows as selected if the given value is true. - /// Returns true if pressed. - /// - /// # Example - /// ```rust,no_run - /// # use imgui::*; - /// # let mut imgui = Context::create(); - /// # let ui = imgui.frame(); - /// # let mut radio_button_test = "cats".to_string(); - /// if ui.radio_button_bool(im_str!("Cats"), radio_button_test == "cats") { - /// radio_button_test = "cats".to_string(); - /// } - /// if ui.radio_button_bool(im_str!("Dogs"), radio_button_test == "dogs") { - /// radio_button_test = "dogs".to_string(); - /// } - /// ``` - pub fn radio_button_bool<'p>(&self, label: &'p ImStr, value: bool) -> bool { - unsafe { sys::igRadioButtonBool(label.as_ptr(), value) } - } -} - impl<'ui> Ui<'ui> { pub fn plot_lines<'p>(&self, label: &'p ImStr, values: &'p [f32]) -> PlotLines<'ui, 'p> { PlotLines::new(self, label, values) diff --git a/src/widget/misc.rs b/src/widget/misc.rs new file mode 100644 index 0000000..3412137 --- /dev/null +++ b/src/widget/misc.rs @@ -0,0 +1,80 @@ +use std::ops::{BitAnd, BitAndAssign, BitOrAssign, Not}; + +use crate::string::ImStr; +use crate::sys; +use crate::{Direction, Ui}; + +/// # Widgets: Miscellaneous +impl<'ui> Ui<'ui> { + /// Renders a clickable button. + /// + /// Returns true if this button was clicked. + pub fn button(&self, label: &ImStr, size: [f32; 2]) -> bool { + unsafe { sys::igButton(label.as_ptr(), size.into()) } + } + /// Renders a small clickable button that is easy to embed in text. + /// + /// Returns true if this button was clicked. + pub fn small_button(&self, label: &ImStr) -> bool { + unsafe { sys::igSmallButton(label.as_ptr()) } + } + /// Renders a button behaviour without the visual look. + /// + /// Returns true if this button was clicked. + pub fn invisible_button(&self, id: &ImStr, size: [f32; 2]) -> bool { + unsafe { sys::igInvisibleButton(id.as_ptr(), size.into()) } + } + /// Renders a square button with an arrow shape. + /// + /// Returns true if this button was clicked. + pub fn arrow_button(&self, id: &ImStr, direction: Direction) -> bool { + unsafe { sys::igArrowButton(id.as_ptr(), direction as i32) } + } + /// Renders a simple checkbox. + /// + /// Returns true if this checkbox was clicked. + pub fn checkbox(&self, label: &ImStr, value: &mut bool) -> bool { + unsafe { sys::igCheckbox(label.as_ptr(), value as *mut bool) } + } + /// Renders a checkbox suitable for toggling bit flags using a mask. + /// + /// Returns true if this checkbox was clicked. + pub fn checkbox_flags(&self, label: &ImStr, flags: &mut T, mask: T) -> bool + where + T: Copy + PartialEq + BitOrAssign + BitAndAssign + BitAnd + Not, + { + let mut value = *flags & mask == mask; + let pressed = self.checkbox(label, &mut value); + if pressed { + if value { + *flags |= mask; + } else { + *flags &= !mask; + } + } + pressed + } + /// Renders a simple radio button. + /// + /// Returns true if this radio button was clicked. + pub fn radio_button_bool(&self, label: &ImStr, active: bool) -> bool { + unsafe { sys::igRadioButtonBool(label.as_ptr(), active) } + } + /// Renders a radio button suitable for choosing an arbitrary value. + /// + /// Returns true if this radio button was clicked. + pub fn radio_button(&self, label: &ImStr, value: &mut T, button_value: T) -> bool + where + T: Copy + PartialEq, + { + let pressed = self.radio_button_bool(label, *value == button_value); + if pressed { + *value = button_value; + } + pressed + } + /// Renders a small circle and keeps the cursor on the same line + pub fn bullet(&self) { + unsafe { sys::igBullet() }; + } +} diff --git a/src/widget/mod.rs b/src/widget/mod.rs index 481c63a..c827b6a 100644 --- a/src/widget/mod.rs +++ b/src/widget/mod.rs @@ -1 +1,2 @@ +pub mod misc; pub mod text;