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