1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Nodes - Disconnect pins that changed their type but stayed an enum

This commit is contained in:
Robert 2022-08-25 19:27:58 +02:00
parent 30be7a9915
commit da0d3abbdd
4 changed files with 47 additions and 24 deletions

View File

@ -94,18 +94,11 @@ public sealed class InputPin : Pin
/// <param name="type">The new type of the pin.</param> /// <param name="type">The new type of the pin.</param>
public void ChangeType(Type type) public void ChangeType(Type type)
{ {
if (_type == type) if (type == _type)
return; return;
// Disconnect pins incompatible with the new type base.ChangeType(type, ref _type);
List<IPin> toDisconnect = ConnectedTo.Where(p => !p.IsTypeCompatible(type)).ToList();
foreach (IPin pin in toDisconnect)
DisconnectFrom(pin);
// Change the type
SetAndNotify(ref _type, type, nameof(Type));
Value = type.GetDefault(); Value = type.GetDefault();
IsNumeric = type == typeof(Numeric);
} }
private void Evaluate() private void Evaluate()
@ -117,10 +110,10 @@ public sealed class InputPin : Pin
else else
Value = Type.GetDefault()!; Value = Type.GetDefault()!;
} }
else else if (ConnectedTo.Count > 0)
{
Value = ConnectedTo[0].PinValue; Value = ConnectedTo[0].PinValue;
} else
Value = null;
} }
#endregion #endregion

View File

@ -85,6 +85,7 @@ public interface IPin
/// Determines whether this pin is compatible with the given type /// Determines whether this pin is compatible with the given type
/// </summary> /// </summary>
/// <param name="type">The type to check for compatibility</param> /// <param name="type">The type to check for compatibility</param>
/// <param name="forgivingEnumMatching">A boolean indicating whether or not enums should be exactly equal or just both be enums</param>
/// <returns><see langword="true" /> if the type is compatible, otherwise <see langword="false" />.</returns> /// <returns><see langword="true" /> if the type is compatible, otherwise <see langword="false" />.</returns>
public bool IsTypeCompatible(Type type); public bool IsTypeCompatible(Type type, bool forgivingEnumMatching = true);
} }

View File

@ -84,15 +84,11 @@ public sealed class OutputPin : Pin
/// <param name="type">The new type of the pin.</param> /// <param name="type">The new type of the pin.</param>
public void ChangeType(Type type) public void ChangeType(Type type)
{ {
// Disconnect pins incompatible with the new type if (type == _type)
List<IPin> toDisconnect = ConnectedTo.Where(p => !p.IsTypeCompatible(type)).ToList(); return;
foreach (IPin pin in toDisconnect)
DisconnectFrom(pin);
// Change the type base.ChangeType(type, ref _type);
SetAndNotify(ref _type, type, nameof(Type));
Value = type.GetDefault(); Value = type.GetDefault();
IsNumeric = type == typeof(Numeric);
} }
#endregion #endregion

View File

@ -127,14 +127,47 @@ public abstract class Pin : CorePropertyChanged, IPin
} }
/// <inheritdoc /> /// <inheritdoc />
public bool IsTypeCompatible(Type type) public bool IsTypeCompatible(Type type, bool forgivingEnumMatching = true)
{ {
return Type == type return Type == type
|| (Type == typeof(Enum) && type.IsEnum) || (Direction == PinDirection.Input && Type == typeof(Enum) && type.IsEnum && forgivingEnumMatching)
|| (Type.IsEnum && type == typeof(Enum)) || (Direction == PinDirection.Output && type == typeof(Enum) && Type.IsEnum && forgivingEnumMatching)
|| (Direction == PinDirection.Input && Type == typeof(object)) || (Direction == PinDirection.Input && Type == typeof(object))
|| (Direction == PinDirection.Output && type == typeof(object)); || (Direction == PinDirection.Output && type == typeof(object));
} }
/// <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)
{
// Enums are a special case that disconnect and, if still compatible, reconnect
if (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)))
ConnectTo(pin);
}
// Disconnect pins incompatible with the new type
else
{
List<IPin> toDisconnect = ConnectedTo.Where(p => !p.IsTypeCompatible(type, false)).ToList();
foreach (IPin pin in toDisconnect)
DisconnectFrom(pin);
// Change the type
SetAndNotify(ref currentType, type, nameof(Type));
IsNumeric = type == typeof(Numeric);
}
}
#endregion #endregion
} }