From c27ea845ebe7a2ff725fe0e814fb3396ce61f6be Mon Sep 17 00:00:00 2001 From: huiyadanli Date: Sun, 8 Oct 2023 23:56:41 +0800 Subject: [PATCH] interface modification and logic optimization --- .../AutoPick/Assets/AutoPickAssets.cs | 2 +- .../GameTask/AutoPick/AutoPickTrigger.cs | 1 + .../GameTask/AutoSkip/AutoSkipTrigger.cs | 20 ++++++++---- .../GameTask/GameTaskManager.cs | 4 +++ BetterGenshinImpact/GameTask/SystemControl.cs | 6 ++++ ...Dispatcher.cs => TaskTriggerDispatcher.cs} | 31 +++++++++++-------- .../View/Pages/TriggerSettingsPage.xaml | 24 +++++++------- .../View/Pages/TriggerSettingsPage.xaml.cs | 2 +- .../ViewModel/Pages/HomePageViewModel.cs | 2 +- .../Pages/MacroSettingsPageViewModel.cs | 2 +- .../Pages/TriggerSettingsPageViewModel.cs | 17 +++++++++- .../Graphics/GraphicsCapture.cs | 3 -- 12 files changed, 76 insertions(+), 38 deletions(-) rename BetterGenshinImpact/GameTask/{TaskDispatcher.cs => TaskTriggerDispatcher.cs} (91%) diff --git a/BetterGenshinImpact/GameTask/AutoPick/Assets/AutoPickAssets.cs b/BetterGenshinImpact/GameTask/AutoPick/Assets/AutoPickAssets.cs index bf2acf71..c3530638 100644 --- a/BetterGenshinImpact/GameTask/AutoPick/Assets/AutoPickAssets.cs +++ b/BetterGenshinImpact/GameTask/AutoPick/Assets/AutoPickAssets.cs @@ -20,7 +20,7 @@ namespace BetterGenshinImpact.GameTask.AutoPick.Assets RegionOfInterest = new Rect(info.CaptureAreaRect.Width / 2, info.CaptureAreaRect.Height / 3, info.CaptureAreaRect.Width - info.CaptureAreaRect.Width / 2, - info.CaptureAreaRect.Height - info.CaptureAreaRect.Height / 3), + info.CaptureAreaRect.Height - info.CaptureAreaRect.Height / 2), DrawOnWindow = false }.InitTemplate(); diff --git a/BetterGenshinImpact/GameTask/AutoPick/AutoPickTrigger.cs b/BetterGenshinImpact/GameTask/AutoPick/AutoPickTrigger.cs index e0c057e0..6805cc1f 100644 --- a/BetterGenshinImpact/GameTask/AutoPick/AutoPickTrigger.cs +++ b/BetterGenshinImpact/GameTask/AutoPick/AutoPickTrigger.cs @@ -119,6 +119,7 @@ public class AutoPickTrigger : ITaskTrigger { // 二值化 Cv2.Threshold(mat, mat, 0, 255, ThresholdTypes.Otsu | ThresholdTypes.Binary); + //Cv2.AdaptiveThreshold(mat, mat, 255, AdaptiveThresholdTypes.GaussianC, ThresholdTypes.Binary, 31, 3); // 效果不错 但是和模型不搭 //mat = OpenCvCommonHelper.Threshold(mat, Scalar.FromRgb(235, 235, 235), Scalar.FromRgb(255, 255, 255)); // 识别物品不太行 // 不知道为什么要强制拉伸到 221x32 mat = ResizeHelper.ResizeTo(mat, 221, 32); diff --git a/BetterGenshinImpact/GameTask/AutoSkip/AutoSkipTrigger.cs b/BetterGenshinImpact/GameTask/AutoSkip/AutoSkipTrigger.cs index 3f8a4b37..16663ba8 100644 --- a/BetterGenshinImpact/GameTask/AutoSkip/AutoSkipTrigger.cs +++ b/BetterGenshinImpact/GameTask/AutoSkip/AutoSkipTrigger.cs @@ -39,6 +39,11 @@ public class AutoSkipTrigger : ITaskTrigger /// private int _prevClickFrameIndex = -1; + /// + /// 左上角剧情自动的按钮位置 + /// + private Rect _prevButtonRect = Rect.Empty; + public void OnCapture(CaptureContent content) { if (content.IsReachInterval(TimeSpan.FromMilliseconds(200))) @@ -46,14 +51,17 @@ public class AutoSkipTrigger : ITaskTrigger return; } - if (TaskContext.Instance().Config.AutoSkipConfig.QuicklySkipConversationsEnabled) + + // 找左上角剧情自动的按钮 + content.CaptureRectArea.Find(_autoSkipAssets.StopAutoButtonRo, foundRectArea => { - // 找左上角剧情自动的按钮 - content.CaptureRectArea.Find(_autoSkipAssets.StopAutoButtonRo, (_) => + _prevButtonRect = foundRectArea.ToRect(); + if (TaskContext.Instance().Config.AutoSkipConfig.QuicklySkipConversationsEnabled) { new InputSimulator().Keyboard.KeyPress(VirtualKeyCode.SPACE); - }); - } + } + }); + // 不存在则找右下的选项按钮 content.CaptureRectArea.Find(_autoSkipAssets.OptionButtonRo, (optionButtonRectArea) => @@ -68,6 +76,7 @@ public class AutoSkipTrigger : ITaskTrigger { _logger.LogInformation("自动剧情:{Text}", "点击选项"); } + _prevClickFrameIndex = content.FrameIndex; } }); @@ -84,6 +93,5 @@ public class AutoSkipTrigger : ITaskTrigger } // TODO 自动交付材料 - } } \ No newline at end of file diff --git a/BetterGenshinImpact/GameTask/GameTaskManager.cs b/BetterGenshinImpact/GameTask/GameTaskManager.cs index 99dd6842..0aa6bec7 100644 --- a/BetterGenshinImpact/GameTask/GameTaskManager.cs +++ b/BetterGenshinImpact/GameTask/GameTaskManager.cs @@ -14,6 +14,10 @@ namespace BetterGenshinImpact.GameTask { public static Dictionary? TriggerDictionary { get; set; } + /// + /// 一定要在任务上下文初始化完毕后使用 + /// + /// public static List LoadTriggers() { TriggerDictionary = new Dictionary() diff --git a/BetterGenshinImpact/GameTask/SystemControl.cs b/BetterGenshinImpact/GameTask/SystemControl.cs index 117d6096..6c0efec6 100644 --- a/BetterGenshinImpact/GameTask/SystemControl.cs +++ b/BetterGenshinImpact/GameTask/SystemControl.cs @@ -112,5 +112,11 @@ namespace BetterGenshinImpact.GameTask var bottom = top + gameScreenRect.Height; return new RECT(left, top, right, bottom); } + + public static void ActivateWindow(IntPtr hWnd) + { + User32.ShowWindow(hWnd, ShowWindowCommand.SW_RESTORE); + User32.SetForegroundWindow(hWnd); + } } } \ No newline at end of file diff --git a/BetterGenshinImpact/GameTask/TaskDispatcher.cs b/BetterGenshinImpact/GameTask/TaskTriggerDispatcher.cs similarity index 91% rename from BetterGenshinImpact/GameTask/TaskDispatcher.cs rename to BetterGenshinImpact/GameTask/TaskTriggerDispatcher.cs index 94584d26..8766b21b 100644 --- a/BetterGenshinImpact/GameTask/TaskDispatcher.cs +++ b/BetterGenshinImpact/GameTask/TaskTriggerDispatcher.cs @@ -10,9 +10,9 @@ using Vanara.PInvoke; namespace BetterGenshinImpact.GameTask { - public class TaskDispatcher : IDisposable + public class TaskTriggerDispatcher : IDisposable { - private readonly ILogger _logger = App.GetLogger(); + private readonly ILogger _logger = App.GetLogger(); private readonly System.Timers.Timer _timer = new(); private List? _triggers; @@ -28,7 +28,7 @@ namespace BetterGenshinImpact.GameTask public bool Enabled => _timer.Enabled; - public TaskDispatcher() + public TaskTriggerDispatcher() { _timer.Elapsed += Tick; //_timer.Tick += Tick; @@ -36,17 +36,19 @@ namespace BetterGenshinImpact.GameTask public void Start(IntPtr hWnd, CaptureModes mode, int interval = 50) { - // 初始化任务上下文(一定要在初始化触发器前完成) - TaskContext.Instance().Init(hWnd); - - PrintSystemInfo(); - - // 初始化触发器 - _triggers = GameTaskManager.LoadTriggers(); - // 初始化截图器 _capture = GameCaptureFactory.Create(mode); - //_capture.IsClientEnabled = true; + // 激活窗口 保证后面能够正常获取窗口信息 + SystemControl.ActivateWindow(hWnd); + + // 初始化任务上下文(一定要在初始化触发器前完成) + TaskContext.Instance().Init(hWnd); + PrintSystemInfo(); + + // 初始化触发器(一定要在任务上下文初始化完毕后使用) + _triggers = GameTaskManager.LoadTriggers(); + + // 启动截图 _capture.Start(hWnd); // 启动定时器 @@ -172,7 +174,10 @@ namespace BetterGenshinImpact.GameTask sw.Start(); trigger.OnCapture(content); sw.Stop(); - Debug.WriteLine($"{trigger.Name}耗时:" + sw.ElapsedMilliseconds); + if (sw.ElapsedMilliseconds > 0) + { + Debug.WriteLine($"{trigger.Name}耗时:" + sw.ElapsedMilliseconds); + } } } } diff --git a/BetterGenshinImpact/View/Pages/TriggerSettingsPage.xaml b/BetterGenshinImpact/View/Pages/TriggerSettingsPage.xaml index 0c7ea507..fbbb9506 100644 --- a/BetterGenshinImpact/View/Pages/TriggerSettingsPage.xaml +++ b/BetterGenshinImpact/View/Pages/TriggerSettingsPage.xaml @@ -4,7 +4,7 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:BetterGenshinImpact.View.Pages" - xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml" + xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml" xmlns:pages="clr-namespace:BetterGenshinImpact.ViewModel.Pages" mc:Ignorable="d" d:DataContext="{d:DesignInstance Type=pages:TriggerSettingsPageViewModel}" @@ -41,13 +41,13 @@ Grid.Column="0" Foreground="{ui:ThemeResource TextFillColorTertiaryBrush}" TextWrapping="Wrap" - Text="选项不在黑名单时,自动按下 F 拾取/对话" /> + Text="选项不是NPC对话且不在黑名单时,自动按下 F 拾取/交互" /> + IsChecked="{Binding Config.AutoPickConfig.Enabled, Mode=TwoWay}" /> @@ -77,7 +77,8 @@ Grid.RowSpan="2" Grid.Column="1" Content="前往设置" - Margin="0,0,36,0" /> + Margin="0,0,36,0" + Command="{Binding EditBlacklistCommand}" /> @@ -105,7 +106,8 @@ Grid.RowSpan="2" Grid.Column="1" Content="前往设置" - Margin="0,0,36,0" /> + Margin="0,0,36,0" + Command="{Binding EditWhitelistCommand}" /> @@ -138,7 +140,7 @@ Grid.RowSpan="2" Grid.Column="1" Margin="0,0,24,0" - IsChecked="{Binding Config.AutoSkipConfig.Enabled, Mode=TwoWay}"/> + IsChecked="{Binding Config.AutoSkipConfig.Enabled, Mode=TwoWay}" /> @@ -168,7 +170,7 @@ Grid.RowSpan="2" Grid.Column="1" Margin="0,0,36,0" - IsChecked="{Binding Config.AutoSkipConfig.QuicklySkipConversationsEnabled, Mode=TwoWay}"/> + IsChecked="{Binding Config.AutoSkipConfig.QuicklySkipConversationsEnabled, Mode=TwoWay}" /> @@ -201,7 +203,7 @@ Grid.RowSpan="2" Grid.Column="1" Margin="0,0,24,0" - IsChecked="{Binding Config.AutoFishingConfig.Enabled, Mode=TwoWay}"/> + IsChecked="{Binding Config.AutoFishingConfig.Enabled, Mode=TwoWay}" /> @@ -231,7 +233,7 @@ Grid.RowSpan="2" Grid.Column="1" Margin="0,0,36,0" - IsChecked="{Binding Config.AutoFishingConfig.AutoThrowRodEnabled, Mode=TwoWay}"/> + IsChecked="{Binding Config.AutoFishingConfig.AutoThrowRodEnabled, Mode=TwoWay}" /> @@ -265,7 +267,7 @@ - + \ No newline at end of file diff --git a/BetterGenshinImpact/View/Pages/TriggerSettingsPage.xaml.cs b/BetterGenshinImpact/View/Pages/TriggerSettingsPage.xaml.cs index b3959dba..125c62d1 100644 --- a/BetterGenshinImpact/View/Pages/TriggerSettingsPage.xaml.cs +++ b/BetterGenshinImpact/View/Pages/TriggerSettingsPage.xaml.cs @@ -17,7 +17,7 @@ namespace BetterGenshinImpact.View.Pages { public partial class TriggerSettingsPage { - TriggerSettingsPageViewModel ViewModel { get; } + private TriggerSettingsPageViewModel ViewModel { get; } public TriggerSettingsPage(TriggerSettingsPageViewModel viewModel) { diff --git a/BetterGenshinImpact/ViewModel/Pages/HomePageViewModel.cs b/BetterGenshinImpact/ViewModel/Pages/HomePageViewModel.cs index c572d57d..75aa43b5 100644 --- a/BetterGenshinImpact/ViewModel/Pages/HomePageViewModel.cs +++ b/BetterGenshinImpact/ViewModel/Pages/HomePageViewModel.cs @@ -41,7 +41,7 @@ public partial class HomePageViewModel : ObservableObject, INavigationAware private MaskWindow? _maskWindow; private readonly ILogger _logger = App.GetLogger(); - private readonly TaskDispatcher _taskDispatcher = new(); + private readonly TaskTriggerDispatcher _taskDispatcher = new(); private readonly MouseKeyMonitor _mouseKeyMonitor = new(); public HomePageViewModel(IConfigService configService) diff --git a/BetterGenshinImpact/ViewModel/Pages/MacroSettingsPageViewModel.cs b/BetterGenshinImpact/ViewModel/Pages/MacroSettingsPageViewModel.cs index c8a0bb42..c30f6eb6 100644 --- a/BetterGenshinImpact/ViewModel/Pages/MacroSettingsPageViewModel.cs +++ b/BetterGenshinImpact/ViewModel/Pages/MacroSettingsPageViewModel.cs @@ -10,7 +10,7 @@ using Wpf.Ui.Controls; namespace BetterGenshinImpact.ViewModel.Pages { - public class MacroSettingsPageViewModel : ObservableObject, INavigationAware + public partial class MacroSettingsPageViewModel : ObservableObject, INavigationAware { public AllConfig Config { get; set; } diff --git a/BetterGenshinImpact/ViewModel/Pages/TriggerSettingsPageViewModel.cs b/BetterGenshinImpact/ViewModel/Pages/TriggerSettingsPageViewModel.cs index 73f32ba7..b08b5f52 100644 --- a/BetterGenshinImpact/ViewModel/Pages/TriggerSettingsPageViewModel.cs +++ b/BetterGenshinImpact/ViewModel/Pages/TriggerSettingsPageViewModel.cs @@ -1,16 +1,18 @@ using CommunityToolkit.Mvvm.ComponentModel; using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; using BetterGenshinImpact.Core.Config; using BetterGenshinImpact.Service.Interface; using Wpf.Ui.Controls; +using CommunityToolkit.Mvvm.Input; namespace BetterGenshinImpact.ViewModel.Pages; -public class TriggerSettingsPageViewModel : ObservableObject, INavigationAware +public partial class TriggerSettingsPageViewModel : ObservableObject, INavigationAware { public AllConfig Config { get; set; } @@ -26,4 +28,17 @@ public class TriggerSettingsPageViewModel : ObservableObject, INavigationAware public void OnNavigatedFrom() { } + + + [RelayCommand] + private void OnEditBlacklist() + { + Process.Start("notepad.exe", Global.Absolute(@"Config\pick_black_lists.json")); + } + + [RelayCommand] + private void OnEditWhitelist() + { + Process.Start("notepad.exe", Global.Absolute(@"Config\pick_white_lists.json")); + } } \ No newline at end of file diff --git a/Fischless.GameCapture/Graphics/GraphicsCapture.cs b/Fischless.GameCapture/Graphics/GraphicsCapture.cs index f9ea6628..04474872 100644 --- a/Fischless.GameCapture/Graphics/GraphicsCapture.cs +++ b/Fischless.GameCapture/Graphics/GraphicsCapture.cs @@ -25,9 +25,6 @@ public class GraphicsCapture : IGameCapture { _hWnd = hWnd; - User32.ShowWindow(hWnd, ShowWindowCommand.SW_RESTORE); - User32.SetForegroundWindow(hWnd); - _region = GetGameScreenRegion(hWnd); IsCapturing = true;