Skip to content

Commit

Permalink
WI00737678 : Remove DragNodeBehavior-Update-3
Browse files Browse the repository at this point in the history
  • Loading branch information
gnawisetech committed May 29, 2024
1 parent 047efee commit 3d5ff6b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 39 deletions.
44 changes: 43 additions & 1 deletion src/Blazor.Diagrams.Core/Behaviors/DragMovablesBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public DragMovablesBehavior(Diagram diagram) : base(diagram)
Diagram.PointerDown += OnPointerDown;
Diagram.PointerMove += OnPointerMove;
Diagram.PointerUp += OnPointerUp;
Diagram.PanChanged += OnPanChanged;
}

private void OnPointerDown(Model? model, PointerEventArgs e)
Expand Down Expand Up @@ -114,6 +115,45 @@ private void OnPointerMove(Model? model, PointerEventArgs e)
_lastClientY = e.ClientY;
}

public void OnPanChanged(double deltaX, double deltaY)
{
if (_initialPositions.Count == 0 || _lastClientX == null || _lastClientY == null)
return;

_moved = true;

_totalMovedX += deltaX;
_totalMovedY += deltaY;

MoveNodesOnPan(null, _totalMovedX, _totalMovedY);
}

private void MoveNodesOnPan(Model? model, double deltaX, double deltaY)
{
foreach (var (movable, initialPosition) in _initialPositions)
{
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);
}
else
{
movable.SetPosition(ndx, ndy);
}
}
}

private double ApplyGridSize(double n)
{
if (Diagram.Options.GridSize == null)
return n;

var gridSize = Diagram.Options.GridSize.Value;
return gridSize * Math.Floor((n + gridSize / 2.0) / gridSize);
}

void MoveNodes(Model? model, double deltaX, double deltaY)
{
foreach (var (node, positions) in _initialPositions)
Expand Down Expand Up @@ -143,8 +183,9 @@ void SetPosition(NodeModel node, double x, double y)
var parentH = nodeModel.ParentNode.Size?.Height - CHILD_NODE_MIN_OFFSET_TOP - CHILD_NODE_MIN_OFFSET_BOTTOM;
y = Clamp(y, nodeModel.Size?.Height, parentY, parentH);

nodeModel.SetPosition(x, y);
}

node.SetPosition(x, y);
}

double Clamp(double position, double? size, double? parentPosition, double? parentSize)
Expand Down Expand Up @@ -203,5 +244,6 @@ public override void Dispose()
Diagram.PointerDown -= OnPointerDown;
Diagram.PointerMove -= OnPointerMove;
Diagram.PointerUp -= OnPointerUp;
Diagram.PanChanged -= OnPanChanged;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,36 +30,6 @@ public void Behavior_ShouldCallSetPosition()
nodeMock.Verify(n => n.SetPosition(50, 50), Times.Once);
}

[Theory]
[InlineData(false, 0, 0, 45, 45)]
[InlineData(true, 0, 0, 35, 35)]
[InlineData(false, 3, 3, 45, 45)]
[InlineData(true, 3, 3, 50, 50)]
public void Behavior_SnapToGrid_ShouldCallSetPosition(bool gridSnapToCenter, double initialX, double initialY, double deltaX, double deltaY)
{
// Arrange
var diagram = new TestDiagram(new DiagramOptions
{
GridSize = 15,
GridSnapToCenter = gridSnapToCenter
});
var nodeMock = new Mock<NodeModel>(Point.Zero);
var node = diagram.Nodes.Add(nodeMock.Object);
node.Size = new Size(20, 20);
node.Position = new Point(initialX, initialY);
diagram.SelectModel(node, false);

// Act
//Move 40px in X and Y
diagram.TriggerPointerDown(node,
new PointerEventArgs(20, 20, 0, 0, false, false, false, 0, 0, 0, 0, 0, 0, string.Empty, true));
diagram.TriggerPointerMove(null,
new PointerEventArgs(60, 60, 0, 0, false, false, false, 0, 0, 0, 0, 0, 0, string.Empty, true));

// Assert
nodeMock.Verify(n => n.SetPosition(deltaX, deltaY), Times.Once);
}

[Fact]
public void Behavior_ShouldTriggerMoved()
{
Expand Down Expand Up @@ -103,12 +73,12 @@ public void Behavior_ShouldNotTriggerMoved_WhenMovableDidntMove()
}

[Fact]
public void Behavior_ShouldNotCallSetPosition_WhenGroupHasNoAutoSize()
public void Behavior_ShouldCallSetPosition_WhenGroupHasAutoSize()
{
// Arrange
var diagram = new TestDiagram();
var nodeMock = new Mock<NodeModel>(Point.Zero);
var group = new GroupModel(new[] { nodeMock.Object }, autoSize: false);
var group = new GroupModel(new[] { nodeMock.Object }, autoSize: true);
var node = diagram.Nodes.Add(nodeMock.Object);
diagram.SelectModel(node, false);

Expand All @@ -119,26 +89,26 @@ public void Behavior_ShouldNotCallSetPosition_WhenGroupHasNoAutoSize()
new PointerEventArgs(150, 150, 0, 0, false, false, false, 0, 0, 0, 0, 0, 0, string.Empty, true));

// Assert
nodeMock.Verify(n => n.SetPosition(50, 50), Times.Never);
nodeMock.Verify(n => n.SetPosition(50, 50), Times.Once);
}

[Fact]
public void Behavior_ShouldCallSetPosition_WhenGroupHasAutoSize()
public void Behavior_ShouldCallSetPosition_WhenPanChanges()
{
// Arrange
var diagram = new TestDiagram();
var nodeMock = new Mock<NodeModel>(Point.Zero);
var group = new GroupModel(new[] { nodeMock.Object }, autoSize: true);
var node = diagram.Nodes.Add(nodeMock.Object);
diagram.SelectModel(node, false);
diagram.BehaviorOptions.DiagramWheelBehavior = diagram.GetBehavior<ScrollBehavior>();
diagram.SetContainer(new Rectangle(0, 0, 100, 100));

// Act
diagram.TriggerPointerDown(node,
new PointerEventArgs(100, 100, 0, 0, false, false, false, 0, 0, 0, 0, 0, 0, string.Empty, true));
diagram.TriggerPointerMove(null,
new PointerEventArgs(150, 150, 0, 0, false, false, false, 0, 0, 0, 0, 0, 0, string.Empty, true));
diagram.TriggerWheel(new WheelEventArgs(100, 100, 0, 0, false, false, false, 100, 100, 0, 0));

// Assert
nodeMock.Verify(n => n.SetPosition(50, 50), Times.Once);
nodeMock.Verify(n => n.SetPosition(100, 100), Times.Once);
}
}

0 comments on commit 3d5ff6b

Please sign in to comment.