Skip to content

Commit 69a2516

Browse files
author
YCode
committed
chore: 自动布局
1 parent fa7a6c0 commit 69a2516

File tree

6 files changed

+64
-7
lines changed

6 files changed

+64
-7
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
<Grid Background="#1E1E1E">
6060
<ycode:FluxoDesigner x:Name="Designer"
6161
GridType="Dot"
62-
Orientation="Vertical"
6362
Source="{Binding Source}"
6463
ToolTemplate="{StaticResource ToolDataTemplate}"
6564
ItemTemplateSelector="{StaticResource NodeTemplateSelector}"

YCode.Designer.Demo/ViewModels/AutoLayoutViewModel.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public AutoLayoutViewModel()
5858
{
5959
Id = "IfEnd1",
6060
// Location = new(1729, 303),
61+
IsEmpty = true,
6162
Context = new NodeContext()
6263
{
6364
Type = NodeType.Empty
@@ -101,6 +102,7 @@ public AutoLayoutViewModel()
101102
{
102103
Id = "IfEnd2",
103104
// Location = new(1618, 207),
105+
IsEmpty = true,
104106
Context = new NodeContext()
105107
{
106108
Type = NodeType.Empty

YCode.Designer.Fluxo/FluxoDesigner.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,8 @@ public void AutoLayout()
589589

590590
var dfsScope = nodes.ToDictionary(k => k, v => 0);
591591

592+
var emptyNodes = new List<FluxoLayoutTree>();
593+
592594
var pos = new Point(this.ViewportLocation.X + 160d, this.ViewportLocation.Y + 100d);
593595

594596
var hashTree = new Dictionary<int, List<FluxoLayoutTree>>();
@@ -608,6 +610,8 @@ void Sorting()
608610
BuildHash(tree, 0);
609611

610612
Hierarchy(tree, pos);
613+
614+
EmptyReLayout();
611615
}
612616

613617
void Hierarchy(FluxoLayoutTree root, Point position)
@@ -642,6 +646,36 @@ void Translate(FluxoLayoutTree node, Point location)
642646
}
643647
}
644648

649+
void EmptyReLayout()
650+
{
651+
foreach (var emptyNode in emptyNodes)
652+
{
653+
var sources = emptyNode.Node.Lines
654+
.Where(x => emptyNode.Node.NodeId.Equals(x.TargetId))
655+
.Select(x => x.Source)
656+
.OrderByDescending(x => x?.Location.X);
657+
658+
var maxX = sources.FirstOrDefault();
659+
660+
var x = maxX != null && maxX.Location.X + maxX.ActualWidth + span > emptyNode.Node.Location.X
661+
? maxX.Location.X + maxX.ActualWidth + span
662+
: emptyNode.Node.Location.X;
663+
664+
var y = (sources.FirstOrDefault()?.Location.Y + sources.LastOrDefault()?.Location.Y +
665+
sources.LastOrDefault()?.ActualHeight) / 2 ?? emptyNode.Node.Location.Y;
666+
667+
emptyNode.Node.Location = new Point(x, y);
668+
669+
for (var i = 0; i < emptyNode.Nexts.Count; i++)
670+
{
671+
if (!emptyNode.Nexts[i].Node.IsEmpty)
672+
{
673+
Hierarchy(emptyNode.Nexts[i], new Point(x + span, y + i * span));
674+
}
675+
}
676+
}
677+
}
678+
645679
void BuildHash(FluxoLayoutTree root, int depth)
646680
{
647681
if (hashTree.TryGetValue(depth, out var value))
@@ -664,6 +698,11 @@ FluxoLayoutTree BuildTree(FluxoNode root, int depth)
664698
Depth = depth
665699
};
666700

701+
if (root.IsEmpty)
702+
{
703+
emptyNodes.Add(tree);
704+
}
705+
667706
dfsScope[root] = 1;
668707

669708
var nexts = root.Lines.Where(x => root.NodeId.Equals(x.SourceId)).Select(x => x.Target);

YCode.Designer.Fluxo/Nodes/FluxoNode.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ public FluxoNode(FluxoDesigner designer)
6060
public static readonly DependencyProperty SelectedBrushProperty = DependencyProperty.Register(
6161
nameof(SelectedBrush), typeof(Brush), typeof(FluxoNode), new PropertyMetadata(Brushes.Orange));
6262

63+
public static readonly DependencyProperty IsEmptyProperty = DependencyProperty.Register(
64+
nameof(IsEmpty), typeof(bool), typeof(FluxoNode), new PropertyMetadata(false));
65+
66+
public bool IsEmpty
67+
{
68+
get => (bool)GetValue(IsEmptyProperty);
69+
set => SetValue(IsEmptyProperty, value);
70+
}
71+
6372
public Brush SelectedBrush
6473
{
6574
get => (Brush)GetValue(SelectedBrushProperty);

YCode.Designer.Fluxo/Themes/Views/Nodes.xaml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@
214214
<Style TargetType="{x:Type local:FluxoNode}">
215215
<Setter Property="NodeId" Value="{Binding Id}" />
216216
<Setter Property="Location" Value="{Binding Location}" />
217+
<Setter Property="IsEmpty" Value="{Binding IsEmpty}" />
217218
<Setter Property="BorderThickness" Value="1.8" />
218219
<Setter Property="Background" Value="CadetBlue" />
219220
<Setter Property="BorderBrush" Value="DodgerBlue" />
@@ -234,14 +235,14 @@
234235
CornerRadius="{StaticResource Radius}">
235236
<ContentPresenter />
236237
</Border>
238+
<ControlTemplate.Triggers>
239+
<Trigger Property="IsSelected" Value="True">
240+
<Setter Property="Panel.ZIndex" Value="1" />
241+
<Setter Property="BorderBrush" Value="{Binding SelectedBrush, RelativeSource={RelativeSource Self}}" />
242+
</Trigger>
243+
</ControlTemplate.Triggers>
237244
</ControlTemplate>
238245
</Setter.Value>
239246
</Setter>
240-
<Style.Triggers>
241-
<Trigger Property="IsSelected" Value="True">
242-
<Setter Property="Panel.ZIndex" Value="1" />
243-
<Setter Property="BorderBrush" Value="{Binding SelectedBrush, RelativeSource={RelativeSource Self}}" />
244-
</Trigger>
245-
</Style.Triggers>
246247
</Style>
247248
</ResourceDictionary>

YCode.Designer.Fluxo/ViewModels/FluxoNodeViewModel.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public class FluxoNodeViewModel : FluxoNotifyPropertyChanged
77
private string _description = String.Empty;
88
private Point _location = default(Point);
99
private object? _context;
10+
private bool _isEmpty;
1011

1112
public string Id
1213
{
@@ -32,6 +33,12 @@ public Point Location
3233
set => this.OnPropertyChanged(ref _location, value);
3334
}
3435

36+
public bool IsEmpty
37+
{
38+
get => _isEmpty;
39+
set => this.OnPropertyChanged(ref _isEmpty, value);
40+
}
41+
3542
public object? Context
3643
{
3744
get => _context;

0 commit comments

Comments
 (0)