First commit
This commit is contained in:
194
Runtime/Models/Nodes/BaseNode.cs
Normal file
194
Runtime/Models/Nodes/BaseNode.cs
Normal file
@@ -0,0 +1,194 @@
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Misaki.GraphView
|
||||
{
|
||||
[Serializable]
|
||||
public abstract class BaseNode : SlotContainer
|
||||
{
|
||||
[SerializeField]
|
||||
private GraphObject _graphObject;
|
||||
[SerializeField]
|
||||
private string _id = Guid.NewGuid().ToString();
|
||||
|
||||
private bool _isExecuted;
|
||||
|
||||
public Rect position;
|
||||
|
||||
public GraphObject GraphObject => _graphObject;
|
||||
public string Id => _id;
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the node with the graph object, this method is called when the node is added to the graph.
|
||||
/// </summary>
|
||||
public virtual void Initialize(GraphObject graph)
|
||||
{
|
||||
_graphObject = graph;
|
||||
|
||||
var type = GetType();
|
||||
var fields = type.GetFields(ConstResource.NODE_FIELD_BINDING_FLAGS);
|
||||
|
||||
var inputSlotIndex = 0;
|
||||
var outputSlotIndex = 0;
|
||||
foreach (var field in fields)
|
||||
{
|
||||
var inputAttribute = field.GetCustomAttribute<NodeInputAttribute>();
|
||||
if (inputAttribute != null)
|
||||
{
|
||||
var inputSlot = new Slot(this, new SlotData
|
||||
{
|
||||
slotName = field.Name,
|
||||
nodeID = Id,
|
||||
slotIndex = inputSlotIndex++,
|
||||
direction = SlotDirection.Input,
|
||||
valueType = field.FieldType.FullName
|
||||
});
|
||||
AddInput(inputSlot);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
var outputAttribute = field.GetCustomAttribute<NodeOutputAttribute>();
|
||||
if (outputAttribute != null)
|
||||
{
|
||||
var outputSlot = new Slot(this, new SlotData
|
||||
{
|
||||
slotName = field.Name,
|
||||
nodeID = Id,
|
||||
slotIndex = outputSlotIndex++,
|
||||
direction = SlotDirection.Output,
|
||||
valueType = field.FieldType.FullName
|
||||
});
|
||||
AddOutput(outputSlot);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unload the node from the graph, this method is called when the node is removed from the graph.
|
||||
/// </summary>
|
||||
public virtual void UnLoad()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the slot by the index and direction.
|
||||
/// </summary>
|
||||
/// <param name="index"></param>
|
||||
/// <param name="direction"></param>
|
||||
/// <returns></returns>
|
||||
public Slot GetSlot(int index, SlotDirection direction)
|
||||
{
|
||||
return direction switch
|
||||
{
|
||||
SlotDirection.Input => Inputs[index],
|
||||
SlotDirection.Output => Outputs[index],
|
||||
_ => null
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unlink all the slots of the node.
|
||||
/// </summary>
|
||||
public void UnlinkAllSlots()
|
||||
{
|
||||
foreach (var input in Inputs)
|
||||
{
|
||||
input.UnlinkAll();
|
||||
_graphObject.RemoveAllConnectionsForSlot(input);
|
||||
}
|
||||
|
||||
foreach (var output in Outputs)
|
||||
{
|
||||
output.UnlinkAll();
|
||||
_graphObject.RemoveAllConnectionsForSlot(output);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Execute the node.
|
||||
/// </summary>
|
||||
public void Execute()
|
||||
{
|
||||
if (_isExecuted)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
PullData();
|
||||
OnExecute();
|
||||
PushData();
|
||||
|
||||
_isExecuted = true;
|
||||
}
|
||||
|
||||
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 slot = _graphObject.GetNode(slotData.nodeID).GetSlot(slotData.slotIndex, slotData.direction);
|
||||
|
||||
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)
|
||||
{
|
||||
}
|
||||
|
||||
public bool IsExecuted()
|
||||
{
|
||||
return _isExecuted;
|
||||
}
|
||||
|
||||
public void ClearExecuteFlag()
|
||||
{
|
||||
_isExecuted = false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The execution logic of the node.
|
||||
/// </summary>
|
||||
protected abstract void OnExecute();
|
||||
}
|
||||
}
|
||||
2
Runtime/Models/Nodes/BaseNode.cs.meta
Normal file
2
Runtime/Models/Nodes/BaseNode.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 670887239e46bea498021b59a16eef15
|
||||
26
Runtime/Models/Nodes/PropertyInputNode.cs
Normal file
26
Runtime/Models/Nodes/PropertyInputNode.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Misaki.GraphView
|
||||
{
|
||||
public class PropertyInputNode : BaseNode
|
||||
{
|
||||
[SerializeReference]
|
||||
private ExposedProperty _property;
|
||||
|
||||
public ExposedProperty Property => _property;
|
||||
|
||||
[NodeOutput]
|
||||
public object value;
|
||||
|
||||
public PropertyInputNode(ExposedProperty property)
|
||||
{
|
||||
_property = property;
|
||||
}
|
||||
|
||||
protected override void OnExecute()
|
||||
{
|
||||
value = _property.Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Runtime/Models/Nodes/PropertyInputNode.cs.meta
Normal file
3
Runtime/Models/Nodes/PropertyInputNode.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 18828dc1acb341d382ed33d371b71225
|
||||
timeCreated: 1730511664
|
||||
40
Runtime/Models/Nodes/SlotContainer.cs
Normal file
40
Runtime/Models/Nodes/SlotContainer.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using UnityEngine;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace Misaki.GraphView
|
||||
{
|
||||
[Serializable]
|
||||
public abstract class SlotContainer
|
||||
{
|
||||
[SerializeField]
|
||||
private List<Slot> _inputs = new ();
|
||||
[SerializeField]
|
||||
private List<Slot> _outputs = new ();
|
||||
|
||||
public ReadOnlyCollection<Slot> Inputs => _inputs.AsReadOnly();
|
||||
public ReadOnlyCollection<Slot> Outputs => _outputs.AsReadOnly();
|
||||
|
||||
public void AddInput(Slot input)
|
||||
{
|
||||
_inputs.Add(input);
|
||||
}
|
||||
|
||||
public void AddOutput(Slot output)
|
||||
{
|
||||
_outputs.Add(output);
|
||||
}
|
||||
|
||||
public void ClearInputs()
|
||||
{
|
||||
_inputs.Clear();
|
||||
}
|
||||
|
||||
public void ClearOutputs()
|
||||
{
|
||||
_outputs.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Runtime/Models/Nodes/SlotContainer.cs.meta
Normal file
3
Runtime/Models/Nodes/SlotContainer.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0a8f9433f38644d78ab2a8bc47cc54bd
|
||||
timeCreated: 1730116658
|
||||
Reference in New Issue
Block a user