123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- using System.Collections.ObjectModel;
- using Avalonia;
- using ChunkyImageLib;
- using CommunityToolkit.Mvvm.ComponentModel;
- using PixiEditor.AvaloniaUI.Models.DocumentModels;
- using PixiEditor.AvaloniaUI.Models.Handlers;
- using PixiEditor.AvaloniaUI.Models.Structures;
- using PixiEditor.AvaloniaUI.ViewModels.Document;
- using PixiEditor.ChangeableDocument.Actions.Generated;
- using PixiEditor.ChangeableDocument.Changeables.Interfaces;
- using PixiEditor.Numerics;
- namespace PixiEditor.AvaloniaUI.ViewModels.Nodes;
- internal class NodeViewModel : ObservableObject, INodeHandler
- {
- private string nodeName;
- private VecD position;
- private ObservableRangeCollection<INodePropertyHandler> inputs = new();
- private ObservableRangeCollection<INodePropertyHandler> outputs = new();
- private Surface resultPreview;
- private bool isSelected;
- protected Guid id;
- public Guid Id
- {
- get => id;
- init => id = value;
- }
- public string NodeName
- {
- get => nodeName;
- set => SetProperty(ref nodeName, value);
- }
- public VecD PositionBindable
- {
- get => position;
- set
- {
- if (!Document.UpdateableChangeActive)
- {
- Internals.ActionAccumulator.AddFinishedActions(
- new NodePosition_Action(Id, value),
- new EndNodePosition_Action());
- }
- }
- }
- public ObservableRangeCollection<INodePropertyHandler> Inputs
- {
- get => inputs;
- set => SetProperty(ref inputs, value);
- }
- public ObservableRangeCollection<INodePropertyHandler> Outputs
- {
- get => outputs;
- set => SetProperty(ref outputs, value);
- }
- public Surface ResultPreview
- {
- get => resultPreview;
- set => SetProperty(ref resultPreview, value);
- }
-
- public bool IsSelected
- {
- get => isSelected;
- set => SetProperty(ref isSelected, value);
- }
- internal DocumentViewModel Document { get; init; }
- internal DocumentInternalParts Internals { get; init; }
-
-
- public NodeViewModel()
- {
-
- }
- public NodeViewModel(string nodeName, Guid id, VecD position, DocumentViewModel document, DocumentInternalParts internals)
- {
- this.nodeName = nodeName;
- this.id = id;
- this.position = position;
- Document = document;
- Internals = internals;
- }
-
- public void SetPosition(VecD newPosition)
- {
- position = newPosition;
- OnPropertyChanged(nameof(PositionBindable));
- }
- public void TraverseBackwards(Func<INodeHandler, bool> func)
- {
- var visited = new HashSet<INodeHandler>();
- var queueNodes = new Queue<INodeHandler>();
- queueNodes.Enqueue(this);
- while (queueNodes.Count > 0)
- {
- var node = queueNodes.Dequeue();
- if (!visited.Add(node))
- {
- continue;
- }
- if (!func(node))
- {
- return;
- }
- foreach (var inputProperty in node.Inputs)
- {
- if (inputProperty.ConnectedOutput != null)
- {
- queueNodes.Enqueue(inputProperty.ConnectedOutput.Node);
- }
- }
- }
- }
- public void TraverseForwards(Func<INodeHandler, bool> func)
- {
- var visited = new HashSet<INodeHandler>();
- var queueNodes = new Queue<INodeHandler>();
- queueNodes.Enqueue(this);
- while (queueNodes.Count > 0)
- {
- var node = queueNodes.Dequeue();
- if (!visited.Add(node))
- {
- continue;
- }
- if (!func(node))
- {
- return;
- }
- foreach (var outputProperty in node.Outputs)
- {
- foreach (var connection in outputProperty.ConnectedInputs)
- {
- queueNodes.Enqueue(connection.Node);
- }
- }
- }
- }
- public NodePropertyViewModel FindInputProperty(string propName)
- {
- return Inputs.FirstOrDefault(x => x.PropertyName == propName) as NodePropertyViewModel;
- }
-
- public NodePropertyViewModel FindOutputProperty(string propName)
- {
- return Outputs.FirstOrDefault(x => x.PropertyName == propName) as NodePropertyViewModel;
- }
- }
|