From e26fb9b7926fa0e399ab02e9dd1dc1578997f866 Mon Sep 17 00:00:00 2001 From: Hakoyu Date: Fri, 12 Jan 2024 22:17:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20VPet.Solution?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SettingEditor/InteractiveSettingModel.cs | 39 ++++++++---- VPet.Solution/Styles.xaml | 16 ++--- VPet.Solution/Utils/ReflectionUtils.cs | 20 ++++++- .../SettingEditor/SettingWindowVM.cs | 59 ++++++++++++------- .../SettingEditor/InteractiveSettingPage.xaml | 49 ++++++++------- 5 files changed, 121 insertions(+), 62 deletions(-) diff --git a/VPet.Solution/Models/SettingEditor/InteractiveSettingModel.cs b/VPet.Solution/Models/SettingEditor/InteractiveSettingModel.cs index 725d4e3..5527351 100644 --- a/VPet.Solution/Models/SettingEditor/InteractiveSettingModel.cs +++ b/VPet.Solution/Models/SettingEditor/InteractiveSettingModel.cs @@ -1,5 +1,6 @@ using HKW.HKWUtils.Observable; using System.Collections.ObjectModel; +using System.ComponentModel; using VPet_Simulator.Core; namespace VPet.Solution.Models.SettingEditor; @@ -165,11 +166,12 @@ public class InteractiveSettingModel : ObservableClass #endregion #region SmartMoveInterval - private int _smartMoveInterval; + private int _smartMoveInterval = 0; /// /// 智能移动周期 (秒) /// + [DefaultValue(1)] [ReflectionProperty(nameof(VPet_Simulator.Windows.Interface.Setting.SmartMoveInterval))] [ReflectionPropertyConverter(typeof(SecondToMinuteConverter))] public int SmartMoveInterval @@ -178,7 +180,7 @@ public class InteractiveSettingModel : ObservableClass set => SetProperty(ref _smartMoveInterval, value); } - public static ObservableCollection SmartMoveIntervals = + public static ObservableCollection SmartMoveIntervals { get; } = new() { 1, 2, 5, 10, 20, 30, 40, 50, 60 }; #endregion @@ -197,13 +199,14 @@ public class InteractiveSettingModel : ObservableClass #endregion #region MusicCatch - private double _musicCatch; + private int _musicCatch; /// /// 当实时播放音量达到该值时运行音乐动作 /// [ReflectionProperty(nameof(VPet_Simulator.Windows.Interface.Setting.MusicCatch))] - public double MusicCatch + [ReflectionPropertyConverter(typeof(DoubleToInt32Converter))] + public int MusicCatch { get => _musicCatch; set => SetProperty(ref _musicCatch, value); @@ -211,13 +214,14 @@ public class InteractiveSettingModel : ObservableClass #endregion #region MusicMax - private double _musicMax; + private int _musicMax; /// /// 当实时播放音量达到该值时运行特殊音乐动作 /// [ReflectionProperty(nameof(VPet_Simulator.Windows.Interface.Setting.MusicMax))] - public double MusicMax + [ReflectionPropertyConverter(typeof(DoubleToInt32Converter))] + public int MusicMax { get => _musicMax; set => SetProperty(ref _musicMax, value); @@ -281,14 +285,27 @@ public class SecondToMinuteConverter : ReflectionConverterBase { public override int Convert(int sourceValue) { - if (sourceValue == 30) - return 1; - else - return sourceValue / 60; + return sourceValue * 60; } public override int ConvertBack(int targetValue) { - return targetValue * 60; + if (targetValue == 30) + return 1; + else + return targetValue / 60; + } +} + +public class DoubleToInt32Converter : ReflectionConverterBase +{ + public override double Convert(int sourceValue) + { + return sourceValue; + } + + public override int ConvertBack(double targetValue) + { + return System.Convert.ToInt32(targetValue); } } diff --git a/VPet.Solution/Styles.xaml b/VPet.Solution/Styles.xaml index 0b1c5b7..98cecb4 100644 --- a/VPet.Solution/Styles.xaml +++ b/VPet.Solution/Styles.xaml @@ -56,7 +56,7 @@ x:Key="TextBlock_BaseStyle" BasedOn="{StaticResource {x:Type TextBlock}}" TargetType="TextBlock"> - + @@ -139,7 +139,7 @@ \ No newline at end of file diff --git a/VPet.Solution/Utils/ReflectionUtils.cs b/VPet.Solution/Utils/ReflectionUtils.cs index 0c2cebb..bb87422 100644 --- a/VPet.Solution/Utils/ReflectionUtils.cs +++ b/VPet.Solution/Utils/ReflectionUtils.cs @@ -14,6 +14,8 @@ public static class ReflectionUtils private static readonly BindingFlags _propertyBindingFlags = BindingFlags.Instance | BindingFlags.Public; + private static readonly Dictionary _reflectionConverters = new(); + /// /// 类型信息 /// @@ -102,7 +104,19 @@ public static class ReflectionUtils is ReflectionPropertyConverterAttribute propertyConverterAttribute ) { - propertyInfo.Converter = propertyConverterAttribute.Converter; + if ( + _reflectionConverters.TryGetValue( + propertyConverterAttribute.ConverterType, + out var converter + ) + is false + ) + converter = _reflectionConverters[propertyConverterAttribute.ConverterType] = + (IReflectionConverter) + TypeAccessor + .Create(propertyConverterAttribute.ConverterType) + .CreateNew(); + propertyInfo.Converter = converter; } objectInfo.PropertyInfos[property.Name] = propertyInfo; } @@ -189,11 +203,11 @@ public class ReflectionPropertyConverterAttribute : Attribute /// /// 反射转换器 /// - public IReflectionConverter Converter { get; } + public Type ConverterType { get; } public ReflectionPropertyConverterAttribute(Type converterType) { - Converter = (IReflectionConverter)TypeAccessor.Create(converterType).CreateNew(); + ConverterType = converterType; } } diff --git a/VPet.Solution/ViewModels/SettingEditor/SettingWindowVM.cs b/VPet.Solution/ViewModels/SettingEditor/SettingWindowVM.cs index a0f7e12..f478f03 100644 --- a/VPet.Solution/ViewModels/SettingEditor/SettingWindowVM.cs +++ b/VPet.Solution/ViewModels/SettingEditor/SettingWindowVM.cs @@ -77,9 +77,8 @@ public class SettingWindowVM : ObservableClass { Current = this; ShowSettings = _settings; + LoadSettings(); - foreach (var s in LoadSettings()) - _settings.Add(s); PropertyChanged += MainWindowVM_PropertyChanged; OpenFileCommand.ExecuteCommand += OpenFileCommand_ExecuteCommand; OpenFileInExplorerCommand.ExecuteCommand += OpenFileInExplorerCommand_ExecuteCommand; @@ -159,27 +158,47 @@ public class SettingWindowVM : ObservableClass } } - public static IEnumerable LoadSettings() + private void LoadSettings() { - foreach ( - var file in Directory - .EnumerateFiles(Environment.CurrentDirectory) - .Where( - (s) => - { - if (s.EndsWith(".lps") is false) - return false; - return Path.GetFileName(s).StartsWith("Setting"); - } - ) - ) + foreach (var file in GetSettingFiles()) { - var setting = new Setting(File.ReadAllText(file)); - yield return new SettingModel(setting) + var fileName = Path.GetFileNameWithoutExtension(file); + try { - Name = Path.GetFileNameWithoutExtension(file), - FilePath = file - }; + var setting = new Setting(File.ReadAllText(file)); + var settingModel = new SettingModel(setting) { Name = fileName, FilePath = file }; + _settings.Add(settingModel); + } + catch (Exception ex) + { + if ( + MessageBox.Show( + "设置载入失败, 是否强制载入并重置\n[是]: 载入并重置\t[否]: 取消载入\n名称: {0}\n路径: {1}\n{2}".Translate( + fileName, + file, + ex.ToString() + ), + "载入设置出错".Translate(), + MessageBoxButton.YesNo, + MessageBoxImage.Warning + ) is MessageBoxResult.Yes + ) + _settings.Add(new SettingModel() { Name = fileName, FilePath = file }); + } } } + + private static IEnumerable GetSettingFiles() + { + return Directory + .EnumerateFiles(Environment.CurrentDirectory) + .Where( + (s) => + { + if (s.EndsWith(".lps") is false) + return false; + return Path.GetFileName(s).StartsWith("Setting"); + } + ); + } } diff --git a/VPet.Solution/Views/SettingEditor/InteractiveSettingPage.xaml b/VPet.Solution/Views/SettingEditor/InteractiveSettingPage.xaml index 5c00028..9e3b4b1 100644 --- a/VPet.Solution/Views/SettingEditor/InteractiveSettingPage.xaml +++ b/VPet.Solution/Views/SettingEditor/InteractiveSettingPage.xaml @@ -54,6 +54,7 @@ - + Content="{ll:Str 秒}" + Style="{DynamicResource Label_BaseStyle}" /> @@ -108,22 +110,23 @@ LargeChange="1" Maximum="60" Minimum="5" - SmallChange=".5" + SmallChange="0.5" Style="{DynamicResource Slider_BaseStyle}" TickFrequency="0.1" Value="{Binding InteractiveSetting.LogicInterval}" /> - + Content="{ll:Str 秒}" + Style="{DynamicResource Label_BaseStyle}" /> @@ -143,6 +146,7 @@ - + Content="{ll:Str 秒}" + Style="{DynamicResource Label_BaseStyle}" /> - + Content="{ll:Str 分钟}" + Style="{DynamicResource Label_BaseStyle}" />