整理代码结构

This commit is contained in:
ww-rm
2025-03-29 21:15:34 +08:00
parent 5e3bd972e5
commit aa8321d13c
9 changed files with 88 additions and 95 deletions

View File

@@ -47,7 +47,7 @@ namespace SpineViewer.Spine.Implementations.Spine
// 2.1.x 不支持剪裁 // 2.1.x 不支持剪裁
//private SkeletonClipping clipping = new(); //private SkeletonClipping clipping = new();
public Spine21(string skelPath, string? atlasPath = null) : base(skelPath, atlasPath) public Spine21(string skelPath, string atlasPath) : base(skelPath, atlasPath)
{ {
atlas = new Atlas(AtlasPath, textureLoader); atlas = new Atlas(AtlasPath, textureLoader);
try try
@@ -76,12 +76,12 @@ namespace SpineViewer.Spine.Implementations.Spine
foreach (var skin in skeletonData.Skins) foreach (var skin in skeletonData.Skins)
skinNames.Add(skin.Name); skinNames.Add(skin.Name);
animationStateData = new AnimationStateData(skeletonData);
skeleton = new Skeleton(skeletonData);
animationState = new AnimationState(animationStateData);
foreach (var anime in skeletonData.Animations) foreach (var anime in skeletonData.Animations)
animationNames.Add(anime.Name); animationNames.Add(anime.Name);
skeleton = new Skeleton(skeletonData);
animationStateData = new AnimationStateData(skeletonData);
animationState = new AnimationState(animationStateData);
} }
protected override void Dispose(bool disposing) protected override void Dispose(bool disposing)

View File

@@ -46,7 +46,7 @@ namespace SpineViewer.Spine.Implementations.Spine
private SkeletonClipping clipping = new(); private SkeletonClipping clipping = new();
public Spine36(string skelPath, string? atlasPath = null) : base(skelPath, atlasPath) public Spine36(string skelPath, string atlasPath) : base(skelPath, atlasPath)
{ {
atlas = new Atlas(AtlasPath, textureLoader); atlas = new Atlas(AtlasPath, textureLoader);
try try
@@ -75,12 +75,12 @@ namespace SpineViewer.Spine.Implementations.Spine
foreach (var skin in skeletonData.Skins) foreach (var skin in skeletonData.Skins)
skinNames.Add(skin.Name); skinNames.Add(skin.Name);
animationStateData = new AnimationStateData(skeletonData);
skeleton = new Skeleton(skeletonData);
animationState = new AnimationState(animationStateData);
foreach (var anime in skeletonData.Animations) foreach (var anime in skeletonData.Animations)
animationNames.Add(anime.Name); animationNames.Add(anime.Name);
skeleton = new Skeleton(skeletonData);
animationStateData = new AnimationStateData(skeletonData);
animationState = new AnimationState(animationStateData);
} }
protected override void Dispose(bool disposing) protected override void Dispose(bool disposing)

View File

@@ -44,7 +44,7 @@ namespace SpineViewer.Spine.Implementations.Spine
private SkeletonClipping clipping = new(); private SkeletonClipping clipping = new();
public Spine37(string skelPath, string? atlasPath = null) : base(skelPath, atlasPath) public Spine37(string skelPath, string atlasPath) : base(skelPath, atlasPath)
{ {
atlas = new Atlas(AtlasPath, textureLoader); atlas = new Atlas(AtlasPath, textureLoader);
try try
@@ -73,12 +73,12 @@ namespace SpineViewer.Spine.Implementations.Spine
foreach (var skin in skeletonData.Skins) foreach (var skin in skeletonData.Skins)
skinNames.Add(skin.Name); skinNames.Add(skin.Name);
animationStateData = new AnimationStateData(skeletonData);
skeleton = new Skeleton(skeletonData);
animationState = new AnimationState(animationStateData);
foreach (var anime in skeletonData.Animations) foreach (var anime in skeletonData.Animations)
animationNames.Add(anime.Name); animationNames.Add(anime.Name);
skeleton = new Skeleton(skeletonData);
animationStateData = new AnimationStateData(skeletonData);
animationState = new AnimationState(animationStateData);
} }
protected override void Dispose(bool disposing) protected override void Dispose(bool disposing)

