Skip to content

Commit c7f76ab

Browse files
author
YCode
committed
chore: AutoLayout(未完)
1 parent 7e4dc9d commit c7f76ab

5 files changed

Lines changed: 131 additions & 13 deletions

File tree

YCode.Designer.Demo/Demo/AutoLayout/AutoLayout.xaml

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,21 @@
3030
CornerRadius="3" />
3131
</DataTemplate>
3232

33+
<DataTemplate x:Key="ToolDataTemplate">
34+
<Grid>
35+
<Border Background="White"
36+
CornerRadius="5"
37+
Opacity="0.5" />
38+
<StackPanel Margin="16"
39+
Orientation="Horizontal">
40+
<Button Height="24"
41+
Content="AutoLayout"
42+
Background="Transparent"
43+
Click="OnAutoLayoutClick" />
44+
</StackPanel>
45+
</Grid>
46+
</DataTemplate>
47+
3348
<local:NodeTemplateSelector x:Key="NodeTemplateSelector"
3449
NormalTemplate="{StaticResource NormalNodeTemplate}"
3550
EmptyTemplate="{StaticResource EmptyNodeTemplate}" />
@@ -43,10 +58,11 @@
4358

4459
<Grid Background="#1E1E1E">
4560
<ycode:FluxoDesigner x:Name="Designer"
61+
GridType="Dot"
62+
EnableMove="False"
4663
Source="{Binding Source}"
64+
ToolTemplate="{StaticResource ToolDataTemplate}"
4765
ItemTemplateSelector="{StaticResource NodeTemplateSelector}"
48-
ItemContainerStyle="{StaticResource FluxoNodeStyle}"
49-
EnableMove="False"
50-
GridType="Dot" />
66+
ItemContainerStyle="{StaticResource FluxoNodeStyle}" />
5167
</Grid>
5268
</UserControl>

YCode.Designer.Demo/Demo/AutoLayout/AutoLayout.xaml.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Diagnostics;
2+
using System.Windows;
23
using System.Windows.Controls;
34
using YCode.Designer.Fluxo;
45

@@ -10,4 +11,9 @@ public AutoLayout()
1011
{
1112
InitializeComponent();
1213
}
14+
15+
private void OnAutoLayoutClick(object sender, RoutedEventArgs e)
16+
{
17+
Designer.AutoLayout();
18+
}
1319
}

