newlon/scripts/gui/choose_your_seeds/Previewport.cs
2025-07-26 05:35:53 +05:00

66 lines
1.4 KiB
C#

using Godot;
using Newlon;
using Newlon.Components;
using Newlon.Components.GUI.Seedpackets;
public partial class Previewport : SubViewport
{
private Node current_display;
private Texture2D start_Field;
[Export] private Label title;
[Export] private RichTextLabel description;
[Export] private Sprite2D _frameField;
public override void _Ready()
{
GetParent().GetViewport().GuiFocusChanged += OnFocusChanged;
start_Field = _frameField.Texture;
}
public void OnFocusChanged(Control node)
{
if (GetParent<Control>().IsVisibleInTree() == false) return;
if (node is Seedpacket packet)
{
ChangeDisplay(packet.GetResource());
}
}
private void ChangeDisplay(EntityResource resource)
{
// Expand with updates
if (current_display != null)
{
current_display.QueueFree();
}
if (resource.CustomFrame != null && resource.CustomFrame.almanachField != null)
{
_frameField.Texture = resource.CustomFrame.almanachField;
}
else
_frameField.Texture = start_Field;
current_display = resource.Scene.Instantiate();
title.Text = Tr(resource.NameKey);
AddChild(current_display);
if (current_display is Entity entity)
entity.DisableBrain();
var rwd = Tr("rwd_" + resource.NameKey);
if (rwd == "rwd_" + resource.NameKey)
{
rwd = "";
}
else
{
rwd += "\n";
}
description.Text = rwd + DescriptionParser.GetParsed(resource,current_display);
}
}