Finished new node approach
This commit is contained in:
+251
-42
@@ -15,6 +15,14 @@ public partial class CodingWindow : PanelContainer
|
||||
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()
|
||||
{
|
||||
DSLNodes = ResourceLoader.LoadDSLNodes();
|
||||
@@ -140,16 +148,19 @@ public partial class CodingWindow : PanelContainer
|
||||
return;
|
||||
}
|
||||
|
||||
availableNodes = BuildAvailableNodeLookup();
|
||||
List<ProgramNode> nodes = BuildScriptOrder(startNode, new List<ProgramNode>());
|
||||
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;
|
||||
}
|
||||
|
||||
private System.Collections.Generic.Dictionary<StringName, ProgramNode> BuildAvailableNodeLookup()
|
||||
private void BuildAvailableNodeLookup()
|
||||
{
|
||||
System.Collections.Generic.Dictionary<StringName, ProgramNode> availableNodes =
|
||||
new System.Collections.Generic.Dictionary<StringName, ProgramNode>();
|
||||
availableNodes = new System.Collections.Generic.Dictionary<StringName, ProgramNode>();
|
||||
|
||||
for (int i = 0; i < editorWindow.GetChildCount(); i++)
|
||||
{
|
||||
@@ -159,12 +170,18 @@ public partial class CodingWindow : PanelContainer
|
||||
nodeDisplay.ReadParameters();
|
||||
availableNodes.Add(nodeDisplay.Name, nodeDisplay.node);
|
||||
}
|
||||
|
||||
return availableNodes;
|
||||
}
|
||||
|
||||
private List<ProgramNode> BuildScriptOrder(NodeDisplay node, List<ProgramNode> program)
|
||||
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);
|
||||
@@ -172,9 +189,13 @@ public partial class CodingWindow : PanelContainer
|
||||
node.node.SetNextNode(nextConnections, availableNodes);
|
||||
foreach (Dictionary connection in nextConnections)
|
||||
{
|
||||
NodeDisplay nextNode = editorWindow.GetNodeOrNull<NodeDisplay>(
|
||||
new NodePath(connection["to_node"].AsStringName())
|
||||
);
|
||||
program = BuildScriptOrder(
|
||||
editorWindow.GetNode<NodeDisplay>(new NodePath(connection["to_node"].AsStringName())),
|
||||
program
|
||||
nextNode,
|
||||
program,
|
||||
visitedNodes
|
||||
);
|
||||
}
|
||||
return program;
|
||||
@@ -220,44 +241,197 @@ public partial class CodingWindow : PanelContainer
|
||||
|
||||
ClearWindow();
|
||||
string scriptContent = FileHandler.LoadProgram(availableScripts.GetItemText(index));
|
||||
string[] nodes = scriptContent.Split(";");
|
||||
foreach (string node in nodes)
|
||||
{
|
||||
NodeDisplay nodeDisplay = NodeDisplay.Load(node, DSLNodes);
|
||||
if (nodeDisplay != null)
|
||||
{
|
||||
editorWindow.AddChild(nodeDisplay);
|
||||
RegisterEditorNode(nodeDisplay);
|
||||
}
|
||||
}
|
||||
LoadStructuredProgram(scriptContent);
|
||||
scriptName.Text = availableScripts.GetItemText(index);
|
||||
availableScripts.Select(0);
|
||||
}
|
||||
|
||||
private void LoadStructuredProgram(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>();
|
||||
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;
|
||||
}
|
||||
|
||||
public void LoadTemporaryProgram()
|
||||
{
|
||||
if (robot == null) return;
|
||||
if (robot.currentNode == null) return;
|
||||
|
||||
HashSet<ProgramNode> loadedNodes = new HashSet<ProgramNode>();
|
||||
ProgramNode nodeToLoad = robot.currentNode;
|
||||
while (nodeToLoad != null && !loadedNodes.Contains(nodeToLoad))
|
||||
{
|
||||
loadedNodes.Add(nodeToLoad);
|
||||
|
||||
NodeDisplay nodeDisplay = NodeDisplay.Load(nodeToLoad.Save(), DSLNodes);
|
||||
if (nodeDisplay != null)
|
||||
{
|
||||
editorWindow.AddChild(nodeDisplay);
|
||||
RegisterEditorNode(nodeDisplay);
|
||||
}
|
||||
|
||||
nodeToLoad = nodeToLoad.nextNode;
|
||||
}
|
||||
System.Collections.Generic.Dictionary<ProgramNode, NodeDisplay> loadedNodes =
|
||||
new System.Collections.Generic.Dictionary<ProgramNode, NodeDisplay>();
|
||||
LoadTemporaryNode(robot.currentNode, loadedNodes);
|
||||
|
||||
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;
|
||||
@@ -276,20 +450,55 @@ public partial class CodingWindow : PanelContainer
|
||||
|
||||
public void SaveProgram()
|
||||
{
|
||||
string result = "";
|
||||
Array<Dictionary> savedNodes = BuildSavedNodes();
|
||||
if (savedNodes.Count <= 0) return;
|
||||
|
||||
Dictionary scriptData = new Dictionary();
|
||||
scriptData["Nodes"] = savedNodes;
|
||||
scriptData["Connections"] = BuildSavedConnections();
|
||||
|
||||
string result = Json.Stringify(scriptData);
|
||||
if (result.Length <= 0) return;
|
||||
string filename = scriptName.Text.Length <= 0 ? $"Script{availableScripts.ItemCount}" : scriptName.Text;
|
||||
FileHandler.SaveProgram(filename, 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();
|
||||
result += nodeDisplay.node.Save();
|
||||
result += ";\r\n";
|
||||
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);
|
||||
}
|
||||
if (result.Length <= 0) return;
|
||||
string filename = scriptName.Text.Length <= 0 ? $"Script{availableScripts.ItemCount}" : scriptName.Text;
|
||||
FileHandler.SaveProgram(filename, result);
|
||||
SetupScriptOptions();
|
||||
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user