1
0

feat: finish fps to deltatime converter

This commit is contained in:
2026-04-10 21:31:00 +08:00
parent a800b53188
commit fd2a9c3b3f
4 changed files with 41 additions and 87 deletions

View File

@@ -1,50 +0,0 @@
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.Frontend.Converters {
[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 int tv) {
if (tv <= 0) return DependencyProperty.UnsetValue;
else return Backend.FpsConverter.ToDelta(tv).ToString();
} else {
return DependencyProperty.UnsetValue;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
return Binding.DoNothing;
}
}
/// <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;
}
}
}

View File

@@ -0,0 +1,39 @@
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.Frontend.Converters {
[ValueConversion(typeof(string), typeof(string))]
public class StringifiedDeltaTimeConverter : IValueConverter {
public static readonly StringifiedDeltaTimeConverter Instance = new StringifiedDeltaTimeConverter();
public StringifiedDeltaTimeConverter() {
m_Validator = new Validator.FpsValidator();
}
private Validator.FpsValidator m_Validator;
private static readonly string INVALID_DELTA_TIME = "N/A";
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
var stringifiedFps = value as string;
if (stringifiedFps is null) {
return INVALID_DELTA_TIME;
}
return m_Validator.Validate(stringifiedFps).Match(
v => Backend.FpsConverter.ToDelta(v).ToString(),
_ => INVALID_DELTA_TIME
);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
return Binding.DoNothing;
}
}
}

View File

@@ -1,35 +0,0 @@
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.Frontend.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;
}
}
}
}

View File

@@ -51,8 +51,8 @@
<TextBox Margin="5" Padding="3" Grid.Row="1" Grid.Column="2" VerticalAlignment="Center" <TextBox Margin="5" Padding="3" Grid.Row="1" Grid.Column="2" VerticalAlignment="Center"
Text="{Binding Fps, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True}"/> Text="{Binding Fps, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True}"/>
<TextBlock Margin="5" Padding="3" Grid.Row="2" Grid.Column="2" VerticalAlignment="Center" <TextBlock Margin="5" Padding="3" Grid.Row="2" Grid.Column="2" VerticalAlignment="Center"
Text="{Binding Fps, Mode=OneWay, Converter={x:Static conveter:FpsConverter.Instance}, FallbackValue=N/A}"/> Text="{Binding Fps, Mode=OneWay, Converter={x:Static conveter:StringifiedDeltaTimeConverter.Instance}}"
Validation.ErrorTemplate="{x:Null}"/>
</Grid> </Grid>
</Grid> </Grid>