From 7c586b04261902745a7456376d0c4ddab12cfadb Mon Sep 17 00:00:00 2001 From: MakesYT <42534870+MakesYT@users.noreply.github.com> Date: Tue, 6 Feb 2024 19:39:34 +0800 Subject: [PATCH] - Added automatic adaptation to display all Nodes when NodifyEditor is initialized - Optimize child node search --- .../MainWindowViewModel.cs | 3 +- .../Controls/NodifyEditor.axaml.cs | 52 ++++++++++++++++++- .../Helpers/DependencyObjectExtensions.cs | 17 +++--- README.md | 3 ++ 4 files changed, 65 insertions(+), 10 deletions(-) diff --git a/NodifyM.Avalonia.Example/MainWindowViewModel.cs b/NodifyM.Avalonia.Example/MainWindowViewModel.cs index a2b9b68..fd7f86f 100644 --- a/NodifyM.Avalonia.Example/MainWindowViewModel.cs +++ b/NodifyM.Avalonia.Example/MainWindowViewModel.cs @@ -29,7 +29,7 @@ public MainWindowViewModel() Nodes =new(){ new NodeViewModelBase() { - Location = new Point(400, 100), + Location = new Point(400, 2000), Title = "Node 1", Input = new ObservableCollection { @@ -49,6 +49,7 @@ public MainWindowViewModel() new NodeViewModelBase() { Title = "Node 2", + Location = new Point(-100,-100), Input = new ObservableCollection { new ConnectorViewModelBase() diff --git a/NodifyM.Avalonia/Controls/NodifyEditor.axaml.cs b/NodifyM.Avalonia/Controls/NodifyEditor.axaml.cs index e9fdbf1..e3e6061 100644 --- a/NodifyM.Avalonia/Controls/NodifyEditor.axaml.cs +++ b/NodifyM.Avalonia/Controls/NodifyEditor.axaml.cs @@ -197,10 +197,60 @@ protected override void OnApplyTemplate(TemplateAppliedEventArgs e) RenderTransform = renderTransform; AutoPanningTimer=new DispatcherTimer(TimeSpan.FromMilliseconds(10), DispatcherPriority.Normal,HandleAutoPanning); AutoPanningTimer.Stop(); - ViewTranslateTransform = new TranslateTransform(); + AlignmentLine = new AvaloniaList(); + } + protected override void OnLoaded(RoutedEventArgs e) + { + base.OnLoaded(e); + var T = 0.0d; + var L = 0.0d; + var B = this.Bounds.Height; + var R = this.Bounds.Width; + var childOfType = this.GetChildOfType("NodeItemsPresenter"); + foreach (var logicalChild in childOfType.GetVisualChildren()) + { + var logicalChildLogicalChild = ((BaseNode)logicalChild.GetVisualChildren().First()); + var location = logicalChildLogicalChild.Location; + if (location.Y< L) + { + L = location.Y; + } + if (location.X < T) + { + T = location.X; + } + if (location.Y+ logicalChildLogicalChild.Bounds.Height> B) + { + B = location.Y+ logicalChildLogicalChild.Bounds.Height; + } + if (location.X + logicalChildLogicalChild.Bounds.Width > R) + { + R = location.X + logicalChildLogicalChild.Bounds.Width; + } + + } + ViewTranslateTransform = new TranslateTransform(-L,-T); + OffsetY = -T; + OffsetX = -L; + if (1/(Math.Abs(T - B) / _initHeight) diff --git a/NodifyM.Avalonia/Helpers/DependencyObjectExtensions.cs b/NodifyM.Avalonia/Helpers/DependencyObjectExtensions.cs index 5929397..256fc0a 100644 --- a/NodifyM.Avalonia/Helpers/DependencyObjectExtensions.cs +++ b/NodifyM.Avalonia/Helpers/DependencyObjectExtensions.cs @@ -1,4 +1,5 @@ -using Avalonia; +using System.Diagnostics; +using Avalonia; using Avalonia.Controls; using Avalonia.VisualTree; @@ -36,13 +37,13 @@ internal static class DependencyObjectExtensions public static T? GetChildOfType(this Control control, string? name = null) where T : Control { - var stack = new Stack(); - stack.Push(control); + var queue = new Queue(); + queue.Enqueue(control); - while (stack.Count > 0) + while (queue.Count > 0) { - var currentControl = stack.Pop(); - + var currentControl = queue.Dequeue(); + if (string.IsNullOrEmpty(name) && currentControl is T targetControl) { return targetControl; @@ -60,8 +61,8 @@ internal static class DependencyObjectExtensions return targetChild; } } - - stack.Push(childControl); + + queue.Enqueue(childControl); } } } diff --git a/README.md b/README.md index 2c63ee4..2fb55bb 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,9 @@ This project is a refactoring of [Nodify](https://github.com/miroiu/nodify) on t #### You can git clone the project and run `NodifyM.Avalonia.Example` ## Changelog +### 1.0.11 +- Added automatic adaptation to display all Nodes when NodifyEditor is initialized +- Optimize child node search ### 1.0.10 - Fixed Node Header/Input/OutputTemplate allow use IDataTemplate - Added Avalonia.Diagnostics Condition