更新 VPet.Solution

This commit is contained in:
Hakoyu 2024-01-12 22:17:50 +08:00
parent cae830a086
commit e26fb9b792
5 changed files with 121 additions and 62 deletions

View File

@ -1,5 +1,6 @@
using HKW.HKWUtils.Observable; using HKW.HKWUtils.Observable;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.ComponentModel;
using VPet_Simulator.Core; using VPet_Simulator.Core;
namespace VPet.Solution.Models.SettingEditor; namespace VPet.Solution.Models.SettingEditor;
@ -165,11 +166,12 @@ public class InteractiveSettingModel : ObservableClass<InteractiveSettingModel>
#endregion #endregion
#region SmartMoveInterval #region SmartMoveInterval
private int _smartMoveInterval; private int _smartMoveInterval = 0;
/// <summary> /// <summary>
/// 智能移动周期 (秒) /// 智能移动周期 (秒)
/// </summary> /// </summary>
[DefaultValue(1)]
[ReflectionProperty(nameof(VPet_Simulator.Windows.Interface.Setting.SmartMoveInterval))] [ReflectionProperty(nameof(VPet_Simulator.Windows.Interface.Setting.SmartMoveInterval))]
[ReflectionPropertyConverter(typeof(SecondToMinuteConverter))] [ReflectionPropertyConverter(typeof(SecondToMinuteConverter))]
public int SmartMoveInterval public int SmartMoveInterval
@ -178,7 +180,7 @@ public class InteractiveSettingModel : ObservableClass<InteractiveSettingModel>
set => SetProperty(ref _smartMoveInterval, value); set => SetProperty(ref _smartMoveInterval, value);
} }
public static ObservableCollection<int> SmartMoveIntervals = public static ObservableCollection<int> SmartMoveIntervals { get; } =
new() { 1, 2, 5, 10, 20, 30, 40, 50, 60 }; new() { 1, 2, 5, 10, 20, 30, 40, 50, 60 };
#endregion #endregion
@ -197,13 +199,14 @@ public class InteractiveSettingModel : ObservableClass<InteractiveSettingModel>
#endregion #endregion
#region MusicCatch #region MusicCatch
private double _musicCatch; private int _musicCatch;
/// <summary> /// <summary>
/// 当实时播放音量达到该值时运行音乐动作 /// 当实时播放音量达到该值时运行音乐动作
/// </summary> /// </summary>
[ReflectionProperty(nameof(VPet_Simulator.Windows.Interface.Setting.MusicCatch))] [ReflectionProperty(nameof(VPet_Simulator.Windows.Interface.Setting.MusicCatch))]
public double MusicCatch [ReflectionPropertyConverter(typeof(DoubleToInt32Converter))]
public int MusicCatch
{ {
get => _musicCatch; get => _musicCatch;
set => SetProperty(ref _musicCatch, value); set => SetProperty(ref _musicCatch, value);
@ -211,13 +214,14 @@ public class InteractiveSettingModel : ObservableClass<InteractiveSettingModel>
#endregion #endregion
#region MusicMax #region MusicMax
private double _musicMax; private int _musicMax;
/// <summary> /// <summary>
/// 当实时播放音量达到该值时运行特殊音乐动作 /// 当实时播放音量达到该值时运行特殊音乐动作
/// </summary> /// </summary>
[ReflectionProperty(nameof(VPet_Simulator.Windows.Interface.Setting.MusicMax))] [ReflectionProperty(nameof(VPet_Simulator.Windows.Interface.Setting.MusicMax))]
public double MusicMax [ReflectionPropertyConverter(typeof(DoubleToInt32Converter))]
public int MusicMax
{ {
get => _musicMax; get => _musicMax;
set => SetProperty(ref _musicMax, value); set => SetProperty(ref _musicMax, value);
@ -281,14 +285,27 @@ public class SecondToMinuteConverter : ReflectionConverterBase<int, int>
{ {
public override int Convert(int sourceValue) public override int Convert(int sourceValue)
{ {
if (sourceValue == 30) return sourceValue * 60;
return 1;
else
return sourceValue / 60;
} }
public override int ConvertBack(int targetValue) public override int ConvertBack(int targetValue)
{ {
return targetValue * 60; if (targetValue == 30)
return 1;
else
return targetValue / 60;
}
}
public class DoubleToInt32Converter : ReflectionConverterBase<int, double>
{
public override double Convert(int sourceValue)
{
return sourceValue;
}
public override int ConvertBack(double targetValue)
{
return System.Convert.ToInt32(targetValue);
} }
} }

View File

@ -56,7 +56,7 @@
x:Key="TextBlock_BaseStyle" x:Key="TextBlock_BaseStyle"
BasedOn="{StaticResource {x:Type TextBlock}}" BasedOn="{StaticResource {x:Type TextBlock}}"
TargetType="TextBlock"> TargetType="TextBlock">
<Setter Property="Margin" Value="10,5,10,5" /> <Setter Property="Margin" Value="5" />
<Setter Property="VerticalAlignment" Value="Center" /> <Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="HorizontalAlignment" Value="Center" /> <Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="ToolTip" Value="{Binding Text, RelativeSource={RelativeSource Mode=Self}}" /> <Setter Property="ToolTip" Value="{Binding Text, RelativeSource={RelativeSource Mode=Self}}" />
@ -139,7 +139,7 @@
</Style> </Style>
<Style x:Key="Switch_BaseStyle" TargetType="pu:Switch"> <Style x:Key="Switch_BaseStyle" TargetType="pu:Switch">
<Setter Property="HorizontalAlignment" Value="Left" /> <Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="Margin" Value="10,5,10,5" /> <Setter Property="Margin" Value="5" />
<Setter Property="Background" Value="{x:Null}" /> <Setter Property="Background" Value="{x:Null}" />
<Setter Property="BorderBrush" Value="{DynamicResource PrimaryDark}" /> <Setter Property="BorderBrush" Value="{DynamicResource PrimaryDark}" />
<Setter Property="BoxHeight" Value="20" /> <Setter Property="BoxHeight" Value="20" />
@ -152,7 +152,7 @@
</Style> </Style>
<Style x:Key="Label_BaseStyle" TargetType="Label"> <Style x:Key="Label_BaseStyle" TargetType="Label">
<Setter Property="FontSize" Value="14" /> <Setter Property="FontSize" Value="14" />
<Setter Property="Margin" Value="10,0,10,0" /> <Setter Property="Margin" Value="10,5,10,5" />
<Setter Property="Height" Value="30" /> <Setter Property="Height" Value="30" />
<Setter Property="HorizontalContentAlignment" Value="Center" /> <Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="VerticalContentAlignment" Value="Center" /> <Setter Property="VerticalContentAlignment" Value="Center" />
@ -160,7 +160,7 @@
</Style> </Style>
<Style x:Key="NumberInput_BaseStyle" TargetType="pu:NumberInput"> <Style x:Key="NumberInput_BaseStyle" TargetType="pu:NumberInput">
<Setter Property="Height" Value="30" /> <Setter Property="Height" Value="30" />
<Setter Property="Margin" Value="10,5,10,5" /> <Setter Property="Margin" Value="5" />
<Setter Property="VerticalAlignment" Value="Center" /> <Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Background" Value="{x:Null}" /> <Setter Property="Background" Value="{x:Null}" />
<Setter Property="FontSize" Value="14" /> <Setter Property="FontSize" Value="14" />
@ -171,8 +171,8 @@
x:Key="Button_BaseStyle" x:Key="Button_BaseStyle"
BasedOn="{StaticResource {x:Type Button}}" BasedOn="{StaticResource {x:Type Button}}"
TargetType="Button"> TargetType="Button">
<Setter Property="Margin" Value="10,5,10,5" /> <Setter Property="Margin" Value="5" />
<Setter Property="Padding" Value="10,5,10,5" /> <Setter Property="Padding" Value="5" />
<Setter Property="HorizontalAlignment" Value="Center" /> <Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="VerticalAlignment" Value="Center" /> <Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="pu:ButtonHelper.CornerRadius" Value="4" /> <Setter Property="pu:ButtonHelper.CornerRadius" Value="4" />
@ -184,7 +184,7 @@
x:Key="ComboBox_BaseStyle" x:Key="ComboBox_BaseStyle"
BasedOn="{StaticResource StandardComboBoxStyle}" BasedOn="{StaticResource StandardComboBoxStyle}"
TargetType="ComboBox"> TargetType="ComboBox">
<Setter Property="Margin" Value="10,5,10,5" /> <Setter Property="Margin" Value="5" />
</Style> </Style>
<Style <Style
x:Key="Expander_BaseStyle" x:Key="Expander_BaseStyle"
@ -202,7 +202,7 @@
BasedOn="{StaticResource StandardSliderStyle}" BasedOn="{StaticResource StandardSliderStyle}"
TargetType="Slider"> TargetType="Slider">
<Setter Property="IsSnapToTickEnabled" Value="True" /> <Setter Property="IsSnapToTickEnabled" Value="True" />
<Setter Property="Margin" Value="10,5,10,5" /> <Setter Property="Margin" Value="5" />
<Setter Property="VerticalAlignment" Value="Center" /> <Setter Property="VerticalAlignment" Value="Center" />
</Style> </Style>
</ResourceDictionary> </ResourceDictionary>

View File

@ -14,6 +14,8 @@ public static class ReflectionUtils
private static readonly BindingFlags _propertyBindingFlags = private static readonly BindingFlags _propertyBindingFlags =
BindingFlags.Instance | BindingFlags.Public; BindingFlags.Instance | BindingFlags.Public;
private static readonly Dictionary<Type, IReflectionConverter> _reflectionConverters = new();
/// <summary> /// <summary>
/// 类型信息 /// 类型信息
/// <para> /// <para>
@ -102,7 +104,19 @@ public static class ReflectionUtils
is ReflectionPropertyConverterAttribute propertyConverterAttribute 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; objectInfo.PropertyInfos[property.Name] = propertyInfo;
} }
@ -189,11 +203,11 @@ public class ReflectionPropertyConverterAttribute : Attribute
/// <summary> /// <summary>
/// 反射转换器 /// 反射转换器
/// </summary> /// </summary>
public IReflectionConverter Converter { get; } public Type ConverterType { get; }
public ReflectionPropertyConverterAttribute(Type converterType) public ReflectionPropertyConverterAttribute(Type converterType)
{ {
Converter = (IReflectionConverter)TypeAccessor.Create(converterType).CreateNew(); ConverterType = converterType;
} }
} }

