From 7fdc0dccb7c0ce34db026002d7a071abede83879 Mon Sep 17 00:00:00 2001 From: vasilenkoalexey Date: Sat, 6 Aug 2022 21:41:53 +0300 Subject: [PATCH 1/3] Prevent range changing during drag Fix small range changing during axis drag. This occurs because SetMin uses max value for validating range before it is set by calling SetMax. It is easy to observe with for example the following code const double plot_l = x_axis.PixelsToPlot(plot.PlotRect.Min.x - IO.MouseDelta.x); const double plot_r = x_axis.PixelsToPlot(plot.PlotRect.Max.x - IO.MouseDelta.x); const auto range = x_axis.Range.Size(); x_axis.SetMin(x_axis.IsInverted() ? plot_r : plot_l); x_axis.SetMax(x_axis.IsInverted() ? plot_l : plot_r); IM_ASSERT_USER_ERROR(range == x_axis.Range.Size(), "Range chnaged during drag!"); --- implot.cpp | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/implot.cpp b/implot.cpp index c1990384..72cdcc2a 100644 --- a/implot.cpp +++ b/implot.cpp @@ -1909,8 +1909,7 @@ bool UpdateInput(ImPlotPlot& plot) { if (IO.MouseDelta.x != 0 && !x_axis.IsPanLocked(increasing)) { const double plot_l = x_axis.PixelsToPlot(plot.PlotRect.Min.x - IO.MouseDelta.x); const double plot_r = x_axis.PixelsToPlot(plot.PlotRect.Max.x - IO.MouseDelta.x); - x_axis.SetMin(x_axis.IsInverted() ? plot_r : plot_l); - x_axis.SetMax(x_axis.IsInverted() ? plot_l : plot_r); + x_axis.SetRange(x_axis.IsInverted() ? plot_r : plot_l, x_axis.IsInverted() ? plot_l : plot_r); if (axis_equal && x_axis.OrthoAxis != NULL) x_axis.OrthoAxis->SetAspect(x_axis.GetAspect()); changed = true; @@ -1925,8 +1924,7 @@ bool UpdateInput(ImPlotPlot& plot) { if (IO.MouseDelta.y != 0 && !y_axis.IsPanLocked(increasing)) { const double plot_t = y_axis.PixelsToPlot(plot.PlotRect.Min.y - IO.MouseDelta.y); const double plot_b = y_axis.PixelsToPlot(plot.PlotRect.Max.y - IO.MouseDelta.y); - y_axis.SetMin(y_axis.IsInverted() ? plot_t : plot_b); - y_axis.SetMax(y_axis.IsInverted() ? plot_b : plot_t); + y_axis.SetRange(y_axis.IsInverted() ? plot_t : plot_b, y_axis.IsInverted() ? plot_b : plot_t); if (axis_equal && y_axis.OrthoAxis != NULL) y_axis.OrthoAxis->SetAspect(y_axis.GetAspect()); changed = true; @@ -1962,8 +1960,7 @@ bool UpdateInput(ImPlotPlot& plot) { float correction = (plot.Hovered && equal_zoom) ? 0.5f : 1.0f; const double plot_l = x_axis.PixelsToPlot(plot.PlotRect.Min.x - rect_size.x * tx * zoom_rate * correction); const double plot_r = x_axis.PixelsToPlot(plot.PlotRect.Max.x + rect_size.x * (1 - tx) * zoom_rate * correction); - x_axis.SetMin(x_axis.IsInverted() ? plot_r : plot_l); - x_axis.SetMax(x_axis.IsInverted() ? plot_l : plot_r); + x_axis.SetRange(x_axis.IsInverted() ? plot_r : plot_l, x_axis.IsInverted() ? plot_l : plot_r); if (axis_equal && x_axis.OrthoAxis != NULL) x_axis.OrthoAxis->SetAspect(x_axis.GetAspect()); changed = true; @@ -1977,8 +1974,7 @@ bool UpdateInput(ImPlotPlot& plot) { float correction = (plot.Hovered && equal_zoom) ? 0.5f : 1.0f; const double plot_t = y_axis.PixelsToPlot(plot.PlotRect.Min.y - rect_size.y * ty * zoom_rate * correction); const double plot_b = y_axis.PixelsToPlot(plot.PlotRect.Max.y + rect_size.y * (1 - ty) * zoom_rate * correction); - y_axis.SetMin(y_axis.IsInverted() ? plot_t : plot_b); - y_axis.SetMax(y_axis.IsInverted() ? plot_b : plot_t); + y_axis.SetRange(y_axis.IsInverted() ? plot_t : plot_b, y_axis.IsInverted() ? plot_b : plot_t); if (axis_equal && y_axis.OrthoAxis != NULL) y_axis.OrthoAxis->SetAspect(y_axis.GetAspect()); changed = true; @@ -1999,8 +1995,7 @@ bool UpdateInput(ImPlotPlot& plot) { if (!x_axis.IsInputLocked() && x_can_change) { const double p1 = x_axis.PixelsToPlot(plot.SelectStart.x); const double p2 = x_axis.PixelsToPlot(IO.MousePos.x); - x_axis.SetMin(ImMin(p1, p2)); - x_axis.SetMax(ImMax(p1, p2)); + x_axis.SetRange(ImMin(p1, p2), ImMax(p1, p2)); changed = true; } } @@ -2009,8 +2004,7 @@ bool UpdateInput(ImPlotPlot& plot) { if (!y_axis.IsInputLocked() && y_can_change) { const double p1 = y_axis.PixelsToPlot(plot.SelectStart.y); const double p2 = y_axis.PixelsToPlot(IO.MousePos.y); - y_axis.SetMin(ImMin(p1, p2)); - y_axis.SetMax(ImMax(p1, p2)); + y_axis.SetRange(ImMin(p1, p2), ImMax(p1, p2)); changed = true; } } From 131c5070fc7752d1724230ff15636ec5a0eaeb65 Mon Sep 17 00:00:00 2001 From: vasilenkoalexey Date: Wed, 14 Sep 2022 23:19:42 +0300 Subject: [PATCH 2/3] Fix scroll issue --- implot.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/implot.cpp b/implot.cpp index 72cdcc2a..a928538f 100644 --- a/implot.cpp +++ b/implot.cpp @@ -1960,7 +1960,8 @@ bool UpdateInput(ImPlotPlot& plot) { float correction = (plot.Hovered && equal_zoom) ? 0.5f : 1.0f; const double plot_l = x_axis.PixelsToPlot(plot.PlotRect.Min.x - rect_size.x * tx * zoom_rate * correction); const double plot_r = x_axis.PixelsToPlot(plot.PlotRect.Max.x + rect_size.x * (1 - tx) * zoom_rate * correction); - x_axis.SetRange(x_axis.IsInverted() ? plot_r : plot_l, x_axis.IsInverted() ? plot_l : plot_r); + if(x_axis.Range.Size() > x_axis.ConstraintZoom.Min || ImAbs(plot_l - plot_r) > x_axis.ConstraintZoom.Min) + x_axis.SetRange(x_axis.IsInverted() ? plot_r : plot_l, x_axis.IsInverted() ? plot_l : plot_r); if (axis_equal && x_axis.OrthoAxis != NULL) x_axis.OrthoAxis->SetAspect(x_axis.GetAspect()); changed = true; @@ -1974,7 +1975,8 @@ bool UpdateInput(ImPlotPlot& plot) { float correction = (plot.Hovered && equal_zoom) ? 0.5f : 1.0f; const double plot_t = y_axis.PixelsToPlot(plot.PlotRect.Min.y - rect_size.y * ty * zoom_rate * correction); const double plot_b = y_axis.PixelsToPlot(plot.PlotRect.Max.y + rect_size.y * (1 - ty) * zoom_rate * correction); - y_axis.SetRange(y_axis.IsInverted() ? plot_t : plot_b, y_axis.IsInverted() ? plot_b : plot_t); + if(y_axis.Range.Size() > y_axis.ConstraintZoom.Min || ImAbs(plot_t - plot_b) > y_axis.ConstraintZoom.Min) + y_axis.SetRange(y_axis.IsInverted() ? plot_t : plot_b, y_axis.IsInverted() ? plot_b : plot_t); if (axis_equal && y_axis.OrthoAxis != NULL) y_axis.OrthoAxis->SetAspect(y_axis.GetAspect()); changed = true; From 544fb08ff536c5ca625a5983ab266a6976ab4785 Mon Sep 17 00:00:00 2001 From: vasilenkoalexey Date: Sun, 18 Sep 2022 01:21:38 +0300 Subject: [PATCH 3/3] Revert back scroll and selection --- implot.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/implot.cpp b/implot.cpp index a928538f..465d226a 100644 --- a/implot.cpp +++ b/implot.cpp @@ -1909,7 +1909,7 @@ bool UpdateInput(ImPlotPlot& plot) { if (IO.MouseDelta.x != 0 && !x_axis.IsPanLocked(increasing)) { const double plot_l = x_axis.PixelsToPlot(plot.PlotRect.Min.x - IO.MouseDelta.x); const double plot_r = x_axis.PixelsToPlot(plot.PlotRect.Max.x - IO.MouseDelta.x); - x_axis.SetRange(x_axis.IsInverted() ? plot_r : plot_l, x_axis.IsInverted() ? plot_l : plot_r); + x_axis.SetRange(x_axis.IsInverted() ? plot_r : plot_l, x_axis.IsInverted() ? plot_l : plot_r); if (axis_equal && x_axis.OrthoAxis != NULL) x_axis.OrthoAxis->SetAspect(x_axis.GetAspect()); changed = true; @@ -1924,7 +1924,7 @@ bool UpdateInput(ImPlotPlot& plot) { if (IO.MouseDelta.y != 0 && !y_axis.IsPanLocked(increasing)) { const double plot_t = y_axis.PixelsToPlot(plot.PlotRect.Min.y - IO.MouseDelta.y); const double plot_b = y_axis.PixelsToPlot(plot.PlotRect.Max.y - IO.MouseDelta.y); - y_axis.SetRange(y_axis.IsInverted() ? plot_t : plot_b, y_axis.IsInverted() ? plot_b : plot_t); + y_axis.SetRange(y_axis.IsInverted() ? plot_t : plot_b, y_axis.IsInverted() ? plot_b : plot_t); if (axis_equal && y_axis.OrthoAxis != NULL) y_axis.OrthoAxis->SetAspect(y_axis.GetAspect()); changed = true; @@ -1960,8 +1960,8 @@ bool UpdateInput(ImPlotPlot& plot) { float correction = (plot.Hovered && equal_zoom) ? 0.5f : 1.0f; const double plot_l = x_axis.PixelsToPlot(plot.PlotRect.Min.x - rect_size.x * tx * zoom_rate * correction); const double plot_r = x_axis.PixelsToPlot(plot.PlotRect.Max.x + rect_size.x * (1 - tx) * zoom_rate * correction); - if(x_axis.Range.Size() > x_axis.ConstraintZoom.Min || ImAbs(plot_l - plot_r) > x_axis.ConstraintZoom.Min) - x_axis.SetRange(x_axis.IsInverted() ? plot_r : plot_l, x_axis.IsInverted() ? plot_l : plot_r); + x_axis.SetMin(x_axis.IsInverted() ? plot_r : plot_l); + x_axis.SetMax(x_axis.IsInverted() ? plot_l : plot_r); if (axis_equal && x_axis.OrthoAxis != NULL) x_axis.OrthoAxis->SetAspect(x_axis.GetAspect()); changed = true; @@ -1975,8 +1975,8 @@ bool UpdateInput(ImPlotPlot& plot) { float correction = (plot.Hovered && equal_zoom) ? 0.5f : 1.0f; const double plot_t = y_axis.PixelsToPlot(plot.PlotRect.Min.y - rect_size.y * ty * zoom_rate * correction); const double plot_b = y_axis.PixelsToPlot(plot.PlotRect.Max.y + rect_size.y * (1 - ty) * zoom_rate * correction); - if(y_axis.Range.Size() > y_axis.ConstraintZoom.Min || ImAbs(plot_t - plot_b) > y_axis.ConstraintZoom.Min) - y_axis.SetRange(y_axis.IsInverted() ? plot_t : plot_b, y_axis.IsInverted() ? plot_b : plot_t); + y_axis.SetMin(y_axis.IsInverted() ? plot_t : plot_b); + y_axis.SetMax(y_axis.IsInverted() ? plot_b : plot_t); if (axis_equal && y_axis.OrthoAxis != NULL) y_axis.OrthoAxis->SetAspect(y_axis.GetAspect()); changed = true; @@ -1997,7 +1997,8 @@ bool UpdateInput(ImPlotPlot& plot) { if (!x_axis.IsInputLocked() && x_can_change) { const double p1 = x_axis.PixelsToPlot(plot.SelectStart.x); const double p2 = x_axis.PixelsToPlot(IO.MousePos.x); - x_axis.SetRange(ImMin(p1, p2), ImMax(p1, p2)); + x_axis.SetMin(ImMin(p1, p2)); + x_axis.SetMax(ImMax(p1, p2)); changed = true; } } @@ -2006,7 +2007,8 @@ bool UpdateInput(ImPlotPlot& plot) { if (!y_axis.IsInputLocked() && y_can_change) { const double p1 = y_axis.PixelsToPlot(plot.SelectStart.y); const double p2 = y_axis.PixelsToPlot(IO.MousePos.y); - y_axis.SetRange(ImMin(p1, p2), ImMax(p1, p2)); + y_axis.SetMin(ImMin(p1, p2)); + y_axis.SetMax(ImMax(p1, p2)); changed = true; } }