Skip to content

Commit

Permalink
WI00737678---RemoveDragNodesBehavior---Update-2
Browse files Browse the repository at this point in the history
  • Loading branch information
gnawisetech committed Jul 3, 2024
1 parent ac98c3d commit 579b876
Showing 1 changed file with 6 additions and 12 deletions.
18 changes: 6 additions & 12 deletions src/Blazor.Diagrams.Core/Behaviors/DragMovablesBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<NodeModel, DiagramPoint> ChildPositions { get; } = new();
}

protected readonly Dictionary<NodeModel, NodeMoveablePositions> _initialPositions;
protected readonly Dictionary<MovableModel, Point> _initialPositions;
protected double? _lastClientX;
protected double? _lastClientY;
protected bool _moved;
Expand All @@ -24,7 +18,7 @@ public record NodeMoveablePositions(Point position)

public DragMovablesBehavior(Diagram diagram) : base(diagram)
{
_initialPositions = new Dictionary<NodeModel, NodeMoveablePositions>();
_initialPositions = new Dictionary<MovableModel, Point>();
Diagram.PanChanged += OnPanChanged;
}

Expand Down Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -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);
Expand Down

0 comments on commit 579b876

Please sign in to comment.