using System.Collections.Generic;
using Artemis.Core;
namespace Artemis.UI.Shared.Services.NodeEditor.Commands;
///
/// Represents a node editor command that can be used to connect two pins.
///
public class ConnectPins : INodeEditorCommand
{
private readonly IPin _source;
private readonly IPin _target;
private readonly List? _originalConnections;
///
/// Creates a new instance of the class.
///
/// The source of the connection.
/// The target of the connection.
public ConnectPins(IPin source, IPin target)
{
_source = source;
_target = target;
_originalConnections = _target.Direction == PinDirection.Input ? new List(_target.ConnectedTo) : null;
}
#region Implementation of INodeEditorCommand
///
public string DisplayName => "Connect pins";
///
public void Execute()
{
if (_target.Direction == PinDirection.Input)
_target.DisconnectAll();
_source.ConnectTo(_target);
}
///
public void Undo()
{
_target.DisconnectFrom(_source);
if (_originalConnections == null)
return;
foreach (IPin pin in _originalConnections)
_target.ConnectTo(pin);
}
#endregion
}