Node editor - Added node collection pin add/remove

Node editor - Added first static value node
This commit is contained in:
Robert committed 2022-03-20 15:54:36 +01:00
1 parent 2907b86174
commit d9d237e0eb
36 files changed
+575 -188

No files matched your search

+4
View File
@@ -43,6 +43,10 @@ namespace Artemis.Core.Services
if (match != null)
return match;
// Objects represent an input that can take any type, these are hardcoded white
if (type == typeof(object))
return new TypeColorRegistration(type, new SKColor(255, 255, 255, 255), Constants.CorePlugin);
// Come up with a random color based on the type name that should be the same each time
MD5 md5Hasher = MD5.Create();
byte[] hashed = md5Hasher.ComputeHash(Encoding.UTF8.GetBytes(type.FullName!));
+114 -2
View File
@@ -11,7 +11,7 @@ namespace Artemis.Core
/// Usage outside that context is not recommended due to conversion overhead.
/// </para>
/// </summary>
public readonly struct Numeric : IComparable<Numeric>
public readonly struct Numeric : IComparable<Numeric>, IConvertible
{
private readonly float _value;
@@ -140,6 +140,11 @@ namespace Artemis.Core
return p._value;
}
public static implicit operator decimal(Numeric p)
{
return (decimal) p._value;
}
public static implicit operator byte(Numeric p)
{
return (byte) Math.Clamp(p._value, 0, 255);
@@ -260,6 +265,112 @@ namespace Artemis.Core
type == typeof(int) ||
type == typeof(byte);
}
#region Implementation of IConvertible
/// <inheritdoc />
public TypeCode GetTypeCode()
{
return _value.GetTypeCode();
}
/// <inheritdoc />
public bool ToBoolean(IFormatProvider? provider)
{
return Convert.ToBoolean(_value);
}
/// <inheritdoc />
public byte ToByte(IFormatProvider? provider)
{
return (byte) Math.Clamp(_value, 0, 255);
}
/// <inheritdoc />
public char ToChar(IFormatProvider? provider)
{
return Convert.ToChar(_value);
}
/// <inheritdoc />
public DateTime ToDateTime(IFormatProvider? provider)
{
return Convert.ToDateTime(_value);
}
/// <inheritdoc />
public decimal ToDecimal(IFormatProvider? provider)
{
return (decimal) _value;
}
/// <inheritdoc />
public double ToDouble(IFormatProvider? provider)
{
return _value;
}
/// <inheritdoc />
public short ToInt16(IFormatProvider? provider)
{
return (short) MathF.Round(_value, MidpointRounding.AwayFromZero);
}
/// <inheritdoc />
public int ToInt32(IFormatProvider? provider)
{
return (int) MathF.Round(_value, MidpointRounding.AwayFromZero);
}
/// <inheritdoc />
public long ToInt64(IFormatProvider? provider)
{
return (long) MathF.Round(_value, MidpointRounding.AwayFromZero);
}
/// <inheritdoc />
public sbyte ToSByte(IFormatProvider? provider)
{
return (sbyte) Math.Clamp(_value, 0, 255);
}
/// <inheritdoc />
public float ToSingle(IFormatProvider? provider)
{
return _value;
}
/// <inheritdoc />
public string ToString(IFormatProvider? provider)
{
return _value.ToString(provider);
}
/// <inheritdoc />
public object ToType(Type conversionType, IFormatProvider? provider)
{
return Convert.ChangeType(_value, conversionType);
}
/// <inheritdoc />
public ushort ToUInt16(IFormatProvider? provider)
{
return (ushort) MathF.Round(_value, MidpointRounding.AwayFromZero);
}
/// <inheritdoc />
public uint ToUInt32(IFormatProvider? provider)
{
return (uint) MathF.Round(_value, MidpointRounding.AwayFromZero);
}
/// <inheritdoc />
public ulong ToUInt64(IFormatProvider? provider)
{
return (ulong) MathF.Round(_value, MidpointRounding.AwayFromZero);
}
#endregion
}
/// <summary>
@@ -279,7 +390,8 @@ namespace Artemis.Core
if (source == null) throw new ArgumentNullException(nameof(source));
float sum = 0;
foreach (float v in source) sum += v;
foreach (float v in source)
sum += v;
return new Numeric(sum);
}
@@ -14,8 +14,11 @@ namespace Artemis.Core
#region Constructors
internal InputPinCollection(INode node, string name, int initialCount)
: base(node, name, initialCount)
: base(node, name)
{
// Can't do this in the base constructor because the type won't be set yet
for (int i = 0; i < initialCount; i++)
Add(CreatePin());
}
#endregion
@@ -23,7 +26,7 @@ namespace Artemis.Core
#region Methods
/// <inheritdoc />
protected override IPin CreatePin()
public override IPin CreatePin()
{
return new InputPin<T>(Node, string.Empty);
}
@@ -59,13 +62,13 @@ namespace Artemis.Core
#region Constructors
internal InputPinCollection(INode node, Type type, string name, int initialCount)
: base(node, name, 0)
: base(node, name)
{
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++)
AddPin();
Add(CreatePin());
}
#endregion
@@ -73,7 +76,7 @@ namespace Artemis.Core
#region Methods
/// <inheritdoc />
protected override IPin CreatePin()
public override IPin CreatePin()
{
return new InputPin(Node, Type, string.Empty);
}
@@ -40,10 +40,15 @@ namespace Artemis.Core
event EventHandler<SingleValueEventArgs<IPin>> PinRemoved;
/// <summary>
/// Creates a new pin and adds it to the collection
/// Creates a new pin compatible with this collection
/// </summary>
/// <returns>The newly added pin</returns>
IPin AddPin();
/// <returns>The newly created pin</returns>
IPin CreatePin();
/// <summary>
/// Adds the provided <paramref name="pin" /> to the collection
/// </summary>
void Add(IPin pin);
/// <summary>
/// Removes the provided <paramref name="pin" /> from the collection
@@ -199,7 +199,7 @@ namespace Artemis.Core
while (collection.Count() > entityNodePinCollection.Amount)
collection.Remove(collection.Last());
while (collection.Count() < entityNodePinCollection.Amount)
collection.AddPin();
collection.Add(collection.CreatePin());
}
return node;
@@ -20,15 +20,19 @@ namespace Artemis.Core
#region Constructors
internal OutputPinCollection(INode node, string name, int initialCount)
: base(node, name, initialCount)
{ }
: base(node, name)
{
// Can't do this in the base constructor because the type won't be set yet
for (int i = 0; i < initialCount; i++)
Add(CreatePin());
}
#endregion
#region Methods
/// <inheritdoc />
protected override IPin CreatePin() => new OutputPin<T>(Node, string.Empty);
public override IPin CreatePin() => new OutputPin<T>(Node, string.Empty);
#endregion
}
@@ -16,15 +16,11 @@ namespace Artemis.Core
/// </summary>
/// <param name="node">The node the pin collection belongs to</param>
/// <param name="name">The name of the pin collection</param>
/// <param name="initialCount">The amount of pins to initially add to the collection</param>
/// <returns>The resulting output pin collection</returns>
protected PinCollection(INode node, string name, int initialCount)
protected PinCollection(INode node, string name)
{
Node = node ?? throw new ArgumentNullException(nameof(node));
Name = name;
for (int i = 0; i < initialCount; i++)
AddPin();
}
#endregion
@@ -61,14 +57,18 @@ namespace Artemis.Core
#region Methods
/// <inheritdoc />
public IPin AddPin()
public void Add(IPin pin)
{
IPin pin = CreatePin();
if (pin.Direction != Direction)
throw new ArtemisCoreException($"Can't add a {pin.Direction} pin to an {Direction} pin collection.");
if (pin.Type != Type)
throw new ArtemisCoreException($"Can't add a {pin.Type} pin to an {Type} pin collection.");
if (_pins.Contains(pin))
return;
_pins.Add(pin);
PinAdded?.Invoke(this, new SingleValueEventArgs<IPin>(pin));
return pin;
}
/// <inheritdoc />
@@ -89,11 +89,8 @@ namespace Artemis.Core
pin.Reset();
}
/// <summary>
/// Creates a new pin to be used in this collection
/// </summary>
/// <returns>The resulting pin</returns>
protected abstract IPin CreatePin();
/// <inheritdoc />
public abstract IPin CreatePin();
/// <inheritdoc />
public IEnumerator<IPin> GetEnumerator()