Skip to content

Commit b132f24

Browse files
committed
fix: zoom around cursor when axes are equal
1 parent 93c801b commit b132f24

1 file changed

Lines changed: 16 additions & 10 deletions

File tree

implot.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2026,20 +2026,22 @@ bool UpdateInput(ImPlotPlot& plot) {
20262026
float tx = ImRemap(IO.MousePos.x, plot.PlotRect.Min.x, plot.PlotRect.Max.x, 0.0f, 1.0f);
20272027
float ty = ImRemap(IO.MousePos.y, plot.PlotRect.Min.y, plot.PlotRect.Max.y, 0.0f, 1.0f);
20282028

2029+
// Track which axis to use as reference for equal aspect
2030+
ImPlotAxis* equal_ref_axis = nullptr;
2031+
20292032
for (int i = 0; i < IMPLOT_NUM_X_AXES; i++) {
20302033
ImPlotAxis& x_axis = plot.XAxis(i);
20312034
const bool equal_zoom = axis_equal && x_axis.OrthoAxis != nullptr;
20322035
const bool equal_locked = (equal_zoom != false) && x_axis.OrthoAxis->IsInputLocked();
20332036
if (x_hov[i] && !x_axis.IsInputLocked() && !equal_locked) {
20342037
ImGui::SetKeyOwner(ImGuiKey_MouseWheelY, plot.ID);
20352038
if (zoom_rate != 0.0f) {
2036-
float correction = (plot.Hovered && equal_zoom) ? 0.5f : 1.0f;
2037-
const double plot_l = x_axis.PixelsToPlot(plot.PlotRect.Min.x - rect_size.x * tx * zoom_rate * correction);
2038-
const double plot_r = x_axis.PixelsToPlot(plot.PlotRect.Max.x + rect_size.x * (1 - tx) * zoom_rate * correction);
2039+
const double plot_l = x_axis.PixelsToPlot(plot.PlotRect.Min.x - rect_size.x * tx * zoom_rate);
2040+
const double plot_r = x_axis.PixelsToPlot(plot.PlotRect.Max.x + rect_size.x * (1 - tx) * zoom_rate);
20392041
x_axis.SetMin(x_axis.IsInverted() ? plot_r : plot_l);
20402042
x_axis.SetMax(x_axis.IsInverted() ? plot_l : plot_r);
2041-
if (axis_equal && x_axis.OrthoAxis != nullptr)
2042-
x_axis.OrthoAxis->SetAspect(x_axis.GetAspect());
2043+
if (equal_zoom)
2044+
equal_ref_axis = &x_axis;
20432045
changed = true;
20442046
}
20452047
}
@@ -2051,17 +2053,21 @@ bool UpdateInput(ImPlotPlot& plot) {
20512053
if (y_hov[i] && !y_axis.IsInputLocked() && !equal_locked) {
20522054
ImGui::SetKeyOwner(ImGuiKey_MouseWheelY, plot.ID);
20532055
if (zoom_rate != 0.0f) {
2054-
float correction = (plot.Hovered && equal_zoom) ? 0.5f : 1.0f;
2055-
const double plot_t = y_axis.PixelsToPlot(plot.PlotRect.Min.y - rect_size.y * ty * zoom_rate * correction);
2056-
const double plot_b = y_axis.PixelsToPlot(plot.PlotRect.Max.y + rect_size.y * (1 - ty) * zoom_rate * correction);
2056+
const double plot_t = y_axis.PixelsToPlot(plot.PlotRect.Min.y - rect_size.y * ty * zoom_rate);
2057+
const double plot_b = y_axis.PixelsToPlot(plot.PlotRect.Max.y + rect_size.y * (1 - ty) * zoom_rate);
20572058
y_axis.SetMin(y_axis.IsInverted() ? plot_t : plot_b);
20582059
y_axis.SetMax(y_axis.IsInverted() ? plot_b : plot_t);
2059-
if (axis_equal && y_axis.OrthoAxis != nullptr)
2060-
y_axis.OrthoAxis->SetAspect(y_axis.GetAspect());
2060+
if (equal_zoom)
2061+
equal_ref_axis = &y_axis;
20612062
changed = true;
20622063
}
20632064
}
20642065
}
2066+
2067+
// Apply equal aspect constraint after zooming both axes
2068+
if (equal_ref_axis != nullptr && equal_ref_axis->OrthoAxis != nullptr) {
2069+
equal_ref_axis->OrthoAxis->SetAspect(equal_ref_axis->GetAspect());
2070+
}
20652071
}
20662072

20672073
// BOX-SELECTION ----------------------------------------------------------

0 commit comments

Comments
 (0)