521 lines
14 KiB
C#
521 lines
14 KiB
C#
using Godot;
|
|
using Godot.Collections;
|
|
using System.Collections.Generic;
|
|
|
|
public partial class CodingWindow : PanelContainer
|
|
{
|
|
private Robot robot;
|
|
|
|
[Export] VBoxContainer codeBlocks;
|
|
[Export] GraphEdit editorWindow;
|
|
[Export] OptionButton availableScripts;
|
|
[Export] LineEdit scriptName;
|
|
[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()
|
|
{
|
|
DSLNodes = ResourceLoader.LoadDSLNodes();
|
|
GenerateCodingBlocks();
|
|
}
|
|
|
|
public override void _Notification(int id)
|
|
{
|
|
if (id == NotificationVisibilityChanged)
|
|
{
|
|
if (Visible) LoadWindow();
|
|
}
|
|
}
|
|
|
|
private void LoadWindow()
|
|
{
|
|
if (robot == null) return;
|
|
|
|
nameInput.Text = robot.Name;
|
|
SetupScriptOptions();
|
|
ClearWindow();
|
|
LoadTemporaryProgram();
|
|
}
|
|
|
|
private void SetupScriptOptions()
|
|
{
|
|
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)
|
|
{
|
|
availableScripts.AddItem(script);
|
|
}
|
|
}
|
|
|
|
public void SaveRobotName()
|
|
{
|
|
if (robot == null) return;
|
|
|
|
robot.Name = nameInput.Text;
|
|
}
|
|
|
|
public void CloseWindow()
|
|
{
|
|
Hide();
|
|
}
|
|
|
|
public void GenerateCodingBlocks()
|
|
{
|
|
Button nodeListButton;
|
|
foreach (ProgramNode nodeTemplate in DSLNodes.Keys)
|
|
{
|
|
nodeListButton = new Button
|
|
{
|
|
Name = nodeTemplate.DisplayText,
|
|
Text = nodeTemplate.DisplayText
|
|
};
|
|
nodeListButton.Pressed += () =>
|
|
{
|
|
AddEditorNode(nodeTemplate);
|
|
};
|
|
codeBlocks.AddChild(nodeListButton);
|
|
}
|
|
}
|
|
|
|
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 += () =>
|
|
{
|
|
editorWindow.RemoveChild(editorDisplay);
|
|
editorDisplay.QueueFree();
|
|
};
|
|
}
|
|
|
|
public void ClearWindow()
|
|
{
|
|
foreach (Dictionary connection in editorWindow.GetConnectionList())
|
|
{
|
|
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 = "";
|
|
}
|
|
|
|
public void CompileProgram()
|
|
{
|
|
if (robot == null) return;
|
|
|
|
NodeDisplay startNode = FindStartNode();
|
|
if (startNode == null)
|
|
{
|
|
robot.StopExecution("(FAILED) Script needs exactly one Start node");
|
|
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;
|
|
}
|
|
|
|
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);
|
|
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;
|
|
}
|
|
|
|
public void SetRobot(Robot robot)
|
|
{
|
|
this.robot = robot;
|
|
}
|
|
|
|
public void LoadProgram(int index)
|
|
{
|
|
if (index <= 0) return;
|
|
|
|
ClearWindow();
|
|
string scriptContent = FileHandler.LoadProgram(availableScripts.GetItemText(index));
|
|
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;
|
|
|
|
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;
|
|
int selectedIndex = availableScripts.GetSelectedId();
|
|
if (selectedIndex > 0)
|
|
{
|
|
filename = availableScripts.GetItemText(selectedIndex);
|
|
}
|
|
|
|
if (filename.Length <= 0) return;
|
|
if (!FileHandler.DeleteProgram(filename)) return;
|
|
|
|
ClearWindow();
|
|
SetupScriptOptions();
|
|
}
|
|
|
|
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);
|
|
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();
|
|
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;
|
|
}
|
|
|
|
editorWindow.ConnectNode(from, fromPort, to, toPort);
|
|
}
|
|
|
|
public void OnNodeDisconnect(StringName from, int fromPort, StringName to, int toPort)
|
|
{
|
|
editorWindow.DisconnectNode(from, fromPort, to, toPort);
|
|
}
|
|
}
|