View File

@@ -50,7 +50,7 @@ namespace SpineViewer.Spine.Implementations.Spine
private SkeletonClipping clipping = new(); private SkeletonClipping clipping = new();
public Spine38(string skelPath, string? atlasPath = null) : base(skelPath, atlasPath) public Spine38(string skelPath, string atlasPath) : base(skelPath, atlasPath)
{ {
atlas = new Atlas(AtlasPath, textureLoader); atlas = new Atlas(AtlasPath, textureLoader);
try try
@@ -79,12 +79,12 @@ namespace SpineViewer.Spine.Implementations.Spine
foreach (var skin in skeletonData.Skins) foreach (var skin in skeletonData.Skins)
skinNames.Add(skin.Name); skinNames.Add(skin.Name);
animationStateData = new AnimationStateData(skeletonData);
skeleton = new Skeleton(skeletonData);
animationState = new AnimationState(animationStateData);
foreach (var anime in skeletonData.Animations) foreach (var anime in skeletonData.Animations)
animationNames.Add(anime.Name); animationNames.Add(anime.Name);
skeleton = new Skeleton(skeletonData);
animationStateData = new AnimationStateData(skeletonData);
animationState = new AnimationState(animationStateData);
} }
protected override void Dispose(bool disposing) protected override void Dispose(bool disposing)

View File

@@ -46,7 +46,7 @@ namespace SpineViewer.Spine.Implementations.Spine
private SkeletonClipping clipping = new(); private SkeletonClipping clipping = new();
public Spine40(string skelPath, string? atlasPath = null) : base(skelPath, atlasPath) public Spine40(string skelPath, string atlasPath) : base(skelPath, atlasPath)
{ {
atlas = new Atlas(AtlasPath, textureLoader); atlas = new Atlas(AtlasPath, textureLoader);
try try
@@ -75,12 +75,12 @@ namespace SpineViewer.Spine.Implementations.Spine
foreach (var skin in skeletonData.Skins) foreach (var skin in skeletonData.Skins)
skinNames.Add(skin.Name); skinNames.Add(skin.Name);
animationStateData = new AnimationStateData(skeletonData);
skeleton = new Skeleton(skeletonData);
animationState = new AnimationState(animationStateData);
foreach (var anime in skeletonData.Animations) foreach (var anime in skeletonData.Animations)
animationNames.Add(anime.Name); animationNames.Add(anime.Name);
skeleton = new Skeleton(skeletonData);
animationStateData = new AnimationStateData(skeletonData);
animationState = new AnimationState(animationStateData);
} }
protected override void Dispose(bool disposing) protected override void Dispose(bool disposing)

View File

@@ -46,7 +46,7 @@ namespace SpineViewer.Spine.Implementations.Spine
private SkeletonClipping clipping = new(); private SkeletonClipping clipping = new();
public Spine41(string skelPath, string? atlasPath = null) : base(skelPath, atlasPath) public Spine41(string skelPath, string atlasPath) : base(skelPath, atlasPath)
{ {
atlas = new Atlas(AtlasPath, textureLoader); atlas = new Atlas(AtlasPath, textureLoader);
try try
@@ -75,12 +75,12 @@ namespace SpineViewer.Spine.Implementations.Spine
foreach (var skin in skeletonData.Skins) foreach (var skin in skeletonData.Skins)
skinNames.Add(skin.Name); skinNames.Add(skin.Name);
animationStateData = new AnimationStateData(skeletonData);
skeleton = new Skeleton(skeletonData);
animationState = new AnimationState(animationStateData);
foreach (var anime in skeletonData.Animations) foreach (var anime in skeletonData.Animations)
animationNames.Add(anime.Name); animationNames.Add(anime.Name);
skeleton = new Skeleton(skeletonData);
animationStateData = new AnimationStateData(skeletonData);
animationState = new AnimationState(animationStateData);
} }
protected override void Dispose(bool disposing) protected override void Dispose(bool disposing)

