From 05278ca418b07dd0d829c39d21b6232dd48880c7 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Wed, 17 Apr 2024 23:28:10 +0200 Subject: [PATCH] Add undo for Multi Blend points moving #1980 --- .../Archetypes/Animation.MultiBlend.cs | 46 ++++++++++++++----- 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/Source/Editor/Surface/Archetypes/Animation.MultiBlend.cs b/Source/Editor/Surface/Archetypes/Animation.MultiBlend.cs index 1b7ec5fc1..89eda77b4 100644 --- a/Source/Editor/Surface/Archetypes/Animation.MultiBlend.cs +++ b/Source/Editor/Surface/Archetypes/Animation.MultiBlend.cs @@ -6,6 +6,7 @@ using System.IO; using FlaxEditor.GUI; using FlaxEditor.GUI.Input; using FlaxEditor.Scripting; +using FlaxEditor.Surface.Undo; using FlaxEngine; using FlaxEngine.GUI; @@ -18,6 +19,7 @@ namespace FlaxEditor.Surface.Archetypes [HideInEditor] public abstract class BlendPointsEditor : ContainerControl { + private readonly Animation.MultiBlend _node; private readonly bool _is2D; private Float2 _rangeX; private Float2 _rangeY; @@ -34,7 +36,8 @@ namespace FlaxEditor.Surface.Archetypes private static Matrix3x3 _transform = Matrix3x3.RotationZ(45.0f * Mathf.DegreesToRadians) * Matrix3x3.Translation2D(4.0f, 0.5f); private readonly BlendPointsEditor _editor; private readonly int _index; - private bool _isMouseDown; + private bool _isMouseDown, _mouseMoved; + private object[] _mouseMoveStartValues; /// /// The default size for the blend points. @@ -53,6 +56,18 @@ namespace FlaxEditor.Surface.Archetypes _index = index; } + private void EndMove() + { + _isMouseDown = false; + EndMouseCapture(); + if (_mouseMoveStartValues != null) + { + // Add undo action + _editor._node.Surface.AddBatchedUndoAction(new EditNodeValuesAction(_editor._node, _mouseMoveStartValues, true)); + _mouseMoveStartValues = null; + } + } + /// public override void OnGotFocus() { @@ -80,6 +95,8 @@ namespace FlaxEditor.Surface.Archetypes { Focus(); _isMouseDown = true; + _mouseMoved = false; + _mouseMoveStartValues = null; StartMouseCapture(); return true; } @@ -92,8 +109,7 @@ namespace FlaxEditor.Surface.Archetypes { if (button == MouseButton.Left && _isMouseDown) { - _isMouseDown = false; - EndMouseCapture(); + EndMove(); return true; } @@ -105,6 +121,13 @@ namespace FlaxEditor.Surface.Archetypes { if (_isMouseDown) { + if (!_mouseMoved) + { + // Capture initial state for undo + _mouseMoved = true; + _mouseMoveStartValues = _editor._node.Surface.Undo != null ? (object[])_editor._node.Values.Clone() : null; + } + _editor.SetLocation(_index, _editor.BlendPointPosToBlendSpacePos(Location + location)); } @@ -116,8 +139,7 @@ namespace FlaxEditor.Surface.Archetypes { if (_isMouseDown) { - _isMouseDown = false; - EndMouseCapture(); + EndMove(); } base.OnMouseLeave(); @@ -128,8 +150,7 @@ namespace FlaxEditor.Surface.Archetypes { if (_isMouseDown) { - _isMouseDown = false; - EndMouseCapture(); + EndMove(); } base.OnLostFocus(); @@ -152,14 +173,16 @@ namespace FlaxEditor.Surface.Archetypes /// /// Initializes a new instance of the class. /// + /// The node. /// The value indicating whether blend space is 2D, otherwise it is 1D. /// The X location. /// The Y location. /// The width. /// The height. - public BlendPointsEditor(bool is2D, float x, float y, float width, float height) + public BlendPointsEditor(Animation.MultiBlend node, bool is2D, float x, float y, float width, float height) : base(x, y, width, height) { + _node = node; _is2D = is2D; } @@ -330,11 +353,10 @@ namespace FlaxEditor.Surface.Archetypes Render2D.DrawLine(new Float2(1, y), new Float2(rect.Width - 2, y), gridColor); } - // Base base.Draw(); // Frame - Render2D.DrawRectangle(new Rectangle(1, 1, rect.Width - 2, rect.Height - 2), containsFocus ? style.ProgressNormal : style.BackgroundSelected); + Render2D.DrawRectangle(new Rectangle(1, 1, rect.Width - 2, rect.Height - 2), containsFocus ? style.BackgroundSelected : style.ForegroundDisabled); } } @@ -585,7 +607,7 @@ namespace FlaxEditor.Surface.Archetypes /// The width. /// The height. public Editor(MultiBlend1D node, float x, float y, float width, float height) - : base(false, x, y, width, height) + : base(node, false, x, y, width, height) { _node = node; } @@ -719,7 +741,7 @@ namespace FlaxEditor.Surface.Archetypes /// The width. /// The height. public Editor(MultiBlend2D node, float x, float y, float width, float height) - : base(true, x, y, width, height) + : base(node, true, x, y, width, height) { _node = node; }