Skip to content

Commit

Permalink
feat: Touch support for dragging movables
Browse files Browse the repository at this point in the history
  • Loading branch information
zHaytam committed Mar 20, 2021
1 parent 6cc9626 commit ba8916d
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 39 deletions.
58 changes: 39 additions & 19 deletions src/Blazor.Diagrams.Core/Behaviors/DragMovablesBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,29 @@ public class DragMovablesBehavior : Behavior

public DragMovablesBehavior(Diagram diagram) : base(diagram)
{
Diagram.MouseDown += Diagram_MouseDown;
Diagram.MouseMove += Diagram_MouseMove;
Diagram.MouseUp += Diagram_MouseUp;
Diagram.MouseDown += OnMouseDown;
Diagram.MouseMove += OnMouseMove;
Diagram.MouseUp += OnMouseUp;
Diagram.TouchStart += OnTouchStart;
Diagram.TouchMove += OnTouchMove;
Diagram.TouchEnd += OnTouchEnd;
}

private void Diagram_MouseDown(Model model, MouseEventArgs e)
private void OnTouchStart(Model model, TouchEventArgs e)
=> Start(model, e.ChangedTouches[0].ClientX, e.ChangedTouches[0].ClientY);

private void OnTouchMove(Model model, TouchEventArgs e)
=> Move(e.ChangedTouches[0].ClientX, e.ChangedTouches[0].ClientY);

private void OnTouchEnd(Model model, TouchEventArgs e) => End();

private void OnMouseDown(Model model, MouseEventArgs e) => Start(model, e.ClientX, e.ClientY);

private void OnMouseMove(Model model, MouseEventArgs e) => Move(e.ClientX, e.ClientY);

private void OnMouseUp(Model model, MouseEventArgs e) => End();

private void Start(Model model, double clientX, double clientY)
{
if (!(model is MovableModel))
return;
Expand All @@ -30,17 +47,17 @@ private void Diagram_MouseDown(Model model, MouseEventArgs e)
.Select(m => (m as MovableModel)!.Position)
.ToArray();

_lastClientX = e.ClientX;
_lastClientY = e.ClientY;
_lastClientX = clientX;
_lastClientY = clientY;
}

private void Diagram_MouseMove(Model model, MouseEventArgs e)
private void Move(double clientX, double clientY)
{
if (_initialPositions == null || _lastClientX == null || _lastClientY == null)
return;

var deltaX = (e.ClientX - _lastClientX.Value) / Diagram.Zoom;
var deltaY = (e.ClientY - _lastClientY.Value) / Diagram.Zoom;
var deltaX = (clientX - _lastClientX.Value) / Diagram.Zoom;
var deltaY = (clientY - _lastClientY.Value) / Diagram.Zoom;
var i = 0;

foreach (var sm in Diagram.GetSelectedModels())
Expand All @@ -56,6 +73,13 @@ private void Diagram_MouseMove(Model model, MouseEventArgs e)
}
}

private void End()
{
_initialPositions = null;
_lastClientX = null;
_lastClientY = null;
}

private double ApplyGridSize(double n)
{
if (Diagram.Options.GridSize == null)
Expand All @@ -69,18 +93,14 @@ private double ApplyGridSize(double n)
return gridSize * Math.Floor((n + gridSize / 2) / gridSize);
}

private void Diagram_MouseUp(Model model, MouseEventArgs e)
{
_initialPositions = null;
_lastClientX = null;
_lastClientY = null;
}

public override void Dispose()
{
Diagram.MouseDown -= Diagram_MouseDown;
Diagram.MouseMove -= Diagram_MouseMove;
Diagram.MouseUp -= Diagram_MouseUp;
Diagram.MouseDown -= OnMouseDown;
Diagram.MouseMove -= OnMouseMove;
Diagram.MouseUp -= OnMouseUp;
Diagram.TouchStart -= OnTouchStart;
Diagram.TouchMove -= OnTouchMove;
Diagram.TouchEnd -= OnTouchEnd;
}
}
}
55 changes: 40 additions & 15 deletions src/Blazor.Diagrams.Core/Behaviors/PanBehavior.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Blazor.Diagrams.Core.Geometry;
using Blazor.Diagrams.Core.Models.Base;
using Microsoft.AspNetCore.Components.Web;
using System;

