forked from Blazor-Diagrams/Blazor.Diagrams
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Controls to implement node resizing (#10)
- Loading branch information
1 parent
7d72283
commit 3beabc4
Showing
22 changed files
with
876 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/Blazor.Diagrams.Core/Controls/Default/ResizeControl.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using Blazor.Diagrams.Core.Events; | ||
using Blazor.Diagrams.Core.Geometry; | ||
using Blazor.Diagrams.Core.Models.Base; | ||
using Blazor.Diagrams.Core.Positions.Resizing; | ||
using System.Threading.Tasks; | ||
|
||
namespace Blazor.Diagrams.Core.Controls.Default | ||
{ | ||
public class ResizeControl : ExecutableControl | ||
{ | ||
private readonly IResizerProvider _resizeProvider; | ||
|
||
public ResizeControl(IResizerProvider resizeProvider) | ||
{ | ||
_resizeProvider = resizeProvider; | ||
} | ||
|
||
public override Point? GetPosition(Model model) => _resizeProvider.GetPosition(model); | ||
|
||
public string? Class => _resizeProvider.Class; | ||
|
||
public override ValueTask OnPointerDown(Diagram diagram, Model model, PointerEventArgs e) | ||
{ | ||
_resizeProvider.OnResizeStart(diagram, model, e); | ||
diagram.PointerMove += _resizeProvider.OnPointerMove; | ||
diagram.PointerUp += _resizeProvider.OnResizeEnd; | ||
diagram.PointerUp += (_, _) => OnResizeEnd(diagram); | ||
|
||
return ValueTask.CompletedTask; | ||
} | ||
|
||
void OnResizeEnd(Diagram diagram) | ||
{ | ||
diagram.PointerMove -= _resizeProvider.OnPointerMove; | ||
diagram.PointerUp -= _resizeProvider.OnResizeEnd; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/Blazor.Diagrams.Core/Positions/Resizing/BottomLeftResizerProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using Blazor.Diagrams.Core.Events; | ||
using Blazor.Diagrams.Core.Geometry; | ||
using Blazor.Diagrams.Core.Models; | ||
using Blazor.Diagrams.Core.Models.Base; | ||
|
||
namespace Blazor.Diagrams.Core.Positions.Resizing | ||
{ | ||
public class BottomLeftResizerProvider : IResizerProvider | ||
{ | ||
public string? Class => "bottomleft"; | ||
|
||
private Size _originalSize = null!; | ||
private Point _originalPosition = null!; | ||
private Point _originalMousePosition = null!; | ||
private NodeModel _nodeModel = null!; | ||
|
||
public Point? GetPosition(Model model) | ||
{ | ||
if (model is NodeModel nodeModel && nodeModel.Size is not null) | ||
{ | ||
return new Point(nodeModel.Position.X - 5, nodeModel.Position.Y + nodeModel.Size.Height + 5); | ||
} | ||
return null; | ||
} | ||
|
||
public void OnResizeStart(Diagram diagram, Model model, PointerEventArgs eventArgs) | ||
{ | ||
if (model is NodeModel nodeModel) | ||
{ | ||
_originalPosition = new Point(nodeModel.Position.X, nodeModel.Position.Y); | ||
_originalMousePosition = new Point(eventArgs.ClientX, eventArgs.ClientY); | ||
_originalSize = nodeModel.Size!; | ||
_nodeModel = nodeModel; | ||
} | ||
} | ||
|
||
public void OnPointerMove(Model? model, PointerEventArgs args) | ||
{ | ||
if (_nodeModel is null) | ||
{ | ||
return; | ||
} | ||
|
||
var height = _originalSize.Height + (args.ClientY - _originalMousePosition.Y); | ||
var width = _originalSize.Width - (args.ClientX - _originalMousePosition.X); | ||
|
||
var positionX = _originalPosition.X + (args.ClientX - _originalMousePosition.X); | ||
var positionY = _originalPosition.Y; | ||
|
||
if (width < _nodeModel.MinimumDimensions.Width) | ||
{ | ||
width = _nodeModel.MinimumDimensions.Width; | ||
positionX = _nodeModel.Position.X; | ||
} | ||
if (height < _nodeModel.MinimumDimensions.Height) | ||
{ | ||
height = _nodeModel.MinimumDimensions.Height; | ||
positionY = _nodeModel.Position.Y; | ||
} | ||
|
||
_nodeModel.SetPosition(positionX, positionY); | ||
_nodeModel.SetSize(width, height); | ||
} | ||
|
||
public void OnResizeEnd(Model? model, PointerEventArgs args) | ||
{ | ||
_nodeModel?.TriggerSizeChanged(); | ||
_originalSize = null!; | ||
_originalPosition = null!; | ||
_originalMousePosition = null!; | ||
_nodeModel = null!; | ||
} | ||
|
||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
src/Blazor.Diagrams.Core/Positions/Resizing/BottomRightResizerProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using Blazor.Diagrams.Core.Events; | ||
using Blazor.Diagrams.Core.Geometry; | ||
using Blazor.Diagrams.Core.Models; | ||
using Blazor.Diagrams.Core.Models.Base; | ||
|
||
namespace Blazor.Diagrams.Core.Positions.Resizing | ||
{ | ||
public class BottomRightResizerProvider : IResizerProvider | ||
{ | ||
public string? Class => "bottomright"; | ||
|
||
private Size _originalSize = null!; | ||
private Point _originalMousePosition = null!; | ||
private NodeModel _nodeModel = null!; | ||
|
||
public Point? GetPosition(Model model) | ||
{ | ||
if (model is NodeModel nodeModel && nodeModel.Size is not null) | ||
{ | ||
return new Point(nodeModel.Position.X + nodeModel.Size.Width + 5, nodeModel.Position.Y + nodeModel.Size.Height + 5); | ||
} | ||
return null; | ||
} | ||
|
||
public void OnResizeStart(Diagram diagram, Model model, PointerEventArgs eventArgs) | ||
{ | ||
if (model is NodeModel nodeModel) | ||
{ | ||
_originalMousePosition = new Point(eventArgs.ClientX, eventArgs.ClientY); | ||
_originalSize = nodeModel.Size!; | ||
_nodeModel = nodeModel; | ||
} | ||
} | ||
|
||
public void OnPointerMove(Model? model, PointerEventArgs args) | ||
{ | ||
if (_nodeModel is null) | ||
{ | ||
return; | ||
} | ||
|
||
var height = _originalSize.Height + (args.ClientY - _originalMousePosition.Y); | ||
var width = _originalSize.Width + (args.ClientX - _originalMousePosition.X); | ||
|
||
if (width < _nodeModel.MinimumDimensions.Width) | ||
{ | ||
width = _nodeModel.MinimumDimensions.Width; | ||
} | ||
if (height < _nodeModel.MinimumDimensions.Height) | ||
{ | ||
height = _nodeModel.MinimumDimensions.Height; | ||
} | ||
|
||
_nodeModel.SetSize(width, height); | ||
} | ||
|
||
public void OnResizeEnd(Model? model, PointerEventArgs args) | ||
{ | ||
_nodeModel?.TriggerSizeChanged(); | ||
_originalSize = null!; | ||
_originalMousePosition = null!; | ||
_nodeModel = null!; | ||
} | ||
|
||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/Blazor.Diagrams.Core/Positions/Resizing/IResizerProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using Blazor.Diagrams.Core.Events; | ||
using Blazor.Diagrams.Core.Models; | ||
using Blazor.Diagrams.Core.Models.Base; | ||
|
||
namespace Blazor.Diagrams.Core.Positions.Resizing | ||
{ | ||
public interface IResizerProvider : IPositionProvider | ||
{ | ||
public string? Class { get; } | ||
public void OnResizeStart(Diagram diagram, Model model, PointerEventArgs eventArgs); | ||
public void OnPointerMove(Model? model, PointerEventArgs args); | ||
public void OnResizeEnd(Model? model, PointerEventArgs args); | ||
} | ||
} |
Oops, something went wrong.