From 6073a4ebb5fea563eef7259a64edaed2d628f79d Mon Sep 17 00:00:00 2001 From: Evan Pezent Date: Wed, 29 Apr 2020 09:55:07 -0500 Subject: [PATCH] make query ranges dragable --- implot.cpp | 77 +++++++++++++++++++++++++++++++++++++++++-------- implot.h | 2 +- implot_demo.cpp | 25 ++++++++++++++-- 3 files changed, 88 insertions(+), 16 deletions(-) diff --git a/implot.cpp b/implot.cpp index ffa5208..ff0849c 100644 --- a/implot.cpp +++ b/implot.cpp @@ -221,7 +221,7 @@ struct ImPlotAxis { /// Holds Plot state information that must persist between frames struct ImPlot { ImPlot() { - Selecting = Querying = Queried = false; + Selecting = Querying = Queried = DraggingQuery = false; SelectStart = QueryStart = ImVec2(0,0); Flags = ImPlotFlags_Default; ColorIdx = 0; @@ -234,7 +234,8 @@ struct ImPlot { bool Querying; bool Queried; ImVec2 QueryStart; - ImRect QueryRect; + ImRect QueryRect; // relative to BB_grid!! + bool DraggingQuery; ImPlotRange QueryRange; ImPlotAxis XAxis; ImPlotAxis YAxis; @@ -655,7 +656,57 @@ bool BeginPlot(const char* title, const char* x_label, const char* y_label, cons const bool hov_y_axis_region = yAxisRegion_bb.Contains(IO.MousePos); // legend hovered from last frame - const bool hov_legend = HasFlag(plot.Flags, ImPlotFlags_Legend) ? gp.Hov_Frame && plot.BB_Legend.Contains(IO.MousePos) : false; + const bool hov_legend = HasFlag(plot.Flags, ImPlotFlags_Legend) ? gp.Hov_Frame && plot.BB_Legend.Contains(IO.MousePos) : false; + + bool hov_query = false; + if (plot.Queried && !plot.Querying) { + ImRect bb_query; + if (HasFlag(plot.Flags, ImPlotFlags_PixelQuery)) { + bb_query = plot.QueryRect; + bb_query.Min += gp.BB_Grid.Min; + bb_query.Max += gp.BB_Grid.Min; + } + else { + gp.UpdateTransforms(); + ImVec2 p1 = gp.ToPixels(plot.QueryRange.XMin, plot.QueryRange.YMin); + ImVec2 p2 = gp.ToPixels(plot.QueryRange.XMax, plot.QueryRange.YMax); + bb_query.Min = ImVec2(ImMin(p1.x,p2.x), ImMin(p1.y,p2.y)); + bb_query.Max = ImVec2(ImMax(p1.x,p2.x), ImMax(p1.y,p2.y)); + } + hov_query = bb_query.Contains(IO.MousePos); + } + + // QUERY DRAG ------------------------------------------------------------- + if (plot.DraggingQuery && (IO.MouseReleased[0] || !IO.MouseDown[0])) { + plot.DraggingQuery = false; + } + if (plot.DraggingQuery) { + SetMouseCursor(ImGuiMouseCursor_ResizeAll); + if (!HasFlag(plot.Flags, ImPlotFlags_PixelQuery)) { + ImVec2 p1 = gp.ToPixels(plot.QueryRange.XMin, plot.QueryRange.YMin); + ImVec2 p2 = gp.ToPixels(plot.QueryRange.XMax, plot.QueryRange.YMax); + plot.QueryRect.Min = ImVec2(ImMin(p1.x,p2.x), ImMin(p1.y,p2.y)) + IO.MouseDelta; + plot.QueryRect.Max = ImVec2(ImMax(p1.x,p2.x), ImMax(p1.y,p2.y)) + IO.MouseDelta; + p1 = gp.FromPixels(plot.QueryRect.Min); + p2 = gp.FromPixels(plot.QueryRect.Max); + plot.QueryRect.Min -= gp.BB_Grid.Min; + plot.QueryRect.Max -= gp.BB_Grid.Min; + plot.QueryRange.XMin = ImMin(p1.x, p2.x); + plot.QueryRange.XMax = ImMax(p1.x, p2.x); + plot.QueryRange.YMin = ImMin(p1.y, p2.y); + plot.QueryRange.YMax = ImMax(p1.y, p2.y); + } + else { + plot.QueryRect.Min += IO.MouseDelta; + plot.QueryRect.Max += IO.MouseDelta; + } + } + if (gp.Hov_Frame && hov_query && !plot.DraggingQuery && !plot.Selecting && !hov_legend) { + SetMouseCursor(ImGuiMouseCursor_ResizeAll); + if (IO.MouseDown[0] && !plot.XAxis.Dragging && !plot.YAxis.Dragging) { + plot.DraggingQuery = true; + } + } // DRAG INPUT ------------------------------------------------------------- @@ -695,9 +746,9 @@ bool BeginPlot(const char* title, const char* x_label, const char* y_label, cons ImGui::SetMouseCursor(ImGuiMouseCursor_ResizeAll); } // start drag - if (gp.Hov_Frame && hov_x_axis_region && IO.MouseDragMaxDistanceSqr[0] > 5 && !plot.Selecting && !hov_legend) + if (gp.Hov_Frame && hov_x_axis_region && IO.MouseDragMaxDistanceSqr[0] > 5 && !plot.Selecting && !hov_legend && !hov_query && !plot.DraggingQuery) plot.XAxis.Dragging = true; - if (gp.Hov_Frame && hov_y_axis_region && IO.MouseDragMaxDistanceSqr[0] > 5 && !plot.Selecting && !hov_legend) + if (gp.Hov_Frame && hov_y_axis_region && IO.MouseDragMaxDistanceSqr[0] > 5 && !plot.Selecting && !hov_legend && !hov_query && !plot.DraggingQuery) plot.YAxis.Dragging = true; // SCROLL INPUT ----------------------------------------------------------- @@ -761,12 +812,14 @@ bool BeginPlot(const char* title, const char* x_label, const char* y_label, cons // update query if (plot.Querying) { gp.UpdateTransforms(); - plot.QueryRect.Min.x = IO.KeyAlt ? gp.BB_Grid.Min.x : ImMin(plot.QueryStart.x, IO.MousePos.x); - plot.QueryRect.Max.x = IO.KeyAlt ? gp.BB_Grid.Max.x : ImMax(plot.QueryStart.x, IO.MousePos.x); + plot.QueryRect.Min.x = IO.KeyAlt ? gp.BB_Grid.Min.x : ImMin(plot.QueryStart.x, IO.MousePos.x); + plot.QueryRect.Max.x = IO.KeyAlt ? gp.BB_Grid.Max.x : ImMax(plot.QueryStart.x, IO.MousePos.x); plot.QueryRect.Min.y = IO.KeyShift ? gp.BB_Grid.Min.y : ImMin(plot.QueryStart.y, IO.MousePos.y); plot.QueryRect.Max.y = IO.KeyShift ? gp.BB_Grid.Max.y : ImMax(plot.QueryStart.y, IO.MousePos.y); ImVec2 p1 = gp.FromPixels(plot.QueryRect.Min); ImVec2 p2 = gp.FromPixels(plot.QueryRect.Max); + plot.QueryRect.Min -= gp.BB_Grid.Min; + plot.QueryRect.Max -= gp.BB_Grid.Min; plot.QueryRange.XMin = ImMin(p1.x, p2.x); plot.QueryRange.XMax = ImMax(p1.x, p2.x); plot.QueryRange.YMin = ImMin(p1.y, p2.y); @@ -810,7 +863,7 @@ bool BeginPlot(const char* title, const char* x_label, const char* y_label, cons // DOUBLE CLICK ----------------------------------------------------------- - if ( IO.MouseDoubleClicked[0] && gp.Hov_Frame && gp.Hov_Grid && !hov_legend) + if ( IO.MouseDoubleClicked[0] && gp.Hov_Frame && gp.Hov_Grid && !hov_legend && !hov_query) gp.FitThisFrame = true; else gp.FitThisFrame = false; @@ -1078,8 +1131,8 @@ void EndPlot() { if (plot.Querying || (HasFlag(plot.Flags, ImPlotFlags_PixelQuery) && plot.Queried)) { if (plot.QueryRect.GetWidth() > 2 && plot.QueryRect.GetHeight() > 2) { - DrawList.AddRectFilled(plot.QueryRect.Min, plot.QueryRect.Max, gp.Col_QryBg); - DrawList.AddRect( plot.QueryRect.Min, plot.QueryRect.Max, gp.Col_QryBd); + DrawList.AddRectFilled(plot.QueryRect.Min + gp.BB_Grid.Min, plot.QueryRect.Max + gp.BB_Grid.Min, gp.Col_QryBg); + DrawList.AddRect( plot.QueryRect.Min + gp.BB_Grid.Min, plot.QueryRect.Max + gp.BB_Grid.Min, gp.Col_QryBd); } } else if (plot.Queried) { @@ -1273,8 +1326,8 @@ ImPlotRange GetPlotQuery() { ImPlot& plot = *gp.CurrentPlot; if (HasFlag(plot.Flags, ImPlotFlags_PixelQuery)) { gp.UpdateTransforms(); - ImVec2 p1 = gp.FromPixels(plot.QueryRect.Min); - ImVec2 p2 = gp.FromPixels(plot.QueryRect.Max); + ImVec2 p1 = gp.FromPixels(plot.QueryRect.Min + gp.BB_Grid.Min); + ImVec2 p2 = gp.FromPixels(plot.QueryRect.Max + gp.BB_Grid.Min); plot.QueryRange.XMin = ImMin(p1.x, p2.x); plot.QueryRange.XMax = ImMax(p1.x, p2.x); plot.QueryRange.YMin = ImMin(p1.y, p2.y); diff --git a/implot.h b/implot.h index ec8bcb3..a12c536 100644 --- a/implot.h +++ b/implot.h @@ -225,4 +225,4 @@ void PopPlotStyleVar(int count = 1); // Shows the ImPlot demo. Add implot_demo.cpp to your sources! void ShowImPlotDemoWindow(bool* p_open = NULL); -} // namespace ImGui +} // namespace ImGui \ No newline at end of file diff --git a/implot_demo.cpp b/implot_demo.cpp index af231ce..ee8a940 100644 --- a/implot_demo.cpp +++ b/implot_demo.cpp @@ -55,12 +55,21 @@ struct RollingData { } }; +// Put big data here +struct DemoData { + DemoData() { + + } +}; + } namespace ImGui { void ShowImPlotDemoWindow(bool* p_open) { + static DemoData data; + ImVec2 main_viewport_pos = ImGui::GetMainViewport()->Pos; ImGui::SetNextWindowPos(ImVec2(main_viewport_pos.x + 650, main_viewport_pos.y + 20), ImGuiCond_FirstUseEver); ImGui::SetNextWindowSize(ImVec2(550, 680), ImGuiCond_FirstUseEver); @@ -389,6 +398,7 @@ void ShowImPlotDemoWindow(bool* p_open) { //------------------------------------------------------------------------- if (ImGui::CollapsingHeader("Drag and Drop")) { + srand(10000000 * ImGui::GetTime()); static bool paused = false; static bool init = true; static ScrollingData data[10]; @@ -396,15 +406,17 @@ void ShowImPlotDemoWindow(bool* p_open) { if (init) { for (int i = 0; i < 10; ++i) { show[i] = false; - data[i].AddPoint(0, 0.25f + 0.5f * (float)rand() / float(RAND_MAX)); } init = false; } ImGui::BulletText("Drag data items from the left column onto the plot."); ImGui::BeginGroup(); if (ImGui::Button("Clear", {100, 0})) { - for (int i = 0; i < 10; ++i) + for (int i = 0; i < 10; ++i) { show[i] = false; + data[i].Data.shrink(0); + data[i].Offset = 0; + } } if (ImGui::Button(paused ? "Resume" : "Pause", {100,0})) paused = !paused; @@ -425,7 +437,10 @@ void ShowImPlotDemoWindow(bool* p_open) { if (!paused) { t += ImGui::GetIO().DeltaTime; for (int i = 0; i < 10; ++i) { - data[i].AddPoint(t, data[i].Data.back().y + (0.005f + 0.0002f * (float)rand() / float(RAND_MAX)) * (-1 + 2 * (float)rand() / float(RAND_MAX))); + if (show[i]) + data[i].AddPoint(t, data[i].Data.empty() ? + 0.25f + 0.5f * (float)rand() / float(RAND_MAX) : + data[i].Data.back().y + (0.005f + 0.0002f * (float)rand() / float(RAND_MAX)) * (-1 + 2 * (float)rand() / float(RAND_MAX))); } } ImGui::SetNextPlotRangeX(t - 10, t, paused ? ImGuiCond_Once : ImGuiCond_Always); @@ -479,6 +494,10 @@ void ShowImPlotDemoWindow(bool* p_open) { ImGui::RestorePlotPalette(); } //------------------------------------------------------------------------- + // if (ImGui::CollapsingHeader("Benchmark")) { + + // } + //------------------------------------------------------------------------- ImGui::End(); }