diff --git a/Spine/Interfaces/SpineExtension.cs b/Spine/Interfaces/SpineExtension.cs
index b6f3c0d..e47a96e 100644
--- a/Spine/Interfaces/SpineExtension.cs
+++ b/Spine/Interfaces/SpineExtension.cs
@@ -1,4 +1,5 @@
-using SkiaSharp;
+using NLog;
+using SkiaSharp;
using Spine.Interfaces.Attachments;
using System;
using System.Collections.Generic;
@@ -8,8 +9,25 @@ using System.Threading.Tasks;
namespace Spine.Interfaces
{
+ ///
+ /// 命中测试等级枚举值
+ ///
+ public enum HitTestLevel { Bounds, Meshes, Pixels }
+
public static class SpineExtension
{
+ private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
+
+ ///
+ /// 命中检测精确度等级
+ ///
+ public static HitTestLevel HitTestLevel { get; set; } = HitTestLevel.Bounds;
+
+ ///
+ /// 命中测试时输出命中的插槽名称
+ ///
+ public static bool LogHitSlots { get; set; }
+
///
/// 获取当前状态包围盒
///
@@ -105,12 +123,12 @@ namespace Spine.Interfaces
///
/// 是否精确命中检测, 否则仅使用包围盒进行命中检测
/// 调用方管理的缓存表
- public static bool HitTest(this ISlot self, float x, float y, bool precise = false, Dictionary cache = null)
+ public static bool HitTest(this ISlot self, float x, float y, Dictionary cache = null)
{
if (self.A <= 0 || !self.Bone.Active || self.Disabled)
return false;
- if (!precise)
+ if (HitTestLevel == HitTestLevel.Bounds)
{
self.GetBounds(out var bx, out var by, out var bw, out var bh);
return x >= bx && x <= (bx + bw) && y >= by && y <= (by + bh);
@@ -140,22 +158,7 @@ namespace Spine.Interfaces
return false;
}
- SFML.Graphics.Image img = null;
- if (cache is not null)
- {
- if (!cache.TryGetValue(tex, out img))
- {
- img = cache[tex] = tex.CopyToImage();
- }
- }
- else
- {
- img = tex.CopyToImage();
- }
-
- bool hit = false;
var trianglesLength = triangles.Length;
- var texSize = img.Size;
for (int i = 0; i + 2 < trianglesLength; i += 3)
{
var idx0 = triangles[i] << 1;
@@ -173,6 +176,9 @@ namespace Spine.Interfaces
// 判断是否全部同号 (或为 0, 点在边上)
if ((c0 >= 0 && c1 >= 0 && c2 >= 0) || (c0 <= 0 && c1 <= 0 && c2 <= 0))
{
+ if (HitTestLevel == HitTestLevel.Meshes)
+ return true;
+
float u0 = uvs[idx0], v0 = uvs[idx0 + 1];
float u1 = uvs[idx1], v1 = uvs[idx1 + 1];
float u2 = uvs[idx2], v2 = uvs[idx2 + 1];
@@ -183,43 +189,61 @@ namespace Spine.Interfaces
float u = u0 * w0 + u1 * w1 + u2 * w2;
float v = v0 * w0 + v1 * w1 + v2 * w2;
+ SFML.Graphics.Image img = null;
+ if (cache is not null)
+ {
+ if (!cache.TryGetValue(tex, out img))
+ {
+ img = cache[tex] = tex.CopyToImage();
+ }
+ }
+ else
+ {
+ img = tex.CopyToImage();
+ }
+
+ var texSize = img.Size;
var pixel = img.GetPixel((uint)(u * texSize.X), (uint)(v * texSize.Y));
- hit = pixel.A > 0;
- break;
+ bool hit = pixel.A > 0;
+
+ // 无缓存需要立即释放资源
+ if (cache is null)
+ {
+ img.Dispose();
+ }
+
+ return hit;
}
}
-
- // 无缓存需要立即释放资源
- if (cache is null)
- {
- img.Dispose();
- }
-
- return hit;
+ return false;
}
}
///
/// 逐插槽的命中测试, 命中后会提前返回结果中止计算
///
- public static bool HitTest(this ISkeleton self, float x, float y, bool precise = false)
+ public static bool HitTest(this ISkeleton self, float x, float y)
{
var cache = new Dictionary();
- bool hit = self.IterDrawOrder().Any(st => st.HitTest(x, y, precise, cache));
+ bool hit = false;
+ List slotNames = [];
+ foreach (var st in self.IterDrawOrder().Reverse())
+ {
+ if (st.HitTest(x, y, cache))
+ {
+ hit = true;
+ if (!LogHitSlots)
+ break;
+ slotNames.Add(st.Name);
+ }
+ }
foreach (var img in cache.Values) img.Dispose();
- return hit;
- }
- ///
- /// 逐插槽的命中测试, 会完整计算所有插槽的命中情况并按顶层至底层的顺序返回命中的插槽
- /// /// 是否精确命中检测, 否则仅使用每个插槽的包围盒进行命中检测
- ///
- public static ISlot[] HitTestFull(this ISkeleton self, float x, float y, bool precise = false)
- {
- var cache = new Dictionary();
- var hitSlots = self.IterDrawOrder().Where(st => st.HitTest(x, y, precise, cache)).Reverse().ToArray();
- foreach (var img in cache.Values) img.Dispose();
- return hitSlots;
+ if (LogHitSlots && slotNames.Count > 0)
+ {
+ _logger.Debug("Hit ({0}): [{1}]", self.Name, string.Join(", ", slotNames));
+ }
+ return hit;
}
///
diff --git a/SpineViewer/Models/PreferenceModel.cs b/SpineViewer/Models/PreferenceModel.cs
index 61beced..a4336b2 100644
--- a/SpineViewer/Models/PreferenceModel.cs
+++ b/SpineViewer/Models/PreferenceModel.cs
@@ -1,5 +1,6 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
+using Spine.Interfaces;
using SpineViewer.Services;
using System;
using System.Collections.Generic;
@@ -89,7 +90,7 @@ namespace SpineViewer.Models
private bool _renderSelectedOnly;
[ObservableProperty]
- private bool _usePreciseHitTest;
+ private HitTestLevel _hitTestLevel;
[ObservableProperty]
private bool _logHitSlots;
diff --git a/SpineViewer/Models/SpineObjectModel.cs b/SpineViewer/Models/SpineObjectModel.cs
index ddb920f..e3a17b7 100644
--- a/SpineViewer/Models/SpineObjectModel.cs
+++ b/SpineViewer/Models/SpineObjectModel.cs
@@ -429,19 +429,11 @@ namespace SpineViewer.Models
}
///
- /// 命中检测, 可选是否使用精确检测, 会有性能损失
+ /// 命中检测
///
- public bool HitTest(float x, float y, bool precise = false)
+ public bool HitTest(float x, float y)
{
- lock (_lock) return _spineObject.Skeleton.HitTest(x, y, precise);
- }
-
- ///
- /// 完整的命中检测, 会检测所有插槽是否命中并返回命中的插槽名称
- ///
- public string[] HitTestFull(float x, float y, bool precise = false)
- {
- lock (_lock) return _spineObject.Skeleton.HitTestFull(x, y, precise).Select(v => v.Name).ToArray();
+ lock (_lock) return _spineObject.Skeleton.HitTest(x, y);
}
public SpineObjectConfigModel ObjectConfig
diff --git a/SpineViewer/Resources/Strings/en.xaml b/SpineViewer/Resources/Strings/en.xaml
index ffa0d04..be3fd86 100644
--- a/SpineViewer/Resources/Strings/en.xaml
+++ b/SpineViewer/Resources/Strings/en.xaml
@@ -120,10 +120,9 @@
Playback Speed
Wallpaper View
Render Selected Only
- Use Precise Hit Testing
- When enabled, click detection will be performed based on pixel transparency of the model.
- Log Hit Slot Names
- When enabled, the log box will output the model and slot information hit by each click operation (will not output when using Ctrl for multi-selection).
+ Hit Test Accuracy Level
+ Output Hit Test Slot Names
+ When enabled, the log box will output the model and slot information for each click hit test.
Show Axis
Background Color
Background Image Path
diff --git a/SpineViewer/Resources/Strings/ja.xaml b/SpineViewer/Resources/Strings/ja.xaml
index 4759e47..6790f47 100644
--- a/SpineViewer/Resources/Strings/ja.xaml
+++ b/SpineViewer/Resources/Strings/ja.xaml
@@ -120,10 +120,9 @@
再生速度
壁紙表示
選択のみレンダリング
- 精密ヒットテストを使用
- 有効にすると、モデルのピクセル透過度に基づいてクリック判定を行います。
- ヒットしたスロット名を出力
- 有効にすると、ログボックスに各クリック操作でヒットしたモデルとスロットの情報を出力します(Ctrlを押しながら複数選択する場合は出力されません)。
+ ヒットテスト精度レベル
+ ヒットテスト結果のスロット名を出力
+ 有効にすると、ログボックスに各クリック操作で命中したモデルとスロットの情報が出力されます。
座標軸を表示
背景色
背景画像のパス
diff --git a/SpineViewer/Resources/Strings/zh.xaml b/SpineViewer/Resources/Strings/zh.xaml
index 13421bd..200e717 100644
--- a/SpineViewer/Resources/Strings/zh.xaml
+++ b/SpineViewer/Resources/Strings/zh.xaml
@@ -120,10 +120,9 @@
播放速度
桌面投影
仅渲染选中
- 使用精确命中检测
- 启用后将会按像素透明度来检测点击操作是否命中了模型
- 输出命中的插槽名称
- 启用后将会在日志框内输出每一次点击操作命中的模型和插槽情况(按下 Ctrl 进行多选时不会输出)
+ 命中检测准确度等级
+ 输出命中检测结果的插槽名称
+ 启用后将会在日志框内输出每一次点击操作命中的模型和插槽情况
显示坐标轴
背景颜色
背景图片路径
diff --git a/SpineViewer/ViewModels/MainWindow/PreferenceViewModel.cs b/SpineViewer/ViewModels/MainWindow/PreferenceViewModel.cs
index 02cfa54..bb38428 100644
--- a/SpineViewer/ViewModels/MainWindow/PreferenceViewModel.cs
+++ b/SpineViewer/ViewModels/MainWindow/PreferenceViewModel.cs
@@ -109,7 +109,7 @@ namespace SpineViewer.ViewModels.MainWindow
AppLanguage = AppLanguage,
RenderSelectedOnly = RenderSelectedOnly,
- UsePreciseHitTest = UsePreciseHitTest,
+ HitTestLevel = HitTestLevel,
LogHitSlots = LogHitSlots,
WallpaperView = WallpaperView,
CloseToTray = CloseToTray,
@@ -140,7 +140,7 @@ namespace SpineViewer.ViewModels.MainWindow
AppLanguage = value.AppLanguage;
RenderSelectedOnly = value.RenderSelectedOnly;
- UsePreciseHitTest = value.UsePreciseHitTest;
+ HitTestLevel = value.HitTestLevel;
LogHitSlots = value.LogHitSlots;
WallpaperView = value.WallpaperView;
CloseToTray = value.CloseToTray;
@@ -252,6 +252,8 @@ namespace SpineViewer.ViewModels.MainWindow
public static ImmutableArray AppLanguageOptions { get; } = Enum.GetValues().ToImmutableArray();
+ public static ImmutableArray HitTestLevelOptions { get; } = Enum.GetValues().ToImmutableArray();
+
public AppLanguage AppLanguage
{
get => ((App)App.Current).Language;
@@ -264,16 +266,16 @@ namespace SpineViewer.ViewModels.MainWindow
set => SetProperty(_vmMain.SFMLRendererViewModel.RenderSelectedOnly, value, v => _vmMain.SFMLRendererViewModel.RenderSelectedOnly = v);
}
- public bool UsePreciseHitTest
+ public HitTestLevel HitTestLevel
{
- get => _vmMain.SFMLRendererViewModel.UsePreciseHitTest;
- set => SetProperty(_vmMain.SFMLRendererViewModel.UsePreciseHitTest, value, v => _vmMain.SFMLRendererViewModel.UsePreciseHitTest = v);
+ get => SpineExtension.HitTestLevel;
+ set => SetProperty(SpineExtension.HitTestLevel, value, v => SpineExtension.HitTestLevel = v);
}
public bool LogHitSlots
{
- get => _vmMain.SFMLRendererViewModel.LogHitSlots;
- set => SetProperty(_vmMain.SFMLRendererViewModel.LogHitSlots, value, v => _vmMain.SFMLRendererViewModel.LogHitSlots = v);
+ get => SpineExtension.LogHitSlots;
+ set => SetProperty(SpineExtension.LogHitSlots, value, v => SpineExtension.LogHitSlots = v);
}
public bool WallpaperView
diff --git a/SpineViewer/ViewModels/MainWindow/SFMLRendererViewModel.cs b/SpineViewer/ViewModels/MainWindow/SFMLRendererViewModel.cs
index b574eb5..8ac6320 100644
--- a/SpineViewer/ViewModels/MainWindow/SFMLRendererViewModel.cs
+++ b/SpineViewer/ViewModels/MainWindow/SFMLRendererViewModel.cs
@@ -250,26 +250,6 @@ namespace SpineViewer.ViewModels.MainWindow
}
private bool _renderSelectedOnly;
- ///
- /// 启用精确命中测试
- ///
- public bool UsePreciseHitTest
- {
- get => _usePreciseHitTest;
- set => SetProperty(ref _usePreciseHitTest, value);
- }
- private bool _usePreciseHitTest;
-
- ///
- /// 启用完整的命中测试并在日志中输出命中测试的插槽结果
- ///
- public bool LogHitSlots
- {
- get => _logHitSlots;
- set => SetProperty(ref _logHitSlots, value);
- }
- private bool _logHitSlots;
-
///
/// 启用桌面投影
///
@@ -368,22 +348,9 @@ namespace SpineViewer.ViewModels.MainWindow
if (_renderSelectedOnly)
{
bool hit = false;
- if (!_logHitSlots)
- {
- // 只在被选中的对象里判断是否有效命中
- hit = _models.Any(m => m.IsSelected && m.HitTest(src.X, src.Y, _usePreciseHitTest));
- }
- else
- {
- foreach (var sp in _models.Where(m => m.IsSelected))
- {
- var slotNames = sp.HitTestFull(src.X, src.Y, _usePreciseHitTest);
- if (slotNames.Length <= 0) continue;
- hit = true;
- _logger.Debug("Model Hit ({0}): [{1}]", sp.Name, string.Join(", ", slotNames));
- }
- }
+ // 只在被选中的对象里判断是否有效命中
+ hit = _models.Any(m => m.IsSelected && m.HitTest(src.X, src.Y));
// 如果没点到被选中的模型, 则不允许拖动
if (!hit) _draggingSrc = null;
@@ -395,40 +362,19 @@ namespace SpineViewer.ViewModels.MainWindow
// 没按 Ctrl 的情况下, 如果命中了已选中对象, 则就算普通命中
bool hit = false;
- if (!_logHitSlots)
+ foreach (var sp in _models.Where(m => m.IsShown))
{
- foreach (var sp in _models.Where(m => m.IsShown))
+ if (!sp.HitTest(src.X, src.Y)) continue;
+
+ hit = true;
+
+ // 如果点到了没被选中的东西, 则清空原先选中的, 改为只选中这一次点的
+ if (!sp.IsSelected)
{
- if (!sp.HitTest(src.X, src.Y, _usePreciseHitTest)) continue;
-
- hit = true;
-
- // 如果点到了没被选中的东西, 则清空原先选中的, 改为只选中这一次点的
- if (!sp.IsSelected)
- {
- RequestSelectionChanging?.Invoke(this, new(NotifyCollectionChangedAction.Reset));
- RequestSelectionChanging?.Invoke(this, new(NotifyCollectionChangedAction.Add, sp));
- }
- break;
- }
- }
- else
- {
- foreach (var sp in _models.Where(m => m.IsShown))
- {
- var slotNames = sp.HitTestFull(src.X, src.Y, _usePreciseHitTest);
- if (slotNames.Length <= 0) continue;
-
- // 如果点到了没被选中的东西, 则清空原先选中的, 改为只选中这一次点的
- // 仅判断顶层对象 (首次命中)
- if (!hit && !sp.IsSelected)
- {
- RequestSelectionChanging?.Invoke(this, new(NotifyCollectionChangedAction.Reset));
- RequestSelectionChanging?.Invoke(this, new(NotifyCollectionChangedAction.Add, sp));
- }
- hit = true;
- _logger.Debug("Model Hit ({0}): [{1}]", sp.Name, string.Join(", ", slotNames));
+ RequestSelectionChanging?.Invoke(this, new(NotifyCollectionChangedAction.Reset));
+ RequestSelectionChanging?.Invoke(this, new(NotifyCollectionChangedAction.Add, sp));
}
+ break;
}
// 如果点了空白的地方, 就清空选中列表
@@ -437,7 +383,7 @@ namespace SpineViewer.ViewModels.MainWindow
else
{
// 按下 Ctrl 的情况就执行多选, 并且点空白处也不会清空选中, 如果点击了本来就是选中的则取消选中
- if (_models.FirstOrDefault(m => m.IsShown && m.HitTest(src.X, src.Y, _usePreciseHitTest), null) is SpineObjectModel sp)
+ if (_models.FirstOrDefault(m => m.IsShown && m.HitTest(src.X, src.Y), null) is SpineObjectModel sp)
{
if (sp.IsSelected)
RequestSelectionChanging?.Invoke(this, new(NotifyCollectionChangedAction.Remove, sp));
diff --git a/SpineViewer/Views/PreferenceDialog.xaml b/SpineViewer/Views/PreferenceDialog.xaml
index 8610285..b139ae2 100644
--- a/SpineViewer/Views/PreferenceDialog.xaml
+++ b/SpineViewer/Views/PreferenceDialog.xaml
@@ -223,8 +223,10 @@
-
-
+
+