View File

@ -77,9 +77,8 @@ public class SettingWindowVM : ObservableClass<SettingWindowVM>
{ {
Current = this; Current = this;
ShowSettings = _settings; ShowSettings = _settings;
LoadSettings();
foreach (var s in LoadSettings())
_settings.Add(s);
PropertyChanged += MainWindowVM_PropertyChanged; PropertyChanged += MainWindowVM_PropertyChanged;
OpenFileCommand.ExecuteCommand += OpenFileCommand_ExecuteCommand; OpenFileCommand.ExecuteCommand += OpenFileCommand_ExecuteCommand;
OpenFileInExplorerCommand.ExecuteCommand += OpenFileInExplorerCommand_ExecuteCommand; OpenFileInExplorerCommand.ExecuteCommand += OpenFileInExplorerCommand_ExecuteCommand;
@ -159,10 +158,39 @@ public class SettingWindowVM : ObservableClass<SettingWindowVM>
} }
} }
public static IEnumerable<SettingModel> LoadSettings() private void LoadSettings()
{ {
foreach ( foreach (var file in GetSettingFiles())
var file in Directory {
var fileName = Path.GetFileNameWithoutExtension(file);
try
{
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<string> GetSettingFiles()
{
return Directory
.EnumerateFiles(Environment.CurrentDirectory) .EnumerateFiles(Environment.CurrentDirectory)
.Where( .Where(
(s) => (s) =>
@ -171,15 +199,6 @@ public class SettingWindowVM : ObservableClass<SettingWindowVM>
return false; return false;
return Path.GetFileName(s).StartsWith("Setting"); return Path.GetFileName(s).StartsWith("Setting");
} }
) );
)
{
var setting = new Setting(File.ReadAllText(file));
yield return new SettingModel(setting)
{
Name = Path.GetFileNameWithoutExtension(file),
FilePath = file
};
}
} }
} }

