Files
RuinAdventurer/Scripts/UI/DSL/NodeDisplay.cs
T

83 lines
1.7 KiB
C#

using System.Collections.Generic;
using Godot;
public partial class NodeDisplay : GraphNode
{
public ProgramNode node;
private bool isHighlighted = false;
public override void _Ready()
{
if (node == null)
{
node = CreateProgramNode();
}
if (node == null) return;
SetupDisplay();
}
public static NodeDisplay Load(
string nodeName,
string content,
Dictionary<ProgramNode, PackedScene> DSLNodes
)
{
string nodeSanitized = content.Replace("\r\n", "").Trim();
if (nodeSanitized.Length <= 0) return null;
string normalizedNodeName = nodeName.Trim().ToLower();
PackedScene prefab = GetPrefab(normalizedNodeName, DSLNodes);
if (prefab == null) return null;
NodeDisplay result = prefab.Instantiate<NodeDisplay>();
result.node = result.CreateProgramNode();
result.LoadContent(result, nodeSanitized);
return result;
}
protected virtual ProgramNode CreateProgramNode()
{
return null;
}
protected virtual void LoadContent(NodeDisplay display, string content) { }
public virtual void SetupDisplay() { }
public virtual void ReadParameters() { }
public void SetHighlighted(bool highlighted)
{
if (isHighlighted == highlighted) return;
isHighlighted = highlighted;
SelfModulate = highlighted ? UIStyle.GetWarningColor() : Colors.White;
}
public HBoxContainer GetValueContainer()
{
return GetNode<HBoxContainer>("./Values");
}
protected HBoxContainer GetValueContainer(NodeDisplay display)
{
return display.GetValueContainer();
}
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;
}
}