Nodes - Reorganized code

This commit is contained in:
Robert committed 2022-03-30 20:10:43 +02:00
1 parent 69b881b044
commit 7c83d5345f
73 files changed
+1055 -738

No files matched your search

@@ -24,6 +24,7 @@ namespace Artemis.UI.Shared.Controls
private readonly ObservableCollection<(Enum, string)> _currentValues = new();
private ComboBox? _enumComboBox;
private Type _currentType;
/// <summary>
/// Creates a new instance of the <see cref="EnumComboBox" /> class.
@@ -69,11 +70,14 @@ namespace Artemis.UI.Shared.Controls
private void UpdateValues()
{
Type? newType = Value?.GetType();
if (_enumComboBox == null || _currentValues.Any() || newType is not {IsEnum: true})
if (_enumComboBox == null || newType == null || _currentType == newType)
return;
_currentValues.Clear();
foreach ((Enum, string) valueDesc in EnumUtilities.GetAllValuesAndDescriptions(newType))
_currentValues.Add(valueDesc);
_currentType = newType;
}
private void UpdateSelection()
@@ -45,8 +45,8 @@ public class AddNode : INodeEditorCommand, IDisposable
/// <inheritdoc />
public void Dispose()
{
if (_isRemoved)
_nodeScript.Dispose();
if (_isRemoved && _node is IDisposable disposableNode)
disposableNode.Dispose();
}
#endregion
@@ -11,7 +11,7 @@ public class DeleteNode : INodeEditorCommand, IDisposable
{
private readonly INode _node;
private readonly INodeScript _nodeScript;
private readonly Dictionary<IPin, List<IPin>> _pinConnections = new();
private readonly NodeConnectionStore _connections;
private bool _isRemoved;
/// <summary>
@@ -23,58 +23,15 @@ public class DeleteNode : INodeEditorCommand, IDisposable
{
_nodeScript = nodeScript;
_node = node;
}
private void StoreConnections()
{
_pinConnections.Clear();
foreach (IPin nodePin in _node.Pins)
{
_pinConnections.Add(nodePin, new List<IPin>(nodePin.ConnectedTo));
nodePin.DisconnectAll();
}
foreach (IPinCollection nodePinCollection in _node.PinCollections)
{
foreach (IPin nodePin in nodePinCollection)
{
_pinConnections.Add(nodePin, new List<IPin>(nodePin.ConnectedTo));
nodePin.DisconnectAll();
}
}
}
private void RestoreConnections()
{
foreach (IPin nodePin in _node.Pins)
{
if (_pinConnections.TryGetValue(nodePin, out List<IPin>? connections))
{
foreach (IPin connection in connections)
nodePin.ConnectTo(connection);
}
}
foreach (IPinCollection nodePinCollection in _node.PinCollections)
{
foreach (IPin nodePin in nodePinCollection)
{
if (_pinConnections.TryGetValue(nodePin, out List<IPin>? connections))
{
foreach (IPin connection in connections)
nodePin.ConnectTo(connection);
}
}
}
_pinConnections.Clear();
_connections = new NodeConnectionStore(_node);
}
/// <inheritdoc />
public void Dispose()
{
if (_isRemoved)
_nodeScript.Dispose();
if (_isRemoved && _node is IDisposable disposableNode)
disposableNode.Dispose();
}
/// <inheritdoc />
@@ -83,7 +40,7 @@ public class DeleteNode : INodeEditorCommand, IDisposable
/// <inheritdoc />
public void Execute()
{
StoreConnections();
_connections.Store();
_nodeScript.RemoveNode(_node);
_isRemoved = true;
@@ -93,7 +50,7 @@ public class DeleteNode : INodeEditorCommand, IDisposable
public void Undo()
{
_nodeScript.AddNode(_node);
RestoreConnections();
_connections.Restore();
_isRemoved = false;
}
@@ -0,0 +1,73 @@
using System.Collections.Generic;
using Artemis.Core;
namespace Artemis.UI.Shared.Services.NodeEditor;
/// <summary>
/// Represents a class that can store and restore a node's connections
/// </summary>
public class NodeConnectionStore
{
private readonly Dictionary<IPin, List<IPin>> _pinConnections = new();
/// <summary>
/// Creates a new instance of the <see cref="NodeConnectionStore" /> class.
/// </summary>
/// <param name="node">The node whose connections to store</param>
public NodeConnectionStore(INode node)
{
Node = node;
}
/// <summary>
/// Gets the node this instance will store connections for.
/// </summary>
public INode Node { get; }
/// <summary>
/// Stores and clears the current connections of the node
/// </summary>
public void Store()
{
_pinConnections.Clear();
foreach (IPin nodePin in Node.Pins)
{
_pinConnections.Add(nodePin, new List<IPin>(nodePin.ConnectedTo));
nodePin.DisconnectAll();
}
foreach (IPinCollection nodePinCollection in Node.PinCollections)
{
foreach (IPin nodePin in nodePinCollection)
{
_pinConnections.Add(nodePin, new List<IPin>(nodePin.ConnectedTo));
nodePin.DisconnectAll();
}
}
}
/// <summary>
/// Restores the connections of the node as they were during the last <see cref="Store" /> call.
/// </summary>
public void Restore()
{
foreach (IPin nodePin in Node.Pins)
{
if (_pinConnections.TryGetValue(nodePin, out List<IPin>? connections))
foreach (IPin connection in connections)
nodePin.ConnectTo(connection);
}
foreach (IPinCollection nodePinCollection in Node.PinCollections)
{
foreach (IPin nodePin in nodePinCollection)
{
if (_pinConnections.TryGetValue(nodePin, out List<IPin>? connections))
foreach (IPin connection in connections)
nodePin.ConnectTo(connection);
}
}
_pinConnections.Clear();
}
}
+1 -1
View File
@@ -58,7 +58,7 @@ public abstract class ViewModelValidationBase : ReactiveValidationObject
public string? DisplayName
{
get => _displayName;
set => this.RaiseAndSetIfChanged(ref _displayName, value);
set => RaiseAndSetIfChanged(ref _displayName, value);
}
/// <summary>