// Copyright © Gamesmiths Guild.
using System;
using Gamesmiths.Forge.Core;
using Gamesmiths.Forge.Statescript;
using Gamesmiths.Forge.Statescript.Properties;
using Godot;
using Godot.Collections;
using ForgeNode = Gamesmiths.Forge.Statescript.Node;
namespace Gamesmiths.Forge.Godot.Resources.Statescript.Resolvers;
///
/// Resolver resource that holds a constant (inline) value for a node property.
///
[Tool]
[GlobalClass]
public partial class VariantResolverResource : StatescriptResolverResource
{
///
public override string ResolverTypeId => "Variant";
///
/// Gets or sets the constant value. Used when is .
///
[Export]
public Variant Value { get; set; }
///
/// Gets or sets the type interpretation for the value.
///
[Export]
public StatescriptVariableType ValueType { get; set; } = StatescriptVariableType.Int;
///
/// Gets or sets a value indicating whether this resolver holds an array of values.
///
[Export]
public bool IsArray { get; set; }
///
/// Gets or sets the array values. Used when is .
///
[Export]
public Array ArrayValues { get; set; } = [];
///
/// Gets or sets a value indicating whether the array section is expanded in the editor.
///
[Export]
public bool IsArrayExpanded { get; set; }
///
public override void BindInput(Graph graph, ForgeNode runtimeNode, string nodeId, byte index)
{
var propertyName = new StringKey($"__const_{nodeId}_{index}");
if (IsArray)
{
var values = new Variant128[ArrayValues.Count];
for (var i = 0; i < ArrayValues.Count; i++)
{
values[i] = StatescriptVariableTypeConverter.GodotVariantToForge(ArrayValues[i], ValueType);
}
Type clrType = StatescriptVariableTypeConverter.ToSystemType(ValueType);
graph.VariableDefinitions.ArrayVariableDefinitions.Add(
new ArrayVariableDefinition(propertyName, values, clrType));
}
else
{
Variant128 value = StatescriptVariableTypeConverter.GodotVariantToForge(Value, ValueType);
Type clrType = StatescriptVariableTypeConverter.ToSystemType(ValueType);
graph.VariableDefinitions.PropertyDefinitions.Add(
new PropertyDefinition(propertyName, new VariantResolver(value, clrType)));
}
runtimeNode.BindInput(index, propertyName);
}
///
public override IPropertyResolver BuildResolver(Graph graph)
{
Variant128 value = StatescriptVariableTypeConverter.GodotVariantToForge(Value, ValueType);
Type clrType = StatescriptVariableTypeConverter.ToSystemType(ValueType);
return new VariantResolver(value, clrType);
}
}