From 9b43f2f03aef92e55f233101495d31dda605f07f Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Fri, 29 Nov 2024 14:33:46 +0100 Subject: [PATCH] Add zooming in curve editor relative to the mouse cursor and adapt to curve size --- Source/Editor/GUI/CurveEditor.Contents.cs | 18 +++++++++++++----- Source/Engine/UI/GUI/Panels/Panel.cs | 4 ++-- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/Source/Editor/GUI/CurveEditor.Contents.cs b/Source/Editor/GUI/CurveEditor.Contents.cs index bd055f590..ea40a4573 100644 --- a/Source/Editor/GUI/CurveEditor.Contents.cs +++ b/Source/Editor/GUI/CurveEditor.Contents.cs @@ -516,17 +516,25 @@ namespace FlaxEditor.GUI var zoomAlt = RootWindow.GetKey(KeyboardKeys.Shift); if (_editor.EnableZoom != UseMode.Off && IsMouseOver && !_leftMouseDown && (zoom || zoomAlt)) { - // TODO: preserve the view center point for easier zooming - var scale = new Float2(delta * 0.1f); - + // Cache mouse location in curve-space + var viewRect = _editor._mainPanel.GetClientArea(); + var locationInKeyframes = PointToKeyframes(location, ref viewRect); + var locationInEditorBefore = _editor.PointFromKeyframes(locationInKeyframes, ref viewRect); + // Scale relative to the curve size + var scale = new Float2(delta * 0.1f); _editor._mainPanel.GetDesireClientArea(out var mainPanelArea); var curveScale = mainPanelArea.Size / _editor._contents.Size; scale *= curveScale; - if (zoomAlt) scale.X = 0; // Scale Y axis only - _editor.ViewScale += GetUseModeMask(_editor.EnableZoom) * scale; + scale *= GetUseModeMask(_editor.EnableZoom); // Mask scale depending on allowed usage + _editor.ViewScale += scale; + + // Zoom towards the mouse position + var locationInEditorAfter = _editor.PointFromKeyframes(locationInKeyframes, ref viewRect); + var locationInEditorDelta = locationInEditorAfter - locationInEditorBefore; + _editor.ViewOffset -= locationInEditorDelta; return true; } diff --git a/Source/Engine/UI/GUI/Panels/Panel.cs b/Source/Engine/UI/GUI/Panels/Panel.cs index 0f802bd45..9e0391cd4 100644 --- a/Source/Engine/UI/GUI/Panels/Panel.cs +++ b/Source/Engine/UI/GUI/Panels/Panel.cs @@ -266,9 +266,9 @@ namespace FlaxEngine.GUI bool wasLocked = _isLayoutLocked; _isLayoutLocked = true; if (HScrollBar != null) - HScrollBar.Value = -value.X; + HScrollBar.TargetValue = -value.X; if (VScrollBar != null) - VScrollBar.Value = -value.Y; + VScrollBar.TargetValue = -value.Y; _isLayoutLocked = wasLocked; base.SetViewOffset(ref value);