Big project cleanup with overhaul of file responsibilities (KISS) and code (DRY, YAGNI)
This commit is contained in:
+70
-327
@@ -13,15 +13,6 @@ public partial class CodingWindow : PanelContainer
|
||||
[Export] LineEdit nameInput;
|
||||
|
||||
public System.Collections.Generic.Dictionary<ProgramNode, PackedScene> DSLNodes;
|
||||
private System.Collections.Generic.Dictionary<StringName, ProgramNode> availableNodes;
|
||||
|
||||
private class ScriptConnection
|
||||
{
|
||||
public string FromNodeId;
|
||||
public int FromPort;
|
||||
public string ToNodeId;
|
||||
public int ToPort;
|
||||
}
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
@@ -51,6 +42,7 @@ public partial class CodingWindow : PanelContainer
|
||||
{
|
||||
availableScripts.Clear();
|
||||
availableScripts.AddItem("Select script to load...");
|
||||
|
||||
List<string> scripts = FileHandler.LoadProgramNames();
|
||||
scripts.Sort((a, b) => a.CompareTo(b));
|
||||
foreach (string script in scripts)
|
||||
@@ -73,10 +65,9 @@ public partial class CodingWindow : PanelContainer
|
||||
|
||||
public void GenerateCodingBlocks()
|
||||
{
|
||||
Button nodeListButton;
|
||||
foreach (ProgramNode nodeTemplate in DSLNodes.Keys)
|
||||
{
|
||||
nodeListButton = new Button
|
||||
Button nodeListButton = new Button
|
||||
{
|
||||
Name = nodeTemplate.DisplayText,
|
||||
Text = nodeTemplate.DisplayText
|
||||
@@ -92,17 +83,14 @@ public partial class CodingWindow : PanelContainer
|
||||
private void AddEditorNode(ProgramNode node)
|
||||
{
|
||||
NodeDisplay editorDisplay = DSLNodes[node].Instantiate<NodeDisplay>();
|
||||
editorDisplay.PositionOffset = (editorWindow.ScrollOffset + editorWindow.Size / 2) / editorWindow.Zoom - editorDisplay.Size / 2;
|
||||
editorDisplay.PositionOffset = GetVisibleGraphCenter() - editorDisplay.Size / 2f;
|
||||
editorWindow.AddChild(editorDisplay);
|
||||
RegisterEditorNode(editorDisplay);
|
||||
}
|
||||
|
||||
private void MoveNodeToVisibleGraphCenter(NodeDisplay nodeDisplay)
|
||||
private Vector2 GetVisibleGraphCenter()
|
||||
{
|
||||
Vector2 visibleCenter = editorWindow.ScrollOffset
|
||||
+ editorWindow.Size / (2f * editorWindow.Zoom);
|
||||
|
||||
nodeDisplay.PositionOffset = visibleCenter - nodeDisplay.Size / (2f * editorWindow.Zoom);
|
||||
return (editorWindow.ScrollOffset + editorWindow.Size / 2f) / editorWindow.Zoom;
|
||||
}
|
||||
|
||||
private void RegisterEditorNode(NodeDisplay editorDisplay)
|
||||
@@ -115,6 +103,13 @@ public partial class CodingWindow : PanelContainer
|
||||
}
|
||||
|
||||
public void ClearWindow()
|
||||
{
|
||||
DisconnectAllNodes();
|
||||
RemoveEditorNodes();
|
||||
scriptName.Text = "";
|
||||
}
|
||||
|
||||
private void DisconnectAllNodes()
|
||||
{
|
||||
foreach (Dictionary connection in editorWindow.GetConnectionList())
|
||||
{
|
||||
@@ -125,109 +120,41 @@ public partial class CodingWindow : PanelContainer
|
||||
(int)connection["to_port"]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private void RemoveEditorNodes()
|
||||
{
|
||||
foreach (Node child in editorWindow.GetChildren())
|
||||
{
|
||||
if (child is GraphNode)
|
||||
{
|
||||
editorWindow.RemoveChild(child);
|
||||
child.QueueFree();
|
||||
}
|
||||
if (child is not GraphNode) continue;
|
||||
|
||||
editorWindow.RemoveChild(child);
|
||||
child.QueueFree();
|
||||
}
|
||||
scriptName.Text = "";
|
||||
}
|
||||
|
||||
public void CompileProgram()
|
||||
{
|
||||
if (robot == null) return;
|
||||
|
||||
NodeDisplay startNode = FindStartNode();
|
||||
if (startNode == null)
|
||||
ScriptGraphCompiler compiler = new ScriptGraphCompiler(editorWindow);
|
||||
string errorMessage;
|
||||
List<ProgramNode> nodes = compiler.BuildProgram(out errorMessage);
|
||||
if (errorMessage.Length > 0)
|
||||
{
|
||||
robot.StopExecution("(FAILED) Script needs exactly one Start node");
|
||||
robot.StopExecution(errorMessage);
|
||||
return;
|
||||
}
|
||||
|
||||
BuildAvailableNodeLookup();
|
||||
List<ProgramNode> nodes = BuildScriptOrder(
|
||||
startNode,
|
||||
new List<ProgramNode>(),
|
||||
new HashSet<StringName>()
|
||||
);
|
||||
if (nodes.Count > 0) robot.SetupExecution(nodes);
|
||||
robot.currentProgram = scriptName.Text.Length <= 0 ? $"Script{availableScripts.ItemCount}" : scriptName.Text;
|
||||
robot.currentProgram = GetCurrentScriptName();
|
||||
}
|
||||
|
||||
private void BuildAvailableNodeLookup()
|
||||
private string GetCurrentScriptName()
|
||||
{
|
||||
availableNodes = new System.Collections.Generic.Dictionary<StringName, ProgramNode>();
|
||||
if (scriptName.Text.Length > 0) return scriptName.Text;
|
||||
|
||||
for (int i = 0; i < editorWindow.GetChildCount(); i++)
|
||||
{
|
||||
NodeDisplay nodeDisplay = editorWindow.GetChild(i) as NodeDisplay;
|
||||
if (nodeDisplay == null) continue;
|
||||
|
||||
nodeDisplay.ReadParameters();
|
||||
availableNodes.Add(nodeDisplay.Name, nodeDisplay.node);
|
||||
}
|
||||
}
|
||||
|
||||
private List<ProgramNode> BuildScriptOrder(
|
||||
NodeDisplay node,
|
||||
List<ProgramNode> program,
|
||||
HashSet<StringName> visitedNodes
|
||||
)
|
||||
{
|
||||
if (node == null) return program;
|
||||
if (visitedNodes.Contains(node.Name)) return program;
|
||||
|
||||
visitedNodes.Add(node.Name);
|
||||
program.Add(node.node);
|
||||
if (editorWindow.GetConnectionListFromNode(node.Name).Count <= 0) return program;
|
||||
List<Dictionary> nextConnections = CheckNodeConnections(node);
|
||||
if (nextConnections.Count <= 0) return program;
|
||||
node.node.SetNextNode(nextConnections, availableNodes);
|
||||
foreach (Dictionary connection in nextConnections)
|
||||
{
|
||||
NodeDisplay nextNode = editorWindow.GetNodeOrNull<NodeDisplay>(
|
||||
new NodePath(connection["to_node"].AsStringName())
|
||||
);
|
||||
program = BuildScriptOrder(
|
||||
nextNode,
|
||||
program,
|
||||
visitedNodes
|
||||
);
|
||||
}
|
||||
return program;
|
||||
}
|
||||
|
||||
private List<Dictionary> CheckNodeConnections(NodeDisplay node)
|
||||
{
|
||||
List<Dictionary> result = new List<Dictionary>();
|
||||
Array<Dictionary> connections = editorWindow.GetConnectionListFromNode(node.Name);
|
||||
for (int i = 0; i < connections.Count; i++)
|
||||
{
|
||||
if (connections[i]["from_node"].AsStringName() == node.Name)
|
||||
{
|
||||
result.Add(connections[i]);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private NodeDisplay FindStartNode()
|
||||
{
|
||||
NodeDisplay startNode = null;
|
||||
for (int i = 0; i < editorWindow.GetChildCount(); i++)
|
||||
{
|
||||
NodeDisplay nodeDisplay = editorWindow.GetChild(i) as NodeDisplay;
|
||||
if (nodeDisplay == null) continue;
|
||||
if (nodeDisplay.node is not StartNode) continue;
|
||||
if (startNode != null) return null;
|
||||
|
||||
startNode = nodeDisplay;
|
||||
}
|
||||
return startNode;
|
||||
return $"Script{availableScripts.ItemCount}";
|
||||
}
|
||||
|
||||
public void SetRobot(Robot robot)
|
||||
@@ -241,131 +168,15 @@ public partial class CodingWindow : PanelContainer
|
||||
|
||||
ClearWindow();
|
||||
string scriptContent = FileHandler.LoadProgram(availableScripts.GetItemText(index));
|
||||
LoadStructuredProgram(scriptContent);
|
||||
CreateSerializer().Load(scriptContent);
|
||||
scriptName.Text = availableScripts.GetItemText(index);
|
||||
availableScripts.Select(0);
|
||||
}
|
||||
|
||||
private void LoadStructuredProgram(string scriptContent)
|
||||
private void AddLoadedNode(NodeDisplay nodeDisplay)
|
||||
{
|
||||
Variant parsedScript = Json.ParseString(scriptContent);
|
||||
if (parsedScript.VariantType != Variant.Type.Dictionary) return;
|
||||
|
||||
Dictionary scriptData = parsedScript.AsGodotDictionary();
|
||||
if (!scriptData.ContainsKey("Nodes")) return;
|
||||
|
||||
System.Collections.Generic.Dictionary<string, NodeDisplay> loadedNodes =
|
||||
new System.Collections.Generic.Dictionary<string, NodeDisplay>();
|
||||
Array nodes = scriptData["Nodes"].AsGodotArray();
|
||||
for (int i = 0; i < nodes.Count; i++)
|
||||
{
|
||||
Dictionary nodeData = nodes[i].AsGodotDictionary();
|
||||
NodeDisplay nodeDisplay = LoadStructuredNode(nodeData);
|
||||
if (nodeDisplay == null) continue;
|
||||
|
||||
editorWindow.AddChild(nodeDisplay);
|
||||
RegisterEditorNode(nodeDisplay);
|
||||
RegisterLoadedNode(nodeData, nodeDisplay, loadedNodes);
|
||||
}
|
||||
|
||||
LoadStructuredConnections(scriptData, loadedNodes);
|
||||
}
|
||||
|
||||
private void RegisterLoadedNode(
|
||||
Dictionary nodeData,
|
||||
NodeDisplay nodeDisplay,
|
||||
System.Collections.Generic.Dictionary<string, NodeDisplay> loadedNodes
|
||||
)
|
||||
{
|
||||
string nodeId = nodeDisplay.Name.ToString();
|
||||
if (nodeData.ContainsKey("Id"))
|
||||
{
|
||||
nodeId = nodeData["Id"].AsString();
|
||||
}
|
||||
|
||||
if (!loadedNodes.ContainsKey(nodeId))
|
||||
{
|
||||
loadedNodes.Add(nodeId, nodeDisplay);
|
||||
}
|
||||
}
|
||||
|
||||
private NodeDisplay LoadStructuredNode(Dictionary nodeData)
|
||||
{
|
||||
if (!nodeData.ContainsKey("Type")) return null;
|
||||
if (!nodeData.ContainsKey("Content")) return null;
|
||||
|
||||
string type = nodeData["Type"].AsString();
|
||||
string content = nodeData["Content"].AsString();
|
||||
NodeDisplay nodeDisplay = NodeDisplay.Load(type, content, DSLNodes);
|
||||
if (nodeDisplay == null) return null;
|
||||
|
||||
if (nodeData.ContainsKey("Id"))
|
||||
{
|
||||
nodeDisplay.Name = nodeData["Id"].AsString();
|
||||
}
|
||||
|
||||
if (nodeData.ContainsKey("PositionX") && nodeData.ContainsKey("PositionY"))
|
||||
{
|
||||
float positionX = (float)nodeData["PositionX"].AsDouble();
|
||||
float positionY = (float)nodeData["PositionY"].AsDouble();
|
||||
nodeDisplay.PositionOffset = new Vector2(positionX, positionY);
|
||||
}
|
||||
|
||||
return nodeDisplay;
|
||||
}
|
||||
|
||||
private void LoadStructuredConnections(
|
||||
Dictionary scriptData,
|
||||
System.Collections.Generic.Dictionary<string, NodeDisplay> loadedNodes
|
||||
)
|
||||
{
|
||||
if (!scriptData.ContainsKey("Connections")) return;
|
||||
|
||||
Array connectionData = scriptData["Connections"].AsGodotArray();
|
||||
for (int i = 0; i < connectionData.Count; i++)
|
||||
{
|
||||
Dictionary savedConnection = connectionData[i].AsGodotDictionary();
|
||||
if (!savedConnection.ContainsKey("From")) continue;
|
||||
if (!savedConnection.ContainsKey("To")) continue;
|
||||
if (!savedConnection.ContainsKey("FromPort")) continue;
|
||||
if (!savedConnection.ContainsKey("ToPort")) continue;
|
||||
|
||||
ScriptConnection connection = new ScriptConnection
|
||||
{
|
||||
FromNodeId = savedConnection["From"].AsString(),
|
||||
FromPort = savedConnection["FromPort"].AsInt32(),
|
||||
ToNodeId = savedConnection["To"].AsString(),
|
||||
ToPort = savedConnection["ToPort"].AsInt32()
|
||||
};
|
||||
if (!loadedNodes.ContainsKey(connection.FromNodeId)) continue;
|
||||
if (!loadedNodes.ContainsKey(connection.ToNodeId)) continue;
|
||||
|
||||
NodeDisplay fromDisplay = loadedNodes[connection.FromNodeId];
|
||||
NodeDisplay toDisplay = loadedNodes[connection.ToNodeId];
|
||||
if (ConnectionExists(fromDisplay.Name, connection.FromPort, toDisplay.Name, connection.ToPort)) continue;
|
||||
|
||||
editorWindow.ConnectNode(
|
||||
fromDisplay.Name,
|
||||
connection.FromPort,
|
||||
toDisplay.Name,
|
||||
connection.ToPort
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private bool ConnectionExists(StringName fromNode, int fromPort, StringName toNode, int toPort)
|
||||
{
|
||||
foreach (Dictionary connection in editorWindow.GetConnectionList())
|
||||
{
|
||||
if (connection["from_node"].AsStringName() != fromNode) continue;
|
||||
if ((int)connection["from_port"] != fromPort) continue;
|
||||
if (connection["to_node"].AsStringName() != toNode) continue;
|
||||
if ((int)connection["to_port"] != toPort) continue;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
editorWindow.AddChild(nodeDisplay);
|
||||
RegisterEditorNode(nodeDisplay);
|
||||
}
|
||||
|
||||
public void LoadTemporaryProgram()
|
||||
@@ -373,65 +184,16 @@ public partial class CodingWindow : PanelContainer
|
||||
if (robot == null) return;
|
||||
if (robot.currentNode == null) return;
|
||||
|
||||
System.Collections.Generic.Dictionary<ProgramNode, NodeDisplay> loadedNodes =
|
||||
new System.Collections.Generic.Dictionary<ProgramNode, NodeDisplay>();
|
||||
LoadTemporaryNode(robot.currentNode, loadedNodes);
|
||||
RunningProgramGraphBuilder builder = new RunningProgramGraphBuilder(
|
||||
DSLNodes,
|
||||
AddLoadedNode,
|
||||
ConnectNodes
|
||||
);
|
||||
builder.Load(robot.currentNode);
|
||||
|
||||
scriptName.Text = robot.currentProgram ?? "";
|
||||
}
|
||||
|
||||
private NodeDisplay LoadTemporaryNode(
|
||||
ProgramNode programNode,
|
||||
System.Collections.Generic.Dictionary<ProgramNode, NodeDisplay> loadedNodes
|
||||
)
|
||||
{
|
||||
if (programNode == null) return null;
|
||||
if (loadedNodes.ContainsKey(programNode)) return loadedNodes[programNode];
|
||||
|
||||
NodeDisplay nodeDisplay = NodeDisplay.Load(
|
||||
programNode.DisplayText,
|
||||
programNode.Save(),
|
||||
DSLNodes
|
||||
);
|
||||
if (nodeDisplay == null) return null;
|
||||
|
||||
editorWindow.AddChild(nodeDisplay);
|
||||
RegisterEditorNode(nodeDisplay);
|
||||
loadedNodes.Add(programNode, nodeDisplay);
|
||||
|
||||
ConnectTemporaryNode(nodeDisplay, 0, programNode.nextNode, loadedNodes);
|
||||
ConnectTemporaryNode(nodeDisplay, 1, programNode.NegativeNode, loadedNodes);
|
||||
|
||||
return nodeDisplay;
|
||||
}
|
||||
|
||||
private void ConnectTemporaryNode(
|
||||
NodeDisplay fromDisplay,
|
||||
int fromPort,
|
||||
ProgramNode targetNode,
|
||||
System.Collections.Generic.Dictionary<ProgramNode, NodeDisplay> loadedNodes
|
||||
)
|
||||
{
|
||||
NodeDisplay toDisplay = LoadTemporaryNode(targetNode, loadedNodes);
|
||||
if (toDisplay == null) return;
|
||||
|
||||
ScriptConnection connection = new ScriptConnection
|
||||
{
|
||||
FromNodeId = fromDisplay.Name.ToString(),
|
||||
FromPort = fromPort,
|
||||
ToNodeId = toDisplay.Name.ToString(),
|
||||
ToPort = 0
|
||||
};
|
||||
if (ConnectionExists(fromDisplay.Name, connection.FromPort, toDisplay.Name, connection.ToPort)) return;
|
||||
|
||||
editorWindow.ConnectNode(
|
||||
fromDisplay.Name,
|
||||
connection.FromPort,
|
||||
toDisplay.Name,
|
||||
connection.ToPort
|
||||
);
|
||||
}
|
||||
|
||||
public void DeleteProgram()
|
||||
{
|
||||
string filename = scriptName.Text;
|
||||
@@ -450,69 +212,50 @@ public partial class CodingWindow : PanelContainer
|
||||
|
||||
public void SaveProgram()
|
||||
{
|
||||
Array<Dictionary> savedNodes = BuildSavedNodes();
|
||||
if (savedNodes.Count <= 0) return;
|
||||
|
||||
Dictionary scriptData = new Dictionary();
|
||||
scriptData["Nodes"] = savedNodes;
|
||||
scriptData["Connections"] = BuildSavedConnections();
|
||||
|
||||
string result = Json.Stringify(scriptData);
|
||||
string result = CreateSerializer().Save();
|
||||
if (result.Length <= 0) return;
|
||||
string filename = scriptName.Text.Length <= 0 ? $"Script{availableScripts.ItemCount}" : scriptName.Text;
|
||||
FileHandler.SaveProgram(filename, result);
|
||||
|
||||
FileHandler.SaveProgram(GetCurrentScriptName(), result);
|
||||
SetupScriptOptions();
|
||||
}
|
||||
|
||||
private Array<Dictionary> BuildSavedNodes()
|
||||
{
|
||||
Array<Dictionary> savedNodes = new Array<Dictionary>();
|
||||
for (int i = 0; i < editorWindow.GetChildCount(); i++)
|
||||
{
|
||||
NodeDisplay nodeDisplay = editorWindow.GetChild(i) as NodeDisplay;
|
||||
if (nodeDisplay == null) continue;
|
||||
|
||||
nodeDisplay.ReadParameters();
|
||||
Dictionary savedNode = new Dictionary();
|
||||
savedNode["Id"] = nodeDisplay.Name.ToString();
|
||||
savedNode["Type"] = nodeDisplay.node.DisplayText.ToLower();
|
||||
savedNode["Content"] = nodeDisplay.node.Save();
|
||||
savedNode["PositionX"] = nodeDisplay.PositionOffset.X;
|
||||
savedNode["PositionY"] = nodeDisplay.PositionOffset.Y;
|
||||
savedNodes.Add(savedNode);
|
||||
}
|
||||
|
||||
return savedNodes;
|
||||
}
|
||||
|
||||
private Array<Dictionary> BuildSavedConnections()
|
||||
{
|
||||
Array<Dictionary> savedConnections = new Array<Dictionary>();
|
||||
foreach (Dictionary connection in editorWindow.GetConnectionList())
|
||||
{
|
||||
Dictionary savedConnection = new Dictionary();
|
||||
savedConnection["From"] = connection["from_node"].AsStringName().ToString();
|
||||
savedConnection["FromPort"] = (int)connection["from_port"];
|
||||
savedConnection["To"] = connection["to_node"].AsStringName().ToString();
|
||||
savedConnection["ToPort"] = (int)connection["to_port"];
|
||||
savedConnections.Add(savedConnection);
|
||||
}
|
||||
|
||||
return savedConnections;
|
||||
}
|
||||
|
||||
public void OnNodeConnect(StringName from, int fromPort, StringName to, int toPort)
|
||||
{
|
||||
if (to == from) return;
|
||||
|
||||
foreach (Dictionary connection in editorWindow.GetConnectionList())
|
||||
{
|
||||
if (connection["from_node"].AsStringName() == from && (int)connection["from_port"] == fromPort) return;
|
||||
}
|
||||
ConnectNodes(from, fromPort, to, toPort);
|
||||
}
|
||||
|
||||
private void ConnectNodes(StringName from, int fromPort, StringName to, int toPort)
|
||||
{
|
||||
if (HasOutputConnection(from, fromPort)) return;
|
||||
|
||||
editorWindow.ConnectNode(from, fromPort, to, toPort);
|
||||
}
|
||||
|
||||
private bool HasOutputConnection(StringName from, int fromPort)
|
||||
{
|
||||
foreach (Dictionary connection in editorWindow.GetConnectionList())
|
||||
{
|
||||
if (connection["from_node"].AsStringName() != from) continue;
|
||||
if ((int)connection["from_port"] != fromPort) continue;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private ScriptGraphSerializer CreateSerializer()
|
||||
{
|
||||
return new ScriptGraphSerializer(
|
||||
editorWindow,
|
||||
DSLNodes,
|
||||
AddLoadedNode,
|
||||
ConnectNodes
|
||||
);
|
||||
}
|
||||
|
||||
public void OnNodeDisconnect(StringName from, int fromPort, StringName to, int toPort)
|
||||
{
|
||||
editorWindow.DisconnectNode(from, fromPort, to, toPort);
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
using Godot;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class RunningProgramGraphBuilder
|
||||
{
|
||||
private readonly Dictionary<ProgramNode, PackedScene> dslNodes;
|
||||
private readonly Action<NodeDisplay> addNode;
|
||||
private readonly Action<StringName, int, StringName, int> connectNodes;
|
||||
|
||||
public RunningProgramGraphBuilder(
|
||||
Dictionary<ProgramNode, PackedScene> dslNodes,
|
||||
Action<NodeDisplay> addNode,
|
||||
Action<StringName, int, StringName, int> connectNodes
|
||||
)
|
||||
{
|
||||
this.dslNodes = dslNodes;
|
||||
this.addNode = addNode;
|
||||
this.connectNodes = connectNodes;
|
||||
}
|
||||
|
||||
public void Load(ProgramNode startNode)
|
||||
{
|
||||
Dictionary<ProgramNode, NodeDisplay> loadedNodes =
|
||||
new Dictionary<ProgramNode, NodeDisplay>();
|
||||
LoadNode(startNode, loadedNodes);
|
||||
}
|
||||
|
||||
private NodeDisplay LoadNode(
|
||||
ProgramNode programNode,
|
||||
Dictionary<ProgramNode, NodeDisplay> loadedNodes
|
||||
)
|
||||
{
|
||||
if (programNode == null) return null;
|
||||
if (loadedNodes.ContainsKey(programNode)) return loadedNodes[programNode];
|
||||
|
||||
NodeDisplay nodeDisplay = NodeDisplay.Load(
|
||||
programNode.DisplayText,
|
||||
programNode.Save(),
|
||||
dslNodes
|
||||
);
|
||||
if (nodeDisplay == null) return null;
|
||||
|
||||
addNode(nodeDisplay);
|
||||
loadedNodes.Add(programNode, nodeDisplay);
|
||||
|
||||
ConnectNode(nodeDisplay, 0, programNode.nextNode, loadedNodes);
|
||||
ConnectNode(nodeDisplay, 1, programNode.NegativeNode, loadedNodes);
|
||||
|
||||
return nodeDisplay;
|
||||
}
|
||||
|
||||
private void ConnectNode(
|
||||
NodeDisplay fromDisplay,
|
||||
int fromPort,
|
||||
ProgramNode targetNode,
|
||||
Dictionary<ProgramNode, NodeDisplay> loadedNodes
|
||||
)
|
||||
{
|
||||
NodeDisplay toDisplay = LoadNode(targetNode, loadedNodes);
|
||||
if (toDisplay == null) return;
|
||||
|
||||
connectNodes(fromDisplay.Name, fromPort, toDisplay.Name, 0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://deaptod52fe2s
|
||||
@@ -0,0 +1,7 @@
|
||||
public class ScriptConnection
|
||||
{
|
||||
public string FromNodeId;
|
||||
public int FromPort;
|
||||
public string ToNodeId;
|
||||
public int ToPort;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://cvhobhufyp2ni
|
||||
@@ -0,0 +1,108 @@
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class ScriptGraphCompiler
|
||||
{
|
||||
private readonly GraphEdit editorWindow;
|
||||
private System.Collections.Generic.Dictionary<StringName, ProgramNode> availableNodes;
|
||||
|
||||
public ScriptGraphCompiler(GraphEdit editorWindow)
|
||||
{
|
||||
this.editorWindow = editorWindow;
|
||||
}
|
||||
|
||||
public List<ProgramNode> BuildProgram(out string errorMessage)
|
||||
{
|
||||
errorMessage = "";
|
||||
NodeDisplay startNode = FindStartNode();
|
||||
if (startNode == null)
|
||||
{
|
||||
errorMessage = "(FAILED) Script needs exactly one Start node";
|
||||
return new List<ProgramNode>();
|
||||
}
|
||||
|
||||
BuildAvailableNodeLookup();
|
||||
return BuildScriptOrder(
|
||||
startNode,
|
||||
new List<ProgramNode>(),
|
||||
new HashSet<StringName>()
|
||||
);
|
||||
}
|
||||
|
||||
private void BuildAvailableNodeLookup()
|
||||
{
|
||||
availableNodes = new System.Collections.Generic.Dictionary<StringName, ProgramNode>();
|
||||
|
||||
for (int i = 0; i < editorWindow.GetChildCount(); i++)
|
||||
{
|
||||
NodeDisplay nodeDisplay = editorWindow.GetChild(i) as NodeDisplay;
|
||||
if (nodeDisplay == null) continue;
|
||||
|
||||
nodeDisplay.ReadParameters();
|
||||
availableNodes.Add(nodeDisplay.Name, nodeDisplay.node);
|
||||
}
|
||||
}
|
||||
|
||||
private List<ProgramNode> BuildScriptOrder(
|
||||
NodeDisplay node,
|
||||
List<ProgramNode> program,
|
||||
HashSet<StringName> visitedNodes
|
||||
)
|
||||
{
|
||||
if (node == null) return program;
|
||||
if (visitedNodes.Contains(node.Name)) return program;
|
||||
|
||||
visitedNodes.Add(node.Name);
|
||||
program.Add(node.node);
|
||||
|
||||
List<Dictionary> nextConnections = GetOutgoingConnections(node);
|
||||
if (nextConnections.Count <= 0) return program;
|
||||
|
||||
node.node.SetNextNode(nextConnections, availableNodes);
|
||||
foreach (Dictionary connection in nextConnections)
|
||||
{
|
||||
NodeDisplay nextNode = editorWindow.GetNodeOrNull<NodeDisplay>(
|
||||
new NodePath(connection["to_node"].AsStringName())
|
||||
);
|
||||
program = BuildScriptOrder(
|
||||
nextNode,
|
||||
program,
|
||||
visitedNodes
|
||||
);
|
||||
}
|
||||
|
||||
return program;
|
||||
}
|
||||
|
||||
private List<Dictionary> GetOutgoingConnections(NodeDisplay node)
|
||||
{
|
||||
List<Dictionary> result = new List<Dictionary>();
|
||||
Array<Dictionary> connections = editorWindow.GetConnectionListFromNode(node.Name);
|
||||
for (int i = 0; i < connections.Count; i++)
|
||||
{
|
||||
if (connections[i]["from_node"].AsStringName() == node.Name)
|
||||
{
|
||||
result.Add(connections[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private NodeDisplay FindStartNode()
|
||||
{
|
||||
NodeDisplay startNode = null;
|
||||
for (int i = 0; i < editorWindow.GetChildCount(); i++)
|
||||
{
|
||||
NodeDisplay nodeDisplay = editorWindow.GetChild(i) as NodeDisplay;
|
||||
if (nodeDisplay == null) continue;
|
||||
if (nodeDisplay.node is not StartNode) continue;
|
||||
if (startNode != null) return null;
|
||||
|
||||
startNode = nodeDisplay;
|
||||
}
|
||||
|
||||
return startNode;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://d122nx50nj3wc
|
||||
@@ -0,0 +1,189 @@
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
using System;
|
||||
public class ScriptGraphSerializer
|
||||
{
|
||||
private readonly GraphEdit editorWindow;
|
||||
private readonly System.Collections.Generic.Dictionary<ProgramNode, PackedScene> dslNodes;
|
||||
private readonly Action<NodeDisplay> addNode;
|
||||
private readonly Action<StringName, int, StringName, int> connectNodes;
|
||||
|
||||
public ScriptGraphSerializer(
|
||||
GraphEdit editorWindow,
|
||||
System.Collections.Generic.Dictionary<ProgramNode, PackedScene> dslNodes,
|
||||
Action<NodeDisplay> addNode,
|
||||
Action<StringName, int, StringName, int> connectNodes
|
||||
)
|
||||
{
|
||||
this.editorWindow = editorWindow;
|
||||
this.dslNodes = dslNodes;
|
||||
this.addNode = addNode;
|
||||
this.connectNodes = connectNodes;
|
||||
}
|
||||
|
||||
public void Load(string scriptContent)
|
||||
{
|
||||
Variant parsedScript = Json.ParseString(scriptContent);
|
||||
if (parsedScript.VariantType != Variant.Type.Dictionary) return;
|
||||
|
||||
Dictionary scriptData = parsedScript.AsGodotDictionary();
|
||||
if (!scriptData.ContainsKey("Nodes")) return;
|
||||
|
||||
System.Collections.Generic.Dictionary<string, NodeDisplay> loadedNodes =
|
||||
new System.Collections.Generic.Dictionary<string, NodeDisplay>();
|
||||
Godot.Collections.Array nodes = scriptData["Nodes"].AsGodotArray();
|
||||
for (int i = 0; i < nodes.Count; i++)
|
||||
{
|
||||
Dictionary nodeData = nodes[i].AsGodotDictionary();
|
||||
NodeDisplay nodeDisplay = LoadNode(nodeData);
|
||||
if (nodeDisplay == null) continue;
|
||||
|
||||
addNode(nodeDisplay);
|
||||
RegisterLoadedNode(nodeData, nodeDisplay, loadedNodes);
|
||||
}
|
||||
|
||||
LoadConnections(scriptData, loadedNodes);
|
||||
}
|
||||
|
||||
public string Save()
|
||||
{
|
||||
Array<Dictionary> savedNodes = BuildSavedNodes();
|
||||
if (savedNodes.Count <= 0) return "";
|
||||
|
||||
Dictionary scriptData = new Dictionary();
|
||||
scriptData["Nodes"] = savedNodes;
|
||||
scriptData["Connections"] = BuildSavedConnections();
|
||||
|
||||
return Json.Stringify(scriptData);
|
||||
}
|
||||
|
||||
private NodeDisplay LoadNode(Dictionary nodeData)
|
||||
{
|
||||
if (!nodeData.ContainsKey("Type")) return null;
|
||||
if (!nodeData.ContainsKey("Content")) return null;
|
||||
|
||||
string type = nodeData["Type"].AsString();
|
||||
string content = nodeData["Content"].AsString();
|
||||
NodeDisplay nodeDisplay = NodeDisplay.Load(type, content, dslNodes);
|
||||
if (nodeDisplay == null) return null;
|
||||
|
||||
if (nodeData.ContainsKey("Id"))
|
||||
{
|
||||
nodeDisplay.Name = nodeData["Id"].AsString();
|
||||
}
|
||||
|
||||
if (nodeData.ContainsKey("PositionX") && nodeData.ContainsKey("PositionY"))
|
||||
{
|
||||
float positionX = (float)nodeData["PositionX"].AsDouble();
|
||||
float positionY = (float)nodeData["PositionY"].AsDouble();
|
||||
nodeDisplay.PositionOffset = new Vector2(positionX, positionY);
|
||||
}
|
||||
|
||||
return nodeDisplay;
|
||||
}
|
||||
|
||||
private void RegisterLoadedNode(
|
||||
Dictionary nodeData,
|
||||
NodeDisplay nodeDisplay,
|
||||
System.Collections.Generic.Dictionary<string, NodeDisplay> loadedNodes
|
||||
)
|
||||
{
|
||||
string nodeId = nodeDisplay.Name.ToString();
|
||||
if (nodeData.ContainsKey("Id"))
|
||||
{
|
||||
nodeId = nodeData["Id"].AsString();
|
||||
}
|
||||
|
||||
if (!loadedNodes.ContainsKey(nodeId))
|
||||
{
|
||||
loadedNodes.Add(nodeId, nodeDisplay);
|
||||
}
|
||||
}
|
||||
|
||||
private void LoadConnections(
|
||||
Dictionary scriptData,
|
||||
System.Collections.Generic.Dictionary<string, NodeDisplay> loadedNodes
|
||||
)
|
||||
{
|
||||
if (!scriptData.ContainsKey("Connections")) return;
|
||||
|
||||
Godot.Collections.Array connectionData = scriptData["Connections"].AsGodotArray();
|
||||
for (int i = 0; i < connectionData.Count; i++)
|
||||
{
|
||||
Dictionary savedConnection = connectionData[i].AsGodotDictionary();
|
||||
if (!IsConnectionDataValid(savedConnection)) continue;
|
||||
|
||||
ScriptConnection connection = CreateConnection(savedConnection);
|
||||
ConnectLoadedNodes(connection, loadedNodes);
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsConnectionDataValid(Dictionary savedConnection)
|
||||
{
|
||||
return savedConnection.ContainsKey("From")
|
||||
&& savedConnection.ContainsKey("To")
|
||||
&& savedConnection.ContainsKey("FromPort")
|
||||
&& savedConnection.ContainsKey("ToPort");
|
||||
}
|
||||
|
||||
private ScriptConnection CreateConnection(Dictionary savedConnection)
|
||||
{
|
||||
return new ScriptConnection
|
||||
{
|
||||
FromNodeId = savedConnection["From"].AsString(),
|
||||
FromPort = savedConnection["FromPort"].AsInt32(),
|
||||
ToNodeId = savedConnection["To"].AsString(),
|
||||
ToPort = savedConnection["ToPort"].AsInt32()
|
||||
};
|
||||
}
|
||||
|
||||
private void ConnectLoadedNodes(
|
||||
ScriptConnection connection,
|
||||
System.Collections.Generic.Dictionary<string, NodeDisplay> loadedNodes
|
||||
)
|
||||
{
|
||||
if (!loadedNodes.ContainsKey(connection.FromNodeId)) return;
|
||||
if (!loadedNodes.ContainsKey(connection.ToNodeId)) return;
|
||||
|
||||
NodeDisplay fromDisplay = loadedNodes[connection.FromNodeId];
|
||||
NodeDisplay toDisplay = loadedNodes[connection.ToNodeId];
|
||||
connectNodes(fromDisplay.Name, connection.FromPort, toDisplay.Name, connection.ToPort);
|
||||
}
|
||||
|
||||
private Array<Dictionary> BuildSavedNodes()
|
||||
{
|
||||
Array<Dictionary> savedNodes = new Array<Dictionary>();
|
||||
for (int i = 0; i < editorWindow.GetChildCount(); i++)
|
||||
{
|
||||
NodeDisplay nodeDisplay = editorWindow.GetChild(i) as NodeDisplay;
|
||||
if (nodeDisplay == null) continue;
|
||||
|
||||
nodeDisplay.ReadParameters();
|
||||
Dictionary savedNode = new Dictionary();
|
||||
savedNode["Id"] = nodeDisplay.Name.ToString();
|
||||
savedNode["Type"] = nodeDisplay.node.DisplayText.ToLower();
|
||||
savedNode["Content"] = nodeDisplay.node.Save();
|
||||
savedNode["PositionX"] = nodeDisplay.PositionOffset.X;
|
||||
savedNode["PositionY"] = nodeDisplay.PositionOffset.Y;
|
||||
savedNodes.Add(savedNode);
|
||||
}
|
||||
|
||||
return savedNodes;
|
||||
}
|
||||
|
||||
private Array<Dictionary> BuildSavedConnections()
|
||||
{
|
||||
Array<Dictionary> savedConnections = new Array<Dictionary>();
|
||||
foreach (Dictionary connection in editorWindow.GetConnectionList())
|
||||
{
|
||||
Dictionary savedConnection = new Dictionary();
|
||||
savedConnection["From"] = connection["from_node"].AsStringName().ToString();
|
||||
savedConnection["FromPort"] = (int)connection["from_port"];
|
||||
savedConnection["To"] = connection["to_node"].AsStringName().ToString();
|
||||
savedConnection["ToPort"] = (int)connection["to_port"];
|
||||
savedConnections.Add(savedConnection);
|
||||
}
|
||||
|
||||
return savedConnections;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://dsrkrw6524c
|
||||
Reference in New Issue
Block a user