using Godot;
using Cthangover.Live2D.Cubism.Framework;
namespace Cthangover.Live2D.Cubism.Services
{
///
/// Configuration for .
/// When is set, defaults to
/// to disable automatic layout, giving manual
/// coordinates full control. Otherwise is used.
///
public class Live2DModelConfig
{
public string ModelPath;
public string ModId;
public string NodeName;
public Vector2? Position;
public Vector2? Scale;
public float AnchorX = 0.5f;
public float AnchorY = 0.5f;
public FitMode ScaleFit = FitMode.FitAuto;
}
///
/// High-level factory for creating Live2D model nodes and attaching them
/// to the scene tree. Encapsulates the boilerplate of instantiating
/// , adding to a parent, and calling
/// .
///
public static class Live2DService
{
///
/// Creates a , adds it to ,
/// and loads the model from the specified mod.
///
/// Parent node to attach the model to.
/// Path to the .model3.json file, relative to the mod root.
/// Registered mod ID containing the model assets.
/// Godot node name for the model instance.
/// The created and loaded model node.
public static CubismModelNode CreateModel(
Node parent,
string modelPath,
string nodeName,
string modId = null)
{
return CreateModel(parent, new Live2DModelConfig
{
ModelPath = modelPath,
ModId = modId,
NodeName = nodeName
});
}
///
/// Creates a using full configuration.
/// When is provided,
/// is forced to
/// — manual coordinates take priority over
/// the automatic .
///
public static CubismModelNode CreateModel(Node parent, Live2DModelConfig config)
{
var fitMode = config.Position.HasValue ? FitMode.None : config.ScaleFit;
var model = new CubismModelNode
{
Name = config.NodeName,
AnchorX = config.AnchorX,
AnchorY = config.AnchorY,
ScaleFit = fitMode
};
parent.AddChild(model);
if (config.Position.HasValue)
model.Position = config.Position.Value;
if (config.Scale.HasValue)
model.Scale = config.Scale.Value;
model.LoadModelFromMod(config.ModelPath, config.ModId);
return model;
}
}
}