Restore color_edit functionality

This commit is contained in:
Joonas Javanainen 2017-11-02 22:53:32 +02:00
parent f54fae81f1
commit 3b87845d6b
No known key found for this signature in database
GPG Key ID: D39CCA5CB19B9179
4 changed files with 183 additions and 3 deletions

View File

@ -18,6 +18,8 @@
- Non-namespaced flags
- Various imgui-sys things that were deprecated in imgui/cimgui 1.51
- `Window::bg_alpha`. Push a color change with `with_color_var` instead
- `color_edit3`. Use `color_edit` instead
- `color_edit4`. Use `color_edit` instead
## [0.0.16] - 2017-10-26

View File

@ -37,6 +37,8 @@ struct State {
vec3f: [f32; 3],
vec2i: [i32; 2],
vec3i: [i32; 3],
col1: [f32; 3],
col2: [f32; 4],
selected_fish: Option<usize>,
auto_resize_state: AutoResizeState,
file_menu: FileMenuState,
@ -79,6 +81,8 @@ impl Default for State {
vec3f: [0.10, 0.20, 0.30],
vec2i: [10, 20],
vec3i: [10, 20, 30],
col1: [1.0, 0.0, 0.2],
col2: [0.4, 0.7, 0.0, 0.5],
selected_fish: None,
auto_resize_state: Default::default(),
file_menu: Default::default(),
@ -371,6 +375,10 @@ fn show_test_window<'a>(ui: &Ui<'a>, state: &mut State, opened: &mut bool) {
.build();
ui.input_float3(im_str!("input float3"), &mut state.vec3f)
.build();
ui.color_edit(im_str!("color 1"), &mut state.col1)
.build();
ui.color_edit(im_str!("color 2"), &mut state.col2)
.build();
ui.tree_node(im_str!("Multi-component Widgets")).build(|| {
ui.input_float2(im_str!("input float2"), &mut state.vec2f)

148
src/color_editors.rs Normal file
View File

@ -0,0 +1,148 @@
use imgui_sys;
use std::marker::PhantomData;
use {ImGuiColorEditFlags, ImStr, Ui};
#[derive(Debug)]
pub enum EditableColor<'p> {
Float3(&'p mut [f32; 3]),
Float4(&'p mut [f32; 4]),
}
impl<'p> From<&'p mut [f32; 3]> for EditableColor<'p> {
fn from(value: &'p mut [f32; 3]) -> EditableColor<'p> { EditableColor::Float3(value) }
}
impl<'p> From<&'p mut [f32; 4]> for EditableColor<'p> {
fn from(value: &'p mut [f32; 4]) -> EditableColor<'p> { EditableColor::Float4(value) }
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ColorEditMode {
RGB,
HSV,
HEX,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum EditableColorFormat {
U8,
Float,
}
#[must_use]
pub struct ColorEdit<'ui, 'p> {
label: &'p ImStr,
value: EditableColor<'p>,
flags: ImGuiColorEditFlags,
_phantom: PhantomData<&'ui Ui<'ui>>,
}
impl<'ui, 'p> ColorEdit<'ui, 'p> {
pub fn new(_: &Ui<'ui>, label: &'p ImStr, value: EditableColor<'p>) -> Self {
ColorEdit {
label,
value,
flags: ImGuiColorEditFlags::empty(),
_phantom: PhantomData,
}
}
#[inline]
pub fn flags(mut self, flags: ImGuiColorEditFlags) -> Self {
self.flags = flags;
self
}
#[inline]
pub fn alpha(mut self, value: bool) -> Self {
self.flags.set(ImGuiColorEditFlags::NoAlpha, !value);
self
}
#[inline]
pub fn picker(mut self, value: bool) -> Self {
self.flags.set(ImGuiColorEditFlags::NoPicker, !value);
self
}
#[inline]
pub fn options(mut self, value: bool) -> Self {
self.flags.set(ImGuiColorEditFlags::NoOptions, !value);
self
}
#[inline]
pub fn small_preview(mut self, value: bool) -> Self {
self.flags.set(ImGuiColorEditFlags::NoSmallPreview, !value);
self
}
#[inline]
pub fn inputs(mut self, value: bool) -> Self {
self.flags.set(ImGuiColorEditFlags::NoInputs, !value);
self
}
#[inline]
pub fn tooltip(mut self, value: bool) -> Self {
self.flags.set(ImGuiColorEditFlags::NoTooltip, !value);
self
}
#[inline]
pub fn label(mut self, value: bool) -> Self {
self.flags.set(ImGuiColorEditFlags::NoLabel, !value);
self
}
#[inline]
pub fn alpha_bar(mut self, value: bool) -> Self {
self.flags.set(ImGuiColorEditFlags::AlphaBar, value);
self
}
#[inline]
pub fn alpha_preview(mut self, value: bool) -> Self {
self.flags.set(ImGuiColorEditFlags::AlphaPreview, value);
self
}
#[inline]
pub fn alpha_preview_half(mut self, value: bool) -> Self {
self.flags.set(ImGuiColorEditFlags::AlphaPreviewHalf, value);
self
}
#[inline]
pub fn hdr(mut self, value: bool) -> Self {
self.flags.set(ImGuiColorEditFlags::HDR, value);
self
}
#[inline]
pub fn mode(mut self, mode: ColorEditMode) -> Self {
self.flags.set(
ImGuiColorEditFlags::RGB,
mode == ColorEditMode::RGB,
);
self.flags.set(
ImGuiColorEditFlags::HSV,
mode == ColorEditMode::HSV,
);
self.flags.set(
ImGuiColorEditFlags::HEX,
mode == ColorEditMode::HEX,
);
self
}
#[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
}
pub fn build(self) -> bool {
match self.value {
EditableColor::Float3(value) => unsafe {
imgui_sys::igColorEdit3(self.label.as_ptr(), value.as_mut_ptr(), self.flags)
},
EditableColor::Float4(value) => unsafe {
imgui_sys::igColorEdit4(self.label.as_ptr(), value.as_mut_ptr(), self.flags)
},
}
}
}

View File

@ -37,10 +37,11 @@ pub use imgui_sys::{ImGuiInputTextFlags_AllowTabInput, ImGuiInputTextFlags_Alway
ImGuiWindowFlags_NoScrollbar, ImGuiWindowFlags_NoTitleBar,
ImGuiWindowFlags_ShowBorders};
pub use imgui_sys::{ImDrawIdx, ImDrawVert, ImGuiInputTextFlags, ImGuiKey, ImGuiSelectableFlags,
ImGuiCond, ImGuiCol, ImGuiStyle, ImGuiTreeNodeFlags, ImGuiWindowFlags, ImVec2,
ImVec4};
pub use imgui_sys::{ImDrawIdx, ImDrawVert, ImGuiColorEditFlags, ImGuiInputTextFlags, ImGuiKey,
ImGuiSelectableFlags, ImGuiCond, ImGuiCol, ImGuiStyle, ImGuiTreeNodeFlags,
ImGuiWindowFlags, ImVec2, ImVec4};
pub use child_frame::ChildFrame;
pub use color_editors::{ColorEdit, ColorEditMode, EditableColor, EditableColorFormat};
pub use input::{InputFloat, InputFloat2, InputFloat3, InputFloat4, InputInt, InputInt2, InputInt3,
InputInt4, InputText};
pub use menus::{Menu, MenuItem};
@ -55,6 +56,7 @@ pub use trees::{CollapsingHeader, TreeNode};
pub use window::Window;
mod child_frame;
mod color_editors;
mod input;
mod menus;
mod plothistogram;
@ -635,6 +637,26 @@ impl<'ui> Ui<'ui> {
}
}
// Widgets: Color Editor/Picker
impl<'ui> Ui<'ui> {
pub fn color_edit<'p, V: Into<EditableColor<'p>>>(
&self,
label: &'p ImStr,
value: V,
) -> ColorEdit<'ui, 'p> {
ColorEdit::new(self, label, value.into())
}
#[deprecated(since = "0.0.17", note = "please use color_edit instead")]
pub fn color_edit3<'p>(&self, label: &'p ImStr, value: &'p mut [f32; 3]) -> ColorEdit<'ui, 'p> {
self.color_edit(label, value)
}
#[deprecated(since = "0.0.17", note = "please use color_edit instead")]
pub fn color_edit4<'p>(&self, label: &'p ImStr, value: &'p mut [f32; 4]) -> ColorEdit<'ui, 'p> {
self.color_edit(label, value)
}
}
// Widgets: Trees
impl<'ui> Ui<'ui> {
pub fn tree_node<'p>(&self, id: &'p ImStr) -> TreeNode<'ui, 'p> { TreeNode::new(self, id) }