Added ability to delete nodes from editor, added complete node load and save for the DSL
This commit is contained in:
@@ -4,11 +4,17 @@ using System.Collections.Generic;
|
||||
|
||||
public partial class CodingWindow : PanelContainer
|
||||
{
|
||||
//General
|
||||
Robot robot;
|
||||
//Scripting
|
||||
[Export] VBoxContainer codeBlocks;
|
||||
[Export] VBoxContainer editorWindow;
|
||||
public Dictionary<ProgramNode, PackedScene> DSLNodes;
|
||||
Robot robot;
|
||||
|
||||
[Export] OptionButton availableScripts;
|
||||
[Export] LineEdit scriptName;
|
||||
|
||||
//Renaming
|
||||
[Export] LineEdit nameInput;
|
||||
|
||||
// Called when the node enters the scene tree for the first time.
|
||||
public override void _Ready()
|
||||
@@ -20,10 +26,46 @@ public partial class CodingWindow : PanelContainer
|
||||
// Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
public override void _Notification(int id)
|
||||
{
|
||||
if (id == NotificationVisibilityChanged)
|
||||
{
|
||||
if (Visible) LoadWindow();
|
||||
}
|
||||
}
|
||||
|
||||
private void LoadWindow()
|
||||
{
|
||||
nameInput.Text = robot.Name;
|
||||
SetupScriptOptions();
|
||||
ClearWindow();
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
robot.Name = nameInput.Text;
|
||||
}
|
||||
|
||||
public void CloseWindow()
|
||||
{
|
||||
Hide();
|
||||
}
|
||||
|
||||
//Move, Harvest, Craft
|
||||
public void GenerateCodingBlocks()
|
||||
{
|
||||
NodeDisplay nodeDisplay;
|
||||
@@ -39,6 +81,10 @@ public partial class CodingWindow : PanelContainer
|
||||
editorDisplay.node = node;
|
||||
editorWindow.AddChild(editorDisplay);
|
||||
editorDisplay.ShowEditorDisplay();
|
||||
editorDisplay.OnDeleteNode += () =>
|
||||
{
|
||||
editorWindow.RemoveChild(editorDisplay);
|
||||
};
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -50,6 +96,7 @@ public partial class CodingWindow : PanelContainer
|
||||
editorWindow.RemoveChild(node);
|
||||
node.QueueFree();
|
||||
}
|
||||
scriptName.Text = "";
|
||||
}
|
||||
|
||||
public void CompileProgram()
|
||||
@@ -62,15 +109,52 @@ public partial class CodingWindow : PanelContainer
|
||||
nodes.Add(editorWindow.GetChild<NodeDisplay>(i).node.Duplicate());
|
||||
if (i != 0)
|
||||
{
|
||||
nodes[i-1].nextNode = nodes[i];
|
||||
nodes[i - 1].nextNode = nodes[i];
|
||||
}
|
||||
}
|
||||
|
||||
if(nodes.Count > 0) robot.SetupExecution(nodes);
|
||||
if (nodes.Count > 0) robot.SetupExecution(nodes);
|
||||
}
|
||||
|
||||
public void SetRobot(Robot robot)
|
||||
{
|
||||
this.robot = robot;
|
||||
}
|
||||
|
||||
public void LoadProgram(int index)
|
||||
{
|
||||
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);
|
||||
nodeDisplay.ShowEditorDisplay();
|
||||
nodeDisplay.OnDeleteNode += () =>
|
||||
{
|
||||
editorWindow.RemoveChild(nodeDisplay);
|
||||
};
|
||||
}
|
||||
}
|
||||
scriptName.Text = availableScripts.GetItemText(index);
|
||||
availableScripts.Select(0);
|
||||
}
|
||||
|
||||
public void SaveProgram()
|
||||
{
|
||||
string result = "";
|
||||
for (int i = 0; i < editorWindow.GetChildCount(); i++)
|
||||
{
|
||||
editorWindow.GetChild<NodeDisplay>(i).node.ReadParameters(editorWindow.GetChild<NodeDisplay>(i));
|
||||
result += editorWindow.GetChild<NodeDisplay>(i).node.Save();
|
||||
result += ";\r\n";
|
||||
}
|
||||
if (result.Length <= 0) return;
|
||||
string filename = scriptName.Text.Length <= 0 ? $"Script{availableScripts.ItemCount}" : scriptName.Text;
|
||||
FileHandler.SaveProgram(filename, result);
|
||||
SetupScriptOptions();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user