From 9e82be5bff952311693aeb193a3fec20627353c9 Mon Sep 17 00:00:00 2001 From: epezent Date: Tue, 1 Sep 2020 01:14:09 -0500 Subject: [PATCH] tidy up legend context menus --- implot.cpp | 6 +++--- implot.h | 5 ++--- implot_demo.cpp | 52 ++++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 48 insertions(+), 15 deletions(-) diff --git a/implot.cpp b/implot.cpp index 1617141..9f099d7 100644 --- a/implot.cpp +++ b/implot.cpp @@ -1892,7 +1892,7 @@ void EndLegendDragDropSource() { ImGui::EndDragDropSource(); } -bool BeginPopupContextLegend(const char* label_id, ImGuiMouseButton mouse_button) { +bool BeginLegendPopup(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; @@ -1901,13 +1901,13 @@ bool BeginPopupContextLegend(const char* label_id, ImGuiMouseButton mouse_button ImGuiID id = ImGui::GetID(label_id); if (ImGui::IsMouseReleased(mouse_button)) { ImPlotItem* item = gp.CurrentPlot->Items.GetByKey(id); - if (item && item->Highlight) + if (item && item->LegendHovered) ImGui::OpenPopupEx(id); } return ImGui::BeginPopupEx(id, ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoSavedSettings); } -void EndPopup() { +void EndLegendPopup() { ImGui::EndPopup(); } diff --git a/implot.h b/implot.h index c10c2e7..640a3fc 100644 --- a/implot.h +++ b/implot.h @@ -521,11 +521,10 @@ bool IsLegendEntryHovered(const char* label_id); bool BeginLegendDragDropSource(const char* label_id, ImGuiDragDropFlags flags = 0); // 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); +bool BeginLegendPopup(const char* label_id, ImGuiMouseButton mouse_button = 1); // End a popup for a legend entry. -void EndPopup(); +void EndLegendPopup(); //----------------------------------------------------------------------------- // Miscellaneous diff --git a/implot_demo.cpp b/implot_demo.cpp index e8f258d..86ff72e 100644 --- a/implot_demo.cpp +++ b/implot_demo.cpp @@ -237,19 +237,10 @@ 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(); } } @@ -1053,6 +1044,49 @@ void ShowDemoWindow(bool* p_open) { } } //------------------------------------------------------------------------- + if (ImGui::CollapsingHeader("Custom Context Menus")) { + ImGui::BulletText("You can implement legend context menus to inject per-item controls and widgets."); + ImGui::BulletText("Right click the legend label/icon to edit custom item attributes."); + + static float frequency = 0.1; + static float amplitude = 0.5f; + static ImVec4 color = ImVec4(1,1,0,1); + static bool line = false; + static float thickness = 1; + static bool markers = false; + + static t_float vals[101]; + for (int i = 0; i < 101; ++i) + vals[i] = amplitude * Sin(frequency * i); + + ImPlot::SetNextPlotLimits(0,100,-1,1); + if (ImPlot::BeginPlot("Right Click the Legend")) { + if (!line) { + ImPlot::SetNextFillStyle(color); + ImPlot::PlotBars("Right Click Me", vals, 101); + } + else { + if (markers) ImPlot::SetNextMarkerStyle(ImPlotMarker_Circle); + ImPlot::SetNextLineStyle(color, thickness); + ImPlot::PlotLine("Right Click Me", vals, 101); + } + // custom legend context menu + if (ImPlot::BeginLegendPopup("Right Click Me")) { + ImGui::DragFloat("Frequency",&frequency,0.01f,0,1); + ImGui::DragFloat("Amplitude",&litude,0.01f,0,1); + ImGui::Separator(); + ImGui::ColorEdit4("Color",&color.x); + ImGui::Checkbox("Line Graph", &line); + if (line) { + ImGui::DragFloat("Thickness", &thickness, 0.1f, 0, 5); + ImGui::Checkbox("Markers", &markers); + } + ImPlot::EndLegendPopup(); + } + ImPlot::EndPlot(); + } + } + //------------------------------------------------------------------------- if (ImGui::CollapsingHeader("Custom Plotters and Tooltips")) { ImGui::BulletText("You can create custom plotters or extend ImPlot using implot_internal.h."); double dates[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217};