From 8a3ccf0a44165f2e0e688beae41af49c9271251f Mon Sep 17 00:00:00 2001 From: epezent Date: Sun, 11 Oct 2020 00:38:18 -0500 Subject: [PATCH] remove BustItemCache from SetColormap and add ShowColormapSelector --- implot.cpp | 27 +++++++++++++++++++++++---- implot.h | 4 +++- implot_demo.cpp | 13 +------------ 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/implot.cpp b/implot.cpp index eb82d26..3d6149a 100644 --- a/implot.cpp +++ b/implot.cpp @@ -2953,9 +2953,6 @@ void SetColormap(ImPlotColormap colormap, int samples) { ResampleColormap(gp.Colormap, gp.ColormapSize, &resampled[0], samples); SetColormap(&resampled[0], samples); } - else { - BustItemCache(); - } } void SetColormap(const ImVec4* colors, int size) { @@ -2969,7 +2966,6 @@ void SetColormap(const ImVec4* colors, int size) { user_colormap.push_back(colors[i]); gp.Colormap = &user_colormap[0]; gp.ColormapSize = size; - BustItemCache(); } const ImVec4* GetColormap(ImPlotColormap colormap, int* size_out) { @@ -3240,6 +3236,24 @@ bool ShowStyleSelector(const char* label) return false; } +bool ShowColormapSelector(const char* label) { + bool set = false; + static const char* map = ImPlot::GetColormapName(ImPlotColormap_Default); + if (ImGui::BeginCombo(label, map)) { + for (int i = 0; i < ImPlotColormap_COUNT; ++i) { + const char* name = GetColormapName(i); + if (ImGui::Selectable(name, map == name)) { + map = name; + ImPlot::SetColormap(i); + ImPlot::BustItemCache(); + set = true; + } + } + ImGui::EndCombo(); + } + return set; +} + void ShowStyleEditor(ImPlotStyle* ref) { ImPlotContext& gp = *GImPlot; ImPlotStyle& style = GetStyle(); @@ -3409,6 +3423,7 @@ void ShowStyleEditor(ImPlotStyle* ref) { ImGui::PushStyleVar(ImGuiStyleVar_Alpha, 0.25f); if (ImGui::Button(GetColormapName(i), ImVec2(75,0))) { SetColormap(i); + BustItemCache(); custom_set = false; } if (!selected) @@ -3434,6 +3449,7 @@ void ShowStyleEditor(ImPlotStyle* ref) { ImGui::PushStyleVar(ImGuiStyleVar_Alpha, 0.25f); if (ImGui::Button("Custom", ImVec2(75, 0))) { SetColormap(&custom[0], custom.Size); + BustItemCache(); custom_set = true; } if (!custom_set_now) @@ -3442,6 +3458,7 @@ void ShowStyleEditor(ImPlotStyle* ref) { custom.push_back(ImVec4(0,0,0,1)); if (custom_set) { SetColormap(&custom[0], custom.Size); + BustItemCache(); } } ImGui::SameLine(); @@ -3449,6 +3466,7 @@ void ShowStyleEditor(ImPlotStyle* ref) { custom.pop_back(); if (custom_set) { SetColormap(&custom[0], custom.Size); + BustItemCache(); } } ImGui::EndGroup(); @@ -3458,6 +3476,7 @@ void ShowStyleEditor(ImPlotStyle* ref) { ImGui::PushID(c); if (ImGui::ColorEdit4("##Col1", &custom[c].x, ImGuiColorEditFlags_NoInputs) && custom_set) { SetColormap(&custom[0], custom.Size); + BustItemCache(); } if ((c + 1) % 12 != 0) ImGui::SameLine(); diff --git a/implot.h b/implot.h index 3424b27..b9d6aaf 100644 --- a/implot.h +++ b/implot.h @@ -464,7 +464,7 @@ IMPLOT_API ImPlotLimits GetPlotQuery(int y_axis = IMPLOT_AUTO); // Plot Tools //----------------------------------------------------------------------------- -// Shows an annotation callout at a chosen point. +// Shows an annotation callout at a chosen point. IMPLOT_API void Annotate(double x, double y, const ImVec2& pix_offset, const char* fmt, ...) IM_FMTARGS(4); IMPLOT_API void Annotate(double x, double y, const ImVec2& pix_offset, const ImVec4& color, const char* fmt, ...) IM_FMTARGS(5); // Same as above, but the annotation will always be clamped to stay inside the plot area. @@ -606,6 +606,8 @@ IMPLOT_API void PopPlotClipRect(); // Shows ImPlot style selector dropdown menu. IMPLOT_API bool ShowStyleSelector(const char* label); +// Shows ImPlot colormap selector dropdown menu. +IMPLOT_API bool ShowColormapSelector(const char* label); // Shows ImPlot style editor block (not a window). IMPLOT_API void ShowStyleEditor(ImPlotStyle* ref = NULL); // Add basic help/info block (not a window): how to manipulate ImPlot as an end-user. diff --git a/implot_demo.cpp b/implot_demo.cpp index 479f699..3a90fa4 100644 --- a/implot_demo.cpp +++ b/implot_demo.cpp @@ -216,18 +216,7 @@ void ShowDemoWindow(bool* p_open) { ImGui::ShowFontSelector("Font"); ImGui::ShowStyleSelector("ImGui Style"); ImPlot::ShowStyleSelector("ImPlot Style"); - - static const char* map = ImPlot::GetColormapName(ImPlotColormap_Default); - if (ImGui::BeginCombo("ImPlot Colormap", map)) { - for (int i = 0; i < ImPlotColormap_COUNT; ++i) { - const char* name = GetColormapName(i); - if (ImGui::Selectable(name, map == name)) { - map = name; - ImPlot::SetColormap(i); - } - } - ImGui::EndCombo(); - } + ImPlot::ShowColormapSelector("ImPlot Colormap"); float indent = ImGui::CalcItemWidth() - ImGui::GetFrameHeight(); ImGui::Indent(ImGui::CalcItemWidth() - ImGui::GetFrameHeight()); ImGui::Checkbox("Anti-Aliased Lines", &ImPlot::GetStyle().AntiAliasedLines);