// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved. using System; using System.Linq; using FlaxEditor.CustomEditors.Elements; using FlaxEngine; namespace FlaxEditor.CustomEditors.Editors { /// /// Default implementation of the inspector used to edit integer value type properties. /// [CustomEditor(typeof(int)), DefaultEditor] public sealed class IntegerEditor : CustomEditor { private IIntegerValueEditor _element; /// public override DisplayStyle Style => DisplayStyle.Inline; /// public override void Initialize(LayoutElementsContainer layout) { _element = null; // Try get limit attribute for value min/max range setting and slider speed var attributes = Values.GetAttributes(); if (attributes != null) { var range = attributes.FirstOrDefault(x => x is RangeAttribute); if (range != null) { // Use slider var element = layout.Slider(); element.SetLimits((RangeAttribute)range); element.Slider.ValueChanged += OnValueChanged; element.Slider.SlidingEnd += ClearToken; _element = element; return; } var limit = attributes.FirstOrDefault(x => x is LimitAttribute); if (limit != null) { // Use int value editor with limit var element = layout.IntegerValue(); element.SetLimits((LimitAttribute)limit); element.IntValue.ValueChanged += OnValueChanged; element.IntValue.SlidingEnd += ClearToken; _element = element; return; } } if (_element == null) { // Use int value editor var element = layout.IntegerValue(); element.IntValue.ValueChanged += OnValueChanged; element.IntValue.SlidingEnd += ClearToken; _element = element; } } private void OnValueChanged() { var isSliding = _element.IsSliding; var token = isSliding ? this : null; SetValue(_element.Value, token); } /// public override void Refresh() { base.Refresh(); if (HasDifferentValues) { // TODO: support different values for ValueBox } else { var value = Values[0]; if (value is int asInt) _element.Value = asInt; else if (value is float asFloat) _element.Value = (int)asFloat; else throw new Exception(string.Format("Invalid value type {0}.", value?.GetType().ToString() ?? "")); } } } /// /// Default implementation of the inspector used to edit signed integer value type properties (maps to the full range of long type). /// public abstract class SignedIntegerValueEditor : CustomEditor { private SignedIntegerValueElement _element; /// public override DisplayStyle Style => DisplayStyle.Inline; /// public override void Initialize(LayoutElementsContainer layout) { _element = null; GetLimits(out var min, out var max); // Try get limit attribute for value min/max range setting and slider speed var attributes = Values.GetAttributes(); if (attributes != null) { var limit = attributes.FirstOrDefault(x => x is LimitAttribute); if (limit != null) { // Use int value editor with limit var element = layout.SignedIntegerValue(); element.LongValue.SetLimits((LimitAttribute)limit); element.LongValue.SetLimits(Mathf.Max(element.LongValue.MinValue, min), Mathf.Min(element.LongValue.MaxValue, max)); element.LongValue.ValueChanged += OnValueChanged; element.LongValue.SlidingEnd += ClearToken; _element = element; return; } } if (_element == null) { // Use int value editor var element = layout.SignedIntegerValue(); element.LongValue.SetLimits(Mathf.Max(element.LongValue.MinValue, min), Mathf.Min(element.LongValue.MaxValue, max)); element.LongValue.ValueChanged += OnValueChanged; element.LongValue.SlidingEnd += ClearToken; _element = element; } } private void OnValueChanged() { var isSliding = _element.IsSliding; var token = isSliding ? this : null; SetValue(SetValue(_element.Value), token); } /// public override void Refresh() { base.Refresh(); if (HasDifferentValues) { // TODO: support different values for ValueBox } else { _element.Value = GetValue(Values[0]); } } /// /// Gets the value limits. /// /// The minimum value. /// The maximum value. protected abstract void GetLimits(out long min, out long max); /// /// Gets the value as long. /// /// The value from object. /// The value for editor. protected abstract long GetValue(object value); /// /// Gets the value from long. /// /// The value from editor. /// The value to object. protected abstract object SetValue(long value); } /// /// Default implementation of the inspector used to edit sbyte value type properties. /// [CustomEditor(typeof(sbyte)), DefaultEditor] public sealed class SByteEditor : SignedIntegerValueEditor { /// protected override void GetLimits(out long min, out long max) { min = sbyte.MinValue; max = sbyte.MaxValue; } /// protected override long GetValue(object value) { return (sbyte)value; } /// protected override object SetValue(long value) { return (sbyte)value; } } /// /// Default implementation of the inspector used to edit short value type properties. /// [CustomEditor(typeof(short)), DefaultEditor] public sealed class ShortEditor : SignedIntegerValueEditor { /// protected override void GetLimits(out long min, out long max) { min = short.MinValue; max = short.MaxValue; } /// protected override long GetValue(object value) { return (short)value; } /// protected override object SetValue(long value) { return (short)value; } } /// /// Default implementation of the inspector used to edit long value type properties. /// [CustomEditor(typeof(long)), DefaultEditor] public sealed class LongEditor : SignedIntegerValueEditor { /// protected override void GetLimits(out long min, out long max) { min = long.MinValue; max = long.MaxValue; } /// protected override long GetValue(object value) { return (long)value; } /// protected override object SetValue(long value) { return value; } } /// /// Default implementation of the inspector used to edit unsigned integer value type properties (maps to the full range of ulong type). /// public abstract class UnsignedIntegerValueEditor : CustomEditor { private UnsignedIntegerValueElement _element; /// public override DisplayStyle Style => DisplayStyle.Inline; /// public override void Initialize(LayoutElementsContainer layout) { _element = null; GetLimits(out var min, out var max); // Try get limit attribute for value min/max range setting and slider speed var attributes = Values.GetAttributes(); if (attributes != null) { var limit = attributes.FirstOrDefault(x => x is LimitAttribute); if (limit != null) { // Use int value editor with limit var element = layout.UnsignedIntegerValue(); element.ULongValue.SetLimits((LimitAttribute)limit); element.ULongValue.SetLimits(Mathf.Max(element.ULongValue.MinValue, min), Mathf.Min(element.ULongValue.MaxValue, max)); element.ULongValue.ValueChanged += OnValueChanged; element.ULongValue.SlidingEnd += ClearToken; _element = element; return; } } if (_element == null) { // Use int value editor var element = layout.UnsignedIntegerValue(); element.ULongValue.SetLimits(Mathf.Max(element.ULongValue.MinValue, min), Mathf.Min(element.ULongValue.MaxValue, max)); element.ULongValue.ValueChanged += OnValueChanged; element.ULongValue.SlidingEnd += ClearToken; _element = element; } } private void OnValueChanged() { var isSliding = _element.IsSliding; var token = isSliding ? this : null; SetValue(SetValue(_element.Value), token); } /// public override void Refresh() { base.Refresh(); if (HasDifferentValues) { // TODO: support different values for ValueBox } else { _element.Value = GetValue(Values[0]); } } /// /// Gets the value limits. /// /// The minimum value. /// The maximum value. protected abstract void GetLimits(out ulong min, out ulong max); /// /// Gets the value as long. /// /// The value from object. /// The value for editor. protected abstract ulong GetValue(object value); /// /// Gets the value from long. /// /// The value from editor. /// The value to object. protected abstract object SetValue(ulong value); } /// /// Default implementation of the inspector used to edit byte value type properties. /// [CustomEditor(typeof(byte)), DefaultEditor] public sealed class ByteEditor : UnsignedIntegerValueEditor { /// protected override void GetLimits(out ulong min, out ulong max) { min = byte.MinValue; max = byte.MaxValue; } /// protected override ulong GetValue(object value) { return (byte)value; } /// protected override object SetValue(ulong value) { return (byte)value; } } /// /// Default implementation of the inspector used to edit char value type properties. /// [CustomEditor(typeof(char)), DefaultEditor] public sealed class CharEditor : UnsignedIntegerValueEditor { /// protected override void GetLimits(out ulong min, out ulong max) { min = char.MinValue; max = char.MaxValue; } /// protected override ulong GetValue(object value) { return (char)value; } /// protected override object SetValue(ulong value) { return (char)value; } } /// /// Default implementation of the inspector used to edit ushort value type properties. /// [CustomEditor(typeof(ushort)), DefaultEditor] public sealed class UShortEditor : UnsignedIntegerValueEditor { /// protected override void GetLimits(out ulong min, out ulong max) { min = ushort.MinValue; max = ushort.MaxValue; } /// protected override ulong GetValue(object value) { return (ushort)value; } /// protected override object SetValue(ulong value) { return (ushort)value; } } /// /// Default implementation of the inspector used to edit uint value type properties. /// [CustomEditor(typeof(uint)), DefaultEditor] public sealed class UintEditor : UnsignedIntegerValueEditor { /// protected override void GetLimits(out ulong min, out ulong max) { min = uint.MinValue; max = uint.MaxValue; } /// protected override ulong GetValue(object value) { return (uint)value; } /// protected override object SetValue(ulong value) { return (uint)value; } } /// /// Default implementation of the inspector used to edit ulong value type properties. /// [CustomEditor(typeof(ulong)), DefaultEditor] public sealed class ULongEditor : UnsignedIntegerValueEditor { /// protected override void GetLimits(out ulong min, out ulong max) { min = ulong.MinValue; max = ulong.MaxValue; } /// protected override ulong GetValue(object value) { return (ulong)value; } /// protected override object SetValue(ulong value) { return (ulong)value; } } }