-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from zHaytam/develop
Version 1.2.0 & new Algorithms package
- Loading branch information
Showing
18 changed files
with
337 additions
and
72 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
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
27 changes: 27 additions & 0 deletions
27
samples/SharedDemo/Demos/Algorithms/ReconnectLinksToClosestPorts.razor
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,27 @@ | ||
@page "/demos/reconnect-links" | ||
@inherits ReconnectLinksToClosestPortsComponent | ||
@layout DemoLayout | ||
@inject LayoutData LayoutData | ||
|
||
@code { | ||
protected override void OnInitialized() | ||
{ | ||
base.OnInitialized(); | ||
|
||
LayoutData.Title = "Reconnect links"; | ||
LayoutData.Info = "An example of reconnecting links to the closest ports."; | ||
LayoutData.DataChanged(); | ||
} | ||
} | ||
|
||
<div style="position: absolute; z-index: 9999;"> | ||
<button @onclick="ReconnectLinks">Reconnect links</button> | ||
</div> | ||
|
||
<CascadingValue Name="DiagramManager" Value="diagramManager"> | ||
<DiagramCanvas> | ||
<Widgets> | ||
<Navigator Width="300" Height="200" DefaultStyle="true"></Navigator> | ||
</Widgets> | ||
</DiagramCanvas> | ||
</CascadingValue> |
41 changes: 41 additions & 0 deletions
41
samples/SharedDemo/Demos/Algorithms/ReconnectLinksToClosestPorts.razor.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,41 @@ | ||
using Blazor.Diagrams.Algorithms; | ||
using Blazor.Diagrams.Core; | ||
using Blazor.Diagrams.Core.Models; | ||
using Blazor.Diagrams.Core.Models.Core; | ||
using Microsoft.AspNetCore.Components; | ||
|
||
namespace SharedDemo.Demos.Algorithms | ||
{ | ||
public class ReconnectLinksToClosestPortsComponent : ComponentBase | ||
{ | ||
protected readonly DiagramManager diagramManager = new DiagramManager(); | ||
|
||
protected override void OnInitialized() | ||
{ | ||
base.OnInitialized(); | ||
|
||
var node1 = NewNode(50, 50); | ||
var node2 = NewNode(300, 300); | ||
var node3 = NewNode(300, 50); | ||
diagramManager.AddLink(node1.GetPort(PortAlignment.Top), node2.GetPort(PortAlignment.Right)); | ||
diagramManager.AddLink(node1.GetPort(PortAlignment.Bottom), node3.GetPort(PortAlignment.Top)); | ||
diagramManager.AddNode(node1); | ||
diagramManager.AddNode(node2); | ||
diagramManager.AddNode(node3); | ||
} | ||
|
||
|
||
protected void ReconnectLinks() => diagramManager.ReconnectLinksToClosestPorts(); | ||
|
||
|
||
private NodeModel NewNode(double x, double y) | ||
{ | ||
var node = new NodeModel(new Point(x, y)); | ||
node.AddPort(PortAlignment.Bottom); | ||
node.AddPort(PortAlignment.Top); | ||
node.AddPort(PortAlignment.Left); | ||
node.AddPort(PortAlignment.Right); | ||
return node; | ||
} | ||
} | ||
} |
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
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
23 changes: 23 additions & 0 deletions
23
src/Blazor.Diagrams.Algorithms/Blazor.Diagrams.Algorithms.csproj
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,23 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.1</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> | ||
<PackageLicenseExpression>MIT</PackageLicenseExpression> | ||
<Authors>zHaytam</Authors> | ||
<Description>Algorithms for Z.Blazor.Diagrams</Description> | ||
<AssemblyVersion>0.1.0</AssemblyVersion> | ||
<FileVersion>0.1.0</FileVersion> | ||
<RepositoryUrl>https://github.com/zHaytam/Blazor.Diagrams</RepositoryUrl> | ||
<Version>0.1.0</Version> | ||
<PackageId>Z.Blazor.Diagrams.Algorithms</PackageId> | ||
<PackageTags>blazor diagrams diagramming svg drag algorithms layouts</PackageTags> | ||
<Product>Z.Blazor.Diagrams.Algorithms</Product> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Blazor.Diagrams.Core\Blazor.Diagrams.Core.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
15 changes: 15 additions & 0 deletions
15
src/Blazor.Diagrams.Algorithms/Extensions/PointExtensions.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,15 @@ | ||
using Blazor.Diagrams.Core.Models.Core; | ||
using System; | ||
|
||
namespace Blazor.Diagrams.Algorithms.Extensions | ||
{ | ||
public static class PointExtensions | ||
{ | ||
public static double DistanceTo(this Point firstPoint, Point secondPoint) | ||
{ | ||
var x = Math.Abs(firstPoint.X - secondPoint.X); | ||
var y = Math.Abs(firstPoint.Y - secondPoint.Y); | ||
return Math.Sqrt(x * x + y * y); | ||
} | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/Blazor.Diagrams.Algorithms/LinksReconnectionAlgorithms.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,62 @@ | ||
using Blazor.Diagrams.Algorithms.Extensions; | ||
using Blazor.Diagrams.Core; | ||
using Blazor.Diagrams.Core.Models; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Blazor.Diagrams.Algorithms | ||
{ | ||
public static class LinksReconnectionAlgorithms | ||
{ | ||
public static void ReconnectLinksToClosestPorts(this DiagramManager diagramManager) | ||
{ | ||
// Only refresh ports once | ||
var portsToRefresh = new HashSet<PortModel>(); | ||
|
||
foreach (var link in diagramManager.AllLinks.ToArray()) | ||
{ | ||
if (link.TargetPort == null) | ||
continue; | ||
|
||
var sourcePorts = link.SourcePort.Parent.Ports; | ||
var targetPorts = link.TargetPort.Parent.Ports; | ||
|
||
// Find the ports with minimal distance | ||
var minDistance = double.MaxValue; | ||
var minSourcePort = link.SourcePort; | ||
var minTargetPort = link.TargetPort; | ||
foreach (var sourcePort in sourcePorts) | ||
{ | ||
foreach (var targetPort in targetPorts) | ||
{ | ||
var distance = sourcePort.Position.DistanceTo(targetPort.Position); | ||
if (distance < minDistance) | ||
{ | ||
minDistance = distance; | ||
minSourcePort = sourcePort; | ||
minTargetPort = targetPort; | ||
} | ||
} | ||
} | ||
|
||
// Reconnect | ||
if (link.SourcePort != minSourcePort) | ||
{ | ||
portsToRefresh.Add(link.SourcePort); | ||
portsToRefresh.Add(minSourcePort); | ||
link.SetSourcePort(minSourcePort); | ||
} | ||
|
||
if (link.TargetPort != minTargetPort) | ||
{ | ||
portsToRefresh.Add(link.TargetPort); | ||
portsToRefresh.Add(minTargetPort); | ||
link.SetTargetPort(minTargetPort); | ||
} | ||
} | ||
|
||
foreach (var port in portsToRefresh) | ||
port.Refresh(); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.