From 8cbbfc241fc94489e4d0e71244bb5e66484a4131 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Mon, 31 Aug 2020 20:08:53 -0700 Subject: [PATCH] Add BeginPopupContextLegend and EndPopup EndPopup is just a wrapper around ImGui::EndPopup. BeginPopupContextLegend provides easy-to-use context menus for legend entries along the lines of ImGui::BeginPopupContextItem. --- implot.cpp | 19 +++++++++++++++++++ implot.h | 5 +++++ implot_demo.cpp | 9 +++++++++ 3 files changed, 33 insertions(+) diff --git a/implot.cpp b/implot.cpp index 48e0dbe8..16171417 100644 --- a/implot.cpp +++ b/implot.cpp @@ -1892,6 +1892,25 @@ void EndLegendDragDropSource() { ImGui::EndDragDropSource(); } +bool BeginPopupContextLegend(const char* label_id, ImGuiMouseButton mouse_button) { + ImPlotContext& gp = *GImPlot; + IM_ASSERT_USER_ERROR(gp.CurrentPlot != NULL, "BeginLegendPopup() needs to be called between BeginPlot() and EndPlot()!"); + ImGuiWindow* window = GImGui->CurrentWindow; + if (window->SkipItems) + return false; + ImGuiID id = ImGui::GetID(label_id); + if (ImGui::IsMouseReleased(mouse_button)) { + ImPlotItem* item = gp.CurrentPlot->Items.GetByKey(id); + if (item && item->Highlight) + ImGui::OpenPopupEx(id); + } + return ImGui::BeginPopupEx(id, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoSavedSettings); +} + +void EndPopup() { + ImGui::EndPopup(); +} + //----------------------------------------------------------------------------- // STYLING //----------------------------------------------------------------------------- diff --git a/implot.h b/implot.h index c41af896..c10c2e79 100644 --- a/implot.h +++ b/implot.h @@ -522,6 +522,11 @@ bool BeginLegendDragDropSource(const char* label_id, ImGuiDragDropFlags flags = // End legend drag and drop source. void EndLegendDragDropSource(); +// Begin a popup for a legend entry. +bool BeginPopupContextLegend(const char* label_id, ImGuiMouseButton mouse_button = 1); +// End a popup for a legend entry. +void EndPopup(); + //----------------------------------------------------------------------------- // Miscellaneous //----------------------------------------------------------------------------- diff --git a/implot_demo.cpp b/implot_demo.cpp index 1fd1dd73..e8f258dd 100644 --- a/implot_demo.cpp +++ b/implot_demo.cpp @@ -237,10 +237,19 @@ void ShowDemoWindow(bool* p_open) { ys2[i] = xs2[i] * xs2[i]; } ImGui::BulletText("Anti-aliasing can be enabled from the plot's context menu (see Help)."); + ImGui::BulletText("Right click on a legend item to bring up its context menu"); if (ImPlot::BeginPlot("Line Plot", "x", "f(x)")) { ImPlot::PlotLine("sin(x)", xs1, ys1, 1001); ImPlot::SetNextMarkerStyle(ImPlotMarker_Circle); ImPlot::PlotLine("x^2", xs2, ys2, 11); + if (ImPlot::BeginPopupContextLegend("sin(x)")) { + ImGui::Text("Context menu for sin(x)"); + ImPlot::EndPopup(); + } + if (ImPlot::BeginPopupContextLegend("x^2")) { + ImGui::Text("Context menu for x^2"); + ImPlot::EndPopup(); + } ImPlot::EndPlot(); } }