Nodes - Made types mutable for non-generic pins/collections

This commit is contained in:
Robert committed 2022-03-24 23:47:40 +01:00
1 parent 30ec28a9a9
commit f824f16658
29 files changed
+303 -259

No files matched your search

+1 -1
View File
@@ -257,7 +257,7 @@ namespace Artemis.Core
/// <summary>
/// Returns a boolean indicating whether the provided type can be used as a <see cref="Numeric" />.
/// </summary>
public static bool IsTypeCompatible(Type type)
public static bool IsTypeCompatible(Type? type)
{
return type == typeof(Numeric) ||
type == typeof(float) ||
+24 -2
View File
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
namespace Artemis.Core
@@ -76,7 +78,7 @@ namespace Artemis.Core
internal InputPin(INode node, Type type, string name)
: base(node, name)
{
Type = type;
_type = type;
_value = type.GetDefault();
}
@@ -84,6 +86,25 @@ namespace Artemis.Core
#region Methods
/// <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>
public void ChangeType(Type type)
{
if (_type == type)
return;
// Disconnect pins incompatible with the new 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();
}
private void Evaluate()
{
if (Type.IsValueType)
@@ -104,7 +125,7 @@ namespace Artemis.Core
#region Properties & Fields
/// <inheritdoc />
public override Type Type { get; }
public override Type Type => _type;
/// <inheritdoc />
public override object? PinValue => Value;
@@ -113,6 +134,7 @@ namespace Artemis.Core
public override PinDirection Direction => PinDirection.Input;
private object? _value;
private Type _type;
/// <summary>
/// Gets or sets the value of the input pin
@@ -59,12 +59,14 @@ namespace Artemis.Core
/// </summary>
public sealed class InputPinCollection : PinCollection
{
private Type _type;
#region Constructors
internal InputPinCollection(INode node, Type type, string name, int initialCount)
: base(node, name)
{
Type = type;
_type = type;
// Can't do this in the base constructor because the type won't be set yet
for (int i = 0; i < initialCount; i++)
@@ -75,6 +77,20 @@ namespace Artemis.Core
#region Methods
/// <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>
public void ChangeType(Type type)
{
if (_type == type)
return;
foreach (IPin pin in Pins)
(pin as InputPin)?.ChangeType(type);
SetAndNotify(ref _type, type, nameof(Type));
}
/// <inheritdoc />
public override IPin CreatePin()
{
@@ -89,17 +105,12 @@ namespace Artemis.Core
public override PinDirection Direction => PinDirection.Input;
/// <inheritdoc />
public override Type Type { get; }
/// <summary>
/// Gets an enumerable of the pins in this collection
/// </summary>
public new IEnumerable<InputPin> Pins => base.Pins.Cast<InputPin>();
public override Type Type => _type;
/// <summary>
/// Gets an enumerable of the values of the pins in this collection
/// </summary>
public IEnumerable Values => Pins.Where(p => p.Value != null).Select(p => p.Value);
public IEnumerable Values => Pins.Where(p => p.PinValue != null).Select(p => p.PinValue);
#endregion
}
@@ -75,5 +75,12 @@ namespace Artemis.Core
/// Disconnects all pins this pin is connected to
/// </summary>
void DisconnectAll();
/// <summary>
/// Determines whether this pin is compatible with the given type
/// </summary>
/// <param name="type">The type to check for compatibility</param>
/// <returns><see langword="true"/> if the type is compatible, otherwise <see langword="false"/>.</returns>
public bool IsTypeCompatible(Type type);
}
}
+25 -2
View File
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
namespace Artemis.Core
@@ -66,16 +68,36 @@ namespace Artemis.Core
internal OutputPin(INode node, Type type, string name)
: base(node, name)
{
Type = type;
_type = type;
_value = type.GetDefault();
}
#endregion
#region Methods
/// <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>
public void ChangeType(Type type)
{
// Disconnect pins incompatible with the new 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();
}
#endregion
#region Properties & Fields
/// <inheritdoc />
public override Type Type { get; }
public override Type Type => _type;
/// <inheritdoc />
public override object? PinValue => Value;
@@ -84,6 +106,7 @@ namespace Artemis.Core
public override PinDirection Direction => PinDirection.Output;
private object? _value;
private Type _type;
/// <summary>
/// Gets or sets the value of the output pin
+9
View File
@@ -101,6 +101,9 @@ namespace Artemis.Core
/// <inheritdoc />
public void DisconnectAll()
{
if (!_connectedTo.Any())
return;
List<IPin> connectedPins = new(_connectedTo);
_connectedTo.Clear();
@@ -114,6 +117,12 @@ namespace Artemis.Core
OnPropertyChanged(nameof(ConnectedTo));
}
/// <inheritdoc />
public bool IsTypeCompatible(Type type) => Type == type
|| Type == typeof(Enum) && type.IsEnum
|| Direction == PinDirection.Input && Type == typeof(object)
|| Direction == PinDirection.Output && type == typeof(object);
#endregion
}
}
@@ -7,7 +7,7 @@ using Artemis.Core.Events;
namespace Artemis.Core
{
/// <inheritdoc cref="IPinCollection"/>
public abstract class PinCollection : IPinCollection
public abstract class PinCollection : CorePropertyChanged, IPinCollection
{
#region Constructors