Skip to content

Commit

Permalink
feat: Add algorithms project with links reconnection algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
zHaytam committed Nov 8, 2020
1 parent d39d77d commit ccd1dc2
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Blazor.Diagrams.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SharedDemo", "samples\Share
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ServerSide", "samples\ServerSide\ServerSide.csproj", "{B9EE910B-8FE7-490C-B20C-CEC27A2890D6}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blazor.Diagrams.Algorithms", "src\Blazor.Diagrams.Algorithms\Blazor.Diagrams.Algorithms.csproj", "{CB3A42B6-3C87-4ECB-B60C-D98275AB1FB6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -43,6 +45,10 @@ Global
{B9EE910B-8FE7-490C-B20C-CEC27A2890D6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B9EE910B-8FE7-490C-B20C-CEC27A2890D6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B9EE910B-8FE7-490C-B20C-CEC27A2890D6}.Release|Any CPU.Build.0 = Release|Any CPU
{CB3A42B6-3C87-4ECB-B60C-D98275AB1FB6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CB3A42B6-3C87-4ECB-B60C-D98275AB1FB6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CB3A42B6-3C87-4ECB-B60C-D98275AB1FB6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CB3A42B6-3C87-4ECB-B60C-D98275AB1FB6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -53,6 +59,7 @@ Global
{A1A4F8A5-7C97-41A8-9ADD-54E9D1725B56} = {EE32E278-A887-454E-987D-FFE9E37169FE}
{1CBCC8E6-111C-4364-9882-50881E4CC8B4} = {DA819127-3EF6-4EB9-A2DA-BC056B284A50}
{B9EE910B-8FE7-490C-B20C-CEC27A2890D6} = {DA819127-3EF6-4EB9-A2DA-BC056B284A50}
{CB3A42B6-3C87-4ECB-B60C-D98275AB1FB6} = {EE32E278-A887-454E-987D-FFE9E37169FE}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {969540A2-8162-4063-A4E3-B488F69BD582}
Expand Down
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>
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;
}
}
}
3 changes: 3 additions & 0 deletions samples/SharedDemo/Layouts/DemoLayout.razor
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
<a href="demos/custom-node" class="list-group-item list-group-item-action bg-light">Custom node</a>
<a href="demos/custom-link" class="list-group-item list-group-item-action bg-light">Custom link</a>
<a href="demos/custom-port" class="list-group-item list-group-item-action bg-light">Custom port</a>

<div class="list-group-item bg-primary text-light">Algorithms</div>
<a href="demos/reconnect-links" class="list-group-item list-group-item-action bg-light">Reconnect links</a>
</div>
</div>
<div id="page-content-wrapper">
Expand Down
1 change: 1 addition & 0 deletions samples/SharedDemo/SharedDemo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Blazor.Diagrams.Algorithms\Blazor.Diagrams.Algorithms.csproj" />
<ProjectReference Include="..\..\src\Blazor.Diagrams.Core\Blazor.Diagrams.Core.csproj" />
<ProjectReference Include="..\..\src\Blazor.Diagrams\Blazor.Diagrams.csproj" />
</ItemGroup>
Expand Down
23 changes: 23 additions & 0 deletions src/Blazor.Diagrams.Algorithms/Blazor.Diagrams.Algorithms.csproj
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>A fully customizable and extensible all-purpose diagrams library for Blazor</Description>
<AssemblyVersion>0.1</AssemblyVersion>
<FileVersion>0.1</FileVersion>
<RepositoryUrl>https://github.com/zHaytam/Blazor.Diagrams</RepositoryUrl>
<Version>0.10</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 src/Blazor.Diagrams.Algorithms/Extensions/PointExtensions.cs
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 src/Blazor.Diagrams.Algorithms/LinksReconnectionAlgorithms.cs
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();
}
}
}
1 change: 0 additions & 1 deletion src/Blazor.Diagrams.Core/DiagramManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ public void AttachLink(LinkModel link, PortModel targetPort)
return;

link.SetTargetPort(targetPort);
targetPort.AddLink(link);
link.Refresh();
targetPort.Refresh();
LinkAttached?.Invoke(link);
Expand Down

0 comments on commit ccd1dc2

Please sign in to comment.