using System.Collections.Generic;
using System.Linq;
using Artemis.Core;
namespace Artemis.UI.Shared.Services.NodeEditor;
///
/// Represents a class that can store and restore a node's connections
///
public class NodeConnectionStore
{
private readonly Dictionary> _pinConnections = new();
///
/// Creates a new instance of the class.
///
/// The node whose connections to store
public NodeConnectionStore(INode node)
{
Node = node;
}
///
/// Gets the node this instance will store connections for.
///
public INode Node { get; }
///
/// Stores and clears the current connections of the node
///
public void Store()
{
_pinConnections.Clear();
// Iterate to save
foreach (IPin nodePin in Node.Pins.ToList())
_pinConnections.Add(nodePin, new List(nodePin.ConnectedTo));
foreach (IPin nodePin in Node.PinCollections.ToList().SelectMany(nodePinCollection => nodePinCollection))
_pinConnections.Add(nodePin, new List(nodePin.ConnectedTo));
// Iterate to disconnect
foreach (IPin nodePin in Node.Pins.ToList())
nodePin.DisconnectAll();
foreach (IPin nodePin in Node.PinCollections.ToList().SelectMany(nodePinCollection => nodePinCollection))
nodePin.DisconnectAll();
}
///
/// Restores the connections of the node as they were during the last call.
///
public void Restore()
{
foreach ((IPin? pin, List? connections) in _pinConnections)
{
foreach (IPin connection in connections)
pin.ConnectTo(connection);
}
_pinConnections.Clear();
}
}