Unity graph tool solution based on different implementation now focused on Unity.Experimental.Graphview
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.5 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using TNode.TNodeCore.Runtime.Models;
using TNode.TNodeCore.Runtime.Tools;
using TNodeCore.Runtime.Models;
namespace TNodeCore.Runtime.RuntimeModels{
public class StaticGraph:IRuntimeNodeGraph{
private Dictionary<string,RuntimeNode> _nodes;
private IEnumerator<RuntimeNode> _breathFirstEnumerator;
private readonly GraphTool _graphTool;
private readonly GraphData _originalData;
private void ModifyLinks(NodeLink linkData){
var outNodeId = linkData.outPort.nodeDataId;
var outNode = _nodes[outNodeId];
outNode.OutputLinks.Add(linkData);
var inNodeId = linkData.inPort.nodeDataId;
var inNode = _nodes[inNodeId];
inNode.InputLinks.Add(linkData);
}
public StaticGraph(GraphData graphData){
_originalData = graphData;
var nodes = graphData.NodeDictionary.Values.ToList();
var links = graphData.NodeLinks;
_nodes = new Dictionary<string, RuntimeNode>();
foreach (var nodeData in nodes){
if(_nodes.ContainsKey(nodeData.id)) continue;
if (nodeData is ConditionalNode conditionalNode){
ConditionalRuntimeNode conditionalRuntimeNode = new ConditionalRuntimeNode(conditionalNode);
_nodes.Add(conditionalNode.id,conditionalRuntimeNode);
}
else{
_nodes.Add(nodeData.id,new RuntimeNode(nodeData));
}
RuntimeNode currentNode = _nodes[nodeData.id];
currentNode.InputLinks = new List<NodeLink>();
currentNode.OutputLinks = new List<NodeLink>();
}
foreach (var link in links){
ModifyLinks(link);
}
_graphTool = new GraphTool(this);
_breathFirstEnumerator = _graphTool.BreathFirstSearch();
}
public void ResetState(){
_breathFirstEnumerator = _graphTool.BreathFirstSearch();
}
public RuntimeNode GetRuntimeNode(NodeData nodeData){
return _nodes[nodeData.id];
}
public RuntimeNode GetRuntimeNode(string id){
return _nodes[id];
}
public BlackboardData GetBlackboardData(){
return _originalData.blackboardData;
}
public List<RuntimeNode> GetRuntimeNodes(){
return _nodes.Values.ToList();
}
public Dictionary<string, RuntimeNode> GetRuntimeNodesDictionary(){
return _nodes;
}
public NodeData GetNode(string id){
return _nodes[id].NodeData;
}
public List<RuntimeNode> GetRuntimeNodesOfType(Type type){
return _nodes.Where(x=>x.Value.NodeType == type).Select(x=>x.Value).ToList();
}
public List<RuntimeNode> GetRuntimeNodesOfType<T>(){
return _nodes.Where(x=>x.Value.NodeType == typeof(T)).Select(x=>x.Value).ToList();
}
public NodeData CurrentNode(){
return CurrentRuntimeNode().NodeData;
}
public RuntimeNode MoveNext(){
_breathFirstEnumerator.MoveNext();
return _breathFirstEnumerator.Current;
}
public RuntimeNode CurrentRuntimeNode(){
if (_breathFirstEnumerator.Current == null){
_breathFirstEnumerator.MoveNext();
}
return _breathFirstEnumerator.Current;
}
}
}