Cleaned up project with better structure.

This commit is contained in:
2026-05-09 11:29:48 +02:00
parent 1ad3454f6a
commit 6708aa277f
95 changed files with 711 additions and 700 deletions
+165
View File
@@ -0,0 +1,165 @@
using Godot;
using System.Collections.Generic;
public partial class CodingWindow : PanelContainer
{
private Robot robot;
[Export] VBoxContainer codeBlocks;
[Export] VBoxContainer editorWindow;
[Export] OptionButton availableScripts;
[Export] LineEdit scriptName;
[Export] LineEdit nameInput;
public Dictionary<ProgramNode, PackedScene> DSLNodes;
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();
}
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()
{
NodeDisplay nodeDisplay;
foreach (ProgramNode nodeTemplate in DSLNodes.Keys)
{
nodeDisplay = DSLNodes[nodeTemplate].Instantiate<NodeDisplay>();
nodeDisplay.node = nodeTemplate;
codeBlocks.AddChild(nodeDisplay);
nodeDisplay.ShowListDisplay();
nodeDisplay.listDisplay.Pressed += () =>
{
AddEditorNode(DSLNodes[nodeTemplate], nodeTemplate.Duplicate());
};
}
}
private void AddEditorNode(PackedScene prefab, ProgramNode node)
{
NodeDisplay editorDisplay = prefab.Instantiate<NodeDisplay>();
editorDisplay.node = node;
editorWindow.AddChild(editorDisplay);
editorDisplay.ShowEditorDisplay();
editorDisplay.OnDeleteNode += () =>
{
editorWindow.RemoveChild(editorDisplay);
editorDisplay.QueueFree();
};
}
public void ClearWindow()
{
foreach (Node node in editorWindow.GetChildren())
{
editorWindow.RemoveChild(node);
node.QueueFree();
}
scriptName.Text = "";
}
public void CompileProgram()
{
List<ProgramNode> nodes = new List<ProgramNode>();
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)
{
nodes[i - 1].nextNode = nodes[i];
}
}
if (robot == null) return;
if (nodes.Count > 0) robot.SetupExecution(nodes);
robot.currentProgram = scriptName.Text.Length <= 0 ? $"Script{availableScripts.ItemCount}" : scriptName.Text;
}
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);
nodeDisplay.QueueFree();
};
}
}
scriptName.Text = availableScripts.GetItemText(index);
availableScripts.Select(0);
}
public void SaveProgram()
{
string result = "";
for (int i = 0; i < editorWindow.GetChildCount(); i++)
{
NodeDisplay nodeDisplay = editorWindow.GetChild<NodeDisplay>(i);
nodeDisplay.node.ReadParameters(nodeDisplay);
result += nodeDisplay.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();
}
}
+1
View File
@@ -0,0 +1 @@
uid://bsd6n6b06a4pe
+135
View File
@@ -0,0 +1,135 @@
using System.Collections.Generic;
using Godot;
public partial class NodeDisplay : PanelContainer
{
[Export] PanelContainer editorDisplay;
[Export] public Button listDisplay;
public ProgramNode node;
[Signal]
public delegate void OnDeleteNodeEventHandler();
public void SetNode(ProgramNode node)
{
this.node = node;
}
public override void _Ready()
{
if (node == null) return;
node.Setup(this);
}
public void ShowListDisplay()
{
editorDisplay.Visible = false;
listDisplay.Visible = true;
}
public void ShowEditorDisplay()
{
editorDisplay.Visible = true;
listDisplay.Visible = false;
}
public void DeleteNodePressed()
{
EmitSignal(SignalName.OnDeleteNode);
}
public static NodeDisplay Load(string content, Dictionary<ProgramNode, PackedScene> DSLNodes)
{
string nodeSanitized = content.Replace("\r\n", "").Trim();
if (nodeSanitized.Length <= 0) return null;
string nodeName = nodeSanitized.Split(",")[0].Replace("Name: ", "").ToLower();
PackedScene prefab = GetPrefab(nodeName, DSLNodes);
if (prefab == null) return null;
NodeDisplay result = prefab.Instantiate<NodeDisplay>();
switch (nodeName)
{
case "move":
result.node = new MoveNode();
result.LoadMove(nodeSanitized);
break;
case "harvest":
result.node = new HarvestNode();
result.LoadHarvest(nodeSanitized);
break;
case "explore":
result.node = new ExploreNode();
result.LoadExplore(nodeSanitized);
break;
case "craft":
result.node = new CraftNode();
result.LoadCraft(nodeSanitized);
break;
default:
result.QueueFree();
return null;
}
return result;
}
private static PackedScene GetPrefab(string nodeName, Dictionary<ProgramNode, PackedScene> DSLNodes)
{
foreach (ProgramNode programNode in DSLNodes.Keys)
{
if (programNode.DisplayText.ToLower() == nodeName)
{
return DSLNodes[programNode];
}
}
return null;
}
public void LoadHarvest(string content)
{
}
public void LoadMove(string content)
{
HBoxContainer valueContainer = GetNode<HBoxContainer>("./EditorDisplay/HBoxContainer/");
string[] parts = content.Split(",");
string positionValues = parts[1].Replace("Position: ", "").Replace("(", "").Replace(")", "");
int posX = int.Parse(positionValues.Split("|")[0]);
int posY = int.Parse(positionValues.Split("|")[1]);
int posZ = int.Parse(positionValues.Split("|")[2]);
valueContainer.GetNode<SpinBox>("./CoordinateX").Value = posX;
valueContainer.GetNode<SpinBox>("./CoordinateY").Value = posY;
valueContainer.GetNode<SpinBox>("./CoordinateZ").Value = posZ;
MoveNode moveNode = node as MoveNode;
if (moveNode != null)
{
moveNode.targetPosition = new Vector3I(posX, posY, posZ);
}
}
public void LoadExplore(string content)
{
}
public void LoadCraft(string content)
{
HBoxContainer valueContainer = GetNode<HBoxContainer>("./EditorDisplay/HBoxContainer/");
string[] parts = content.Split(",");
string itemString = parts[1].Replace("Item: ", "").Replace(" ", "");
if (itemString.ToLower() != "empty")
{
CraftNode craftNode = node as CraftNode;
if (craftNode != null)
{
craftNode.selectedItem = new Item { data = GameData.availableItems[itemString] };
}
}
string amountString = parts[2].Replace("Amount: ", "");
valueContainer.GetNode<SpinBox>("./Amount").Value = int.Parse(amountString);
}
}
+1
View File
@@ -0,0 +1 @@
uid://b6kxwmuhmruul