View File

@@ -46,7 +46,7 @@ namespace SpineViewer.Spine.Implementations.Spine
private SkeletonClipping clipping = new(); private SkeletonClipping clipping = new();
public Spine42(string skelPath, string? atlasPath = null) : base(skelPath, atlasPath) public Spine42(string skelPath, string atlasPath) : base(skelPath, atlasPath)
{ {
atlas = new Atlas(AtlasPath, textureLoader); atlas = new Atlas(AtlasPath, textureLoader);
try try
@@ -75,12 +75,12 @@ namespace SpineViewer.Spine.Implementations.Spine
foreach (var skin in skeletonData.Skins) foreach (var skin in skeletonData.Skins)
skinNames.Add(skin.Name); skinNames.Add(skin.Name);
animationStateData = new AnimationStateData(skeletonData);
skeleton = new Skeleton(skeletonData);
animationState = new AnimationState(animationStateData);
foreach (var anime in skeletonData.Animations) foreach (var anime in skeletonData.Animations)
animationNames.Add(anime.Name); animationNames.Add(anime.Name);
skeleton = new Skeleton(skeletonData);
animationStateData = new AnimationStateData(skeletonData);
animationState = new AnimationState(animationStateData);
} }
protected override void Dispose(bool disposing) protected override void Dispose(bool disposing)

View File