namespace Blazor.Diagrams.Core.Behaviors
{
Expand All @@ -13,32 +12,55 @@ public class PanBehavior : Behavior

public PanBehavior(Diagram diagram) : base(diagram)
{
Diagram.MouseDown += Diagram_MouseDown;
Diagram.MouseMove += Diagram_MouseMove;
Diagram.MouseUp += Diagram_MouseUp;
Diagram.MouseDown += OnMouseDown;
Diagram.MouseMove += OnMouseMove;
Diagram.MouseUp += OnMouseUp;
Diagram.TouchStart += OnTouchStart;
Diagram.TouchMove += OnTouchmove;
Diagram.TouchEnd += OnTouchEnd;
}

private void Diagram_MouseDown(Model model, MouseEventArgs e)
private void OnTouchStart(Model model, TouchEventArgs e)
=> Start(model, e.ChangedTouches[0].ClientX, e.ChangedTouches[0].ClientY, e.ShiftKey);

private void OnTouchmove(Model model, TouchEventArgs e)
=> Move(e.ChangedTouches[0].ClientX, e.ChangedTouches[0].ClientY);

private void OnTouchEnd(Model model, TouchEventArgs e) => End();

private void OnMouseDown(Model model, MouseEventArgs e)
{
if (e.Button != (int)MouseEventButton.Left)
return;

Start(model, e.ClientX, e.ClientY, e.ShiftKey);
}

private void OnMouseMove(Model model, MouseEventArgs e) => Move(e.ClientX, e.ClientY);

private void OnMouseUp(Model model, MouseEventArgs e) => End();

private void Start(Model model, double clientX, double clientY, bool shiftKey)
{
if (!Diagram.Options.AllowPanning || model != null || e.ShiftKey || e.Button != (int)MouseEventButton.Left)
if (!Diagram.Options.AllowPanning || model != null || shiftKey)
return;

_initialPan = Diagram.Pan;
_lastClientX = e.ClientX;
_lastClientY = e.ClientY;
_lastClientX = clientX;
_lastClientY = clientY;
}

private void Diagram_MouseMove(Model model, MouseEventArgs e)
private void Move(double clientX, double clientY)
{
if (!Diagram.Options.AllowPanning || _initialPan == null)
return;

var deltaX = e.ClientX - _lastClientX - (Diagram.Pan.X - _initialPan.X);
var deltaY = e.ClientY - _lastClientY - (Diagram.Pan.Y - _initialPan.Y);
var deltaX = clientX - _lastClientX - (Diagram.Pan.X - _initialPan.X);
var deltaY = clientY - _lastClientY - (Diagram.Pan.Y - _initialPan.Y);
Diagram.UpdatePan(deltaX, deltaY);
}

private void Diagram_MouseUp(Model model, MouseEventArgs e)
private void End()
{
if (!Diagram.Options.AllowPanning)
return;
Expand All @@ -48,9 +70,12 @@ private void Diagram_MouseUp(Model model, MouseEventArgs e)

public override void Dispose()
{
Diagram.MouseDown -= Diagram_MouseDown;
Diagram.MouseMove -= Diagram_MouseMove;
Diagram.MouseUp -= Diagram_MouseUp;
Diagram.MouseDown -= OnMouseDown;
Diagram.MouseMove -= OnMouseMove;
Diagram.MouseUp -= OnMouseUp;
Diagram.TouchStart -= OnTouchStart;
Diagram.TouchMove -= OnTouchmove;
Diagram.TouchEnd -= OnTouchEnd;
}
}
}
5 changes: 4 additions & 1 deletion src/Blazor.Diagrams/Components/DiagramCanvas.razor
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
@onkeydown="OnKeyDown"
@onwheel="OnWheel"
@onwheel:stopPropagation
@ontouchmove="OnTouchMove">
@ontouchstart="OnTouchStart"
@ontouchmove="OnTouchMove"
@ontouchend="OnTouchEnd"
@ontouchend:preventDefault>

