From fc16cdb370f6004e5a2135ec55cd7a4c0c8b5f9c Mon Sep 17 00:00:00 2001 From: dustinhartlyn <79132671+dustinhartlyn@users.noreply.github.com> Date: Mon, 28 Feb 2022 11:22:35 -0800 Subject: [PATCH] Fix for zoom in/out error (#1221) * minor fix open/save dialogue on windows On windows 10 the open/save dialogue box has an minor error, and I believe I fixed it. When "Open" is selected from the menu, the title of the dialogue box says "SolveSpace - Save File" and the entered file name is "united". My fix correctly titles the dialoged box, and leaves the address bar blank when a file is being opened because "united" is only needed as a default name when a file being saved. I found that class FileDialogImplWin32 from guiwin.cpp contains two if statements for "isSaveDialog". This is redundant. I removed the first where the title was originally set, but not working. I then set the title in the second if statement and moved the 'if isEmpty'' to this section. * Update guiwin.cpp replaced tabs with spaces * Created ZoomToMouse function in graphicswin.cpp which referances the mouse position directly. Simplified MouseScroll in mouse.cpp to point to this function instead of altering zoom directly. Also pointed zoom commpand from keyboard and menu to ZoomToMouse so that it works avoids different behavior. * clean up some comments --- src/graphicswin.cpp | 39 +++++++++++++++++++++++++++++++++++---- src/mouse.cpp | 29 ++++------------------------- src/ui.h | 3 ++- 3 files changed, 41 insertions(+), 30 deletions(-) diff --git a/src/graphicswin.cpp b/src/graphicswin.cpp index c7c74d98..98ed54d6 100644 --- a/src/graphicswin.cpp +++ b/src/graphicswin.cpp @@ -712,16 +712,47 @@ double GraphicsWindow::ZoomToFit(const Camera &camera, return scale; } + +void GraphicsWindow::ZoomToMouse(double zoomMultiplyer) { + double offsetRight = offset.Dot(projRight); + double offsetUp = offset.Dot(projUp); + + double width, height; + window->GetContentSize(&width, &height); + + double righti = currentMousePosition.x / scale - offsetRight; + double upi = currentMousePosition.y / scale - offsetUp; + + // zoomMultiplyer of 1 gives a default zoom factor of 1.2x: zoomMultiplyer * 1.2 + // zoom = adjusted zoom negative zoomMultiplyer will zoom out, positive will zoom in + // + + scale *= exp(0.1823216 * zoomMultiplyer); // ln(1.2) = 0.1823216 + + double rightf = currentMousePosition.x / scale - offsetRight; + double upf = currentMousePosition.y / scale - offsetUp; + + offset = offset.Plus(projRight.ScaledBy(rightf - righti)); + offset = offset.Plus(projUp.ScaledBy(upf - upi)); + + if(SS.TW.shown.screen == TextWindow::Screen::EDIT_VIEW) { + if(havePainted) { + SS.ScheduleShowTW(); + } + } + havePainted = false; + Invalidate(); +} + + void GraphicsWindow::MenuView(Command id) { switch(id) { case Command::ZOOM_IN: - SS.GW.scale *= 1.2; - SS.ScheduleShowTW(); + SS.GW.ZoomToMouse(1); break; case Command::ZOOM_OUT: - SS.GW.scale /= 1.2; - SS.ScheduleShowTW(); + SS.GW.ZoomToMouse(-1); break; case Command::ZOOM_TO_FIT: diff --git a/src/mouse.cpp b/src/mouse.cpp index 5e7cde26..d183f58a 100644 --- a/src/mouse.cpp +++ b/src/mouse.cpp @@ -914,7 +914,7 @@ bool GraphicsWindow::MouseEvent(Platform::MouseEvent event) { break; case MouseEvent::Type::SCROLL_VERT: - this->MouseScroll(event.x, event.y, event.shiftDown ? event.scrollDelta / 10 : event.scrollDelta); + this->MouseScroll(event.shiftDown ? event.scrollDelta / 10 : event.scrollDelta); break; case MouseEvent::Type::LEAVE: @@ -1478,17 +1478,10 @@ void GraphicsWindow::EditControlDone(const std::string &s) { } } -void GraphicsWindow::MouseScroll(double x, double y, double delta) { - double offsetRight = offset.Dot(projRight); - double offsetUp = offset.Dot(projUp); - - double righti = x/scale - offsetRight; - double upi = y/scale - offsetUp; - - // The default zoom factor is 1.2x for one scroll wheel click (delta==1). +void GraphicsWindow::MouseScroll(double zoomMultiplyer) { // To support smooth scrolling where scroll wheel events come in increments // smaller (or larger) than 1 we do: - // scale *= exp(ln(1.2) * delta); + // scale *= exp(ln(1.2) * zoomMultiplyer); // to ensure that the same total scroll delta always results in the same // total zoom irrespective of in how many increments the zoom was applied. // For example if we scroll a total delta of a+b in two events vs. one then @@ -1496,21 +1489,7 @@ void GraphicsWindow::MouseScroll(double x, double y, double delta) { // while // scale * a * b != scale * (a+b) // So this constant is ln(1.2) = 0.1823216 to make the default zoom 1.2x - scale *= exp(0.1823216 * delta); - - double rightf = x/scale - offsetRight; - double upf = y/scale - offsetUp; - - offset = offset.Plus(projRight.ScaledBy(rightf - righti)); - offset = offset.Plus(projUp.ScaledBy(upf - upi)); - - if(SS.TW.shown.screen == TextWindow::Screen::EDIT_VIEW) { - if(havePainted) { - SS.ScheduleShowTW(); - } - } - havePainted = false; - Invalidate(); + ZoomToMouse(zoomMultiplyer); } void GraphicsWindow::MouseLeave() { diff --git a/src/ui.h b/src/ui.h index 0d121b64..c4ffff2b 100644 --- a/src/ui.h +++ b/src/ui.h @@ -622,6 +622,7 @@ public: void HandlePointForZoomToFit(Vector p, Point2d *pmax, Point2d *pmin, double *wmin, bool usePerspective, const Camera &camera); + void ZoomToMouse(double delta); void LoopOverPoints(const std::vector &entities, const std::vector &constraints, const std::vector &faces, @@ -842,7 +843,7 @@ public: void MouseLeftDoubleClick(double x, double y); void MouseMiddleOrRightDown(double x, double y); void MouseRightUp(double x, double y); - void MouseScroll(double x, double y, double delta); + void MouseScroll(double delta); void MouseLeave(); bool KeyboardEvent(Platform::KeyboardEvent event); void EditControlDone(const std::string &s);