From eefdd34f14cf649e1f1721f918d860b967fc1752 Mon Sep 17 00:00:00 2001 From: Gulam Nabi Azad Date: Tue, 2 Jul 2024 13:20:04 +0530 Subject: [PATCH 1/6] WI00737678---RemoveDragNodesBehavior --- .../Behaviors/DragMovablesBehavior.cs | 51 ++++++++++--------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/src/Blazor.Diagrams.Core/Behaviors/DragMovablesBehavior.cs b/src/Blazor.Diagrams.Core/Behaviors/DragMovablesBehavior.cs index e8418d02..c2d270f5 100644 --- a/src/Blazor.Diagrams.Core/Behaviors/DragMovablesBehavior.cs +++ b/src/Blazor.Diagrams.Core/Behaviors/DragMovablesBehavior.cs @@ -5,12 +5,17 @@ using Blazor.Diagrams.Core.Models.Base; using System; using System.Collections.Generic; - +using DiagramPoint = Blazor.Diagrams.Core.Geometry.Point; namespace Blazor.Diagrams.Core.Behaviors; public class DragMovablesBehavior : DragBehavior { - private readonly Dictionary _initialPositions; + public record NodeMoveablePositions(Point position) + { + public Dictionary ChildPositions { get; } = new(); + } + + protected readonly Dictionary _initialPositions; protected double? _lastClientX; protected double? _lastClientY; protected bool _moved; @@ -19,19 +24,22 @@ public class DragMovablesBehavior : DragBehavior public DragMovablesBehavior(Diagram diagram) : base(diagram) { - _initialPositions = new Dictionary(); + _initialPositions = new Dictionary(); Diagram.PanChanged += OnPanChanged; } protected override void OnPointerDown(Model? model, PointerEventArgs e) { - if (model is not MovableModel) + if (model is null) return; + ResetPan(); + _initialPositions.Clear(); + foreach (var sm in Diagram.GetSelectedModels()) { - if (sm is not MovableModel movable || movable.Locked) + if (sm is not NodeModel movable || movable.Locked) continue; // Special case: groups without auto size on @@ -45,7 +53,7 @@ protected override void OnPointerDown(Model? model, PointerEventArgs e) movable.Position.Y + (n.Size?.Height ?? 0) / 2); } - _initialPositions.Add(movable, position); + _initialPositions.Add(movable, new NodeMoveablePositions(position)); } _lastClientX = e.ClientX; @@ -58,7 +66,6 @@ protected override void OnPointerMove(Model? model, PointerEventArgs e) if (_initialPositions.Count == 0 || _lastClientX == null || _lastClientY == null) return; - _moved = true; var deltaX = (e.ClientX - _lastClientX.Value) / Diagram.Zoom; var deltaY = (e.ClientY - _lastClientY.Value) / Diagram.Zoom; @@ -67,29 +74,31 @@ protected override void OnPointerMove(Model? model, PointerEventArgs e) MoveNodes(_totalMovedX, _totalMovedY); + _moved = true; _lastClientX = e.ClientX; _lastClientY = e.ClientY; } + protected virtual void OnPanChanged(double deltaX, double deltaY) { if (_initialPositions.Count == 0 || _lastClientX == null || _lastClientY == null) return; - _moved = true; - - _totalMovedX += deltaX; - _totalMovedY += deltaY; + _totalMovedX += deltaX / Diagram.Zoom; + _totalMovedY += deltaY / Diagram.Zoom; MoveNodes(_totalMovedX, _totalMovedY); + + _moved = true; } protected virtual void MoveNodes(double deltaX, double deltaY) { foreach (var (movable, initialPosition) in _initialPositions) { - var ndx = ApplyGridSize(deltaX + initialPosition.X); - var ndy = ApplyGridSize(deltaY + initialPosition.Y); + var ndx = ApplyGridSize(deltaX + initialPosition.position.X); + var ndy = ApplyGridSize(deltaY + initialPosition.position.Y); if (Diagram.Options.GridSnapToCenter && movable is NodeModel node) { node.SetPosition(ndx - (node.Size?.Width ?? 0) / 2, ndy - (node.Size?.Height ?? 0) / 2); @@ -103,21 +112,12 @@ protected virtual void MoveNodes(double deltaX, double deltaY) protected override void OnPointerUp(Model? model, PointerEventArgs e) { - if (_initialPositions.Count == 0) - return; - - if (_moved) - { - foreach (var (movable, _) in _initialPositions) - { - movable.TriggerMoved(); - } - } _initialPositions.Clear(); _totalMovedX = 0; _totalMovedY = 0; _lastClientX = null; _lastClientY = null; + _moved = false; } private double ApplyGridSize(double n) @@ -129,6 +129,11 @@ private double ApplyGridSize(double n) return gridSize * Math.Floor((n + gridSize / 2.0) / gridSize); } + void ResetPan() + { + Diagram.SetPan(0, 0); + } + public override void Dispose() { _initialPositions.Clear(); From ac98c3d8f3e1f58fb2581fc5b32c8c2c687aab13 Mon Sep 17 00:00:00 2001 From: Gulam Nabi Azad Date: Tue, 2 Jul 2024 13:56:39 +0530 Subject: [PATCH 2/6] WI00737678---RemoveDragNodesBehavior---Update-1 --- .../Behaviors/DragMovablesBehavior.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/Blazor.Diagrams.Core/Behaviors/DragMovablesBehavior.cs b/src/Blazor.Diagrams.Core/Behaviors/DragMovablesBehavior.cs index c2d270f5..db49df57 100644 --- a/src/Blazor.Diagrams.Core/Behaviors/DragMovablesBehavior.cs +++ b/src/Blazor.Diagrams.Core/Behaviors/DragMovablesBehavior.cs @@ -112,6 +112,17 @@ protected virtual void MoveNodes(double deltaX, double deltaY) protected override void OnPointerUp(Model? model, PointerEventArgs e) { + if (_initialPositions.Count == 0) + return; + + if (_moved) + { + foreach (var (movable, _) in _initialPositions) + { + movable.TriggerMoved(); + } + } + _initialPositions.Clear(); _totalMovedX = 0; _totalMovedY = 0; From 579b8764f8e0655baecc02dac831383a5ce9cc64 Mon Sep 17 00:00:00 2001 From: Gulam Nabi Azad Date: Wed, 3 Jul 2024 17:05:29 +0530 Subject: [PATCH 3/6] WI00737678---RemoveDragNodesBehavior---Update-2 --- .../Behaviors/DragMovablesBehavior.cs | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/src/Blazor.Diagrams.Core/Behaviors/DragMovablesBehavior.cs b/src/Blazor.Diagrams.Core/Behaviors/DragMovablesBehavior.cs index db49df57..16769128 100644 --- a/src/Blazor.Diagrams.Core/Behaviors/DragMovablesBehavior.cs +++ b/src/Blazor.Diagrams.Core/Behaviors/DragMovablesBehavior.cs @@ -5,17 +5,11 @@ using Blazor.Diagrams.Core.Models.Base; using System; using System.Collections.Generic; -using DiagramPoint = Blazor.Diagrams.Core.Geometry.Point; namespace Blazor.Diagrams.Core.Behaviors; public class DragMovablesBehavior : DragBehavior { - public record NodeMoveablePositions(Point position) - { - public Dictionary ChildPositions { get; } = new(); - } - - protected readonly Dictionary _initialPositions; + protected readonly Dictionary _initialPositions; protected double? _lastClientX; protected double? _lastClientY; protected bool _moved; @@ -24,7 +18,7 @@ public record NodeMoveablePositions(Point position) public DragMovablesBehavior(Diagram diagram) : base(diagram) { - _initialPositions = new Dictionary(); + _initialPositions = new Dictionary(); Diagram.PanChanged += OnPanChanged; } @@ -53,7 +47,7 @@ protected override void OnPointerDown(Model? model, PointerEventArgs e) movable.Position.Y + (n.Size?.Height ?? 0) / 2); } - _initialPositions.Add(movable, new NodeMoveablePositions(position)); + _initialPositions.Add(movable, position); } _lastClientX = e.ClientX; @@ -79,7 +73,7 @@ protected override void OnPointerMove(Model? model, PointerEventArgs e) _lastClientY = e.ClientY; } - + protected virtual void OnPanChanged(double deltaX, double deltaY) { if (_initialPositions.Count == 0 || _lastClientX == null || _lastClientY == null) @@ -97,8 +91,8 @@ protected virtual void MoveNodes(double deltaX, double deltaY) { foreach (var (movable, initialPosition) in _initialPositions) { - var ndx = ApplyGridSize(deltaX + initialPosition.position.X); - var ndy = ApplyGridSize(deltaY + initialPosition.position.Y); + var ndx = ApplyGridSize(deltaX + initialPosition.X); + var ndy = ApplyGridSize(deltaY + initialPosition.Y); if (Diagram.Options.GridSnapToCenter && movable is NodeModel node) { node.SetPosition(ndx - (node.Size?.Width ?? 0) / 2, ndy - (node.Size?.Height ?? 0) / 2); From b852ab229a0ae8887f668d44b2efcaa955c8ba7c Mon Sep 17 00:00:00 2001 From: Gulam Nabi Azad Date: Wed, 3 Jul 2024 17:14:12 +0530 Subject: [PATCH 4/6] WI00737678---RemoveDragNodesBehavior---Update-3 --- src/Blazor.Diagrams.Core/Behaviors/DragMovablesBehavior.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Blazor.Diagrams.Core/Behaviors/DragMovablesBehavior.cs b/src/Blazor.Diagrams.Core/Behaviors/DragMovablesBehavior.cs index 16769128..0bd146b1 100644 --- a/src/Blazor.Diagrams.Core/Behaviors/DragMovablesBehavior.cs +++ b/src/Blazor.Diagrams.Core/Behaviors/DragMovablesBehavior.cs @@ -24,16 +24,15 @@ public DragMovablesBehavior(Diagram diagram) : base(diagram) protected override void OnPointerDown(Model? model, PointerEventArgs e) { - if (model is null) + if (model is not MovableModel) return; ResetPan(); _initialPositions.Clear(); - foreach (var sm in Diagram.GetSelectedModels()) { - if (sm is not NodeModel movable || movable.Locked) + if (sm is not MovableModel movable || movable.Locked) continue; // Special case: groups without auto size on @@ -116,7 +115,6 @@ protected override void OnPointerUp(Model? model, PointerEventArgs e) movable.TriggerMoved(); } } - _initialPositions.Clear(); _totalMovedX = 0; _totalMovedY = 0; From 42411c39fb295757c92c8201c12986b5a5c407fb Mon Sep 17 00:00:00 2001 From: Gulam Nabi Azad Date: Mon, 8 Jul 2024 10:44:49 +0530 Subject: [PATCH 5/6] WI00737678---RemoveDragNodesBehavior---Update-4 --- .../Behaviors/DragMovablesBehavior.cs | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/Blazor.Diagrams.Core/Behaviors/DragMovablesBehavior.cs b/src/Blazor.Diagrams.Core/Behaviors/DragMovablesBehavior.cs index 0bd146b1..15106cd2 100644 --- a/src/Blazor.Diagrams.Core/Behaviors/DragMovablesBehavior.cs +++ b/src/Blazor.Diagrams.Core/Behaviors/DragMovablesBehavior.cs @@ -27,8 +27,6 @@ protected override void OnPointerDown(Model? model, PointerEventArgs e) if (model is not MovableModel) return; - ResetPan(); - _initialPositions.Clear(); foreach (var sm in Diagram.GetSelectedModels()) { @@ -72,7 +70,7 @@ protected override void OnPointerMove(Model? model, PointerEventArgs e) _lastClientY = e.ClientY; } - + protected virtual void OnPanChanged(double deltaX, double deltaY) { if (_initialPositions.Count == 0 || _lastClientX == null || _lastClientY == null) @@ -132,14 +130,9 @@ private double ApplyGridSize(double n) return gridSize * Math.Floor((n + gridSize / 2.0) / gridSize); } - void ResetPan() - { - Diagram.SetPan(0, 0); - } - public override void Dispose() { _initialPositions.Clear(); Diagram.PanChanged -= OnPanChanged; } -} +} \ No newline at end of file From 87b6778fd0d90502706f326c8b2b106d48b3de69 Mon Sep 17 00:00:00 2001 From: Gulam Nabi Azad Date: Mon, 8 Jul 2024 10:47:13 +0530 Subject: [PATCH 6/6] WI00737678---RemoveDragNodesBehavior---Update-5 --- src/Blazor.Diagrams.Core/Behaviors/DragMovablesBehavior.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Blazor.Diagrams.Core/Behaviors/DragMovablesBehavior.cs b/src/Blazor.Diagrams.Core/Behaviors/DragMovablesBehavior.cs index 15106cd2..250a5365 100644 --- a/src/Blazor.Diagrams.Core/Behaviors/DragMovablesBehavior.cs +++ b/src/Blazor.Diagrams.Core/Behaviors/DragMovablesBehavior.cs @@ -5,6 +5,7 @@ using Blazor.Diagrams.Core.Models.Base; using System; using System.Collections.Generic; + namespace Blazor.Diagrams.Core.Behaviors; public class DragMovablesBehavior : DragBehavior