// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved. using System; using FlaxEditor.Utilities; using FlaxEngine; namespace FlaxEditor.GUI.Input { /// /// Unsigned integer (ulong type) value editor. /// /// [HideInEditor] public class ULongValueBox : ValueBox { /// public override ulong Value { get => _value; set { value = Mathf.Clamp(value, _min, _max); if (_value != value) { // Set value _value = value; // Update UpdateText(); OnValueChanged(); } } } /// public override ulong MinValue { get => _min; set { if (_min != value) { if (value > _max) throw new ArgumentException(); _min = value; Value = Value; } } } /// public override ulong MaxValue { get => _max; set { if (_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 ULongValueBox(ulong value, float x = 0, float y = 0, float width = 120, ulong min = ulong.MinValue, ulong max = ulong.MaxValue, float slideSpeed = 1) : base(Mathf.Clamp(value, min, max), x, y, width, min, max, slideSpeed) { UpdateText(); } /// /// Sets the limits from the attribute. /// /// The limits. public void SetLimits(LimitAttribute limits) { _min = limits.Min < 0.0f ? 0 : (ulong)limits.Min; _max = Math.Max(_min, limits.Max == float.MaxValue ? ulong.MaxValue : (ulong)limits.Max); _slideSpeed = limits.SliderSpeed; Value = Value; } /// /// Sets the limits at once. /// /// The minimum value. /// The minimum value. public void SetLimits(ulong min, ulong max) { _min = Math.Min(min, max); _max = Math.Max(min, max); Value = Value; } /// protected sealed override void UpdateText() { var text = _value.ToString(); SetText(text); } /// protected override void TryGetValue() { try { var value = ShuntingYard.Parse(Text); Value = (ulong)value; } catch (Exception ex) { // Fall back to previous value Editor.LogWarning(ex); } } /// protected override void ApplySliding(float delta) { // Check for negative overflow to positive numbers if (delta < 0 && _startSlideValue > delta) Value = (ulong)Mathf.Max(0, (long)_startSlideValue + (long)delta); else if (delta < 0) Value = _startSlideValue - (ulong)(-delta); else Value = _startSlideValue + (ulong)delta; } } }