@@ -26,23 +26,22 @@ namespace SpineViewer.Spine
/// <summary> /// <summary>
/// 空动画标记 /// 空动画标记
/// </summary> /// </summary>
public const string EMPTY_ANIMATION = "<Empty>"; protected const string EMPTY_ANIMATION = "<Empty>";
/// <summary> /// <summary>
/// 预览图宽 /// 预览图宽
/// </summary> /// </summary>
public const uint PREVIEW_WIDTH = 256; protected const uint PREVIEW_WIDTH = 256;
/// <summary> /// <summary>
/// 预览图高 /// 预览图高
/// </summary> /// </summary>
public const uint PREVIEW_HEIGHT = 256; protected const uint PREVIEW_HEIGHT = 256;
/// <summary> /// <summary>
/// 缩放最小值 /// 缩放最小值
/// </summary> /// </summary>
public const float SCALE_MIN = 0.001f; protected const float SCALE_MIN = 0.001f;
/// <summary> /// <summary>
/// 创建特定版本的 Spine /// 创建特定版本的 Spine
@@ -50,10 +49,31 @@ namespace SpineViewer.Spine
public static Spine New(SpineVersion version, string skelPath, string? atlasPath = null) public static Spine New(SpineVersion version, string skelPath, string? atlasPath = null)
{ {
if (version == SpineVersion.Auto) version = SpineHelper.GetVersion(skelPath); if (version == SpineVersion.Auto) version = SpineHelper.GetVersion(skelPath);
var spine = New(version, [skelPath, atlasPath]); atlasPath ??= Path.ChangeExtension(skelPath, ".atlas");
return New(version, [skelPath, atlasPath]).PostInit();
}
// 统一初始化 /// <summary>
spine.initBounds = spine.Bounds; /// 构造函数
/// </summary>
public Spine(string skelPath, string atlasPath)
{
Version = GetType().GetCustomAttribute<SpineImplementationAttribute>().ImplementationKey;
AssetsDir = Directory.GetParent(skelPath).FullName;
SkelPath = Path.GetFullPath(skelPath);
AtlasPath = Path.GetFullPath(atlasPath);
Name = Path.GetFileNameWithoutExtension(skelPath);
}
/// <summary>
/// 构造函数之后的初始化工作
/// </summary>
private Spine PostInit()
{
SkinNames = skinNames.AsReadOnly();
AnimationNames = animationNames.AsReadOnly();
InitBounds = Bounds;
// XXX: tex 没办法在这里主动 Dispose // XXX: tex 没办法在这里主动 Dispose
// 批量添加在获取预览图的时候极大概率会和预览线程死锁 // 批量添加在获取预览图的时候极大概率会和预览线程死锁
@@ -61,9 +81,9 @@ namespace SpineViewer.Spine
// 除此之外, 似乎还和 tex 的 Dispose 有关 // 除此之外, 似乎还和 tex 的 Dispose 有关
// 如果不对 tex 进行 Dispose, 那么不管是否 Draw 都正常不会死锁 // 如果不对 tex 进行 Dispose, 那么不管是否 Draw 都正常不会死锁
var tex = new SFML.Graphics.RenderTexture(PREVIEW_WIDTH, PREVIEW_HEIGHT); var tex = new SFML.Graphics.RenderTexture(PREVIEW_WIDTH, PREVIEW_HEIGHT);
tex.SetView(spine.InitBounds.GetView(PREVIEW_WIDTH, PREVIEW_HEIGHT)); tex.SetView(InitBounds.GetView(PREVIEW_WIDTH, PREVIEW_HEIGHT));
tex.Clear(SFML.Graphics.Color.Transparent); tex.Clear(SFML.Graphics.Color.Transparent);
tex.Draw(spine); tex.Draw(this);
tex.Display(); tex.Display();
using (var img = tex.Texture.CopyToImage()) using (var img = tex.Texture.CopyToImage())
@@ -73,50 +93,27 @@ namespace SpineViewer.Spine
// 必须重复构造一个副本才能摆脱对流的依赖, 否则之后使用会报错 // 必须重复构造一个副本才能摆脱对流的依赖, 否则之后使用会报错
using var stream = new MemoryStream(imgBuffer); using var stream = new MemoryStream(imgBuffer);
using var bitmap = new Bitmap(stream); using var bitmap = new Bitmap(stream);
spine.preview = new Bitmap(bitmap); Preview = new Bitmap(bitmap);
} }
} }
// 取最后一个作为初始, 尽可能去显示非默认的内容 // 取最后一个作为初始, 尽可能去显示非默认的内容
spine.Skin = spine.SkinNames.Last(); Skin = SkinNames.Last();
spine.Track0Animation = spine.AnimationNames.Last(); Track0Animation = AnimationNames.Last();
return spine; return this;
}
/// <summary>
/// 构造函数
/// </summary>
public Spine(string skelPath, string? atlasPath = null)
{
// 获取子类类型
var type = GetType();
var attr = type.GetCustomAttribute<SpineImplementationAttribute>();
if (attr is null)
{
throw new InvalidOperationException($"Class {type.Name} has no SpineImplementationAttribute");
}
atlasPath ??= Path.ChangeExtension(skelPath, ".atlas");
// 设置 Version
Version = attr.ImplementationKey;
AssetsDir = Directory.GetParent(skelPath).FullName;
SkelPath = Path.GetFullPath(skelPath);
AtlasPath = Path.GetFullPath(atlasPath);
Name = Path.GetFileNameWithoutExtension(skelPath);
} }
~Spine() { Dispose(false); } ~Spine() { Dispose(false); }
public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); }
protected virtual void Dispose(bool disposing) { preview?.Dispose(); } protected virtual void Dispose(bool disposing) { Preview?.Dispose(); }
#region | [0] #region | [0]
/// <summary> /// <summary>
/// 获取所属版本 /// 获取所属版本
/// </summary> /// </summary>
[TypeConverter(typeof(VersionConverter))] [TypeConverter(typeof(SpineVersionConverter))]
[Category("[0] "), DisplayName("")] [Category("[0] "), DisplayName("")]
public SpineVersion Version { get; } public SpineVersion Version { get; }
@@ -202,8 +199,7 @@ namespace SpineViewer.Spine
/// <summary> /// <summary>
/// 包含的所有皮肤名称 /// 包含的所有皮肤名称
/// </summary> /// </summary>
[Browsable(false)] public ReadOnlyCollection<string> SkinNames { get; private set; }
public ReadOnlyCollection<string> SkinNames { get => skinNames.AsReadOnly(); }
protected List<string> skinNames = []; protected List<string> skinNames = [];
/// <summary> /// <summary>
@@ -216,8 +212,7 @@ namespace SpineViewer.Spine
/// <summary> /// <summary>
/// 包含的所有动画名称 /// 包含的所有动画名称
/// </summary> /// </summary>
[Browsable(false)] public ReadOnlyCollection<string> AnimationNames { get; private set; }
public ReadOnlyCollection<string> AnimationNames { get => animationNames.AsReadOnly(); }
protected List<string> animationNames = [EMPTY_ANIMATION]; protected List<string> animationNames = [EMPTY_ANIMATION];
/// <summary> /// <summary>
@@ -243,16 +238,6 @@ namespace SpineViewer.Spine
[Browsable(false)] [Browsable(false)]
public bool IsDebug { get; set; } = false; public bool IsDebug { get; set; } = false;
/// <summary>
/// 包围盒颜色
/// </summary>
protected static readonly SFML.Graphics.Color BoundsColor = new(120, 200, 0);
/// <summary>
/// 包围盒顶点数组
/// </summary>
protected readonly SFML.Graphics.VertexArray boundsVertices = new(SFML.Graphics.PrimitiveType.LineStrip, 5);
/// <summary> /// <summary>
/// 显示纹理 /// 显示纹理
/// </summary> /// </summary>
@@ -268,7 +253,7 @@ namespace SpineViewer.Spine
/// <summary> /// <summary>
/// 显示骨骼 /// 显示骨骼
/// </summary> /// </summary>
[Category("[4] "), DisplayName("")] [Category("[4] "), DisplayName("")]
public bool DebugBones { get; set; } = false; public bool DebugBones { get; set; } = false;
#endregion #endregion
@@ -294,15 +279,13 @@ namespace SpineViewer.Spine
/// 初始状态下的骨骼包围盒 /// 初始状态下的骨骼包围盒
/// </summary> /// </summary>
[Browsable(false)] [Browsable(false)]
public RectangleF InitBounds { get => initBounds; } public RectangleF InitBounds { get; private set; }
private RectangleF initBounds;
/// <summary> /// <summary>
/// 骨骼预览图 /// 骨骼预览图
/// </summary> /// </summary>
[Browsable(false)] [Browsable(false)]
public Image Preview { get => preview; } public Image Preview { get; private set; }
private Image preview;
/// <summary> /// <summary>
/// 获取动画时长, 如果动画不存在则返回 0 /// 获取动画时长, 如果动画不存在则返回 0
@@ -326,6 +309,16 @@ namespace SpineViewer.Spine
/// </summary> /// </summary>
protected readonly SFML.Graphics.VertexArray vertexArray = new(SFML.Graphics.PrimitiveType.Triangles); protected readonly SFML.Graphics.VertexArray vertexArray = new(SFML.Graphics.PrimitiveType.Triangles);
/// <summary>
/// 包围盒颜色
/// </summary>
protected static readonly SFML.Graphics.Color BoundsColor = new(120, 200, 0);
/// <summary>
/// 包围盒顶点数组
/// </summary>
protected readonly SFML.Graphics.VertexArray boundsVertices = new(SFML.Graphics.PrimitiveType.LineStrip, 5);
/// <summary> /// <summary>
/// SFML.Graphics.Drawable 接口实现 /// SFML.Graphics.Drawable 接口实现
/// </summary> /// </summary>

View File

@@ -9,9 +9,9 @@ using System.Threading.Tasks;
namespace SpineViewer.Spine namespace SpineViewer.Spine
{ {
public class VersionConverter : EnumConverter public class SpineVersionConverter : EnumConverter
{ {
public VersionConverter() : base(typeof(SpineVersion)) { } public SpineVersionConverter() : base(typeof(SpineVersion)) { }
public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type? destinationType) public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type? destinationType)
{ {