(WIP) switched from previous DSL System (UI order based) to a new DSL System (node connection based)
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public partial class CodingWindow : PanelContainer
|
||||
@@ -6,12 +7,12 @@ public partial class CodingWindow : PanelContainer
|
||||
private Robot robot;
|
||||
|
||||
[Export] VBoxContainer codeBlocks;
|
||||
[Export] VBoxContainer editorWindow;
|
||||
[Export] GraphEdit editorWindow;
|
||||
[Export] OptionButton availableScripts;
|
||||
[Export] LineEdit scriptName;
|
||||
[Export] LineEdit nameInput;
|
||||
|
||||
public Dictionary<ProgramNode, PackedScene> DSLNodes;
|
||||
public System.Collections.Generic.Dictionary<ProgramNode, PackedScene> DSLNodes;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
@@ -63,48 +64,57 @@ public partial class CodingWindow : PanelContainer
|
||||
|
||||
public void GenerateCodingBlocks()
|
||||
{
|
||||
NodeDisplay nodeDisplay;
|
||||
Button nodeListButton;
|
||||
foreach (ProgramNode nodeTemplate in DSLNodes.Keys)
|
||||
{
|
||||
nodeDisplay = DSLNodes[nodeTemplate].Instantiate<NodeDisplay>();
|
||||
nodeDisplay.node = nodeTemplate;
|
||||
codeBlocks.AddChild(nodeDisplay);
|
||||
nodeDisplay.ShowListDisplay();
|
||||
nodeDisplay.listDisplay.Pressed += () =>
|
||||
nodeListButton = new Button
|
||||
{
|
||||
AddEditorNode(DSLNodes[nodeTemplate], nodeTemplate.Duplicate());
|
||||
Name = nodeTemplate.DisplayText,
|
||||
Text = nodeTemplate.DisplayText
|
||||
};
|
||||
nodeListButton.Pressed += () =>
|
||||
{
|
||||
AddEditorNode(nodeTemplate);
|
||||
};
|
||||
codeBlocks.AddChild(nodeListButton);
|
||||
}
|
||||
}
|
||||
|
||||
private void AddEditorNode(PackedScene prefab, ProgramNode node)
|
||||
private void AddEditorNode(ProgramNode node)
|
||||
{
|
||||
NodeDisplay editorDisplay = prefab.Instantiate<NodeDisplay>();
|
||||
editorDisplay.node = node;
|
||||
NodeDisplay editorDisplay = DSLNodes[node].Instantiate<NodeDisplay>();
|
||||
editorWindow.AddChild(editorDisplay);
|
||||
editorDisplay.ShowEditorDisplay();
|
||||
RegisterEditorNode(editorDisplay);
|
||||
}
|
||||
|
||||
private void RegisterEditorNode(NodeDisplay editorDisplay)
|
||||
{
|
||||
editorDisplay.OnDeleteNode += () =>
|
||||
{
|
||||
editorWindow.RemoveChild(editorDisplay);
|
||||
editorDisplay.QueueFree();
|
||||
};
|
||||
editorDisplay.OnMoveNode += (int direction) =>
|
||||
{
|
||||
int targetIndex = Mathf.Clamp(
|
||||
editorDisplay.GetIndex() + direction,
|
||||
0,
|
||||
editorWindow.GetChildCount() - 1
|
||||
);
|
||||
editorWindow.MoveChild(editorDisplay, targetIndex);
|
||||
};
|
||||
}
|
||||
|
||||
public void ClearWindow()
|
||||
{
|
||||
foreach (Node node in editorWindow.GetChildren())
|
||||
foreach (Dictionary connection in editorWindow.GetConnectionList())
|
||||
{
|
||||
editorWindow.RemoveChild(node);
|
||||
node.QueueFree();
|
||||
editorWindow.DisconnectNode(
|
||||
connection["from_node"].AsStringName(),
|
||||
(int)connection["from_port"],
|
||||
connection["to_node"].AsStringName(),
|
||||
(int)connection["to_port"]
|
||||
);
|
||||
}
|
||||
|
||||
foreach (Node child in editorWindow.GetChildren())
|
||||
{
|
||||
if (child is GraphNode)
|
||||
{
|
||||
editorWindow.RemoveChild(child);
|
||||
child.QueueFree();
|
||||
}
|
||||
}
|
||||
scriptName.Text = "";
|
||||
}
|
||||
@@ -115,16 +125,20 @@ public partial class CodingWindow : PanelContainer
|
||||
|
||||
for (int i = 0; i < editorWindow.GetChildCount(); i++)
|
||||
{
|
||||
NodeDisplay nodeDisplay = editorWindow.GetChild<NodeDisplay>(i);
|
||||
nodeDisplay.node.ReadParameters(nodeDisplay);
|
||||
nodes.Add(nodeDisplay.node.Duplicate());
|
||||
if (i != 0)
|
||||
NodeDisplay nodeDisplay = editorWindow.GetChild(i) as NodeDisplay;
|
||||
if (nodeDisplay == null) continue;
|
||||
|
||||
nodeDisplay.ReadParameters();
|
||||
ProgramNode executableNode = nodeDisplay.node.Duplicate();
|
||||
nodes.Add(executableNode);
|
||||
|
||||
if (nodes.Count > 1)
|
||||
{
|
||||
nodes[i - 1].nextNode = nodes[i];
|
||||
nodes[nodes.Count - 2].nextNode = executableNode;
|
||||
}
|
||||
if (i > 0)
|
||||
if (nodes.Count > 1)
|
||||
{
|
||||
nodes[i].previousNode = nodes[i-1];
|
||||
executableNode.previousNode = nodes[nodes.Count - 2];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -151,12 +165,7 @@ public partial class CodingWindow : PanelContainer
|
||||
if (nodeDisplay != null)
|
||||
{
|
||||
editorWindow.AddChild(nodeDisplay);
|
||||
nodeDisplay.ShowEditorDisplay();
|
||||
nodeDisplay.OnDeleteNode += () =>
|
||||
{
|
||||
editorWindow.RemoveChild(nodeDisplay);
|
||||
nodeDisplay.QueueFree();
|
||||
};
|
||||
RegisterEditorNode(nodeDisplay);
|
||||
}
|
||||
}
|
||||
scriptName.Text = availableScripts.GetItemText(index);
|
||||
@@ -184,21 +193,7 @@ public partial class CodingWindow : PanelContainer
|
||||
if (nodeDisplay != null)
|
||||
{
|
||||
editorWindow.AddChild(nodeDisplay);
|
||||
nodeDisplay.ShowEditorDisplay();
|
||||
nodeDisplay.OnDeleteNode += () =>
|
||||
{
|
||||
editorWindow.RemoveChild(nodeDisplay);
|
||||
nodeDisplay.QueueFree();
|
||||
};
|
||||
nodeDisplay.OnMoveNode += (int direction) =>
|
||||
{
|
||||
int targetIndex = Mathf.Clamp(
|
||||
nodeDisplay.GetIndex() + direction,
|
||||
0,
|
||||
editorWindow.GetChildCount() - 1
|
||||
);
|
||||
editorWindow.MoveChild(nodeDisplay, targetIndex);
|
||||
};
|
||||
RegisterEditorNode(nodeDisplay);
|
||||
}
|
||||
|
||||
nodeToLoad = nodeToLoad.nextNode;
|
||||
@@ -228,8 +223,10 @@ public partial class CodingWindow : PanelContainer
|
||||
string result = "";
|
||||
for (int i = 0; i < editorWindow.GetChildCount(); i++)
|
||||
{
|
||||
NodeDisplay nodeDisplay = editorWindow.GetChild<NodeDisplay>(i);
|
||||
nodeDisplay.node.ReadParameters(nodeDisplay);
|
||||
NodeDisplay nodeDisplay = editorWindow.GetChild(i) as NodeDisplay;
|
||||
if (nodeDisplay == null) continue;
|
||||
|
||||
nodeDisplay.ReadParameters();
|
||||
result += nodeDisplay.node.Save();
|
||||
result += ";\r\n";
|
||||
}
|
||||
@@ -238,4 +235,23 @@ public partial class CodingWindow : PanelContainer
|
||||
FileHandler.SaveProgram(filename, result);
|
||||
SetupScriptOptions();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
editorWindow.ConnectNode(from, fromPort, to, toPort);
|
||||
}
|
||||
|
||||
public void OnNodeDisconnect(StringName from, int fromPort, StringName to, int toPort)
|
||||
{
|
||||
editorWindow.DisconnectNode(from, fromPort, to, toPort);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user