ColorEdit4: support ImGuiColorEditFlags_NoSliders to display only a button (wip #346)

This commit is contained in:
ocornut 2016-02-21 23:55:01 +01:00
parent f8fede1d8b
commit a43a9e602a
2 changed files with 12 additions and 6 deletions

View File

@ -8525,15 +8525,20 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag
const float w_full = CalcItemWidth(); const float w_full = CalcItemWidth();
const float square_sz_with_spacing = (flags & ImGuiColorEditFlags_NoColorSquare) ? 0.0f : (g.FontSize + style.FramePadding.y * 2.0f + style.ItemInnerSpacing.x); const float square_sz_with_spacing = (flags & ImGuiColorEditFlags_NoColorSquare) ? 0.0f : (g.FontSize + style.FramePadding.y * 2.0f + style.ItemInnerSpacing.x);
// If no mode is specified default to RGB // If no mode is specified, defaults to RGB
if (!(flags & ImGuiColorEditFlags_ModeMask_)) if (!(flags & ImGuiColorEditFlags_ModeMask_))
flags |= ImGuiColorEditFlags_RGB; flags |= ImGuiColorEditFlags_RGB;
// If we're not showing any slider there's no point in querying color mode, nor showing the options menu, nor doing any HSV conversions
if (flags & ImGuiColorEditFlags_NoSliders)
flags = (flags & (~ImGuiColorEditFlags_ModeMask_)) | ImGuiColorEditFlags_RGB | ImGuiColorEditFlags_NoOptions;
// Read back edit mode from persistent storage // Read back edit mode from persistent storage
if (!(flags & ImGuiColorEditFlags_NoOptions)) if (!(flags & ImGuiColorEditFlags_NoOptions))
flags = (flags & (~ImGuiColorEditFlags_ModeMask_)) | (g.ColorEditModeStorage.GetInt(id, (flags & ImGuiColorEditFlags_ModeMask_)) & ImGuiColorEditFlags_ModeMask_); flags = (flags & (~ImGuiColorEditFlags_ModeMask_)) | (g.ColorEditModeStorage.GetInt(id, (flags & ImGuiColorEditFlags_ModeMask_)) & ImGuiColorEditFlags_ModeMask_);
IM_ASSERT(ImIsPowerOfTwo((int)(flags & ImGuiColorEditFlags_ModeMask_))); // Check that exactly one of RGB/HSV/HEX is set // Check that exactly one of RGB/HSV/HEX is set
IM_ASSERT(ImIsPowerOfTwo((int)(flags & ImGuiColorEditFlags_ModeMask_))); //
float f[4] = { col[0], col[1], col[2], col[3] }; float f[4] = { col[0], col[1], col[2], col[3] };
if (flags & ImGuiColorEditFlags_HSV) if (flags & ImGuiColorEditFlags_HSV)
@ -8548,7 +8553,7 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag
ImGui::BeginGroup(); ImGui::BeginGroup();
ImGui::PushID(label); ImGui::PushID(label);
if (flags & (ImGuiColorEditFlags_RGB | ImGuiColorEditFlags_HSV)) if ((flags & (ImGuiColorEditFlags_RGB | ImGuiColorEditFlags_HSV)) != 0 && (flags & ImGuiColorEditFlags_NoSliders) == 0)
{ {
// RGB/HSV 0..255 Sliders // RGB/HSV 0..255 Sliders
const float w_items_all = w_full - square_sz_with_spacing; const float w_items_all = w_full - square_sz_with_spacing;
@ -8577,7 +8582,7 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag
ImGui::PopItemWidth(); ImGui::PopItemWidth();
ImGui::PopItemWidth(); ImGui::PopItemWidth();
} }
else if (flags & ImGuiColorEditFlags_HEX) else if ((flags & ImGuiColorEditFlags_HEX) != 0 && (flags & ImGuiColorEditFlags_NoSliders) == 0)
{ {
// RGB Hexadecimal Input // RGB Hexadecimal Input
const float w_slider_all = w_full - square_sz_with_spacing; const float w_slider_all = w_full - square_sz_with_spacing;
@ -8607,6 +8612,7 @@ bool ImGui::ColorEdit4(const char* label, float col[4], ImGuiColorEditFlags flag
bool picker_active = false; bool picker_active = false;
if (!(flags & ImGuiColorEditFlags_NoColorSquare)) if (!(flags & ImGuiColorEditFlags_NoColorSquare))
{ {
if (!(flags & ImGuiColorEditFlags_NoSliders))
ImGui::SameLine(0, style.ItemInnerSpacing.x); ImGui::SameLine(0, style.ItemInnerSpacing.x);
const ImVec4 col_display(col[0], col[1], col[2], 1.0f); const ImVec4 col_display(col[0], col[1], col[2], 1.0f);

View File

@ -620,7 +620,7 @@ enum ImGuiColorEditFlags_
ImGuiColorEditFlags_NoPicker = 1 << 4, // ColorEdit: Disable picker when clicking on colored square ImGuiColorEditFlags_NoPicker = 1 << 4, // ColorEdit: Disable picker when clicking on colored square
ImGuiColorEditFlags_NoOptions = 1 << 5, // ColorEdit: Disable toggling options menu when right-clicking colored square ImGuiColorEditFlags_NoOptions = 1 << 5, // ColorEdit: Disable toggling options menu when right-clicking colored square
ImGuiColorEditFlags_NoColorSquare = 1 << 6, // ColorEdit: Disable colored square ImGuiColorEditFlags_NoColorSquare = 1 << 6, // ColorEdit: Disable colored square
ImGuiColorEditFlags_NoSliders = 1 << 7, // ColorPicker: Disable RGB/HSV/HEX sliders ImGuiColorEditFlags_NoSliders = 1 << 7, // ColorEdit: Disable sliders, show only a button. ColorPicker: Disable all RGB/HSV/HEX sliders.
ImGuiColorEditFlags_ModeMask_ = ImGuiColorEditFlags_RGB|ImGuiColorEditFlags_HSV|ImGuiColorEditFlags_HEX ImGuiColorEditFlags_ModeMask_ = ImGuiColorEditFlags_RGB|ImGuiColorEditFlags_HSV|ImGuiColorEditFlags_HEX
}; };