From 95f7b76a03dac0200c6897dd219f21b44d3d488a Mon Sep 17 00:00:00 2001 From: qhy040404 Date: Mon, 30 Sep 2024 23:17:10 +0800 Subject: [PATCH 1/2] add TwoStateButton --- .../View/Controls/TwoStateButton.cs | 92 +++++++++++++++ BetterGenshinImpact/View/Pages/HomePage.xaml | 23 ++-- .../View/Pages/TaskSettingsPage.xaml | 109 ++++++++++-------- 3 files changed, 166 insertions(+), 58 deletions(-) create mode 100644 BetterGenshinImpact/View/Controls/TwoStateButton.cs diff --git a/BetterGenshinImpact/View/Controls/TwoStateButton.cs b/BetterGenshinImpact/View/Controls/TwoStateButton.cs new file mode 100644 index 00000000..ae8468b3 --- /dev/null +++ b/BetterGenshinImpact/View/Controls/TwoStateButton.cs @@ -0,0 +1,92 @@ +using System.Windows; +using System.Windows.Input; +using Wpf.Ui.Controls; + +namespace BetterGenshinImpact.View.Controls; + +public class TwoStateButton : Button +{ + private bool _isEnabled; + + public TwoStateButton() + { + if (TryFindResource(typeof(Button)) is Style style) + { + Style = style; + } + + Click += OnClick; + Loaded += OnLoaded; + } + + private void OnLoaded(object sender, RoutedEventArgs e) + { + Content = EnableContent; + Icon = EnableIcon; + } + + private void OnClick(object sender, RoutedEventArgs e) + { + if (_isEnabled) + { + DisableCommand?.Execute(null); + Content = EnableContent; + Icon = EnableIcon; + } + else + { + EnableCommand?.Execute(null); + Content = DisableContent; + Icon = DisableIcon; + } + _isEnabled = !_isEnabled; + } + + private static readonly DependencyProperty EnableContentProperty = DependencyProperty.Register(nameof(EnableContent), typeof(object), typeof(TwoStateButton), new PropertyMetadata("启动")); + + public object EnableContent + { + get => (string)GetValue(EnableContentProperty); + set => SetValue(EnableContentProperty, value); + } + + private static readonly DependencyProperty EnableIconProperty = DependencyProperty.Register(nameof(EnableIcon), typeof(IconElement), typeof(TwoStateButton), new PropertyMetadata(null)); + + public IconElement EnableIcon + { + get => (IconElement)GetValue(EnableIconProperty); + set => SetValue(EnableIconProperty, value); + } + + private static readonly DependencyProperty EnableCommandProperty = DependencyProperty.Register(nameof(EnableCommand), typeof(ICommand), typeof(TwoStateButton), new PropertyMetadata(null)); + + public ICommand EnableCommand + { + get => (ICommand)GetValue(EnableCommandProperty); + set => SetValue(EnableCommandProperty, value); + } + + private static readonly DependencyProperty DisableContentProperty = DependencyProperty.Register(nameof(DisableContent), typeof(object), typeof(TwoStateButton), new PropertyMetadata("停止")); + + public object DisableContent + { + get => (string)GetValue(DisableContentProperty); + set => SetValue(DisableContentProperty, value); + } + + private static readonly DependencyProperty DisableIconProperty = DependencyProperty.Register(nameof(DisableIcon), typeof(IconElement), typeof(TwoStateButton), new PropertyMetadata(null)); + + public IconElement DisableIcon + { + get => (IconElement)GetValue(DisableIconProperty); + set => SetValue(DisableIconProperty, value); + } + + private static readonly DependencyProperty DisableCommandProperty = DependencyProperty.Register(nameof(DisableCommand), typeof(ICommand), typeof(TwoStateButton), new PropertyMetadata(null)); + + public ICommand DisableCommand + { + get => (ICommand)GetValue(DisableCommandProperty); + set => SetValue(DisableCommandProperty, value); + } +} \ No newline at end of file diff --git a/BetterGenshinImpact/View/Pages/HomePage.xaml b/BetterGenshinImpact/View/Pages/HomePage.xaml index 4d89a677..e476842a 100644 --- a/BetterGenshinImpact/View/Pages/HomePage.xaml +++ b/BetterGenshinImpact/View/Pages/HomePage.xaml @@ -2,6 +2,7 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:b="http://schemas.microsoft.com/xaml/behaviors" + xmlns:controls="clr-namespace:BetterGenshinImpact.View.Controls" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:BetterGenshinImpact.View.Pages" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" @@ -95,14 +96,20 @@ Grid.Column="1" Margin="0,0,24,0" Orientation="Horizontal"> - - + + + + + + + + + diff --git a/BetterGenshinImpact/View/Pages/TaskSettingsPage.xaml b/BetterGenshinImpact/View/Pages/TaskSettingsPage.xaml index 85388868..f22c5008 100644 --- a/BetterGenshinImpact/View/Pages/TaskSettingsPage.xaml +++ b/BetterGenshinImpact/View/Pages/TaskSettingsPage.xaml @@ -2,6 +2,7 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:b="http://schemas.microsoft.com/xaml/behaviors" + xmlns:controls="clr-namespace:BetterGenshinImpact.View.Controls" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:helpers="clr-namespace:BetterGenshinImpact.Helpers" xmlns:local="clr-namespace:BetterGenshinImpact.View.Pages" @@ -23,32 +24,32 @@ - - - - - - - - - - - 停止下方独立任务的运行 - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -78,12 +79,14 @@ 点击查看使用教程 - + @@ -178,12 +181,14 @@ 点击查看使用教程 - + @@ -346,12 +351,14 @@ 点击查看使用教程 - + @@ -504,12 +511,14 @@ 点击查看使用教程 - + From 41c5f4c22670ad97c2644763706da3d309210d47 Mon Sep 17 00:00:00 2001 From: qhy040404 Date: Tue, 1 Oct 2024 00:04:35 +0800 Subject: [PATCH 2/2] fix wrong state --- .../View/Controls/TwoStateButton.cs | 45 ++++++++++++------- BetterGenshinImpact/View/Pages/HomePage.xaml | 4 +- .../View/Pages/TaskSettingsPage.xaml | 13 +++--- .../Pages/TaskSettingsPageViewModel.cs | 16 +++++++ 4 files changed, 54 insertions(+), 24 deletions(-) diff --git a/BetterGenshinImpact/View/Controls/TwoStateButton.cs b/BetterGenshinImpact/View/Controls/TwoStateButton.cs index ae8468b3..ff6378a1 100644 --- a/BetterGenshinImpact/View/Controls/TwoStateButton.cs +++ b/BetterGenshinImpact/View/Controls/TwoStateButton.cs @@ -6,8 +6,6 @@ namespace BetterGenshinImpact.View.Controls; public class TwoStateButton : Button { - private bool _isEnabled; - public TwoStateButton() { if (TryFindResource(typeof(Button)) is Style style) @@ -15,31 +13,28 @@ public class TwoStateButton : Button Style = style; } - Click += OnClick; Loaded += OnLoaded; } private void OnLoaded(object sender, RoutedEventArgs e) { - Content = EnableContent; - Icon = EnableIcon; + UpdateButton(); } - private void OnClick(object sender, RoutedEventArgs e) + private static void OnIsCheckedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { - if (_isEnabled) + if (d is TwoStateButton button) { - DisableCommand?.Execute(null); - Content = EnableContent; - Icon = EnableIcon; + button.UpdateButton(); } - else - { - EnableCommand?.Execute(null); - Content = DisableContent; - Icon = DisableIcon; - } - _isEnabled = !_isEnabled; + } + + private static readonly DependencyProperty IsCheckedProperty = DependencyProperty.Register(nameof(IsChecked), typeof(bool), typeof(TwoStateButton), new PropertyMetadata(false, OnIsCheckedChanged)); + + public bool IsChecked + { + get => (bool)GetValue(IsCheckedProperty); + set => SetValue(IsCheckedProperty, value); } private static readonly DependencyProperty EnableContentProperty = DependencyProperty.Register(nameof(EnableContent), typeof(object), typeof(TwoStateButton), new PropertyMetadata("启动")); @@ -89,4 +84,20 @@ public class TwoStateButton : Button get => (ICommand)GetValue(DisableCommandProperty); set => SetValue(DisableCommandProperty, value); } + + private void UpdateButton() + { + if (IsChecked) + { + Command = DisableCommand; + Content = DisableContent; + Icon = DisableIcon; + } + else + { + Command = EnableCommand; + Content = EnableContent; + Icon = EnableIcon; + } + } } \ No newline at end of file diff --git a/BetterGenshinImpact/View/Pages/HomePage.xaml b/BetterGenshinImpact/View/Pages/HomePage.xaml index e476842a..4535bb76 100644 --- a/BetterGenshinImpact/View/Pages/HomePage.xaml +++ b/BetterGenshinImpact/View/Pages/HomePage.xaml @@ -4,7 +4,6 @@ xmlns:b="http://schemas.microsoft.com/xaml/behaviors" xmlns:controls="clr-namespace:BetterGenshinImpact.View.Controls" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" - xmlns:local="clr-namespace:BetterGenshinImpact.View.Pages" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:pages="clr-namespace:BetterGenshinImpact.ViewModel.Pages" xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml" @@ -101,7 +100,8 @@ DisableIcon="{ui:SymbolIcon Dismiss24}" EnableCommand="{Binding StartTriggerCommand}" EnableContent="启动" - EnableIcon="{ui:SymbolIcon Play24}" /> + EnableIcon="{ui:SymbolIcon Play24}" + IsChecked="{Binding TaskDispatcherEnabled, Mode=OneWay}" /> diff --git a/BetterGenshinImpact/View/Pages/TaskSettingsPage.xaml b/BetterGenshinImpact/View/Pages/TaskSettingsPage.xaml index f22c5008..ec109448 100644 --- a/BetterGenshinImpact/View/Pages/TaskSettingsPage.xaml +++ b/BetterGenshinImpact/View/Pages/TaskSettingsPage.xaml @@ -5,7 +5,6 @@ xmlns:controls="clr-namespace:BetterGenshinImpact.View.Controls" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:helpers="clr-namespace:BetterGenshinImpact.Helpers" - xmlns:local="clr-namespace:BetterGenshinImpact.View.Pages" xmlns:markup="clr-namespace:BetterGenshinImpact.Markup" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:pages="clr-namespace:BetterGenshinImpact.ViewModel.Pages" @@ -86,7 +85,8 @@ DisableCommand="{Binding StopSoloTaskCommand}" DisableContent="停止" EnableCommand="{Binding SwitchAutoGeniusInvokationCommand}" - EnableContent="{Binding SwitchAutoGeniusInvokationButtonText}" /> + EnableContent="{Binding SwitchAutoGeniusInvokationButtonText}" + IsChecked="{Binding SwitchAutoGeniusInvokationEnabled}" /> @@ -188,7 +188,8 @@ DisableCommand="{Binding StopSoloTaskCommand}" DisableContent="停止" EnableCommand="{Binding SwitchAutoWoodCommand}" - EnableContent="{Binding SwitchAutoWoodButtonText}" /> + EnableContent="{Binding SwitchAutoWoodButtonText}" + IsChecked="{Binding SwitchAutoWoodEnabled}" /> @@ -358,7 +359,8 @@ DisableCommand="{Binding StopSoloTaskCommand}" DisableContent="停止" EnableCommand="{Binding SwitchAutoFightCommand}" - EnableContent="{Binding SwitchAutoFightButtonText}" /> + EnableContent="{Binding SwitchAutoFightButtonText}" + IsChecked="{Binding SwitchAutoFightEnabled}" /> @@ -518,7 +520,8 @@ DisableCommand="{Binding StopSoloTaskCommand}" DisableContent="停止" EnableCommand="{Binding SwitchAutoDomainCommand}" - EnableContent="{Binding SwitchAutoDomainButtonText}" /> + EnableContent="{Binding SwitchAutoDomainButtonText}" + IsChecked="{Binding SwitchAutoDomainEnabled}" /> diff --git a/BetterGenshinImpact/ViewModel/Pages/TaskSettingsPageViewModel.cs b/BetterGenshinImpact/ViewModel/Pages/TaskSettingsPageViewModel.cs index b0e12f83..26614580 100644 --- a/BetterGenshinImpact/ViewModel/Pages/TaskSettingsPageViewModel.cs +++ b/BetterGenshinImpact/ViewModel/Pages/TaskSettingsPageViewModel.cs @@ -37,15 +37,19 @@ public partial class TaskSettingsPageViewModel : ObservableObject, INavigationAw private static readonly object _locker = new(); [ObservableProperty] private string[] _strategyList; + [ObservableProperty] private bool _switchAutoGeniusInvokationEnabled; [ObservableProperty] private string _switchAutoGeniusInvokationButtonText = "启动"; [ObservableProperty] private int _autoWoodRoundNum; [ObservableProperty] private int _autoWoodDailyMaxCount = 2000; + [ObservableProperty] private bool _switchAutoWoodEnabled; [ObservableProperty] private string _switchAutoWoodButtonText = "启动"; [ObservableProperty] private string[] _combatStrategyList; [ObservableProperty] private int _autoDomainRoundNum; + [ObservableProperty] private bool _switchAutoDomainEnabled; [ObservableProperty] private string _switchAutoDomainButtonText = "启动"; + [ObservableProperty] private bool _switchAutoFightEnabled; [ObservableProperty] private string _switchAutoFightButtonText = "启动"; [ObservableProperty] private string _switchAutoTrackButtonText = "启动"; [ObservableProperty] private string _switchAutoTrackPathButtonText = "启动"; @@ -88,6 +92,10 @@ public partial class TaskSettingsPageViewModel : ObservableObject, INavigationAw private async Task OnStopSoloTask() { CancellationContext.Instance.Cancel(); + SwitchAutoGeniusInvokationEnabled = false; + SwitchAutoWoodEnabled = false; + SwitchAutoDomainEnabled = false; + SwitchAutoFightEnabled = false; await Task.Delay(800); } @@ -139,8 +147,10 @@ public partial class TaskSettingsPageViewModel : ObservableObject, INavigationAw var content = await File.ReadAllTextAsync(path); + SwitchAutoGeniusInvokationEnabled = true; await new TaskRunner(DispatcherTimerOperationEnum.UseSelfCaptureImage) .RunSoloTaskAsync(new AutoGeniusInvokationTask(new GeniusInvokationTaskParam(content))); + SwitchAutoGeniusInvokationEnabled = false; } [RelayCommand] @@ -152,8 +162,10 @@ public partial class TaskSettingsPageViewModel : ObservableObject, INavigationAw [RelayCommand] public async Task OnSwitchAutoWood() { + SwitchAutoWoodEnabled = true; await new TaskRunner(DispatcherTimerOperationEnum.UseSelfCaptureImage) .RunSoloTaskAsync(new AutoWoodTask(new WoodTaskParam(AutoWoodRoundNum, AutoWoodDailyMaxCount))); + SwitchAutoWoodEnabled = false; } [RelayCommand] @@ -176,8 +188,10 @@ public partial class TaskSettingsPageViewModel : ObservableObject, INavigationAw PickDropsAfterFightEnabled = Config.AutoFightConfig.PickDropsAfterFightEnabled }; + SwitchAutoFightEnabled = true; await new TaskRunner(DispatcherTimerOperationEnum.UseCacheImageWithTrigger) .RunSoloTaskAsync(new AutoFightTask(param)); + SwitchAutoFightEnabled = false; } [RelayCommand] @@ -193,8 +207,10 @@ public partial class TaskSettingsPageViewModel : ObservableObject, INavigationAw { return; } + SwitchAutoDomainEnabled = true; await new TaskRunner(DispatcherTimerOperationEnum.UseCacheImage) .RunSoloTaskAsync(new AutoDomainTask(new AutoDomainParam(AutoDomainRoundNum, path))); + SwitchAutoDomainEnabled = false; } private bool GetFightStrategy(out string path)