Compare commits
7
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d9aab28c7f | ||
|
|
419b2a74ed | ||
|
|
c9dbb22a39 | ||
|
|
753c595625 | ||
|
|
88bd60ab93 | ||
|
|
36669b89ef | ||
|
|
70b3f6dd99 |
No files matched your search
@@ -21,10 +21,12 @@ public class BvLocator
|
||||
{
|
||||
private static readonly ILogger Logger = App.GetLogger<BvLocator>();
|
||||
private readonly CancellationToken _cancellationToken;
|
||||
private int? _timeout;
|
||||
private int? _retryInterval;
|
||||
|
||||
public RecognitionObject RecognitionObject { get; }
|
||||
|
||||
public Action<List<Region>>? RetryAction { get; set; }
|
||||
public Func<List<Region>, Task>? RetryAction { get; set; }
|
||||
|
||||
public static int DefaultTimeout { get; set; } = 10000;
|
||||
|
||||
@@ -98,17 +100,21 @@ public class BvLocator
|
||||
|
||||
public async Task<List<Region>> WaitFor(int? timeout = null)
|
||||
{
|
||||
var actualTimeout = timeout ?? DefaultTimeout;
|
||||
var retryCount = actualTimeout / DefaultRetryInterval;
|
||||
var actualTimeout = timeout ?? _timeout ?? DefaultTimeout;
|
||||
var actualRetryInterval = _retryInterval ?? DefaultRetryInterval;
|
||||
var retryCount = Math.Max(1, actualTimeout / actualRetryInterval);
|
||||
|
||||
List<Region> results = [];
|
||||
var retryRes = await NewRetry.WaitForAction(() =>
|
||||
var retryRes = await NewRetry.WaitForAction(async () =>
|
||||
{
|
||||
results = FindAll();
|
||||
var b = results.Count > 0;
|
||||
RetryAction?.Invoke(results);
|
||||
if (!b && RetryAction != null)
|
||||
{
|
||||
await RetryAction(results);
|
||||
}
|
||||
return b;
|
||||
}, _cancellationToken, retryCount, DefaultRetryInterval);
|
||||
}, _cancellationToken, retryCount, actualRetryInterval);
|
||||
|
||||
if (retryRes)
|
||||
{
|
||||
@@ -150,20 +156,21 @@ public class BvLocator
|
||||
|
||||
public async Task WaitForDisappear(int? timeout = null)
|
||||
{
|
||||
var actualTimeout = timeout ?? DefaultTimeout;
|
||||
var retryCount = actualTimeout / DefaultRetryInterval;
|
||||
var actualTimeout = timeout ?? _timeout ?? DefaultTimeout;
|
||||
var actualRetryInterval = _retryInterval ?? DefaultRetryInterval;
|
||||
var retryCount = Math.Max(1, actualTimeout / actualRetryInterval);
|
||||
|
||||
var retryRes = await NewRetry.WaitForAction(() =>
|
||||
var retryRes = await NewRetry.WaitForAction(async () =>
|
||||
{
|
||||
var results = FindAll();
|
||||
var b = results.Count == 0;
|
||||
if (!b)
|
||||
if (!b && RetryAction != null)
|
||||
{
|
||||
RetryAction?.Invoke(results);
|
||||
await RetryAction(results);
|
||||
}
|
||||
|
||||
return b;
|
||||
}, _cancellationToken, retryCount, DefaultRetryInterval);
|
||||
}, _cancellationToken, retryCount, actualRetryInterval);
|
||||
|
||||
if (!retryRes)
|
||||
{
|
||||
@@ -205,19 +212,77 @@ public class BvLocator
|
||||
|
||||
public BvLocator WithRetryAction(Action<List<Region>>? action)
|
||||
{
|
||||
RetryAction = action;
|
||||
if (action == null)
|
||||
{
|
||||
RetryAction = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
RetryAction = (results) =>
|
||||
{
|
||||
action(results);
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置超时时间(毫秒)
|
||||
/// </summary>
|
||||
/// <param name="timeout">超时时间(毫秒)</param>
|
||||
/// <returns></returns>
|
||||
public BvLocator WithTimeout(int timeout)
|
||||
{
|
||||
if (timeout <= 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(timeout), "timeout 必须大于 0");
|
||||
}
|
||||
_timeout = timeout;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置重试间隔(毫秒)
|
||||
/// </summary>
|
||||
/// <param name="retryInterval">重试间隔(毫秒)</param>
|
||||
/// <returns></returns>
|
||||
public BvLocator WithRetryInterval(int retryInterval)
|
||||
{
|
||||
if (retryInterval <= 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(retryInterval), "retryInterval 必须大于 0");
|
||||
}
|
||||
_retryInterval = retryInterval;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 为 JavaScript 提供的动态参数重载
|
||||
/// 解决 ClearScript 无法将 JS 函数隐式转换为 Action 委托的问题
|
||||
/// 支持同步和异步 JS 函数
|
||||
/// </summary>
|
||||
/// <param name="action">JS 回调函数</param>
|
||||
/// <returns></returns>
|
||||
public BvLocator WithRetryAction(dynamic action)
|
||||
{
|
||||
RetryAction = (results) => action(results);
|
||||
if (action == null)
|
||||
{
|
||||
RetryAction = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
RetryAction = async (results) =>
|
||||
{
|
||||
var taskResult = action(results);
|
||||
// 如果 JS 返回的是 Promise/Task,等待其完成
|
||||
if (taskResult is Task task)
|
||||
{
|
||||
await task;
|
||||
}
|
||||
};
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -61,6 +61,9 @@ public class BgiOnnxModel
|
||||
public static readonly BgiOnnxModel BgiQClassify =
|
||||
Register("BgiQClassify", @"Assets\Model\Common\q_classify_sim.onnx");
|
||||
|
||||
public static readonly BgiOnnxModel BgiPickClassify =
|
||||
Register("BgiPickClassify", @"Assets\Model\Common\pick.onnx");
|
||||
|
||||
/// <summary>
|
||||
/// paddleOCR V4 检测模型
|
||||
/// </summary>
|
||||
|
||||
@@ -145,6 +145,35 @@ public class HtmlMask : IDisposable
|
||||
/// </summary>
|
||||
public bool Exists(string id) => HtmlMaskWindow.Exists(id);
|
||||
|
||||
/// <summary>
|
||||
/// 设置窗口的点击穿透模式
|
||||
/// </summary>
|
||||
/// <param name="windowId">窗口ID</param>
|
||||
/// <param name="enabled">true=点击穿透,false=可交互</param>
|
||||
public void SetClickThrough(string windowId, bool enabled)
|
||||
{
|
||||
HtmlMaskWindow.SetClickThrough(windowId, enabled);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取窗口的点击穿透状态
|
||||
/// </summary>
|
||||
/// <param name="windowId">窗口ID</param>
|
||||
/// <returns>true=点击穿透,false=可交互</returns>
|
||||
public bool GetClickThrough(string windowId)
|
||||
{
|
||||
return HtmlMaskWindow.GetClickThrough(windowId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 切换窗口的点击穿透模式
|
||||
/// </summary>
|
||||
/// <param name="windowId">窗口ID</param>
|
||||
public void ToggleClickThrough(string windowId)
|
||||
{
|
||||
HtmlMaskWindow.ToggleClickThrough(windowId);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 消息通信
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using BetterGenshinImpact.Core.Config;
|
||||
using BetterGenshinImpact.Core.Simulator;
|
||||
using BetterGenshinImpact.GameTask.AutoFight.Model;
|
||||
using BetterGenshinImpact.GameTask.AutoFight.Script;
|
||||
using BetterGenshinImpact.Model;
|
||||
@@ -74,6 +75,7 @@ public class OneKeyFightTask : Singleton<OneKeyFightTask>
|
||||
else
|
||||
{
|
||||
_cts.Cancel();
|
||||
Simulation.ReleaseAllKey();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -89,6 +91,7 @@ public class OneKeyFightTask : Singleton<OneKeyFightTask>
|
||||
if (IsHoldOnMode())
|
||||
{
|
||||
_cts?.Cancel();
|
||||
Simulation.ReleaseAllKey();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -179,9 +182,9 @@ public class OneKeyFightTask : Singleton<OneKeyFightTask>
|
||||
// 通用化战斗策略
|
||||
foreach (var command in combatCommands)
|
||||
{
|
||||
if (ct.IsCancellationRequested) break;
|
||||
if (command.ActivatingRound != null && command.ActivatingRound.Count > 0 && !command.ActivatingRound.Contains(round))
|
||||
{
|
||||
// 跳过强制首轮指令
|
||||
continue;
|
||||
}
|
||||
command.Execute(activeAvatar);
|
||||
|
||||
@@ -37,6 +37,14 @@ namespace BetterGenshinImpact.GameTask.AutoPick
|
||||
[ObservableProperty]
|
||||
private string _ocrEngine = PickOcrEngineEnum.Paddle.ToString();
|
||||
|
||||
/// <summary>
|
||||
/// 拾取内容识别方式
|
||||
/// - Ocr
|
||||
/// - Model
|
||||
/// </summary>
|
||||
[ObservableProperty]
|
||||
private string _itemRecognitionMode = PickItemRecognitionModeEnum.Ocr.ToString();
|
||||
|
||||
/// <summary>
|
||||
/// 急速模式
|
||||
/// 无视文字识别结果,直接拾取
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using BetterGenshinImpact.Core.Config;
|
||||
using BetterGenshinImpact.Core.Recognition;
|
||||
using BetterGenshinImpact.Core.Recognition.ONNX;
|
||||
using BetterGenshinImpact.Core.Recognition.OCR;
|
||||
using BetterGenshinImpact.Core.Recognition.ONNX.SVTR;
|
||||
using BetterGenshinImpact.Core.Script.Dependence.Model.TimerConfig;
|
||||
@@ -8,6 +9,8 @@ using BetterGenshinImpact.GameTask.AutoPick.Assets;
|
||||
using BetterGenshinImpact.Helpers;
|
||||
using BetterGenshinImpact.Service;
|
||||
using BetterGenshinImpact.View.Windows;
|
||||
using Compunet.YoloSharp.Data;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using OpenCvSharp;
|
||||
using System;
|
||||
@@ -19,6 +22,7 @@ using System.Text.Json;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using BetterGenshinImpact.GameTask.Model.Area;
|
||||
using Compunet.YoloSharp;
|
||||
|
||||
namespace BetterGenshinImpact.GameTask.AutoPick;
|
||||
|
||||
@@ -157,6 +161,12 @@ public partial class AutoPickTrigger : ITaskTrigger
|
||||
/// </summary>
|
||||
private int _prevClickFrameIndex = -1;
|
||||
|
||||
|
||||
private static readonly Lazy<BgiYoloPredictor> PickClassifierLazy = new(() =>
|
||||
App.ServiceProvider.GetRequiredService<BgiOnnxFactory>().CreateYoloPredictor(BgiOnnxModel.BgiPickClassify));
|
||||
|
||||
private bool _loggedPickModelMissing;
|
||||
|
||||
//private int _fastModePickCount = 0;
|
||||
|
||||
public void OnCapture(CaptureContent content)
|
||||
@@ -271,53 +281,16 @@ public partial class AutoPickTrigger : ITaskTrigger
|
||||
return;
|
||||
}
|
||||
|
||||
string text;
|
||||
if (config.OcrEngine == nameof(PickOcrEngineEnum.Yap))
|
||||
{
|
||||
var textMat = new Mat(content.CaptureRectArea.CacheGreyMat, textRect);
|
||||
text = TextInferenceFactory.Pick.Value.Inference(textMat);
|
||||
}
|
||||
else
|
||||
{
|
||||
using var textMat = new Mat(content.CaptureRectArea.SrcMat, textRect);
|
||||
var boundingRect = TextRectExtractor.GetTextBoundingRect(textMat);
|
||||
// var boundingRect = new Rect(); // 不使用自己写的文字区域提取
|
||||
// 如果找到有效区域
|
||||
if (boundingRect.X < 20 && boundingRect.Width > 5 && boundingRect.Height > 5)
|
||||
{
|
||||
// 截取只包含文字的区域
|
||||
using var textOnlyMat = new Mat(textMat, new Rect(0, 0,
|
||||
boundingRect.Right + 5 < textMat.Width ? boundingRect.Right + 5 : textMat.Width, textMat.Height));
|
||||
text = OcrFactory.Paddle.OcrWithoutDetector(textOnlyMat);
|
||||
var text = RecognizePickItem(content, foundRectArea, textRect, config);
|
||||
|
||||
// if (RuntimeHelper.IsDebug)
|
||||
// {
|
||||
// // 如果不等于正确文字,则保存图片
|
||||
// if (text != "烹饪")
|
||||
// {
|
||||
// var path = Global.Absolute("log/pick");
|
||||
// Directory.CreateDirectory(path);
|
||||
// var str = $"{DateTime.Now:yyyyMMddHHmmssfff}";
|
||||
// // textMat.SaveImage(Path.Combine(path, $"pick_ocr_ori_{str}.png"));
|
||||
// // 画上 boundingRect
|
||||
// Cv2.Rectangle(textMat, boundingRect, new Scalar(0, 0, 255), 1);
|
||||
// textMat.SaveImage(Path.Combine(path, $"pick_ocr_rect_{str}.png"));
|
||||
// bin.SaveImage(Path.Combine(path, $"bin_{str}.png"));
|
||||
// }
|
||||
// }
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.WriteLine("-- 无法识别到有效文字区域,尝试直接OCR DET");
|
||||
text = OcrFactory.Paddle.Ocr(textMat);
|
||||
}
|
||||
}
|
||||
|
||||
speedTimer.Record("文字识别");
|
||||
speedTimer.Record("识别");
|
||||
if (!string.IsNullOrEmpty(text))
|
||||
{
|
||||
// 处理OCR识别结果,清理无效字符并确保引号配对
|
||||
text = ProcessOcrText(text);
|
||||
if (config.ItemRecognitionMode != nameof(PickItemRecognitionModeEnum.Classify))
|
||||
{
|
||||
text = ProcessOcrText(text);
|
||||
}
|
||||
|
||||
if (DoNotPick(text))
|
||||
{
|
||||
@@ -370,6 +343,64 @@ public partial class AutoPickTrigger : ITaskTrigger
|
||||
speedTimer.DebugPrint();
|
||||
}
|
||||
|
||||
private string RecognizePickItem(CaptureContent content, Region foundRectArea, Rect textRect, AutoPickConfig config)
|
||||
{
|
||||
if (config.ItemRecognitionMode == nameof(PickItemRecognitionModeEnum.Classify))
|
||||
{
|
||||
return RecognizePickItemByClassify(content.CaptureRectArea, foundRectArea);
|
||||
}
|
||||
else
|
||||
{
|
||||
return RecognizePickItemByOcr(content, textRect, config);
|
||||
}
|
||||
}
|
||||
|
||||
private string RecognizePickItemByOcr(CaptureContent content, Rect textRect, AutoPickConfig config)
|
||||
{
|
||||
if (config.OcrEngine == nameof(PickOcrEngineEnum.Yap))
|
||||
{
|
||||
using var greyTextMat = new Mat(content.CaptureRectArea.CacheGreyMat, textRect);
|
||||
return TextInferenceFactory.Pick.Value.Inference(greyTextMat);
|
||||
}
|
||||
else
|
||||
{
|
||||
using var textMat = new Mat(content.CaptureRectArea.SrcMat, textRect);
|
||||
var boundingRect = TextRectExtractor.GetTextBoundingRect(textMat);
|
||||
// var boundingRect = new Rect(); // 不使用自己写的文字区域提取
|
||||
// 如果找到有效区域
|
||||
if (boundingRect.X < 20 && boundingRect.Width > 5 && boundingRect.Height > 5)
|
||||
{
|
||||
// 截取只包含文字的区域
|
||||
using var textOnlyMat = new Mat(textMat, new Rect(0, 0,
|
||||
boundingRect.Right + 5 < textMat.Width ? boundingRect.Right + 5 : textMat.Width, textMat.Height));
|
||||
return OcrFactory.Paddle.OcrWithoutDetector(textOnlyMat);
|
||||
}
|
||||
|
||||
Debug.WriteLine("-- 无法识别到有效文字区域,尝试直接OCR DET");
|
||||
return OcrFactory.Paddle.Ocr(textMat);
|
||||
}
|
||||
}
|
||||
|
||||
private string RecognizePickItemByClassify(ImageRegion captureRectArea, Region foundRectArea)
|
||||
{
|
||||
using var itemRegion = captureRectArea.DeriveCrop(GetPickModelRect(foundRectArea));
|
||||
var result = PickClassifierLazy.Value.Predictor.Classify(itemRegion.CacheImage);
|
||||
var topClass = result.GetTopClass();
|
||||
// Debug.WriteLine($"AutoPickTrigger: 拾取,{topClass.Name.Name} {topClass.Confidence:F2}");
|
||||
if (topClass.Confidence <= 0.75)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
// itemRegion.SrcMat.SaveImage(Global.Absolute("log\\pick.png"));
|
||||
|
||||
return topClass.Name.Name;
|
||||
}
|
||||
|
||||
private static Rect GetPickModelRect(Region foundRectArea)
|
||||
{
|
||||
return new Rect(foundRectArea.Right + 20, foundRectArea.Y - 16, 64, 64);
|
||||
}
|
||||
|
||||
private bool DoNotPick(string text)
|
||||
{
|
||||
// 唯一一个动态拾取项,特殊处理,不拾取
|
||||
@@ -405,7 +436,7 @@ public partial class AutoPickTrigger : ITaskTrigger
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
if (text.Contains("月谕圣牌"))
|
||||
{
|
||||
return true;
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace BetterGenshinImpact.GameTask.AutoPick;
|
||||
|
||||
public enum PickItemRecognitionModeEnum
|
||||
{
|
||||
Ocr,
|
||||
Classify
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
using BetterGenshinImpact.GameTask.AutoGeniusInvokation.Exception;
|
||||
using BetterGenshinImpact.GameTask.AutoGeniusInvokation.Exception;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@@ -89,6 +89,28 @@ public static class NewRetry
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 重试执行异步 action,直到返回 true 或达到最大重试次数。
|
||||
/// </summary>
|
||||
/// <param name="action">判断条件(异步)</param>
|
||||
/// <param name="ct">取消令牌</param>
|
||||
/// <param name="retryTimes">最大重试次数</param>
|
||||
/// <param name="delayMs">每次重试间隔(毫秒)</param>
|
||||
/// <returns>是否成功</returns>
|
||||
public static async Task<bool> WaitForAction(Func<Task<bool>> action, CancellationToken ct, int retryTimes = 10, int delayMs = 1000)
|
||||
{
|
||||
for (var i = 0; i < retryTimes; i++)
|
||||
{
|
||||
await TaskControl.Delay(delayMs, ct);
|
||||
if (await action())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 重试直到某个元素出现,可执行键盘或鼠标操作。
|
||||
/// </summary>
|
||||
|
||||
@@ -190,6 +190,7 @@ public class TaskRunner
|
||||
TaskTriggerDispatcher.Instance().SetTriggers(GameTaskManager.LoadInitialTriggers());
|
||||
|
||||
VisionContext.Instance().DrawContent.ClearAll();
|
||||
HtmlMaskWindow.CloseAll();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -9,7 +9,9 @@
|
||||
WindowStartupLocation="Manual"
|
||||
ShowInTaskbar="False"
|
||||
Topmost="True">
|
||||
<Grid>
|
||||
<wv2:WebView2 x:Name="WebView" DefaultBackgroundColor="Transparent"/>
|
||||
</Grid>
|
||||
<Border x:Name="ClickThroughBorder" Background="Transparent">
|
||||
<Grid>
|
||||
<wv2:WebView2 x:Name="WebView" DefaultBackgroundColor="Transparent"/>
|
||||
</Grid>
|
||||
</Border>
|
||||
</Window>
|
||||
@@ -17,7 +17,7 @@ using Microsoft.Web.WebView2.Core;
|
||||
namespace BetterGenshinImpact.View;
|
||||
|
||||
/// <summary>
|
||||
/// HTML遮罩窗口 - 仅用于显示,不可交互(点击穿透)
|
||||
/// HTML遮罩窗口
|
||||
/// </summary>
|
||||
public partial class HtmlMaskWindow : Window
|
||||
{
|
||||
@@ -29,12 +29,21 @@ public partial class HtmlMaskWindow : Window
|
||||
private readonly string _webView2DataPath;
|
||||
private readonly string _pageUrl;
|
||||
private bool _navigationCompleted;
|
||||
private bool _styleCaptured;
|
||||
private int _originalStyle;
|
||||
private volatile bool _isClickThrough = true;
|
||||
private readonly System.Windows.Media.SolidColorBrush _backgroundBrush = new();
|
||||
|
||||
/// <summary>
|
||||
/// 窗口唯一标识
|
||||
/// </summary>
|
||||
public string MaskId => _id;
|
||||
|
||||
/// <summary>
|
||||
/// 当前是否处于点击穿透模式
|
||||
/// </summary>
|
||||
public bool IsClickThrough => _isClickThrough;
|
||||
|
||||
private HtmlMaskWindow(string url, string? id, string workDir)
|
||||
{
|
||||
_id = id ?? Guid.NewGuid().ToString("N");
|
||||
@@ -42,6 +51,7 @@ public partial class HtmlMaskWindow : Window
|
||||
_webView2DataPath = Path.Combine(workDir, "WebView2Data");
|
||||
_pageUrl = url;
|
||||
InitializeComponent();
|
||||
ClickThroughBorder.Background = _backgroundBrush;
|
||||
Loaded += OnLoaded;
|
||||
InitializeAsync(url);
|
||||
}
|
||||
@@ -156,6 +166,47 @@ public partial class HtmlMaskWindow : Window
|
||||
return _windows.ContainsKey(id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取窗口实例,不存在则抛出异常
|
||||
/// </summary>
|
||||
/// <param name="windowId">窗口ID</param>
|
||||
/// <returns>窗口实例</returns>
|
||||
private static HtmlMaskWindow GetWindowOrThrow(string windowId)
|
||||
{
|
||||
if (_windows.TryGetValue(windowId, out var window))
|
||||
return window;
|
||||
throw new InvalidOperationException($"HTML遮罩窗口不存在或已关闭: {windowId}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置指定窗口的点击穿透模式
|
||||
/// </summary>
|
||||
/// <param name="windowId">窗口ID</param>
|
||||
/// <param name="enabled">true=点击穿透,false=可交互</param>
|
||||
public static void SetClickThrough(string windowId, bool enabled)
|
||||
{
|
||||
GetWindowOrThrow(windowId).SetClickThroughMode(enabled);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定窗口的点击穿透状态
|
||||
/// </summary>
|
||||
/// <param name="windowId">窗口ID</param>
|
||||
/// <returns>点击穿透状态</returns>
|
||||
public static bool GetClickThrough(string windowId)
|
||||
{
|
||||
return GetWindowOrThrow(windowId).IsClickThrough;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 原子切换指定窗口的点击穿透模式
|
||||
/// </summary>
|
||||
/// <param name="windowId">窗口ID</param>
|
||||
public static void ToggleClickThrough(string windowId)
|
||||
{
|
||||
GetWindowOrThrow(windowId).ToggleClickThroughCore();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 通知窗口刷新待推送的消息
|
||||
/// </summary>
|
||||
@@ -164,13 +215,24 @@ public partial class HtmlMaskWindow : Window
|
||||
if (!_windows.TryGetValue(windowId, out var window)) return;
|
||||
window.Dispatcher.BeginInvoke(() =>
|
||||
{
|
||||
// 页面还没加载完,消息留在队列中由 NavigationCompleted 统一推送
|
||||
if (!window._navigationCompleted) return;
|
||||
if (window.WebView.CoreWebView2 == null) return;
|
||||
HtmlMask.FlushPendingMessages(windowId, json =>
|
||||
try
|
||||
{
|
||||
window.WebView.CoreWebView2.PostWebMessageAsString(json);
|
||||
});
|
||||
// 页面还没加载完,消息留在队列中由 NavigationCompleted 统一推送
|
||||
if (!window._navigationCompleted) return;
|
||||
if (window.WebView.CoreWebView2 == null) return;
|
||||
HtmlMask.FlushPendingMessages(windowId, json =>
|
||||
{
|
||||
window.WebView.CoreWebView2.PostWebMessageAsString(json);
|
||||
});
|
||||
}
|
||||
catch (ObjectDisposedException)
|
||||
{
|
||||
// WebView 已被释放,忽略此消息推送
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
TaskControl.Logger.LogDebug(ex, "HTML遮罩窗口消息推送异常");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -178,7 +240,7 @@ public partial class HtmlMaskWindow : Window
|
||||
|
||||
private void OnLoaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
SetClickThrough();
|
||||
SetClickThrough(true);
|
||||
UpdatePosition();
|
||||
}
|
||||
|
||||
@@ -387,14 +449,87 @@ public partial class HtmlMaskWindow : Window
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置点击穿透
|
||||
/// 设置点击穿透模式
|
||||
/// </summary>
|
||||
private void SetClickThrough()
|
||||
/// <param name="enabled">true=点击穿透,false=可交互</param>
|
||||
private void SetClickThrough(bool enabled)
|
||||
{
|
||||
var hwnd = new WindowInteropHelper(this).Handle;
|
||||
var style = (int)GetWindowLong(hwnd, WindowLongFlags.GWL_EXSTYLE);
|
||||
User32.SetWindowLong(hwnd, WindowLongFlags.GWL_EXSTYLE,
|
||||
(IntPtr)(style | (int)User32.WindowStylesEx.WS_EX_TRANSPARENT));
|
||||
if (hwnd == IntPtr.Zero) return;
|
||||
|
||||
if (!_styleCaptured)
|
||||
{
|
||||
_originalStyle = (int)GetWindowLong(hwnd, WindowLongFlags.GWL_EXSTYLE);
|
||||
_styleCaptured = true;
|
||||
}
|
||||
|
||||
int newStyle = enabled
|
||||
? (_originalStyle | (int)User32.WindowStylesEx.WS_EX_TRANSPARENT | (int)User32.WindowStylesEx.WS_EX_LAYERED)
|
||||
: _originalStyle;
|
||||
|
||||
User32.SetWindowLong(hwnd, WindowLongFlags.GWL_EXSTYLE, (IntPtr)newStyle);
|
||||
_isClickThrough = enabled;
|
||||
|
||||
SetBackgroundOpacity(!enabled);
|
||||
|
||||
if (!enabled)
|
||||
{
|
||||
// 禁用穿透:激活遮罩窗口,确保获得键盘和鼠标焦点
|
||||
try
|
||||
{
|
||||
User32.SetForegroundWindow(hwnd);
|
||||
User32.BringWindowToTop(hwnd);
|
||||
Dispatcher.Invoke(() => Activate());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
TaskControl.Logger.LogDebug(ex, "HTML遮罩窗口激活失败");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// 开启穿透:将焦点还给游戏窗口
|
||||
try
|
||||
{
|
||||
var gameHandle = TaskContext.Instance().GameHandle;
|
||||
if (gameHandle != IntPtr.Zero)
|
||||
{
|
||||
SystemControl.FocusWindow(gameHandle);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
TaskControl.Logger.LogDebug(ex, "HTML遮罩恢复游戏焦点失败");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置背景透明度
|
||||
/// </summary>
|
||||
/// <param name="isInteractive">是否处于交互模式</param>
|
||||
private void SetBackgroundOpacity(bool isInteractive)
|
||||
{
|
||||
_backgroundBrush.Color = isInteractive
|
||||
? System.Windows.Media.Color.FromArgb(1, 0, 0, 0)
|
||||
: System.Windows.Media.Color.FromArgb(0, 0, 0, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置点击穿透模式
|
||||
/// </summary>
|
||||
/// <param name="enabled">true=点击穿透,false=可交互</param>
|
||||
public void SetClickThroughMode(bool enabled)
|
||||
{
|
||||
Dispatcher.Invoke(() => SetClickThrough(enabled));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 切换点击穿透模式
|
||||
/// </summary>
|
||||
private void ToggleClickThroughCore()
|
||||
{
|
||||
Dispatcher.Invoke(() => SetClickThrough(!_isClickThrough));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -80,6 +80,33 @@
|
||||
Margin="0,0,8,0"
|
||||
IsChecked="{Binding Config.AutoPickConfig.FastModeEnabled, Mode=TwoWay}" />
|
||||
</Grid>-->
|
||||
<Grid Margin="16">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
</Grid.RowDefinitions>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="*" />
|
||||
<ColumnDefinition Width="Auto" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<ui:TextBlock Grid.Row="0"
|
||||
Grid.Column="0"
|
||||
FontTypography="Body"
|
||||
Text="识别方式"
|
||||
TextWrapping="Wrap" />
|
||||
<ui:TextBlock Grid.Row="1"
|
||||
Grid.Column="0"
|
||||
Foreground="{ui:ThemeResource TextFillColorTertiaryBrush}"
|
||||
Text="OCR 通过文字识别拾取内容;Classify 使用物品分类模型识别"
|
||||
TextWrapping="Wrap" />
|
||||
<ComboBox Grid.Row="0"
|
||||
Grid.RowSpan="2"
|
||||
Grid.Column="1"
|
||||
Width="100"
|
||||
Margin="0,0,8,0"
|
||||
ItemsSource="{Binding PickItemRecognitionModeNames}"
|
||||
SelectedItem="{Binding Config.AutoPickConfig.ItemRecognitionMode, Mode=TwoWay}" />
|
||||
</Grid>
|
||||
<Grid Margin="16">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto" />
|
||||
|
||||
@@ -30,6 +30,8 @@ public partial class TriggerSettingsPageViewModel : ViewModel
|
||||
|
||||
[ObservableProperty] private string[] _selectChatOptionTypeNames = [SelectChatOptionTypes.UseMouse, SelectChatOptionTypes.UseInteractionKey];
|
||||
|
||||
[ObservableProperty] private string[] _pickItemRecognitionModeNames = [PickItemRecognitionModeEnum.Ocr.ToString(), PickItemRecognitionModeEnum.Classify.ToString()];
|
||||
|
||||
[ObservableProperty] private string[] _pickOcrEngineNames = [PickOcrEngineEnum.Paddle.ToString(), PickOcrEngineEnum.Yap.ToString()];
|
||||
|
||||
[ObservableProperty] private List<string> _pickButtonNames;
|
||||
|
||||
Reference in new issue
Block a user