mirror of
https://github.com/Artemis-RGB/Artemis
synced 2026-11-09 18:36:27 +00:00
* Core - Keep nodes of missing plugins and the connection an input reads from in node scripts - Saving a script keeps nodes whose plugin is missing, and their connections, so they come back once the plugin is available again - Loading keeps the first connection of an input, the one it reads from - Disabling a node provider only removes the nodes it created, not those of another provider registering the same type - RemoveNode only raises NodeRemoved for nodes that were in the script * Core - Fix pin connections, null values and failing nodes - Connecting pins that are already connected no longer adds a second connection - Untyped pins turn null into the default value for value types, as intended - A new InputPin<T> reads its connection right away instead of after its first reset - Matching a specific enum to a pin of any enum now depends on forgivingEnumMatching - A node that fails to evaluate sets its outputs to their default values, so it is evaluated once per run and old values don't flow on * Core - Round Numeric to long and clamp it to the range of an sbyte
177 lines
4.7 KiB
C#
177 lines
4.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Linq;
|
|
using Artemis.Core.Events;
|
|
|
|
namespace Artemis.Core;
|
|
|
|
/// <inheritdoc cref="IPin" />
|
|
public abstract class Pin : CorePropertyChanged, IPin
|
|
{
|
|
#region Constructors
|
|
|
|
/// <summary>
|
|
/// Creates a new instance of the <see cref="Pin" /> class on the provided node with the provided name
|
|
/// </summary>
|
|
/// <param name="node">The node the pin belongs to</param>
|
|
/// <param name="name">The name of the pin</param>
|
|
protected Pin(INode node, string name = "")
|
|
{
|
|
Node = node;
|
|
_name = name;
|
|
}
|
|
|
|
#endregion
|
|
|
|
/// <inheritdoc />
|
|
public event EventHandler<SingleValueEventArgs<IPin>>? PinConnected;
|
|
|
|
/// <inheritdoc />
|
|
public event EventHandler<SingleValueEventArgs<IPin>>? PinDisconnected;
|
|
|
|
#region Properties & Fields
|
|
|
|
/// <inheritdoc />
|
|
public INode Node { get; }
|
|
|
|
/// <inheritdoc />
|
|
public string Name
|
|
{
|
|
get => _name;
|
|
set => SetAndNotify(ref _name, value);
|
|
}
|
|
|
|
private bool _isEvaluated;
|
|
|
|
/// <inheritdoc />
|
|
public bool IsEvaluated
|
|
{
|
|
get => _isEvaluated;
|
|
set => SetAndNotify(ref _isEvaluated, value);
|
|
}
|
|
|
|
private bool _isNumeric;
|
|
|
|
/// <inheritdoc />
|
|
public bool IsNumeric
|
|
{
|
|
get => _isNumeric;
|
|
protected set => SetAndNotify(ref _isNumeric, value);
|
|
}
|
|
|
|
private readonly List<IPin> _connectedTo = [];
|
|
private string _name;
|
|
|
|
/// <inheritdoc />
|
|
public IReadOnlyList<IPin> ConnectedTo => new ReadOnlyCollection<IPin>(_connectedTo);
|
|
|
|
/// <inheritdoc />
|
|
public abstract PinDirection Direction { get; }
|
|
|
|
/// <inheritdoc />
|
|
public abstract Type Type { get; }
|
|
|
|
/// <inheritdoc />
|
|
public abstract object? PinValue { get; }
|
|
|
|
#endregion
|
|
|
|
#region Methods
|
|
|
|
/// <inheritdoc />
|
|
public void Reset()
|
|
{
|
|
IsEvaluated = false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets the value of an output pin to the default value of its type, used when its node fails to evaluate
|
|
/// </summary>
|
|
internal virtual void SetDefaultValue()
|
|
{
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public void ConnectTo(IPin pin)
|
|
{
|
|
if (_connectedTo.Contains(pin))
|
|
return;
|
|
|
|
_connectedTo.Add(pin);
|
|
if (!pin.ConnectedTo.Contains(this))
|
|
pin.ConnectTo(this);
|
|
|
|
OnPropertyChanged(nameof(ConnectedTo));
|
|
PinConnected?.Invoke(this, new SingleValueEventArgs<IPin>(pin));
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public void DisconnectFrom(IPin pin)
|
|
{
|
|
_connectedTo.Remove(pin);
|
|
if (pin.ConnectedTo.Contains(this))
|
|
pin.DisconnectFrom(this);
|
|
|
|
OnPropertyChanged(nameof(ConnectedTo));
|
|
PinDisconnected?.Invoke(this, new SingleValueEventArgs<IPin>(pin));
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public void DisconnectAll()
|
|
{
|
|
if (!_connectedTo.Any())
|
|
return;
|
|
|
|
List<IPin> connectedPins = new(_connectedTo);
|
|
_connectedTo.Clear();
|
|
|
|
foreach (IPin pin in connectedPins)
|
|
{
|
|
PinDisconnected?.Invoke(this, new SingleValueEventArgs<IPin>(pin));
|
|
if (pin.ConnectedTo.Contains(this))
|
|
pin.DisconnectFrom(this);
|
|
}
|
|
|
|
OnPropertyChanged(nameof(ConnectedTo));
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public bool IsTypeCompatible(Type type, bool forgivingEnumMatching = true)
|
|
{
|
|
if (Type == type)
|
|
return true;
|
|
|
|
// A specific enum and a pin of any enum only match when forgiving, every enum is assignable to Enum otherwise
|
|
if ((Direction == PinDirection.Input && Type == typeof(Enum) && type.IsEnum) || (Direction == PinDirection.Output && type == typeof(Enum) && Type.IsEnum))
|
|
return forgivingEnumMatching;
|
|
|
|
return (Direction == PinDirection.Input && type.IsAssignableTo(Type))
|
|
|| (Direction == PinDirection.Output && type.IsAssignableFrom(Type));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Changes the type of this pin, disconnecting any pins that are incompatible with the new type.
|
|
/// </summary>
|
|
/// <param name="type">The new type of the pin.</param>
|
|
/// <param name="currentType">The backing field of the current type of the pin.</param>
|
|
protected void ChangeType(Type type, ref Type currentType)
|
|
{
|
|
if (currentType == type)
|
|
return;
|
|
|
|
bool changingEnums = type.IsEnum && currentType.IsEnum;
|
|
|
|
List<IPin> connections = new(ConnectedTo);
|
|
DisconnectAll();
|
|
|
|
// Change the type
|
|
SetAndNotify(ref currentType, type, nameof(Type));
|
|
IsNumeric = type == typeof(Numeric);
|
|
|
|
foreach (IPin pin in connections.Where(p => p.IsTypeCompatible(type, changingEnums)))
|
|
ConnectTo(pin);
|
|
}
|
|
|
|
#endregion
|
|
} |