YCode.Designer.Demo/ViewModels/AutoLayoutViewModel.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public AutoLayoutViewModel()
1414
{
1515
Id = "Root",
1616
Name = "Root",
17-
Location = new Point(122, 240),
17+
// Location = new Point(122, 240),
1818
Context = new NodeContext()
1919
{
2020
Type = NodeType.Normal
@@ -25,7 +25,7 @@ public AutoLayoutViewModel()
2525
{
2626
Id = "If1",
2727
Name = "Branch",
28-
Location = new(399, 240),
28+
// Location = new(399, 240),
2929
Context = new NodeContext()
3030
{
3131
Type = NodeType.Normal
@@ -36,7 +36,7 @@ public AutoLayoutViewModel()
3636
{
3737
Id = "Process1",
3838
Name = "Process",
39-
Location = new(799, 177),
39+
// Location = new(799, 177),
4040
Context = new NodeContext()
4141
{
4242
Type = NodeType.Normal
@@ -47,7 +47,7 @@ public AutoLayoutViewModel()
4747
{
4848
Id = "Process2",
4949
Name = "Process",
50-
Location = new(799, 296.5),
50+
// Location = new Point(799, 296.5),
5151
Context = new NodeContext()
5252
{
5353
Type = NodeType.Normal
@@ -57,7 +57,7 @@ public AutoLayoutViewModel()
5757
this.Source.Nodes.Add(new FluxoNodeViewModel()
5858
{
5959
Id = "IfEnd1",
60-
Location = new(1729, 303),
60+
// Location = new(1729, 303),
6161
Context = new NodeContext()
6262
{
6363
Type = NodeType.Empty
@@ -68,7 +68,7 @@ public AutoLayoutViewModel()
6868
{
6969
Id = "If2",
7070
Name = "Branch",
71-
Location = new(1029, 177),
71+
// Location = new(1029, 177),
7272
Context = new NodeContext()
7373
{
7474
Type = NodeType.Normal
@@ -79,7 +79,7 @@ public AutoLayoutViewModel()
7979
{
8080
Id = "Process3",
8181
Name = "Process",
82-
Location = new(1306, 228.5),
82+
// Location = new(1306, 228.5),
8383
Context = new NodeContext()
8484
{
8585
Type = NodeType.Normal
@@ -90,7 +90,7 @@ public AutoLayoutViewModel()
9090
{
9191
Id = "Process4",
9292
Name = "Process",
93-
Location = new(1306, 142),
93+
// Location = new(1306, 142),
9494
Context = new NodeContext()
9595
{
9696
Type = NodeType.Normal
@@ -100,7 +100,7 @@ public AutoLayoutViewModel()
100100
this.Source.Nodes.Add(new FluxoNodeViewModel()
101101
{
102102
Id = "IfEnd2",
103-
Location = new(1618, 207),
103+
// Location = new(1618, 207),
104104
Context = new NodeContext()
105105
{
106106
Type = NodeType.Empty
@@ -111,7 +111,7 @@ public AutoLayoutViewModel()
111111
{
112112
Id = "End",
113113
Name = "End",
114-
Location = new(1917, 290),
114+
// Location = new(1917, 290),
115115
Context = new NodeContext()
116116
{
117117
Type = NodeType.Normal

YCode.Designer.Fluxo/FluxoDesigner.cs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,83 @@ private void OnRenderGrid(DrawingContext dc)
571571
}
572572
}
573573

574+
public void AutoLayout()
575+
{
576+
//TODO: AutoLayout
577+
578+
var nodes = this.ItemsHost.Children.OfType<FluxoNode>();
579+
580+
var pos = new Point(this.ViewportLocation.X + 160d, this.ViewportLocation.Y + 100d);
581+
582+
Sorting();
583+
584+
void Sorting()
585+
{
586+
var span = 100d;
587+
588+
var root = nodes.FirstOrDefault(x => x.Lines.All(y => x.NodeId.Equals(y.SourceId)));
589+
590+
if (root != null)
591+
{
592+
var tree = BuildTree(root, 0);
593+
594+
Hierarchy(tree, pos);
595+
}
596+
597+
void Hierarchy(FluxoLayoutTree root, Point position)
598+
{
599+
if (root.Nexts.Count == 0)
600+
{
601+
return;
602+
}
603+
604+
var start = root.Node.Location.Y - (root.Nexts.Count - 1) * span / 2;
605+
606+
for (var i = 0; i < root.Nexts.Count; i++)
607+
{
608+
var location = new Point(root.Node.Location.X + root.Node.ActualWidth + span, start + i * span);
609+
610+
Translate(root.Nexts[i], location);
611+
612+
Hierarchy(root.Nexts[i], location);
613+
}
614+
}
615+
616+
void Translate(FluxoLayoutTree node, Point location)
617+
{
618+
var dy = location.Y - node.Node.Location.Y;
619+
620+
node.Node.Location = location;
621+
622+
foreach (var child in node.Nexts)
623+
{
624+
Translate(child,
625+
new Point(node.Node.Location.X + node.Node.ActualWidth + span, child.Node.Location.Y + dy));
626+
}
627+
}
628+
}
629+
630+
FluxoLayoutTree BuildTree(FluxoNode root, int depth)
631+
{
632+
var tree = new FluxoLayoutTree(root)
633+
{
634+
Depth = depth
635+
};
636+
637+
var nexts = root.Lines.Where(x => root.NodeId.Equals(x.SourceId)).Select(x => x.Target);
638+
639+
foreach (var next in nexts)
640+
{
641+
if (next != null)
642+
{
643+
tree.AddNext(BuildTree(next, depth + 1));
644+
}
645+
}
646+
647+
return tree;
648+
}
649+
}
650+
574651
private static void OnCanAutoPanningChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
575652
{
576653
if (d is FluxoDesigner designer && e.NewValue is bool canAutoPanning)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace YCode.Designer.Fluxo;
2+
3+
internal class FluxoLayoutTree(FluxoNode node)
4+
{
5+
public FluxoNode Node { get; set; } = node;
6+
7+
public FluxoLayoutTree? Parent { get; set; }
8+
9+
public int Depth { get; set; }
10+
11+
public List<FluxoLayoutTree> Nexts { get; } = [];
12+
13+
public void AddNext(FluxoLayoutTree tree)
14+
{
15+
tree.Parent = this;
16+
17+
this.Nexts.Add(tree);
18+
}
19+
}

0 commit comments

Comments
 (0)