forked from amkovkov/GranuSightSoftware2
feat: logic converters
This commit is contained in:
@@ -0,0 +1,46 @@
|
|||||||
|
using System.Globalization;
|
||||||
|
|
||||||
|
using Avalonia.Data.Converters;
|
||||||
|
|
||||||
|
namespace GSS2.UI.Core.Converters;
|
||||||
|
|
||||||
|
public class EqualsConverter : IValueConverter
|
||||||
|
{
|
||||||
|
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||||
|
{
|
||||||
|
if (value is null && parameter is null)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (value is null || parameter is null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
var valueType = value.GetType();
|
||||||
|
var paramType = parameter.GetType();
|
||||||
|
|
||||||
|
if (valueType == paramType)
|
||||||
|
return value == parameter;
|
||||||
|
|
||||||
|
if (parameter is string ps)
|
||||||
|
return value switch
|
||||||
|
{
|
||||||
|
byte v => byte.TryParse(ps, out var p) && v == p,
|
||||||
|
short v => short.TryParse(ps, out var p) && v == p,
|
||||||
|
ushort v => ushort.TryParse(ps, out var p) && v == p,
|
||||||
|
int v => int.TryParse(ps, out var p) && v == p,
|
||||||
|
uint v => uint.TryParse(ps, out var p) && v == p,
|
||||||
|
long v => long.TryParse(ps, out var p) && v == p,
|
||||||
|
ulong v => ulong.TryParse(ps, out var p) && v == p,
|
||||||
|
float v => float.TryParse(ps, out var p) && v == p,
|
||||||
|
double v => double.TryParse(ps, out var p) && v == p,
|
||||||
|
decimal v => decimal.TryParse(ps, out var p) && v == p,
|
||||||
|
_ => false
|
||||||
|
};
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
using System.Globalization;
|
||||||
|
|
||||||
|
using Avalonia.Data.Converters;
|
||||||
|
|
||||||
|
namespace GSS2.UI.Core.Converters;
|
||||||
|
|
||||||
|
public class GreaterThanConverter : IValueConverter
|
||||||
|
{
|
||||||
|
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||||
|
{
|
||||||
|
if (value is null && parameter is null)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (value is null || parameter is null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
var valueType = value.GetType();
|
||||||
|
var paramType = parameter.GetType();
|
||||||
|
|
||||||
|
if (valueType == paramType)
|
||||||
|
{
|
||||||
|
var greaterThan = valueType.GetMethods(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic)
|
||||||
|
.FirstOrDefault(m => m.Name.EndsWith("op_GreaterThan"));
|
||||||
|
if (greaterThan is not null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return greaterThan.Invoke(null, [value, parameter]);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{ }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parameter is string ps)
|
||||||
|
return value switch
|
||||||
|
{
|
||||||
|
byte v => byte.TryParse(ps, out var p) && v > p,
|
||||||
|
short v => short.TryParse(ps, out var p) && v > p,
|
||||||
|
ushort v => ushort.TryParse(ps, out var p) && v > p,
|
||||||
|
int v => int.TryParse(ps, out var p) && v > p,
|
||||||
|
uint v => uint.TryParse(ps, out var p) && v > p,
|
||||||
|
long v => long.TryParse(ps, out var p) && v > p,
|
||||||
|
ulong v => ulong.TryParse(ps, out var p) && v > p,
|
||||||
|
float v => float.TryParse(ps, out var p) && v > p,
|
||||||
|
double v => double.TryParse(ps, out var p) && v > p,
|
||||||
|
decimal v => decimal.TryParse(ps, out var p) && v > p,
|
||||||
|
_ => false
|
||||||
|
};
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
using System.Globalization;
|
||||||
|
|
||||||
|
using Avalonia.Data.Converters;
|
||||||
|
|
||||||
|
namespace GSS2.UI.Core.Converters;
|
||||||
|
|
||||||
|
public class GreaterThenOrEqualConverter : IValueConverter
|
||||||
|
{
|
||||||
|
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||||
|
{
|
||||||
|
if (value is null && parameter is null)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (value is null || parameter is null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
var valueType = value.GetType();
|
||||||
|
var paramType = parameter.GetType();
|
||||||
|
|
||||||
|
if (valueType == paramType)
|
||||||
|
{
|
||||||
|
var greaterThanOrEqual = valueType.GetMethods(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic)
|
||||||
|
.FirstOrDefault(m => m.Name.EndsWith("op_GreaterThanOrEqual"));
|
||||||
|
if (greaterThanOrEqual is not null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return greaterThanOrEqual.Invoke(null, [value, parameter]);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{ }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parameter is string ps)
|
||||||
|
return value switch
|
||||||
|
{
|
||||||
|
byte v => byte.TryParse(ps, out var p) && v >= p,
|
||||||
|
short v => short.TryParse(ps, out var p) && v >= p,
|
||||||
|
ushort v => ushort.TryParse(ps, out var p) && v >= p,
|
||||||
|
int v => int.TryParse(ps, out var p) && v >= p,
|
||||||
|
uint v => uint.TryParse(ps, out var p) && v >= p,
|
||||||
|
long v => long.TryParse(ps, out var p) && v >= p,
|
||||||
|
ulong v => ulong.TryParse(ps, out var p) && v >= p,
|
||||||
|
float v => float.TryParse(ps, out var p) && v >= p,
|
||||||
|
double v => double.TryParse(ps, out var p) && v >= p,
|
||||||
|
decimal v => decimal.TryParse(ps, out var p) && v >= p,
|
||||||
|
_ => false
|
||||||
|
};
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
using System.Globalization;
|
||||||
|
|
||||||
|
using Avalonia.Data.Converters;
|
||||||
|
|
||||||
|
namespace GSS2.UI.Core.Converters;
|
||||||
|
|
||||||
|
public class LessThenConverter : IValueConverter
|
||||||
|
{
|
||||||
|
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||||
|
{
|
||||||
|
if (value is null && parameter is null)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (value is null || parameter is null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
var valueType = value.GetType();
|
||||||
|
var paramType = parameter.GetType();
|
||||||
|
|
||||||
|
if (valueType == paramType)
|
||||||
|
{
|
||||||
|
var lessThan = valueType.GetMethods(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic)
|
||||||
|
.FirstOrDefault(m => m.Name.EndsWith("op_LessThan"));
|
||||||
|
if (lessThan is not null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return lessThan.Invoke(null, [value, parameter]);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{ }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parameter is string ps)
|
||||||
|
return value switch
|
||||||
|
{
|
||||||
|
byte v => byte.TryParse(ps, out var p) && v < p,
|
||||||
|
short v => short.TryParse(ps, out var p) && v < p,
|
||||||
|
ushort v => ushort.TryParse(ps, out var p) && v < p,
|
||||||
|
int v => int.TryParse(ps, out var p) && v < p,
|
||||||
|
uint v => uint.TryParse(ps, out var p) && v < p,
|
||||||
|
long v => long.TryParse(ps, out var p) && v < p,
|
||||||
|
ulong v => ulong.TryParse(ps, out var p) && v < p,
|
||||||
|
float v => float.TryParse(ps, out var p) && v < p,
|
||||||
|
double v => double.TryParse(ps, out var p) && v < p,
|
||||||
|
decimal v => decimal.TryParse(ps, out var p) && v < p,
|
||||||
|
_ => false
|
||||||
|
};
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
using System.Globalization;
|
||||||
|
|
||||||
|
using Avalonia.Data.Converters;
|
||||||
|
|
||||||
|
namespace GSS2.UI.Core.Converters;
|
||||||
|
|
||||||
|
public class LessThenOrEqualConverter : IValueConverter
|
||||||
|
{
|
||||||
|
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||||
|
{
|
||||||
|
if (value is null && parameter is null)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (value is null || parameter is null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
var valueType = value.GetType();
|
||||||
|
var paramType = parameter.GetType();
|
||||||
|
|
||||||
|
if (valueType == paramType)
|
||||||
|
{
|
||||||
|
var lessThanOrEqual = valueType.GetMethods(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic)
|
||||||
|
.FirstOrDefault(m => m.Name.EndsWith("op_LessThanOrEqual"));
|
||||||
|
if (lessThanOrEqual is not null)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return lessThanOrEqual.Invoke(null, [value, parameter]);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{ }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parameter is string ps)
|
||||||
|
return value switch
|
||||||
|
{
|
||||||
|
byte v => byte.TryParse(ps, out var p) && v <= p,
|
||||||
|
short v => short.TryParse(ps, out var p) && v <= p,
|
||||||
|
ushort v => ushort.TryParse(ps, out var p) && v <= p,
|
||||||
|
int v => int.TryParse(ps, out var p) && v <= p,
|
||||||
|
uint v => uint.TryParse(ps, out var p) && v <= p,
|
||||||
|
long v => long.TryParse(ps, out var p) && v <= p,
|
||||||
|
ulong v => ulong.TryParse(ps, out var p) && v <= p,
|
||||||
|
float v => float.TryParse(ps, out var p) && v <= p,
|
||||||
|
double v => double.TryParse(ps, out var p) && v <= p,
|
||||||
|
decimal v => decimal.TryParse(ps, out var p) && v <= p,
|
||||||
|
_ => false
|
||||||
|
};
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
using System.Globalization;
|
||||||
|
|
||||||
|
using Avalonia.Data.Converters;
|
||||||
|
|
||||||
|
namespace GSS2.UI.Core.Converters;
|
||||||
|
|
||||||
|
public class NotEqualsConverter : IValueConverter
|
||||||
|
{
|
||||||
|
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||||
|
{
|
||||||
|
if (value is null && parameter is null)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (value is null || parameter is null)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
var valueType = value.GetType();
|
||||||
|
var paramType = parameter.GetType();
|
||||||
|
|
||||||
|
if (valueType == paramType)
|
||||||
|
return value != parameter;
|
||||||
|
|
||||||
|
if (parameter is string ps)
|
||||||
|
return value switch
|
||||||
|
{
|
||||||
|
byte v => !(byte.TryParse(ps, out var p) && v == p),
|
||||||
|
short v => !(short.TryParse(ps, out var p) && v == p),
|
||||||
|
ushort v => !(ushort.TryParse(ps, out var p) && v == p),
|
||||||
|
int v => !(int.TryParse(ps, out var p) && v == p),
|
||||||
|
uint v => !(uint.TryParse(ps, out var p) && v == p),
|
||||||
|
long v => !(long.TryParse(ps, out var p) && v == p),
|
||||||
|
ulong v => !(ulong.TryParse(ps, out var p) && v == p),
|
||||||
|
float v => !(float.TryParse(ps, out var p) && v == p),
|
||||||
|
double v => !(double.TryParse(ps, out var p) && v == p),
|
||||||
|
decimal v => !(decimal.TryParse(ps, out var p) && v == p),
|
||||||
|
_ => true
|
||||||
|
};
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user