wabbajack/Wabbajack/Converters/PercentToDoubleConverter.cs
2020-02-07 23:45:45 -06:00

81 lines
2.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using ReactiveUI;
using Wabbajack.Common;
namespace Wabbajack
{
public class PercentToDoubleConverter : IBindingTypeConverter
{
public int GetAffinityForObjects(Type fromType, Type toType)
{
if (toType == typeof(double)) return 1;
if (toType == typeof(double?)) return 1;
if (toType == typeof(Percent)) return 1;
if (toType == typeof(Percent?)) return 1;
return 0;
}
public bool TryConvert(object from, Type toType, object conversionHint, out object result)
{
if (toType == typeof(double))
{
if (from is Percent p)
{
result = p.Value;
return true;
}
result = 0d;
return false;
}
if (toType == typeof(double?))
{
if (from is Percent p)
{
result = p.Value;
return true;
}
if (from == null)
{
result = default(double?);
return true;
}
result = default(double?);
return false;
}
if (toType == typeof(Percent))
{
if (from is double d)
{
result = Percent.FactoryPutInRange(d);
return true;
}
result = Percent.Zero;
return false;
}
if (toType == typeof(Percent?))
{
if (from is double d)
{
result = Percent.FactoryPutInRange(d);
return true;
}
if (from == null)
{
result = default(Percent?);
return true;
}
result = Percent.Zero;
return false;
}
result = null;
return false;
}
}
}