Add ColorButton

This commit is contained in:
Joonas Javanainen 2017-11-04 12:14:47 +02:00
parent 129306c5ba
commit 97fa401fa4
No known key found for this signature in database
GPG Key ID: D39CCA5CB19B9179
3 changed files with 91 additions and 3 deletions

View File

@ -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

View File

@ -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<S: Into<ImVec2>>(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) }
}
}

View File

@ -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<ImVec4>>(
&self,
desc_id: &'p ImStr,
color: C,
) -> ColorButton<'ui, 'p> {
ColorButton::new(self, desc_id, color.into())
}
}
// Widgets: Trees