feat: commit content which I don't know when I create them
This commit is contained in:
@@ -1,4 +1,8 @@
|
||||
using System.Configuration;
|
||||
// Import LanguageExt globally
|
||||
global using LanguageExt;
|
||||
global using static LanguageExt.Prelude;
|
||||
|
||||
using System.Configuration;
|
||||
using System.Data;
|
||||
using System.Windows;
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net8.0-windows</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<UseWPF>true</UseWPF>
|
||||
<ApplicationIcon>Assets\App.ico</ApplicationIcon>
|
||||
<ApplicationManifest>app.manifest</ApplicationManifest>
|
||||
@@ -15,6 +16,7 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.1" />
|
||||
<PackageReference Include="DotNetZip" Version="1.9.1.8" />
|
||||
<PackageReference Include="LanguageExt.Core" Version="4.4.9" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -4,20 +4,20 @@ using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Data;
|
||||
|
||||
namespace BallanceTasEditor.Converters {
|
||||
public class GenericConverter<TIn, TOut> : IValueConverter {
|
||||
|
||||
private readonly Func<TIn, TOut> converter;
|
||||
|
||||
public GenericConverter(Func<TIn, TOut> converter) { this.converter = converter; }
|
||||
[ValueConversion(typeof(int), typeof(string))]
|
||||
public class FpsConverter : IValueConverter {
|
||||
public static FpsConverter Instance = new FpsConverter();
|
||||
|
||||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
|
||||
if (value is TIn t) {
|
||||
return converter(t);
|
||||
if (value is int tv) {
|
||||
if (tv <= 0) return DependencyProperty.UnsetValue;
|
||||
else return Utils.FpsConverter.ToDelta(tv).ToString();
|
||||
} else {
|
||||
return Binding.DoNothing;
|
||||
return DependencyProperty.UnsetValue;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,8 +26,25 @@ namespace BallanceTasEditor.Converters {
|
||||
}
|
||||
}
|
||||
|
||||
public static class ConverterWarehouse {
|
||||
public static readonly GenericConverter<int, string> FpsConverter =
|
||||
new GenericConverter<int, string>((v) => Utils.FpsConverter.ToDelta(v).ToString());
|
||||
/// <summary>
|
||||
/// 将IsEnable转换为Visibility。
|
||||
/// </summary>
|
||||
[ValueConversion(typeof(bool), typeof(Visibility))]
|
||||
public class VisibilityConverter : IValueConverter {
|
||||
public static VisibilityConverter Instance = new VisibilityConverter();
|
||||
|
||||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
|
||||
if (value is bool bv) {
|
||||
if (bv) return Visibility.Visible;
|
||||
else return Visibility.Collapsed;
|
||||
} else {
|
||||
return DependencyProperty.UnsetValue;
|
||||
}
|
||||
}
|
||||
|
||||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
|
||||
return Binding.DoNothing;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
35
BallanceTasEditor/Converters/StringifyConverter.cs
Normal file
35
BallanceTasEditor/Converters/StringifyConverter.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Data;
|
||||
|
||||
namespace BallanceTasEditor.Converters {
|
||||
|
||||
[ValueConversion(typeof(int?), typeof(string))]
|
||||
public class StringifyIntegerConverter : IValueConverter {
|
||||
public static StringifyIntegerConverter Instance = new StringifyIntegerConverter();
|
||||
|
||||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
|
||||
if (value is null) {
|
||||
return "";
|
||||
} else if (value is int iv) {
|
||||
return iv.ToString();
|
||||
} else {
|
||||
return DependencyProperty.UnsetValue;
|
||||
}
|
||||
}
|
||||
|
||||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
|
||||
if (value is string s) {
|
||||
if (int.TryParse(s, out int iv)) return iv;
|
||||
else return null;
|
||||
} else {
|
||||
return DependencyProperty.UnsetValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
97
BallanceTasEditor/Utils/Validator.cs
Normal file
97
BallanceTasEditor/Utils/Validator.cs
Normal file
@@ -0,0 +1,97 @@
|
||||
using LanguageExt.Common;
|
||||
using LanguageExt.TypeClasses;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BallanceTasEditor.Utils {
|
||||
|
||||
// YYC MARK:
|
||||
// 这些验证器尽管服务于UI,但是并不遵循WPF或者CommunityToolkit.Mvvm的Validator模型,
|
||||
// 所以我把他们放在这里。
|
||||
|
||||
/// <summary>
|
||||
/// 验证器接口。
|
||||
/// </summary>
|
||||
/// <typeparam name="TIn">验证器接受的待验证数据的类型。</typeparam>
|
||||
/// <typeparam name="TOut">验证器验证完毕后,会输出的类型。</typeparam>
|
||||
public interface IValidator<TIn, TOut> {
|
||||
/// <summary>
|
||||
/// 验证给定数据是否正确。
|
||||
/// </summary>
|
||||
/// <param name="data">要验证的数据。</param>
|
||||
/// <returns>数据正确,或对应的错误信息。</returns>
|
||||
ValidationResult Validate(TIn data);
|
||||
|
||||
/// <summary>
|
||||
/// 获取验证无误数据转换后的数据。
|
||||
/// </summary>
|
||||
/// <param name="data">验证无误,用于获取输出的数据。</param>
|
||||
/// <returns>输出的数据。</returns>
|
||||
/// <exception cref="ArgumentException">给定数据验证时出现错误。</exception>
|
||||
TOut Fetch(TIn data);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 以字符串呈现的数据的通用验证器
|
||||
/// </summary>
|
||||
public abstract class StringifiedValueValidator<T> : IValidator<string, T> where T : notnull {
|
||||
/// <summary>
|
||||
/// 用户需要实现的验证函数。
|
||||
/// </summary>
|
||||
/// <param name="stringifiedValue">要进行验证的数据。</param>
|
||||
/// <returns>验证完毕用于输出的数值,或者验证失败时的错误消息。</returns>
|
||||
protected abstract Either<T, string> ValidateValue(string stringifiedValue);
|
||||
|
||||
public ValidationResult Validate(string data) {
|
||||
return ValidateValue(data).Match(
|
||||
Left: (_) => ValidationResult.Success,
|
||||
Right: (v) => new ValidationResult(v)
|
||||
);
|
||||
}
|
||||
|
||||
public T Fetch(string data) {
|
||||
return ValidateValue(data).Match(
|
||||
Left: (v) => v,
|
||||
Right: (msg) => throw new ArgumentException($"Given value can not pass Validator due to {msg}.")
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class IntegerValidator : StringifiedValueValidator<int> {
|
||||
protected override Either<int, string> ValidateValue(string stringifiedValue) {
|
||||
if (int.TryParse(stringifiedValue, out int val)) {
|
||||
return Left(val);
|
||||
} else {
|
||||
return Right("Given string do not represent any valid number.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class FpsValidator : IntegerValidator {
|
||||
public static FpsValidator Instance = new FpsValidator();
|
||||
|
||||
protected override Either<int, string> ValidateValue(string stringifiedValue) {
|
||||
return base.ValidateValue(stringifiedValue).BindLeft<int>((v) => {
|
||||
if (v <= 0) return Right("Fps must be greater than zero.");
|
||||
else return Left(v);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public class CountValidator : IntegerValidator {
|
||||
public static FpsValidator Instance = new FpsValidator();
|
||||
|
||||
protected override Either<int, string> ValidateValue(string stringifiedValue) {
|
||||
return base.ValidateValue(stringifiedValue).BindLeft<int>((v) => {
|
||||
if (v < 0) return Right("Count can not lower than zero.");
|
||||
else return Left(v);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,23 +1,85 @@
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using BallanceTasEditor.Utils;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BallanceTasEditor.ViewModels {
|
||||
public partial class NewFileDialog : ObservableObject {
|
||||
public NewFileDialog() {
|
||||
Count = 10000;
|
||||
// 132 or 264
|
||||
Fps = 264;
|
||||
|
||||
public struct NewFileDialogResult {
|
||||
public int Count { get; set; }
|
||||
public float DeltaTime { get; set; }
|
||||
}
|
||||
|
||||
[ObservableProperty]
|
||||
private int count;
|
||||
public partial class NewFileDialog : ObservableValidator {
|
||||
public NewFileDialog() {
|
||||
Count = 10000.ToString();
|
||||
// 132 or 264
|
||||
Fps = 264.ToString();
|
||||
}
|
||||
|
||||
// YYC MARK:
|
||||
// 经过无数次的尝试,我发现将int类型绑定到TextBox中所需要涉及的事情太多了,
|
||||
// 尤其是这种绑定到处存在于这个程序中,以至于每次都要重新写一遍。
|
||||
// 也许后面我会做一个只能接受数字输入的文本框,但现在我累了。
|
||||
//
|
||||
// 具体来说,事情是这样的。我一开始就是使用一个int类型的数据,
|
||||
// 然后按照CommunityToolkit.Mvvm的标准,将其应用了Required和Range Attribute,然后将其绑定到了TextBox的Text之上。
|
||||
// 然而最终的效果很奇怪,当我删除TextBox中的所有字符后,绑定什么也没做(后来我才知道要在Required里改一个选项)。
|
||||
// 其次,当我输入不被Range接受的数值时,它仍会将其同步绑定到属性上。比如我输入0,它仍会将其绑定到Fps上,
|
||||
// 进而导致Delta Time的转换显示抛出除以零的错误。这些都不是我想要的。
|
||||
// 同时,这样做的话,我没有办法检测这些字段到底是不是合规的。
|
||||
//
|
||||
// 然后我就将int改写为了int?类型,用null表示当前值不可接受,非null表示绑定的值。
|
||||
// 然后定义了一个转换器,负责在int?和string之间进行转换,并同时去除了所有的验证性Attribute
|
||||
// 然而遗憾的是,这么做也不符合我的要求。当我尝试输入一个数值的时候,如果我输入了任何无效值,
|
||||
// 那么转换器会将其转换为null值,同时将该值赋给Fps,而被赋值的Fps又反向传播,把这个null传递给了转换器,
|
||||
// 转换器将其转换成为空白值,并显示在界面上。这么做的视觉效果就是一旦我输入无效值,整个文本框就会被清空,非常的反人类。
|
||||
// 而且这一问题是不可调和的,我不能在接收到null时,选择不更新文本框的值,因为有可能这个null是我手动放进去的,而不是从转换器接受的。
|
||||
//
|
||||
// 所以最终,我想通了,我决定抛弃将int和TextBox.Text绑定在一起的想法。
|
||||
// 就直接把string绑定到TextBox.Text上,然后再辅以我自己定义的一套可复用验证逻辑。
|
||||
|
||||
[ObservableProperty]
|
||||
private int fps;
|
||||
[CustomValidation(typeof(NewFileDialog), nameof(ValidateCount))]
|
||||
[NotifyCanExecuteChangedFor(nameof(OkCommand))]
|
||||
private string count;
|
||||
|
||||
[ObservableProperty]
|
||||
[CustomValidation(typeof(NewFileDialog), nameof(ValidateFps))]
|
||||
[NotifyCanExecuteChangedFor(nameof(OkCommand))]
|
||||
private string fps;
|
||||
|
||||
public static ValidationResult ValidateCount(string count, ValidationContext context) {
|
||||
return CountValidator.Instance.Validate(count);
|
||||
}
|
||||
public static ValidationResult ValidateFps(string fps, ValidationContext context) {
|
||||
return FpsValidator.Instance.Validate(fps);
|
||||
}
|
||||
|
||||
[RelayCommand(CanExecute = nameof(CanOk))]
|
||||
private void Ok() {
|
||||
|
||||
}
|
||||
private bool CanOk() {
|
||||
return !HasErrors;
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private void Cancel() {
|
||||
|
||||
}
|
||||
|
||||
public NewFileDialogResult ToResult() {
|
||||
return new NewFileDialogResult {
|
||||
Count = CountValidator.Instance.Fetch(Count),
|
||||
DeltaTime = FpsConverter.ToDelta(FpsValidator.Instance.Fetch(Fps)),
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,18 +47,20 @@
|
||||
<TextBlock Margin="5" Grid.Column="1" Grid.Row="2" Text="Delta Time" VerticalAlignment="Center"/>
|
||||
|
||||
<TextBox Margin="5" Padding="3" Grid.Row="0" Grid.Column="2" VerticalAlignment="Center"
|
||||
Text="{Binding Count, Mode=TwoWay}"/>
|
||||
Text="{Binding Count, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
|
||||
<TextBox Margin="5" Padding="3" Grid.Row="1" Grid.Column="2" VerticalAlignment="Center"
|
||||
Text="{Binding Fps, Mode=TwoWay}"/>
|
||||
Text="{Binding Fps, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
|
||||
<TextBlock Margin="5" Padding="3" Grid.Row="2" Grid.Column="2" VerticalAlignment="Center"
|
||||
Text="{Binding Fps, Mode=OneWay, Converter={x:Static conveter:ConverterWarehouse.FpsConverter}}"/>
|
||||
Text="{Binding Fps, Mode=OneWay, Converter={x:Static conveter:FpsConverter.Instance}, FallbackValue=N/A}"/>
|
||||
|
||||
</Grid>
|
||||
</Grid>
|
||||
|
||||
<StackPanel Orientation="Horizontal" Grid.Row="2" HorizontalAlignment="Right" Margin="10">
|
||||
<Button Content="OK" Margin="5" Style="{StaticResource OkButtonStyle}" IsDefault="True"/>
|
||||
<Button Content="Cancel" Margin="5" Style="{StaticResource CancelButtonStyle}"/>
|
||||
<Button Content="OK" Margin="5" Style="{StaticResource OkButtonStyle}" IsDefault="True"
|
||||
Command="{Binding OkCommand}"/>
|
||||
<Button Content="Cancel" Margin="5" Style="{StaticResource CancelButtonStyle}"
|
||||
Command="{Binding CancelCommand}"/>
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</Window>
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0-windows</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<!--
|
||||
Displays error on console in addition to the log file. Note that this feature comes with a performance impact.
|
||||
|
||||
Reference in New Issue
Block a user