// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
using System;
using FlaxEditor.Utilities;
using FlaxEngine;
namespace FlaxEditor.GUI.Input
{
///
/// Double precision floating point value editor.
///
///
[HideInEditor]
public class DoubleValueBox : ValueBox
{
///
public override double Value
{
get => _value;
set
{
value = Mathd.Clamp(value, _min, _max);
if (Math.Abs(_value - value) > Mathd.Epsilon)
{
// Set value
_value = value;
// Update
UpdateText();
OnValueChanged();
}
}
}
///
public override double MinValue
{
get => _min;
set
{
if (!Mathd.NearEqual(_min, value))
{
if (value > _max)
throw new ArgumentException();
_min = value;
Value = Value;
}
}
}
///
public override double MaxValue
{
get => _max;
set
{
if (!Mathd.NearEqual(_max, value))
{
if (value < _min)
throw new ArgumentException();
_max = value;
Value = Value;
}
}
}
///
/// Initializes a new instance of the class.
///
/// The value.
/// The x location.
/// The y location.
/// The width.
/// The minimum value.
/// The maximum value.
/// The slide speed.
public DoubleValueBox(double value, float x = 0, float y = 0, float width = 120, double min = double.MinValue, double max = double.MaxValue, float slideSpeed = 1)
: base(Mathd.Clamp(value, min, max), x, y, width, min, max, slideSpeed)
{
UpdateText();
}
///
/// Sets the value limits.
///
/// The minimum value (bottom range).
/// The maximum value (upper range).
public void SetLimits(double min, double max)
{
_min = min;
_max = Math.Max(_min, max);
Value = Value;
}
///
/// Sets the limits from the attribute.
///
/// The limits.
public void SetLimits(RangeAttribute limits)
{
_min = limits.Min;
_max = Math.Max(_min, limits.Max);
Value = Value;
}
///
/// Sets the limits from the attribute.
///
/// The limits.
public void SetLimits(LimitAttribute limits)
{
_min = limits.Min;
_max = Math.Max(_min, (double)limits.Max);
_slideSpeed = limits.SliderSpeed;
Value = Value;
}
///
/// Sets the limits from the other .
///
/// The other.
public void SetLimits(DoubleValueBox other)
{
_min = other._min;
_max = other._max;
_slideSpeed = other._slideSpeed;
Value = Value;
}
///
protected sealed override void UpdateText()
{
SetText(Utilities.Utils.FormatFloat(_value));
}
///
protected override void TryGetValue()
{
try
{
Value = ShuntingYard.Parse(Text);
}
catch (Exception ex)
{
// Fall back to previous value
Editor.LogWarning(ex);
}
}
///
protected override void ApplySliding(float delta)
{
Value = _startSlideValue + delta;
}
}
}