From febb797ae222e0476d13288b2fbaa87228e867ac Mon Sep 17 00:00:00 2001 From: ww-rm Date: Wed, 16 Apr 2025 21:37:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0GetResolutionBounds=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SpineViewer/Controls/SpinePreviewPanel.cs | 2 +- SpineViewer/Extensions/SFMLExtension.cs | 50 ++++++----------------- SpineViewer/Spine/SpineObject.cs | 17 ++++---- 3 files changed, 22 insertions(+), 47 deletions(-) diff --git a/SpineViewer/Controls/SpinePreviewPanel.cs b/SpineViewer/Controls/SpinePreviewPanel.cs index eea511e..b8c4abf 100644 --- a/SpineViewer/Controls/SpinePreviewPanel.cs +++ b/SpineViewer/Controls/SpinePreviewPanel.cs @@ -23,7 +23,7 @@ namespace SpineViewer.Controls renderWindow.SetActive(false); // 设置默认参数 - Resolution = Screen.PrimaryScreen.Bounds.Size; + Resolution = new(2048, 2048); Center = new(0, 0); FlipY = true; MaxFps = 30; diff --git a/SpineViewer/Extensions/SFMLExtension.cs b/SpineViewer/Extensions/SFMLExtension.cs index 260158d..4e8d3ad 100644 --- a/SpineViewer/Extensions/SFMLExtension.cs +++ b/SpineViewer/Extensions/SFMLExtension.cs @@ -29,45 +29,21 @@ namespace SpineViewer.Extensions } /// - /// 获取某个包围盒下合适的视图 + /// 获取适合指定画布参数下能够覆盖包围盒的视区包围盒 /// - public static SFML.Graphics.View GetView(this RectangleF bounds, Size resolution, Padding padding) - => bounds.GetView((uint)resolution.Width, (uint)resolution.Height, (uint)padding.Left, (uint)padding.Right, (uint)padding.Top, (uint)padding.Bottom); - - /// - /// 获取某个包围盒下合适的视图 - /// - public static SFML.Graphics.View GetView(this RectangleF bounds, uint width, uint height, Padding padding) - => bounds.GetView(width, height, (uint)padding.Left, (uint)padding.Right, (uint)padding.Top, (uint)padding.Bottom); - - /// - /// 获取某个包围盒下合适的视图 - /// - public static SFML.Graphics.View GetView(this RectangleF bounds, Size resolution, uint paddingL = 1, uint paddingR = 1, uint paddingT = 1, uint paddingB = 1) - => bounds.GetView((uint)resolution.Width, (uint)resolution.Height, paddingL, paddingR, paddingT, paddingB); - - /// - /// 获取某个包围盒下合适的视图 - /// - public static SFML.Graphics.View GetView(this RectangleF bounds, uint width, uint height, uint paddingL = 1, uint paddingR = 1, uint paddingT = 1, uint paddingB = 1) + public static RectangleF GetResolutionBounds(this RectangleF bounds, Size resolution, Padding padding, Padding margin) { - float sizeX = bounds.Width; - float sizeY = bounds.Height; - float innerW = width - paddingL - paddingR; - float innerH = height - paddingT - paddingB; - - float scale = 1; - if (sizeY / sizeX < innerH / innerW) - scale = sizeX / innerW; // 相同的 X, 视窗 Y 更大 - else - scale = sizeY / innerH; // 相同的 Y, 视窗 X 更大 - - var x = bounds.X + bounds.Width / 2 + (paddingL - (float)paddingR) * scale; - var y = bounds.Y + bounds.Height / 2 + (paddingT - (float)paddingB) * scale; - var viewX = width * scale; - var viewY = height * scale; - - return new(new(x, y), new(viewX, -viewY)); + float sizeW = bounds.Width; + float sizeH = bounds.Height; + float innerW = resolution.Width - padding.Horizontal; + float innerH = resolution.Height - padding.Vertical; + float scale = Math.Max(sizeW / innerW, sizeH / innerH); // 取两方向上较大的缩放比, 以此让画布可以覆盖内容 + return new( + bounds.X + (padding.Left + margin.Left - padding.Right - margin.Right) * scale, + bounds.Y + (padding.Top + margin.Top - padding.Bottom - margin.Bottom) * scale, + (resolution.Width + margin.Horizontal) * scale, + (resolution.Height + margin.Vertical) * scale + ); } } } diff --git a/SpineViewer/Spine/SpineObject.cs b/SpineViewer/Spine/SpineObject.cs index 73b5248..7ff6fde 100644 --- a/SpineViewer/Spine/SpineObject.cs +++ b/SpineViewer/Spine/SpineObject.cs @@ -20,14 +20,9 @@ namespace SpineViewer.Spine protected const string EMPTY_ANIMATION = ""; /// - /// 预览图宽 + /// 预览图像素大小 /// - protected const uint PREVIEW_WIDTH = 256; - - /// - /// 预览图高 - /// - protected const uint PREVIEW_HEIGHT = 256; + protected static readonly Size PreviewResolution = new(256, 256); /// /// 创建特定版本的 Spine @@ -81,8 +76,12 @@ namespace SpineViewer.Spine // 虽然两边不会同时调用 Draw, 但是死锁似乎和 Draw 函数有关 // 除此之外, 似乎还和 tex 的 Dispose 有关 // 如果不对 tex 进行 Dispose, 那么不管是否 Draw 都正常不会死锁 - var tex = new SFML.Graphics.RenderTexture(PREVIEW_WIDTH, PREVIEW_HEIGHT); - using var view = getCurrentBounds().GetView(PREVIEW_WIDTH, PREVIEW_HEIGHT); + var tex = new SFML.Graphics.RenderTexture((uint)PreviewResolution.Width, (uint)PreviewResolution.Height); + var bounds = getCurrentBounds().GetResolutionBounds(PreviewResolution, new(0), new(0)); + using var view = new SFML.Graphics.View( + new(bounds.X + bounds.Width / 2, bounds.Y + bounds.Height / 2), + new(bounds.Width, -bounds.Height) + ); tex.SetView(view); tex.Clear(SFML.Graphics.Color.Transparent); tex.Draw(this);