Nodes - Changed storage to be based on generics

Nodes - Added XML docs to most types
This commit is contained in:
Robert committed 2021-09-24 23:05:16 +02:00
1 parent 8413b8d6db
commit c1dab91c16
28 files changed
+522 -347

No files matched your search

@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Artemis.Core;
using Artemis.Core.Events;
@@ -8,19 +9,69 @@ using Artemis.VisualScripting.Nodes.DataModel.CustomViewModels;
namespace Artemis.VisualScripting.Nodes.DataModel
{
[Node("Data Model-Event", "Responds to a data model event trigger", "External", OutputType = typeof(bool))]
public class DataModelEventNode : Node<DataModelEventNodeCustomViewModel>, IDisposable
public class DataModelEventNode : Node<DataModelPathEntity, DataModelEventNodeCustomViewModel>, IDisposable
{
private DataModelPath _dataModelPath;
private DateTime _lastTrigger;
private int _currentIndex;
private Type _currentType;
private bool _updating;
public DataModelEventNode() : base("Data Model-Event", "Responds to a data model event trigger")
{
Input = CreateInputPin<object>();
Input.PinConnected += InputOnPinConnected;
_currentType = typeof(object);
CreateCycleValues(typeof(object), 1);
Output = CreateOutputPin(typeof(object));
}
public InputPin<object> Input { get; }
private void CreateCycleValues(Type type, int initialCount)
{
if (CycleValues != null)
{
CycleValues.PinAdded -= CycleValuesOnPinAdded;
CycleValues.PinRemoved -= CycleValuesOnPinRemoved;
foreach (IPin pin in CycleValues)
{
pin.PinConnected -= OnPinConnected;
pin.PinDisconnected -= OnPinDisconnected;
}
RemovePinCollection(CycleValues);
}
CycleValues = CreateInputPinCollection(type, "", initialCount);
CycleValues.PinAdded += CycleValuesOnPinAdded;
CycleValues.PinRemoved += CycleValuesOnPinRemoved;
foreach (IPin pin in CycleValues)
{
pin.PinConnected += OnPinConnected;
pin.PinDisconnected += OnPinDisconnected;
}
}
private void CycleValuesOnPinAdded(object sender, SingleValueEventArgs<IPin> e)
{
e.Value.PinConnected += OnPinConnected;
e.Value.PinDisconnected += OnPinDisconnected;
}
private void CycleValuesOnPinRemoved(object sender, SingleValueEventArgs<IPin> e)
{
e.Value.PinConnected -= OnPinConnected;
e.Value.PinDisconnected -= OnPinDisconnected;
}
private void OnPinDisconnected(object sender, SingleValueEventArgs<IPin> e)
{
ProcessPinDisconnected();
}
private void OnPinConnected(object sender, SingleValueEventArgs<IPin> e)
{
IPin source = e.Value;
IPin target = (IPin) sender;
ProcessPinConnected(source, target);
}
public InputPinCollection CycleValues { get; set; }
public OutputPin Output { get; set; }
@@ -35,70 +86,101 @@ namespace Artemis.VisualScripting.Nodes.DataModel
public override void Initialize(INodeScript script)
{
Script = script;
CreateCycleValues();
CreateOutput();
if (Storage is not DataModelPathEntity pathEntity)
if (Storage == null)
return;
DataModelPath = new DataModelPath(pathEntity);
DataModelPath = new DataModelPath(Storage);
}
public override void Evaluate()
{
object outputValue = null;
if (DataModelPath?.GetValue() is DataModelEvent dataModelEvent)
if (DataModelPath?.GetValue() is IDataModelEvent dataModelEvent)
{
if (dataModelEvent.LastTrigger > _lastTrigger)
{
_lastTrigger = dataModelEvent.LastTrigger;
_currentIndex++;
if (_currentIndex > CycleValues.Count())
if (_currentIndex >= CycleValues.Count())
_currentIndex = 0;
}
outputValue = _currentIndex == 0
? Input.Value
: CycleValues.ElementAt(_currentIndex - 1).PinValue;
outputValue = CycleValues.ElementAt(_currentIndex).PinValue;
}
Output.Value = outputValue ?? Output.Type.GetDefault();
if (Output.Type.IsInstanceOfType(outputValue))
Output.Value = outputValue;
else if (Output.Type.IsValueType)
Output.Value = Output.Type.GetDefault()!;
}
private void InputOnPinConnected(object sender, SingleValueEventArgs<IPin> e)
private void ProcessPinConnected(IPin source, IPin target)
{
CreateCycleValues();
CreateOutput();
}
if (_updating)
return;
private void CreateCycleValues()
{
int pinCount = CycleValues?.Count() ?? 1;
Type inputType = Input.ConnectedTo.FirstOrDefault()?.Type ?? typeof(object);
if (CycleValues != null)
try
{
if (inputType == CycleValues.Type)
return;
RemovePinCollection(CycleValues);
}
_updating = true;
CycleValues = CreateInputPinCollection(inputType, "", pinCount);
// No need to change anything if the types haven't changed
if (_currentType == source.Type)
return;
int reconnectIndex = CycleValues.ToList().IndexOf(target);
ChangeCurrentType(source.Type);
if (reconnectIndex != -1)
{
CycleValues.ToList()[reconnectIndex].ConnectTo(source);
source.ConnectTo(CycleValues.ToList()[reconnectIndex]);
}
}
finally
{
_updating = false;
}
}
private void CreateOutput()
private void ChangeCurrentType(Type type)
{
Type inputType = Input.ConnectedTo.FirstOrDefault()?.Type ?? typeof(object);
if (Output != null)
CreateCycleValues(type, CycleValues.Count());
List<IPin> oldOutputConnections = Output.ConnectedTo.ToList();
RemovePin(Output);
Output = CreateOutputPin(type);
foreach (IPin oldOutputConnection in oldOutputConnections.Where(o => o.Type.IsAssignableFrom(Output.Type)))
{
if (inputType == Output.Type)
return;
RemovePin(Output);
oldOutputConnection.DisconnectAll();
oldOutputConnection.ConnectTo(Output);
Output.ConnectTo(oldOutputConnection);
}
Output = CreateOutputPin(inputType);
_currentType = type;
}
private void ProcessPinDisconnected()
{
if (_updating)
return;
try
{
// If there's still a connected pin, stick to the current type
if (CycleValues.Any(v => v.ConnectedTo.Any()))
return;
ChangeCurrentType(typeof(object));
}
finally
{
_updating = false;
}
}
private void UpdatePinsType(IPin source)
{
}
/// <inheritdoc />
@@ -7,7 +7,7 @@ using Stylet;
namespace Artemis.VisualScripting.Nodes.DataModel
{
[Node("Data Model-Value", "Outputs a selectable data model value.", "External")]
public class DataModelNode : Node<DataModelNodeCustomViewModel>, IDisposable
public class DataModelNode : Node<DataModelPathEntity, DataModelNodeCustomViewModel>, IDisposable
{
private DataModelPath _dataModelPath;
@@ -28,10 +28,10 @@ namespace Artemis.VisualScripting.Nodes.DataModel
{
Script = script;
if (Storage is not DataModelPathEntity pathEntity)
if (Storage == null)
return;
DataModelPath = new DataModelPath(pathEntity);
DataModelPath = new DataModelPath(Storage);
DataModelPath.PathValidated += DataModelPathOnPathValidated;
UpdateOutputPin(false);