Added RelayNodeView;
Chnaged GraphView to multiple files for better organization;
This commit is contained in:
@@ -10,16 +10,16 @@ namespace Misaki.GraphView
|
||||
public abstract class ExecutableNode : DataNode, ISlotContainer, IExecutable
|
||||
{
|
||||
[SerializeField]
|
||||
private List<Slot> _inputs = new ();
|
||||
private List<Slot> _inputs = new();
|
||||
[SerializeField]
|
||||
private List<Slot> _outputs = new ();
|
||||
|
||||
private List<Slot> _outputs = new();
|
||||
|
||||
public ReadOnlyCollection<Slot> Inputs => _inputs.AsReadOnly();
|
||||
public ReadOnlyCollection<Slot> Outputs => _outputs.AsReadOnly();
|
||||
|
||||
|
||||
private bool _isExecuted;
|
||||
|
||||
public Action OnExecutoinStarted;
|
||||
|
||||
public Action OnExecutionStarted;
|
||||
public Action OnExecutionCompleted;
|
||||
public Action OnExecutionFailed;
|
||||
public Action OnExecuteFlagCleared;
|
||||
@@ -71,7 +71,7 @@ namespace Misaki.GraphView
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <inheritdoc />
|
||||
public void AddSlot(Slot slot)
|
||||
{
|
||||
@@ -85,7 +85,7 @@ namespace Misaki.GraphView
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <inheritdoc />
|
||||
public void RemoveSlot(Slot slot)
|
||||
{
|
||||
@@ -126,7 +126,7 @@ namespace Misaki.GraphView
|
||||
graphObject.RemoveAllConnectionsForSlot(output);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Execute()
|
||||
{
|
||||
@@ -134,29 +134,28 @@ namespace Misaki.GraphView
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
OnExecutoinStarted?.Invoke();
|
||||
|
||||
PullData();
|
||||
|
||||
OnExecutionStarted?.Invoke();
|
||||
Inputs.PullData(OnPullData);
|
||||
|
||||
if (!graphObject.GraphProcessor.IsRunning)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (!OnExecute())
|
||||
{
|
||||
graphObject.GraphProcessor.Break();
|
||||
OnExecutionFailed?.Invoke();
|
||||
return;
|
||||
}
|
||||
|
||||
PushData();
|
||||
|
||||
|
||||
Outputs.PushData(OnPushData);
|
||||
|
||||
_isExecuted = true;
|
||||
OnExecutionCompleted?.Invoke();
|
||||
}
|
||||
|
||||
|
||||
/// <inheritdoc />
|
||||
public void ClearExecutionFlag()
|
||||
{
|
||||
@@ -164,66 +163,10 @@ namespace Misaki.GraphView
|
||||
OnExecuteFlagCleared?.Invoke();
|
||||
}
|
||||
|
||||
private void PullData()
|
||||
{
|
||||
foreach (var input in Inputs)
|
||||
{
|
||||
var property = GetType().GetField(input.slotData.slotName, ConstResource.NODE_FIELD_BINDING_FLAGS);
|
||||
if (property == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
OnPullData(input);
|
||||
|
||||
if (input.LinkedSlotData.Count == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
property.SetValue(this, input.value);
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void OnPullData(Slot input)
|
||||
{
|
||||
}
|
||||
|
||||
private void PushData()
|
||||
{
|
||||
foreach (var output in Outputs)
|
||||
{
|
||||
var property = GetType().GetField(output.slotData.slotName, ConstResource.NODE_FIELD_BINDING_FLAGS);
|
||||
if (property == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
OnPushData(output);
|
||||
|
||||
output.value = property.GetValue(this);
|
||||
foreach (var slotData in output.LinkedSlotData)
|
||||
{
|
||||
var node = graphObject.GetNode(slotData.nodeID);
|
||||
if (node is not ISlotContainer slotContainer)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var slot = slotContainer.GetSlot(slotData.slotIndex, SlotDirection.Input);
|
||||
if (slotData.valueType == output.slotData.valueType || output.slotData.valueType == typeof(object).FullName)
|
||||
{
|
||||
slot.ReceiveData(output.value);
|
||||
}
|
||||
else if (graphObject.ValueConverterManager != null && graphObject.ValueConverterManager.TryConvert(output.slotData.GetValueType(),
|
||||
slotData.GetValueType(), output.value, out var data))
|
||||
{
|
||||
slot.ReceiveData(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void OnPushData(Slot output)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -1,9 +1,78 @@
|
||||
using UnityEngine;
|
||||
using System;
|
||||
|
||||
namespace Misaki.GraphView
|
||||
{
|
||||
public class RelayNode
|
||||
[Serializable]
|
||||
public class RelayNode : DataNode, ISlotContainer, IExecutable
|
||||
{
|
||||
|
||||
private Slot _inputSlot;
|
||||
private Slot _outputSlot;
|
||||
|
||||
private bool _isExecuted;
|
||||
|
||||
public override void Initialize(GraphObject graph)
|
||||
{
|
||||
graphObject = graph;
|
||||
|
||||
_inputSlot = new(this, new()
|
||||
{
|
||||
slotName = "Input",
|
||||
nodeID = Id,
|
||||
slotIndex = 0,
|
||||
direction = SlotDirection.Input,
|
||||
valueType = typeof(object).FullName
|
||||
});
|
||||
|
||||
_outputSlot = new(this, new()
|
||||
{
|
||||
slotName = "Output",
|
||||
nodeID = Id,
|
||||
slotIndex = 0,
|
||||
direction = SlotDirection.Output,
|
||||
valueType = typeof(object).FullName
|
||||
});
|
||||
}
|
||||
|
||||
public void AddSlot(Slot slot)
|
||||
{
|
||||
}
|
||||
|
||||
public void RemoveSlot(Slot slot)
|
||||
{
|
||||
}
|
||||
|
||||
public Slot GetSlot(int index, SlotDirection direction)
|
||||
{
|
||||
return direction switch
|
||||
{
|
||||
SlotDirection.Input => _inputSlot,
|
||||
SlotDirection.Output => _outputSlot,
|
||||
_ => null
|
||||
};
|
||||
}
|
||||
|
||||
public void UnlinkAllSlots()
|
||||
{
|
||||
_inputSlot.UnlinkAll();
|
||||
_outputSlot.UnlinkAll();
|
||||
}
|
||||
|
||||
public void Execute()
|
||||
{
|
||||
if (_isExecuted)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_outputSlot.ReceiveData(_inputSlot.value);
|
||||
_outputSlot.PushData(null);
|
||||
|
||||
_isExecuted = true;
|
||||
}
|
||||
|
||||
public void ClearExecutionFlag()
|
||||
{
|
||||
_isExecuted = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -28,38 +28,38 @@ namespace Misaki.GraphView
|
||||
{
|
||||
return HashCode.Combine(slotName, nodeID, slotIndex, direction, valueType);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator ==(SlotData left, SlotData right)
|
||||
{
|
||||
return left.Equals(right);
|
||||
}
|
||||
|
||||
|
||||
public static bool operator !=(SlotData left, SlotData right)
|
||||
{
|
||||
return !left.Equals(right);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
[Serializable]
|
||||
public class Slot
|
||||
{
|
||||
[SerializeField]
|
||||
[SerializeField]
|
||||
private List<SlotData> _linkedSlotData = new();
|
||||
|
||||
|
||||
public ReadOnlyCollection<SlotData> LinkedSlotData => _linkedSlotData.AsReadOnly();
|
||||
|
||||
[SerializeReference]
|
||||
public DataNode owner;
|
||||
public SlotData slotData;
|
||||
|
||||
|
||||
public object value;
|
||||
|
||||
|
||||
public Slot(DataNode owner, SlotData slotData)
|
||||
{
|
||||
this.owner = owner;
|
||||
this.slotData = slotData;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Link the current slot with another slot.
|
||||
/// </summary>
|
||||
@@ -70,16 +70,16 @@ namespace Misaki.GraphView
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (_linkedSlotData.Contains(other.slotData))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
_linkedSlotData.Add(other.slotData);
|
||||
other._linkedSlotData.Add(slotData);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Unlink the current slot with another slot.
|
||||
/// </summary>
|
||||
@@ -89,12 +89,5 @@ namespace Misaki.GraphView
|
||||
_linkedSlotData.Remove(other.slotData);
|
||||
other._linkedSlotData.Remove(slotData);
|
||||
}
|
||||
|
||||
public void ReceiveData(object data)
|
||||
{
|
||||
value = data;
|
||||
// We move this to PullData method in BaseNode
|
||||
//owner.GetType().GetField(slotData.slotName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)?.SetValue(owner, data);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user