From f334bfb106a5308dd0f765a0cdf01f7f7872c482 Mon Sep 17 00:00:00 2001 From: Jack Mac Date: Sun, 20 Feb 2022 17:23:27 -0500 Subject: [PATCH] init --- imgui-examples/examples/color_button.rs | 36 +-- imgui-examples/examples/test_window_impl.rs | 33 +-- imgui/src/widget/color_editors.rs | 263 +++++++++++++++++--- 3 files changed, 267 insertions(+), 65 deletions(-) diff --git a/imgui-examples/examples/color_button.rs b/imgui-examples/examples/color_button.rs index b8f0484..12be394 100644 --- a/imgui-examples/examples/color_button.rs +++ b/imgui-examples/examples/color_button.rs @@ -47,27 +47,29 @@ fn example_1(ui: &Ui, state: &mut State) { ui.text(state.notify_text); ui.text("This button is black:"); - if ColorButton::new("Black color", [0.0, 0.0, 0.0, 1.0]).build(ui) { + if ui.color_button("Black color", [0.0, 0.0, 0.0, 1.0]) { state.notify_text = "*** Black button was clicked"; } ui.text("This button is red:"); - if ColorButton::new("Red color", [1.0, 0.0, 0.0, 1.0]).build(ui) { + if ui.color_button("Red color", [1.0, 0.0, 0.0, 1.0]) { state.notify_text = "*** Red button was clicked"; } ui.text("This button is BIG because it has a custom size:"); - if ColorButton::new("Green color", [0.0, 1.0, 0.0, 1.0]) + if ui + .color_button_config("Green color", [0.0, 1.0, 0.0, 1.0]) .size([100.0, 50.0]) - .build(ui) + .build() { state.notify_text = "*** BIG button was clicked"; } ui.text("This button doesn't use the tooltip at all:"); - if ColorButton::new("No tooltip", [0.0, 0.0, 1.0, 1.0]) + if ui + .color_button_config("No tooltip", [0.0, 0.0, 1.0, 1.0]) .tooltip(false) - .build(ui) + .build() { state.notify_text = "*** No tooltip button was clicked"; } @@ -87,9 +89,9 @@ fn example_2(ui: &Ui) { ); ui.text("This button ignores the alpha component:"); - ColorButton::new("Red color", [1.0, 0.0, 0.0, 0.5]) + ui.color_button_config("Red color", [1.0, 0.0, 0.0, 0.5]) .alpha(false) - .build(ui); + .build(); ui.spacing(); ui.spacing(); @@ -101,27 +103,27 @@ fn example_2(ui: &Ui) { ui.separator(); ui.text_wrapped("ColorPreview::Opaque (default) doesn't show the alpha component at all"); - ColorButton::new("Red + ColorPreview::Opaque", [1.0, 0.0, 0.0, 0.5]) + ui.color_button_config("Red + ColorPreview::Opaque", [1.0, 0.0, 0.0, 0.5]) .preview(ColorPreview::Opaque) - .build(ui); + .build(); ui.separator(); ui.text_wrapped( "ColorPreview::HalfAlpha divides the color area into two halves and uses a \ checkerboard pattern in one half to illustrate the alpha component", ); - ColorButton::new("Red + ColorPreview::HalfAlpha", [1.0, 0.0, 0.0, 0.5]) + ui.color_button_config("Red + ColorPreview::HalfAlpha", [1.0, 0.0, 0.0, 0.5]) .preview(ColorPreview::HalfAlpha) - .build(ui); + .build(); ui.separator(); ui.text_wrapped( "ColorPreview::Alpha uses a checkerboard pattern in the entire color area to \ illustrate the alpha component", ); - ColorButton::new("Red + ColorPreview::Alpha", [1.0, 0.0, 0.0, 0.5]) + ui.color_button_config("Red + ColorPreview::Alpha", [1.0, 0.0, 0.0, 0.5]) .preview(ColorPreview::Alpha) - .build(ui); + .build(); }); } @@ -132,13 +134,13 @@ fn example_3(ui: &Ui) { .position([20.0, 140.0], Condition::Appearing); w.build(|| { ui.text("This button interprets the input value [1.0, 0.0, 0.0, 1.0] as RGB(A) (default):"); - ColorButton::new("RGBA red", [1.0, 0.0, 0.0, 1.0]).build(ui); + ui.color_button("RGBA red", [1.0, 0.0, 0.0, 1.0]); ui.separator(); ui.text("This button interprets the input value [1.0, 0.0, 0.0, 1.0] as HSV(A):"); - ColorButton::new("HSVA black", [1.0, 0.0, 0.0, 1.0]) + ui.color_button_config("HSVA black", [1.0, 0.0, 0.0, 1.0]) .input_mode(ColorEditInputMode::HSV) - .build(ui); + .build(); }); } diff --git a/imgui-examples/examples/test_window_impl.rs b/imgui-examples/examples/test_window_impl.rs index aa204a7..c89f068 100644 --- a/imgui-examples/examples/test_window_impl.rs +++ b/imgui-examples/examples/test_window_impl.rs @@ -533,8 +533,9 @@ fn show_test_window(ui: &Ui, state: &mut State, opened: &mut bool) { Drag::new("drag float").range(-1.0, 1.0).speed(0.001).build(ui, &mut state.f0); ui.input_float3("input float3", &mut state.vec3f) .build(); - ColorEdit3::new("color 1", &mut state.col1).build(ui); - ColorEdit4::new("color 2", &mut state.col2).build(ui); + + ui.color_edit3("color 1", &mut state.col1); + ui.color_edit4("color 2", &mut state.col2); ui.input_scalar("input scalar i64", &mut state.u0).build(); ui.input_scalar("input scalar f64", &mut state.d0).build(); @@ -595,22 +596,22 @@ fn show_test_window(ui: &Ui, state: &mut State, opened: &mut bool) { "Click on the colored square to open a color picker. CTRL+click on individual component to input value.\n", ); - ColorEdit4::new("MyColor##1", &mut s.color) + ui.color_edit4_config("MyColor##1", &mut s.color) .flags(misc_flags) .alpha(false) - .build(ui); + .build(); ui.text("Color widget HSV with Alpha:"); - ColorEdit4::new("MyColor##2", &mut s.color) + ui.color_edit4_config("MyColor##2", &mut s.color) .flags(misc_flags) .input_mode(ColorEditInputMode::HSV) - .build(ui); + .build(); ui.text("Color widget with Float Display:"); - ColorEdit4::new("MyColor##2f", &mut s.color) + ui.color_edit4_config("MyColor##2f", &mut s.color) .flags(misc_flags) .format(ColorFormat::Float) - .build(ui); + .build(); ui.text("Color button with Picker:"); ui.same_line(); @@ -621,11 +622,11 @@ CTRL+click on individual component to input value.\n", With the label(false) function you can pass a non-empty label which \ will only be used for the tooltip and picker popup.", ); - ColorEdit4::new("MyColor##3", &mut s.color) + ui.color_edit4_config("MyColor##3", &mut s.color) .flags(misc_flags) .inputs(false) .label(false) - .build(ui); + .build(); ui.text("Color picker:"); ui.checkbox("With Alpha", &mut s.alpha); @@ -636,13 +637,13 @@ CTRL+click on individual component to input value.\n", ui.checkbox("With Ref Color", &mut s.ref_color); if s.ref_color { ui.same_line(); - ColorEdit4::new("##RefColor", &mut s.ref_color_v) + ui.color_edit4_config("##RefColor", &mut s.ref_color_v) .flags(misc_flags) .inputs(false) - .build(ui); + .build(); } } - let mut b = ColorPicker4::new + let mut b = ui.color_picker4_config ("MyColor##4", &mut s.color) .flags(misc_flags) .alpha(s.alpha) @@ -653,7 +654,7 @@ CTRL+click on individual component to input value.\n", if s.ref_color { b = b.reference_color(s.ref_color_v) } - b.build(ui); + b.build(); } } @@ -801,7 +802,7 @@ CTRL+click on individual component to input value.\n", let items = &["aaaa", "bbbb", "cccc", "dddd", "eeee"]; ui.combo_simple_string("Combo", &mut state.stacked_modals_item, items); - ColorEdit4::new("color", &mut state.stacked_modals_color).build(ui); + ui.color_edit4_config("color", &mut state.stacked_modals_color).build(); if ui.button("Add another modal..") { ui.open_popup("Stacked 2") ; @@ -970,7 +971,7 @@ fn show_example_app_custom_rendering(ui: &Ui, state: &mut CustomRenderingState, .build(|| { ui.text("Primitives"); // TODO: Add DragFloat to change value of sz - ColorEdit3::new("Color", &mut state.col).build(ui); + ui.color_edit3("Color", &mut state.col); let draw_list = ui.get_window_draw_list(); let p = ui.cursor_screen_pos(); let spacing = 8.0; diff --git a/imgui/src/widget/color_editors.rs b/imgui/src/widget/color_editors.rs index c99fe34..6b7a6cf 100644 --- a/imgui/src/widget/color_editors.rs +++ b/imgui/src/widget/color_editors.rs @@ -186,25 +186,27 @@ bitflags! { /// ``` #[derive(Debug)] #[must_use] -pub struct ColorEdit3<'a, T, C> { - label: T, +pub struct ColorEdit3<'ui, 'a, Label, C> { + label: Label, value: &'a mut C, flags: ColorEditFlags, + ui: &'ui Ui, } -impl<'a, T, C> ColorEdit3<'a, T, C> +impl<'ui, 'a, Label, C> ColorEdit3<'ui, 'a, Label, C> where - T: AsRef, + Label: AsRef, C: Copy + Into, MintVec3: Into + Into<[f32; 3]>, { /// Constructs a new color editor builder. - #[doc(alias = "ColorEdit3")] - pub fn new(label: T, value: &'a mut C) -> Self { + #[deprecated(since = "0.9.0", note = "Use `ui.color_edit3(...)` instead")] + pub fn new(ui: &'ui Ui, label: Label, value: &'a mut C) -> Self { ColorEdit3 { label, value, flags: ColorEditFlags::empty(), + ui, } } /// Replaces all current settings with the given flags. @@ -326,8 +328,7 @@ where /// Builds the color editor. /// /// Returns true if the color value was changed. - pub fn build(mut self, ui: &Ui) -> bool { - // if let EditableColor::Float3(_) = self.value { + pub fn build(mut self) -> bool { self.flags.insert(ColorEditFlags::NO_ALPHA); let as_vec3: MintVec3 = (*self.value).into(); @@ -335,7 +336,7 @@ where let changed = unsafe { sys::igColorEdit3( - ui.scratch_txt(self.label), + self.ui.scratch_txt(self.label), as_vec3.as_mut_ptr(), self.flags.bits() as _, ) @@ -352,6 +353,44 @@ where } } +impl Ui { + /// Edits a color of 3 channels. Use [color_edit3_config](Self::color_edit3_config) + /// for a builder to customize this widget. + pub fn color_edit3(&self, label: Label, value: &mut C) -> bool + where + Label: AsRef, + C: Copy + Into, + MintVec3: Into + Into<[f32; 3]>, + { + ColorEdit3 { + label, + value, + flags: ColorEditFlags::empty(), + ui: self, + } + .build() + } + + /// Constructs a new color editor builder. + pub fn color_edit3_config<'a, Label, C>( + &self, + label: Label, + value: &'a mut C, + ) -> ColorEdit3<'_, 'a, Label, C> + where + Label: AsRef, + C: Copy + Into, + MintVec3: Into + Into<[f32; 3]>, + { + ColorEdit3 { + label, + value, + flags: ColorEditFlags::empty(), + ui: self, + } + } +} + /// Builder for a color editor widget. /// /// # Examples @@ -368,25 +407,27 @@ where /// ``` #[derive(Debug)] #[must_use] -pub struct ColorEdit4<'a, T, C> { +pub struct ColorEdit4<'ui, 'a, T, C> { label: T, value: &'a mut C, flags: ColorEditFlags, + ui: &'ui Ui, } -impl<'a, T, C> ColorEdit4<'a, T, C> +impl<'ui, 'a, T, C> ColorEdit4<'ui, 'a, T, C> where T: AsRef, C: Copy + Into, MintVec4: Into + Into<[f32; 4]>, { /// Constructs a new color editor builder. - #[doc(alias = "ColorEdit4")] - pub fn new(label: T, value: &'a mut C) -> Self { + #[deprecated(since = "0.9.0", note = "Use `ui.color_edit4(...)` instead")] + pub fn new(ui: &'ui Ui, label: T, value: &'a mut C) -> Self { Self { label, value, flags: ColorEditFlags::empty(), + ui, } } /// Replaces all current settings with the given flags. @@ -508,13 +549,13 @@ where /// Builds the color editor. /// /// Returns true if the color value was changed. - pub fn build(self, ui: &Ui) -> bool { + pub fn build(self) -> bool { let as_vec4: MintVec4 = (*self.value).into(); let mut as_vec4: [f32; 4] = as_vec4.into(); let changed = unsafe { sys::igColorEdit4( - ui.scratch_txt(self.label), + self.ui.scratch_txt(self.label), as_vec4.as_mut_ptr(), self.flags.bits() as _, ) @@ -531,6 +572,44 @@ where } } +impl Ui { + /// Edits a color of 4 channels. Use [color_edit4_config](Self::color_edit4_config) + /// for a builder to customize this widget. + pub fn color_edit4(&self, label: Label, value: &mut C) -> bool + where + Label: AsRef, + C: Copy + Into, + MintVec4: Into + Into<[f32; 4]>, + { + ColorEdit4 { + label, + value, + flags: ColorEditFlags::empty(), + ui: self, + } + .build() + } + + /// Constructs a new color editor builder. + pub fn color_edit4_config<'a, Label, C>( + &self, + label: Label, + value: &'a mut C, + ) -> ColorEdit4<'_, 'a, Label, C> + where + Label: AsRef, + C: Copy + Into, + MintVec4: Into + Into<[f32; 4]>, + { + ColorEdit4 { + label, + value, + flags: ColorEditFlags::empty(), + ui: self, + } + } +} + /// Builder for a color picker widget. /// /// # Examples @@ -547,25 +626,27 @@ where /// ``` #[derive(Debug)] #[must_use] -pub struct ColorPicker3<'a, Label, Color> { +pub struct ColorPicker3<'ui, 'a, Label, Color> { label: Label, value: &'a mut Color, flags: ColorEditFlags, + ui: &'ui Ui, } -impl<'a, Label, Color> ColorPicker3<'a, Label, Color> +impl<'ui, 'a, Label, Color> ColorPicker3<'ui, 'a, Label, Color> where Label: AsRef, Color: Copy + Into, MintVec3: Into + Into<[f32; 3]>, { /// Constructs a new color picker builder. - #[doc(alias = "ColorPicker3")] - pub fn new(label: Label, value: &'a mut Color) -> Self { + #[deprecated(since = "0.9.0", note = "Use `ui.color_picker3(...)` instead")] + pub fn new(ui: &'ui Ui, label: Label, value: &'a mut Color) -> Self { ColorPicker3 { label, value, flags: ColorEditFlags::empty(), + ui, } } /// Replaces all current settings with the given flags. @@ -694,12 +775,12 @@ where /// Builds the color picker. /// /// Returns true if the color value was changed. - pub fn build(mut self, ui: &Ui) -> bool { + pub fn build(mut self) -> bool { self.flags.insert(ColorEditFlags::NO_ALPHA); let mut value: [f32; 3] = (*self.value).into().into(); let changed = unsafe { sys::igColorPicker3( - ui.scratch_txt(self.label), + self.ui.scratch_txt(self.label), value.as_mut_ptr(), self.flags.bits() as _, ) @@ -715,6 +796,44 @@ where } } +impl Ui { + /// Edits a color of 3 channels. Use [color_picker3](Self::color_picker3) + /// for a builder to customize this widget. + pub fn color_picker3(&self, label: Label, value: &mut C) -> bool + where + Label: AsRef, + C: Copy + Into, + MintVec3: Into + Into<[f32; 3]>, + { + ColorPicker3 { + label, + value, + flags: ColorEditFlags::empty(), + ui: self, + } + .build() + } + + /// Constructs a new color picker editor builder. + pub fn color_picker3_config<'a, Label, C>( + &self, + label: Label, + value: &'a mut C, + ) -> ColorPicker3<'_, 'a, Label, C> + where + Label: AsRef, + C: Copy + Into, + MintVec3: Into + Into<[f32; 3]>, + { + ColorPicker3 { + label, + value, + flags: ColorEditFlags::empty(), + ui: self, + } + } +} + // Builder for a color picker widget. /// /// # Examples @@ -731,27 +850,29 @@ where /// ``` #[derive(Debug)] #[must_use] -pub struct ColorPicker4<'a, Label, Color> { +pub struct ColorPicker4<'ui, 'a, Label, Color> { label: Label, value: &'a mut Color, flags: ColorEditFlags, ref_color: Option<[f32; 4]>, + ui: &'ui Ui, } -impl<'a, Label, Color> ColorPicker4<'a, Label, Color> +impl<'ui, 'a, Label, Color> ColorPicker4<'ui, 'a, Label, Color> where Label: AsRef, Color: Copy + Into, MintVec4: Into + Into<[f32; 4]>, { /// Constructs a new color picker builder. - #[doc(alias = "ColorPicker4")] - pub fn new(label: Label, value: &'a mut Color) -> Self { + #[deprecated(since = "0.9.0", note = "Use `ui.color_picker4(...)` instead")] + pub fn new(ui: &'ui Ui, label: Label, value: &'a mut Color) -> Self { Self { label, value, flags: ColorEditFlags::empty(), ref_color: None, + ui, } } /// Replaces all current settings with the given flags. @@ -886,13 +1007,13 @@ where /// Builds the color picker. /// /// Returns true if the color value was changed. - pub fn build(self, ui: &Ui) -> bool { + pub fn build(self) -> bool { let mut value: [f32; 4] = (*self.value).into().into(); let ref_color = self.ref_color.map(|c| c.as_ptr()).unwrap_or(ptr::null()); let changed = unsafe { sys::igColorPicker4( - ui.scratch_txt(self.label), + self.ui.scratch_txt(self.label), value.as_mut_ptr(), self.flags.bits() as _, ref_color, @@ -909,6 +1030,46 @@ where } } +impl Ui { + /// Edits a color of 4 channels. Use [color_picker4_config](Self::color_picker4_config) + /// for a builder to customize this widget. + pub fn color_picker4(&self, label: Label, value: &mut C) -> bool + where + Label: AsRef, + C: Copy + Into, + MintVec4: Into + Into<[f32; 4]>, + { + ColorPicker4 { + label, + value, + flags: ColorEditFlags::empty(), + ref_color: None, + ui: self, + } + .build() + } + + /// Constructs a new color picker editor builder. + pub fn color_picker4_config<'a, Label, C>( + &self, + label: Label, + value: &'a mut C, + ) -> ColorPicker4<'_, 'a, Label, C> + where + Label: AsRef, + C: Copy + Into, + MintVec4: Into + Into<[f32; 4]>, + { + ColorPicker4 { + label, + value, + flags: ColorEditFlags::empty(), + ref_color: None, + ui: self, + } + } +} + /// Builder for a color button widget. /// /// # Examples @@ -922,21 +1083,24 @@ where /// ``` #[derive(Copy, Clone, Debug)] #[must_use] -pub struct ColorButton { +pub struct ColorButton<'ui, T> { desc_id: T, color: [f32; 4], flags: ColorEditFlags, size: [f32; 2], + ui: &'ui Ui, } -impl> ColorButton { +impl<'ui, T: AsRef> ColorButton<'ui, T> { /// Constructs a new color button builder. - pub fn new(desc_id: T, color: impl Into) -> Self { + #[deprecated(since = "0.9.0", note = "Use `ui.color_button_config(...)` instead")] + pub fn new(ui: &'ui Ui, desc_id: T, color: impl Into) -> Self { ColorButton { desc_id, color: color.into().into(), flags: ColorEditFlags::empty(), size: [0.0, 0.0], + ui, } } /// Replaces all current settings with the given flags. @@ -1008,10 +1172,10 @@ impl> ColorButton { /// Builds the color button. /// /// Returns true if this color button was clicked. - pub fn build(self, ui: &Ui) -> bool { + pub fn build(self) -> bool { unsafe { sys::igColorButton( - ui.scratch_txt(self.desc_id), + self.ui.scratch_txt(self.desc_id), self.color.into(), self.flags.bits() as _, self.size.into(), @@ -1020,6 +1184,41 @@ impl> ColorButton { } } +impl Ui { + /// Builds a [ColorButton]. + /// + /// Returns true if this color button was clicked. + pub fn color_button>( + &self, + desc_id: Label, + color: impl Into, + ) -> bool { + ColorButton { + desc_id, + color: color.into().into(), + flags: ColorEditFlags::empty(), + size: [0.0, 0.0], + ui: self, + } + .build() + } + + /// Returns a [ColorButton] builder. + pub fn color_button_config>( + &self, + desc_id: Label, + color: impl Into, + ) -> ColorButton<'_, Label> { + ColorButton { + desc_id, + color: color.into().into(), + flags: ColorEditFlags::empty(), + size: [0.0, 0.0], + ui: self, + } + } +} + /// # Widgets: Color Editor/Picker impl Ui { /// Initializes current color editor/picker options (generally on application startup) if you