View File

@ -54,6 +54,7 @@
<!-- TODO: ComboBox使用内部数据 --> <!-- TODO: ComboBox使用内部数据 -->
<ComboBox <ComboBox
Grid.Column="2" Grid.Column="2"
Margin="0,5,10,5"
pu:ComboBoxHelper.Watermark="{ll:Str '当关闭数据计算时\&#13;桌宠显示的状态'}" pu:ComboBoxHelper.Watermark="{ll:Str '当关闭数据计算时\&#13;桌宠显示的状态'}"
IsEnabled="{Binding IsChecked, ElementName=Switch_EnablePetState, Converter={StaticResource BoolInverter}}" IsEnabled="{Binding IsChecked, ElementName=Switch_EnablePetState, Converter={StaticResource BoolInverter}}"
ItemsSource="{Binding InteractiveSetting.ModeTypes}" ItemsSource="{Binding InteractiveSetting.ModeTypes}"
@ -75,23 +76,24 @@
VerticalAlignment="Center" VerticalAlignment="Center"
LargeChange="0.1" LargeChange="0.1"
Maximum="5" Maximum="5"
Minimum="0.05" Minimum="0.5"
SmallChange=".05" SmallChange="0.5"
Style="{DynamicResource Slider_BaseStyle}" Style="{DynamicResource Slider_BaseStyle}"
TickFrequency="0.01" TickFrequency="0.5"
Value="{Binding InteractiveSetting.PressLength}" /> Value="{Binding InteractiveSetting.PressLength}" />
<pu:NumberInput <pu:NumberInput
Grid.Column="2" Grid.Column="2"
MinWidth="80"
Interval="0.1" Interval="0.1"
Maximum="5" Maximum="5"
Minimum="0.05" Minimum="0.05"
Style="{DynamicResource NumberInput_BaseStyle}" Style="{DynamicResource NumberInput_BaseStyle}"
Value="{Binding Value, ElementName=PressLengthSlider}" /> Value="{Binding Value, ElementName=PressLengthSlider}" />
<TextBlock <Label
Grid.Column="3" Grid.Column="3"
Margin="0,5,10,5" Margin="0,5,10,5"
Style="{DynamicResource TextBlock_BaseStyle}" Content="{ll:Str 秒}"
Text="{ll:Str 秒}" /> Style="{DynamicResource Label_BaseStyle}" />
</Grid> </Grid>
<Grid MinHeight="40"> <Grid MinHeight="40">
<Grid.ColumnDefinitions> <Grid.ColumnDefinitions>
@ -108,22 +110,23 @@
LargeChange="1" LargeChange="1"
Maximum="60" Maximum="60"
Minimum="5" Minimum="5"
SmallChange=".5" SmallChange="0.5"
Style="{DynamicResource Slider_BaseStyle}" Style="{DynamicResource Slider_BaseStyle}"
TickFrequency="0.1" TickFrequency="0.1"
Value="{Binding InteractiveSetting.LogicInterval}" /> Value="{Binding InteractiveSetting.LogicInterval}" />
<pu:NumberInput <pu:NumberInput
Grid.Column="2" Grid.Column="2"
MinWidth="80"
Interval="0.5" Interval="0.5"
Maximum="60" Maximum="60"
Minimum="5" Minimum="5"
Style="{DynamicResource NumberInput_BaseStyle}" Style="{DynamicResource NumberInput_BaseStyle}"
Value="{Binding Value, ElementName=Slider_Cal}" /> Value="{Binding Value, ElementName=Slider_Cal}" />
<TextBlock <Label
Grid.Column="3" Grid.Column="3"
Margin="0,5,10,5" Margin="0,5,10,5"
Style="{DynamicResource TextBlock_BaseStyle}" Content="{ll:Str 秒}"
Text="{ll:Str 秒}" /> Style="{DynamicResource Label_BaseStyle}" />
</Grid> </Grid>
<Grid MinHeight="40" ToolTip="{ll:Str 互动周期决定在交互结束后大约经历多少计算间隔后再次进行自主行动}"> <Grid MinHeight="40" ToolTip="{ll:Str 互动周期决定在交互结束后大约经历多少计算间隔后再次进行自主行动}">
<Grid.ColumnDefinitions> <Grid.ColumnDefinitions>
@ -143,6 +146,7 @@
</Grid.RowDefinitions> </Grid.RowDefinitions>
<Slider <Slider
x:Name="Slider_Interaction" x:Name="Slider_Interaction"
d:Value="1000"
LargeChange="5" LargeChange="5"
Maximum="1000" Maximum="1000"
Minimum="30" Minimum="30"
@ -153,16 +157,17 @@
Value="{Binding InteractiveSetting.InteractionCycle}" /> Value="{Binding InteractiveSetting.InteractionCycle}" />
<pu:NumberInput <pu:NumberInput
Grid.Column="1" Grid.Column="1"
MinWidth="80"
Interval="1" Interval="1"
Maximum="1000" Maximum="1000"
Minimum="30" Minimum="30"
Style="{DynamicResource NumberInput_BaseStyle}" Style="{DynamicResource NumberInput_BaseStyle}"
Value="{Binding ElementName=Slider_Interaction, Path=Value}" /> Value="{Binding ElementName=Slider_Interaction, Path=Value}" />
<TextBlock <Label
Grid.Column="2" Grid.Column="2"
Margin="0,5,10,5" Margin="0,5,10,5"
Style="{DynamicResource TextBlock_BaseStyle}" Content="{ll:Str 秒}"
Text="{ll:Str 秒}" /> Style="{DynamicResource Label_BaseStyle}" />
<TextBlock <TextBlock
Grid.Row="1" Grid.Row="1"
Grid.ColumnSpan="3" Grid.ColumnSpan="3"
@ -204,10 +209,10 @@
SelectedItem="{Binding InteractiveSetting.SmartMoveInterval}" SelectedItem="{Binding InteractiveSetting.SmartMoveInterval}"
Style="{DynamicResource StandardComboBoxStyle}" Style="{DynamicResource StandardComboBoxStyle}"
ToolTip="{ll:Str 智能移动判断时间间隔}" /> ToolTip="{ll:Str 智能移动判断时间间隔}" />
<TextBlock <Label
Grid.Column="4" Grid.Column="4"
Style="{DynamicResource TextBlock_BaseStyle}" Content="{ll:Str 分钟}"
Text="{ll:Str 分钟}" /> Style="{DynamicResource Label_BaseStyle}" />
</Grid> </Grid>
<!-- TODO: 移动范围设置 --> <!-- TODO: 移动范围设置 -->
<!--<Grid MinHeight="40"> <!--<Grid MinHeight="40">
@ -286,16 +291,18 @@
Value="{Binding InteractiveSetting.MusicCatch}" /> Value="{Binding InteractiveSetting.MusicCatch}" />
<pu:NumberInput <pu:NumberInput
Grid.Column="2" Grid.Column="2"
MinWidth="80"
Interval="1" Interval="1"
Maximum="100" Maximum="100"
Minimum="0" Minimum="0"
Style="{DynamicResource NumberInput_BaseStyle}" Style="{DynamicResource NumberInput_BaseStyle}"
ToolTip="{ll:Str 当实时播放音量达到该值时运行特殊音乐动作}" ToolTip="{ll:Str 当实时播放音量达到该值时运行特殊音乐动作}"
Value="{Binding Value, ElementName=VoiceCatchSilder}" /> Value="{Binding Value, ElementName=VoiceCatchSilder}" />
<TextBlock <Label
Grid.Column="3" Grid.Column="3"
Margin="0,5,10,5" Margin="0,5,10,5"
Text="%" /> Content="%"
Style="{DynamicResource Label_BaseStyle}" />
</Grid> </Grid>
<Grid MinHeight="40"> <Grid MinHeight="40">
<Grid.ColumnDefinitions> <Grid.ColumnDefinitions>
@ -321,6 +328,7 @@
Value="{Binding InteractiveSetting.MusicMax}" /> Value="{Binding InteractiveSetting.MusicMax}" />
<pu:NumberInput <pu:NumberInput
Grid.Column="2" Grid.Column="2"
MinWidth="80"
Foreground="{DynamicResource DARKPrimaryDarker}" Foreground="{DynamicResource DARKPrimaryDarker}"
Interval="1" Interval="1"
Maximum="100" Maximum="100"
@ -328,10 +336,11 @@
Style="{DynamicResource NumberInput_BaseStyle}" Style="{DynamicResource NumberInput_BaseStyle}"
ToolTip="{ll:Str 当实时播放音量达到该值时运行特殊音乐动作}" ToolTip="{ll:Str 当实时播放音量达到该值时运行特殊音乐动作}"
Value="{Binding Value, ElementName=VoiceMaxSilder}" /> Value="{Binding Value, ElementName=VoiceMaxSilder}" />
<TextBlock <Label
Grid.Column="3" Grid.Column="3"
Margin="0,5,10,5" Margin="0,5,10,5"
Text="%" /> Content="%"
Style="{DynamicResource Label_BaseStyle}" />
</Grid> </Grid>
</StackPanel> </StackPanel>
</ScrollViewer> </ScrollViewer>