using System.Linq;
using Cthangover.Tools.Services;
using Godot;
namespace Cthangover.Core.UI.Tool.ScenarioEditor
{
/// Row of background thumbnails referenced by the scenario in the active text tab.
public partial class ScenarioThumbnailPanel : ScrollContainer
{
private HBoxContainer _thumbContainer;
///
/// Container lookup is lazy: the tscn builder runs the constructor
/// before the child nodes exist.
///
private HBoxContainer Thumbs => _thumbContainer ??= GetNode("ThumbContainer");
/// Loads the background thumbnails referenced by the given scenario text (missing ones render as placeholders).
public void Load(string text)
{
Clear();
if (string.IsNullOrEmpty(text))
return;
var refs = ScenarioFileService.ExtractReferences(text);
if (refs.BackgroundRefs.Count == 0)
{
Thumbs.AddChild(new Label
{
Text = TranslationServer.Translate("tools/scenario_editor/no_backgrounds")
});
return;
}
foreach (var id in refs.BackgroundRefs.Distinct())
{
var tex = ModResourceService.LoadBackgroundTexture(id);
if (tex != null)
{
Thumbs.AddChild(new TextureRect
{
Texture = tex,
ExpandMode = TextureRect.ExpandModeEnum.IgnoreSize,
StretchMode = TextureRect.StretchModeEnum.KeepAspectCentered,
CustomMinimumSize = new Vector2(120, 90),
TooltipText = id
});
}
else
{
Thumbs.AddChild(new Label
{
Text = $"[? {id}]",
TooltipText = string.Format(TranslationServer.Translate("tools/scenario_editor/bg_missing"), id)
});
}
}
}
/// Removes every thumbnail from the row.
public new void Clear()
{
foreach (var c in Thumbs.GetChildren())
c.QueueFree();
}
}
}