From 02f0dbca37d316c533c6fa4cab0eb72551f8c776 Mon Sep 17 00:00:00 2001 From: ocornut Date: Thu, 15 Jan 2015 09:49:12 +0000 Subject: [PATCH] ColorEdit3: clicking on color square change edit-mode, removing color-edit mode button by default. --- imgui.cpp | 16 +++++++++++----- imgui.h | 3 ++- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index da660949..7d4944f6 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -5298,6 +5298,7 @@ bool ImGui::ColorButton(const ImVec4& col, bool small_height, bool outline_borde return false; const ImGuiStyle& style = g.Style; + const ImGuiID id = window->GetID("##colorbutton"); const float square_size = window->FontSize(); const ImGuiAabb bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(square_size + style.FramePadding.x*2, square_size + (small_height ? 0 : style.FramePadding.y*2))); ItemSize(bb); @@ -5305,8 +5306,8 @@ bool ImGui::ColorButton(const ImVec4& col, bool small_height, bool outline_borde if (ClipAdvance(bb)) return false; - const bool hovered = IsHovered(bb, 0); - const bool pressed = hovered && g.IO.MouseClicked[0]; + bool hovered, held; + bool pressed = ButtonBehaviour(bb, id, &hovered, &held, true); RenderFrame(bb.Min, bb.Max, window->Color(col), outline_border, style.FrameRounding); if (hovered) @@ -5350,7 +5351,7 @@ bool ImGui::ColorEdit4(const char* label, float col[4], bool alpha) const float square_sz = (window->FontSize() + style.FramePadding.x * 2.0f); ImGuiColorEditMode edit_mode = window->DC.ColorEditMode; - if (edit_mode == ImGuiColorEditMode_UserSelect) + if (edit_mode == ImGuiColorEditMode_UserSelect || edit_mode == ImGuiColorEditMode_UserSelectShowButton) edit_mode = g.ColorEditModeStorage.GetInt(id, 0) % 3; float fx = col[0]; @@ -5432,9 +5433,13 @@ bool ImGui::ColorEdit4(const char* label, float col[4], bool alpha) } ImGui::SameLine(0, (int)style.ItemInnerSpacing.x); - ImGui::ColorButton(col_display); + if (ImGui::ColorButton(col_display)) + { + // Don't set local copy of 'edit_mode' right away! + g.ColorEditModeStorage.SetInt(id, (edit_mode + 1) % 3); + } - if (window->DC.ColorEditMode == ImGuiColorEditMode_UserSelect) + if (window->DC.ColorEditMode == ImGuiColorEditMode_UserSelectShowButton) { ImGui::SameLine(0, (int)style.ItemInnerSpacing.x); const char* button_titles[3] = { "RGB", "HSV", "HEX" }; @@ -6930,6 +6935,7 @@ void ImGui::ShowStyleEditor(ImGuiStyle* ref) ImGui::RadioButton("HSV", &edit_mode, ImGuiColorEditMode_HSV); ImGui::SameLine(); ImGui::RadioButton("HEX", &edit_mode, ImGuiColorEditMode_HEX); + //ImGui::Text("Tip: Click on colored square to change edit mode."); static ImGuiTextFilter filter; filter.Draw("Filter colors", 200); diff --git a/imgui.h b/imgui.h index 849b0ada..1cc8fb4c 100644 --- a/imgui.h +++ b/imgui.h @@ -431,7 +431,8 @@ enum ImGuiStyleVar_ // Enumeration for ColorEditMode() enum ImGuiColorEditMode_ { - ImGuiColorEditMode_UserSelect = -1, + ImGuiColorEditMode_UserSelect = -2, + ImGuiColorEditMode_UserSelectShowButton = -1, ImGuiColorEditMode_RGB = 0, ImGuiColorEditMode_HSV = 1, ImGuiColorEditMode_HEX = 2