@* Links *@
<svg class="diagram-svg-layer" style="@LayerStyle">
Expand Down
4 changes: 4 additions & 0 deletions src/Blazor.Diagrams/Components/DiagramCanvas.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,12 @@ protected override bool ShouldRender()

private void OnWheel(WheelEventArgs e) => Diagram.OnWheel(e);

private void OnTouchStart(TouchEventArgs e) => Diagram.OnTouchStart(null, e);

private void OnTouchMove(TouchEventArgs e) => Diagram.OnTouchMove(null, e);

private void OnTouchEnd(TouchEventArgs e) => Diagram.OnTouchEnd(null, e);

private void OnDiagramChanged()
{
_shouldReRender = true;
Expand Down
7 changes: 6 additions & 1 deletion src/Blazor.Diagrams/Components/LinkVertexWidget.razor
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@
@onmousedown="OnMouseDown"
@onmousedown:stopPropagation
@onmouseup="OnMouseUp"
@onmouseup:stopPropagation />
@onmouseup:stopPropagation
@ontouchstart="OnTouchStart"
@ontouchstart:stopPropagation
@ontouchend="OnTouchEnd"
@ontouchend:preventDefault
@ontouchend:stopPropagation />
4 changes: 4 additions & 0 deletions src/Blazor.Diagrams/Components/LinkVertexWidget.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ private void OnVertexChanged()

private void OnMouseUp(MouseEventArgs e) => Diagram.OnMouseUp(Vertex, e);

private void OnTouchStart(TouchEventArgs e) => Diagram.OnTouchStart(Vertex, e);

private void OnTouchEnd(TouchEventArgs e) => Diagram.OnTouchEnd(Vertex, e);

private void OnDoubleClick(MouseEventArgs e)
{
Vertex.Parent.Vertices.Remove(Vertex);
Expand Down
4 changes: 3 additions & 1 deletion src/Blazor.Diagrams/Components/LinkWidget.razor
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
stroke-opacity="0"
fill="none"
@onmousedown="e => OnMouseDown(e, index)"
@onmousedown:stopPropagation="@Link.Segmentable" />
@onmousedown:stopPropagation="@Link.Segmentable"
@ontouchstart="e => OnTouchStart(e, index)"
@ontouchstart:stopPropagation="@Link.Segmentable"/>
}
}

Expand Down
19 changes: 17 additions & 2 deletions src/Blazor.Diagrams/Components/LinkWidget.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,25 @@ private void OnMouseDown(MouseEventArgs e, int index)
if (!Link.Segmentable)
return;

var rPt = Diagram.GetRelativeMousePoint(e.ClientX, e.ClientY);
var vertex = CreateVertex(e.ClientX, e.ClientY, index);
Diagram.OnMouseDown(vertex, e);
}

private void OnTouchStart(TouchEventArgs e, int index)
{
if (!Link.Segmentable)
return;

var vertex = CreateVertex(e.ChangedTouches[0].ClientX, e.ChangedTouches[0].ClientY, index);
Diagram.OnTouchStart(vertex, e);
}

private LinkVertexModel CreateVertex(double clientX, double clientY, int index)
{
var rPt = Diagram.GetRelativeMousePoint(clientX, clientY);
var vertex = new LinkVertexModel(Link, rPt);
Link.Vertices.Insert(index, vertex);
Diagram.OnMouseDown(vertex, e);
return vertex;
}

private (Point source, Point target) FindConnectionPoints(Point[] route)
Expand Down

0 comments on commit ba8916d

Please sign in to comment.