(WIP) Switch from previous DSL System (UI order based) to a new DSL System (node connection based) still in Progress.
This commit is contained in:
@@ -13,6 +13,7 @@ public partial class CodingWindow : PanelContainer
|
||||
[Export] LineEdit nameInput;
|
||||
|
||||
public System.Collections.Generic.Dictionary<ProgramNode, PackedScene> DSLNodes;
|
||||
private System.Collections.Generic.Dictionary<StringName, ProgramNode> availableNodes;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
@@ -83,10 +84,19 @@ 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;
|
||||
editorWindow.AddChild(editorDisplay);
|
||||
RegisterEditorNode(editorDisplay);
|
||||
}
|
||||
|
||||
private void MoveNodeToVisibleGraphCenter(NodeDisplay nodeDisplay)
|
||||
{
|
||||
Vector2 visibleCenter = editorWindow.ScrollOffset
|
||||
+ editorWindow.Size / (2f * editorWindow.Zoom);
|
||||
|
||||
nodeDisplay.PositionOffset = visibleCenter - nodeDisplay.Size / (2f * editorWindow.Zoom);
|
||||
}
|
||||
|
||||
private void RegisterEditorNode(NodeDisplay editorDisplay)
|
||||
{
|
||||
editorDisplay.OnDeleteNode += () =>
|
||||
@@ -121,7 +131,25 @@ public partial class CodingWindow : PanelContainer
|
||||
|
||||
public void CompileProgram()
|
||||
{
|
||||
List<ProgramNode> nodes = new List<ProgramNode>();
|
||||
if (robot == null) return;
|
||||
|
||||
NodeDisplay startNode = FindStartNode();
|
||||
if (startNode == null)
|
||||
{
|
||||
robot.StopExecution("(FAILED) Script needs exactly one Start node");
|
||||
return;
|
||||
}
|
||||
|
||||
availableNodes = BuildAvailableNodeLookup();
|
||||
List<ProgramNode> nodes = BuildScriptOrder(startNode, new List<ProgramNode>());
|
||||
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()
|
||||
{
|
||||
System.Collections.Generic.Dictionary<StringName, ProgramNode> availableNodes =
|
||||
new System.Collections.Generic.Dictionary<StringName, ProgramNode>();
|
||||
|
||||
for (int i = 0; i < editorWindow.GetChildCount(); i++)
|
||||
{
|
||||
@@ -129,22 +157,56 @@ public partial class CodingWindow : PanelContainer
|
||||
if (nodeDisplay == null) continue;
|
||||
|
||||
nodeDisplay.ReadParameters();
|
||||
ProgramNode executableNode = nodeDisplay.node.Duplicate();
|
||||
nodes.Add(executableNode);
|
||||
|
||||
if (nodes.Count > 1)
|
||||
{
|
||||
nodes[nodes.Count - 2].nextNode = executableNode;
|
||||
}
|
||||
if (nodes.Count > 1)
|
||||
{
|
||||
executableNode.previousNode = nodes[nodes.Count - 2];
|
||||
}
|
||||
availableNodes.Add(nodeDisplay.Name, nodeDisplay.node);
|
||||
}
|
||||
|
||||
if (robot == null) return;
|
||||
if (nodes.Count > 0) robot.SetupExecution(nodes);
|
||||
robot.currentProgram = scriptName.Text.Length <= 0 ? $"Script{availableScripts.ItemCount}" : scriptName.Text;
|
||||
return availableNodes;
|
||||
}
|
||||
|
||||
private List<ProgramNode> BuildScriptOrder(NodeDisplay node, List<ProgramNode> program)
|
||||
{
|
||||
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)
|
||||
{
|
||||
program = BuildScriptOrder(
|
||||
editorWindow.GetNode<NodeDisplay>(new NodePath(connection["to_node"].AsStringName())),
|
||||
program
|
||||
);
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
public void SetRobot(Robot robot)
|
||||
@@ -177,14 +239,8 @@ public partial class CodingWindow : PanelContainer
|
||||
if (robot == null) return;
|
||||
if (robot.currentNode == null) return;
|
||||
|
||||
ProgramNode firstNode = robot.currentNode;
|
||||
while (firstNode.previousNode != null)
|
||||
{
|
||||
firstNode = firstNode.previousNode;
|
||||
}
|
||||
|
||||
HashSet<ProgramNode> loadedNodes = new HashSet<ProgramNode>();
|
||||
ProgramNode nodeToLoad = firstNode;
|
||||
ProgramNode nodeToLoad = robot.currentNode;
|
||||
while (nodeToLoad != null && !loadedNodes.Contains(nodeToLoad))
|
||||
{
|
||||
loadedNodes.Add(nodeToLoad);
|
||||
@@ -238,12 +294,10 @@ public partial class CodingWindow : PanelContainer
|
||||
|
||||
public void OnNodeConnect(StringName from, int fromPort, StringName to, int toPort)
|
||||
{
|
||||
GD.Print($"From {fromPort} to {toPort}");
|
||||
if (to == from) return;
|
||||
|
||||
foreach (Dictionary connection in editorWindow.GetConnectionList())
|
||||
{
|
||||
if (connection["to_node"].AsStringName() == to && (int)connection["to_port"] == toPort) return;
|
||||
if (connection["from_node"].AsStringName() == from && (int)connection["from_port"] == fromPort) return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user