From 97fa401fa4b494fcbcd9f76833f7b2ab302bb37e Mon Sep 17 00:00:00 2001 From: Joonas Javanainen Date: Sat, 4 Nov 2017 12:14:47 +0200 Subject: [PATCH] Add ColorButton --- CHANGELOG.markdown | 1 + src/color_editors.rs | 81 +++++++++++++++++++++++++++++++++++++++++++- src/lib.rs | 12 +++++-- 3 files changed, 91 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.markdown b/CHANGELOG.markdown index 1a9e6d9..507f521 100644 --- a/CHANGELOG.markdown +++ b/CHANGELOG.markdown @@ -6,6 +6,7 @@ - Namespaced flags (e.g. `ImGuiWindowFlags`) - Color picker widget +- Color button widget - `imgui_sys` is now re-exported as `sys` in the main create ### Changed diff --git a/src/color_editors.rs b/src/color_editors.rs index 2854a56..0139a2b 100644 --- a/src/color_editors.rs +++ b/src/color_editors.rs @@ -3,7 +3,7 @@ use sys; use std::marker::PhantomData; use std::ptr; -use {ImGuiColorEditFlags, ImStr, Ui}; +use {ImGuiColorEditFlags, ImStr, ImVec2, ImVec4, Ui}; /// Mutable reference to an editable color value. #[derive(Debug)] @@ -360,3 +360,82 @@ impl<'ui, 'p> ColorPicker<'ui, 'p> { } } } + +/// Builder for a color button widget. +#[must_use] +pub struct ColorButton<'ui, 'p> { + desc_id: &'p ImStr, + color: ImVec4, + flags: ImGuiColorEditFlags, + size: ImVec2, + _phantom: PhantomData<&'ui Ui<'ui>>, +} + +impl<'ui, 'p> ColorButton<'ui, 'p> { + /// Constructs a new color button builder. + pub fn new(_: &Ui<'ui>, desc_id: &'p ImStr, color: ImVec4) -> Self { + ColorButton { + desc_id, + color, + flags: ImGuiColorEditFlags::empty(), + size: ImVec2::zero(), + _phantom: PhantomData, + } + } + /// Replaces all current settings with the given flags. + #[inline] + pub fn flags(mut self, flags: ImGuiColorEditFlags) -> Self { + self.flags = flags; + self + } + /// Enables/disables the use of the alpha component. + #[inline] + pub fn alpha(mut self, value: bool) -> Self { + self.flags.set(ImGuiColorEditFlags::NoAlpha, !value); + self + } + /// Enables/disables the tooltip that appears when hovering the preview. + #[inline] + pub fn tooltip(mut self, value: bool) -> Self { + self.flags.set(ImGuiColorEditFlags::NoTooltip, !value); + self + } + /// Sets the preview style. + #[inline] + pub fn preview(mut self, preview: EditableColorPreview) -> Self { + self.flags.set( + ImGuiColorEditFlags::AlphaPreviewHalf, + preview == EditableColorPreview::HalfAlpha, + ); + self.flags.set( + ImGuiColorEditFlags::AlphaPreview, + preview == EditableColorPreview::Alpha, + ); + self + } + /// Sets the formatting style of color components. + #[inline] + pub fn format(mut self, format: EditableColorFormat) -> Self { + self.flags.set( + ImGuiColorEditFlags::Uint8, + format == EditableColorFormat::U8, + ); + self.flags.set( + ImGuiColorEditFlags::Float, + format == EditableColorFormat::Float, + ); + self + } + /// Sets the button size. + /// + /// Use 0.0 for width and/or height to use the default size. + #[inline] + pub fn size>(mut self, size: S) -> Self { + self.size = size.into(); + self + } + /// Builds the color button. + pub fn build(self) -> bool { + unsafe { sys::igColorButton(self.desc_id.as_ptr(), self.color, self.flags, self.size) } + } +} diff --git a/src/lib.rs b/src/lib.rs index 32a5560..daf0a3e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -38,8 +38,8 @@ pub use sys::{ImDrawIdx, ImDrawVert, ImGuiColorEditFlags, ImGuiInputTextFlags, I ImGuiSelectableFlags, ImGuiCond, ImGuiCol, ImGuiStyle, ImGuiTreeNodeFlags, ImGuiWindowFlags, ImVec2, ImVec4}; pub use child_frame::ChildFrame; -pub use color_editors::{ColorEdit, ColorEditMode, ColorPicker, ColorPickerMode, EditableColor, - EditableColorFormat, EditableColorPreview}; +pub use color_editors::{ColorButton, ColorEdit, ColorEditMode, ColorPicker, ColorPickerMode, + EditableColor, EditableColorFormat, EditableColorPreview}; pub use input::{InputFloat, InputFloat2, InputFloat3, InputFloat4, InputInt, InputInt2, InputInt3, InputInt4, InputText}; pub use menus::{Menu, MenuItem}; @@ -662,6 +662,14 @@ impl<'ui> Ui<'ui> { ) -> ColorPicker<'ui, 'p> { ColorPicker::new(self, label, value.into()) } + /// Constructs a new color button builder. + pub fn color_button<'p, C: Into>( + &self, + desc_id: &'p ImStr, + color: C, + ) -> ColorButton<'ui, 'p> { + ColorButton::new(self, desc_id, color.into()) + } } // Widgets: Trees