140 lines
2.8 KiB
C#
140 lines
2.8 KiB
C#
using System.Globalization;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace UIWidgets
|
|
{
|
|
[AddComponentMenu("UI/SpinnerFloat", 270)]
|
|
public class SpinnerFloat : SpinnerBase<float>
|
|
{
|
|
[SerializeField]
|
|
private string format = "0.00";
|
|
|
|
public OnChangeEventFloat onValueChangeFloat = new OnChangeEventFloat();
|
|
|
|
public SubmitEventFloat onEndEditFloat = new SubmitEventFloat();
|
|
|
|
private NumberStyles NumberStyle = NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands;
|
|
|
|
private CultureInfo Culture = CultureInfo.InvariantCulture;
|
|
|
|
public string Format
|
|
{
|
|
get
|
|
{
|
|
return format;
|
|
}
|
|
set
|
|
{
|
|
format = value;
|
|
base.text = _value.ToString(format);
|
|
}
|
|
}
|
|
|
|
public SpinnerFloat()
|
|
{
|
|
_max = 100f;
|
|
_step = 1f;
|
|
}
|
|
|
|
public override void Plus()
|
|
{
|
|
base.Value += base.Step;
|
|
}
|
|
|
|
public override void Minus()
|
|
{
|
|
base.Value -= base.Step;
|
|
}
|
|
|
|
protected override void SetValue(float newValue)
|
|
{
|
|
if (_value != InBounds(newValue))
|
|
{
|
|
_value = InBounds(newValue);
|
|
base.text = _value.ToString(format);
|
|
onValueChangeFloat.Invoke(_value);
|
|
}
|
|
}
|
|
|
|
protected override void ValueChange(string value)
|
|
{
|
|
if (Validation != SpinnerValidation.OnEndInput)
|
|
{
|
|
if (value.Length == 0)
|
|
{
|
|
value = "0";
|
|
}
|
|
float result;
|
|
if (float.TryParse(value, NumberStyle, Culture, out result))
|
|
{
|
|
SetValue(result);
|
|
}
|
|
}
|
|
}
|
|
|
|
protected override void ValueEndEdit(string value)
|
|
{
|
|
if (value.Length == 0)
|
|
{
|
|
value = "0";
|
|
}
|
|
float result;
|
|
if (float.TryParse(value, NumberStyle, Culture, out result))
|
|
{
|
|
SetValue(result);
|
|
}
|
|
onEndEditFloat.Invoke(_value);
|
|
}
|
|
|
|
protected override char ValidateShort(string validateText, int charIndex, char addedChar)
|
|
{
|
|
if (charIndex != 0 || validateText.Length <= 0 || validateText[0] != '-')
|
|
{
|
|
if (addedChar >= '0' && addedChar <= '9')
|
|
{
|
|
return addedChar;
|
|
}
|
|
if (addedChar == '-' && charIndex == 0 && base.Min < 0f)
|
|
{
|
|
return addedChar;
|
|
}
|
|
if (addedChar == '.' && base.characterValidation == CharacterValidation.Decimal && !validateText.Contains("."))
|
|
{
|
|
return addedChar;
|
|
}
|
|
}
|
|
return '\0';
|
|
}
|
|
|
|
protected override char ValidateFull(string validateText, int charIndex, char addedChar)
|
|
{
|
|
string text = validateText.Insert(charIndex, addedChar.ToString());
|
|
if (base.Min > 0f && charIndex == 0 && addedChar == '-')
|
|
{
|
|
return '\0';
|
|
}
|
|
int num = ((text.Length <= 0 || text[0] != '-') ? 1 : 2);
|
|
if (text.Length >= num)
|
|
{
|
|
float result;
|
|
if (!float.TryParse(text, NumberStyle, Culture, out result))
|
|
{
|
|
return '\0';
|
|
}
|
|
if (result != InBounds(result))
|
|
{
|
|
return '\0';
|
|
}
|
|
_value = result;
|
|
}
|
|
return addedChar;
|
|
}
|
|
|
|
protected override float InBounds(float value)
|
|
{
|
|
return Mathf.Clamp(value, _min, _max);
|
|
}
|
|
}
|
|
}
|