Add support for using unsigned integer input fields in Visject Surface editor

This commit is contained in:
Wojtek Figat
2020-12-31 15:36:07 +01:00
parent 9b891d0c55
commit d94e777feb
4 changed files with 393 additions and 2 deletions

View File

@@ -0,0 +1,161 @@
// Copyright (c) 2012-2020 Wojciech Figat. All rights reserved.
using System;
using FlaxEditor.Utilities;
using FlaxEngine;
namespace FlaxEditor.GUI.Input
{
/// <summary>
/// Unsigned Integer value editor.
/// </summary>
/// <seealso cref="uint" />
[HideInEditor]
public class UIntValueBox : ValueBox<uint>
{
/// <inheritdoc />
public override uint Value
{
get => _value;
set
{
value = Mathf.Clamp(value, _min, _max);
if (_value != value)
{
// Set value
_value = value;
// Update
UpdateText();
OnValueChanged();
}
}
}
/// <inheritdoc />
public override uint MinValue
{
get => _min;
set
{
if (_min != value)
{
if (value > _max)
throw new ArgumentException();
_min = value;
Value = Value;
}
}
}
/// <inheritdoc />
public override uint MaxValue
{
get => _max;
set
{
if (_max != value)
{
if (value < _min)
throw new ArgumentException();
_max = value;
Value = Value;
}
}
}
/// <summary>
/// Initializes a new instance of the <see cref="UIntValueBox"/> class.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="x">The x location.</param>
/// <param name="y">The y location.</param>
/// <param name="width">The width.</param>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <param name="slideSpeed">The slide speed.</param>
public UIntValueBox(uint value, float x = 0, float y = 0, float width = 120, uint min = uint.MinValue, uint max = uint.MaxValue, float slideSpeed = 1)
: base(Mathf.Clamp(value, min, max), x, y, width, min, max, slideSpeed)
{
UpdateText();
}
/// <summary>
/// Sets the value limits.
/// </summary>
/// <param name="min">The minimum value (bottom range).</param>
/// <param name="max">The maximum value (upper range).</param>
public void SetLimits(uint min, uint max)
{
_min = min;
_max = Mathf.Max(_min, max);
Value = Value;
}
/// <summary>
/// Sets the limits from the attribute.
/// </summary>
/// <param name="limits">The limits.</param>
public void SetLimits(RangeAttribute limits)
{
_min = limits.Min <= 0 ? uint.MinValue : (uint)limits.Min;
_max = Math.Max(_min, limits.Max == float.MaxValue ? uint.MaxValue : (uint)limits.Max);
Value = Value;
}
/// <summary>
/// Sets the limits from the attribute.
/// </summary>
/// <param name="limits">The limits.</param>
public void SetLimits(LimitAttribute limits)
{
_min = limits.Min <= 0 ? uint.MinValue : (uint)limits.Min;
_max = Math.Max(_min, limits.Max == float.MaxValue ? uint.MaxValue : (uint)limits.Max);
_slideSpeed = limits.SliderSpeed;
Value = Value;
}
/// <summary>
/// Sets the limits from the other <see cref="UIntValueBox"/>.
/// </summary>
/// <param name="other">The other.</param>
public void SetLimits(UIntValueBox other)
{
_min = other._min;
_max = other._max;
_slideSpeed = other._slideSpeed;
Value = Value;
}
/// <inheritdoc />
protected sealed override void UpdateText()
{
var text = _value.ToString();
SetText(text);
}
/// <inheritdoc />
protected override void TryGetValue()
{
try
{
var value = ShuntingYard.Parse(Text);
Value = (uint)value;
}
catch (Exception ex)
{
// Fall back to previous value
Editor.LogWarning(ex);
}
}
/// <inheritdoc />
protected override void ApplySliding(float delta)
{
Value = _startSlideValue + (uint)delta;
}
}
}

View File

@@ -105,13 +105,13 @@ namespace FlaxEditor.Surface.Elements
{
public bool CanUse(InputBox box, ref ScriptType type)
{
return type.Type == typeof(int) || type.Type == typeof(uint);
return type.Type == typeof(int);
}
public Control Create(InputBox box, ref Rectangle bounds)
{
var value = IntegerValue.Get(box.ParentNode, box.Archetype);
var control = new IntValueBox(value, bounds.X, bounds.Y, 40, box.CurrentType.Type == typeof(uint) ? 0 : int.MinValue, int.MaxValue, 0.01f)
var control = new IntValueBox(value, bounds.X, bounds.Y, 40, int.MinValue, int.MaxValue, 0.01f)
{
Height = bounds.Height,
Parent = box.Parent,
@@ -145,6 +145,50 @@ namespace FlaxEditor.Surface.Elements
}
}
class UnsignedIntegerDefaultValueEditor : IDefaultValueEditor
{
public bool CanUse(InputBox box, ref ScriptType type)
{
return type.Type == typeof(uint);
}
public Control Create(InputBox box, ref Rectangle bounds)
{
var value = UnsignedIntegerValue.Get(box.ParentNode, box.Archetype);
var control = new UIntValueBox(value, bounds.X, bounds.Y, 40, uint.MinValue, uint.MaxValue, 0.01f)
{
Height = bounds.Height,
Parent = box.Parent,
Tag = box,
};
control.BoxValueChanged += OnValueBoxChanged;
return control;
}
public bool IsValid(InputBox box, Control control)
{
return control is UIntValueBox;
}
public void UpdateDefaultValue(InputBox box, Control control)
{
if (control is UIntValueBox intValue)
{
intValue.Value = UnsignedIntegerValue.Get(box.ParentNode, box.Archetype);
}
}
public void UpdateAttributes(InputBox box, object[] attributes, Control control)
{
}
private void OnValueBoxChanged(ValueBox<uint> control)
{
var box = (InputBox)control.Tag;
UnsignedIntegerValue.Set(box.ParentNode, box.Archetype, control.Value);
}
}
class FloatingDefaultValueEditor : IDefaultValueEditor
{
public bool CanUse(InputBox box, ref ScriptType type)
@@ -817,6 +861,7 @@ namespace FlaxEditor.Surface.Elements
{
new BooleanDefaultValueEditor(),
new IntegerDefaultValueEditor(),
new UnsignedIntegerDefaultValueEditor(),
new FloatingDefaultValueEditor(),
new StringDefaultValueEditor(),
new Vector2DefaultValueEditor(),

View File

@@ -0,0 +1,153 @@
// Copyright (c) 2012-2020 Wojciech Figat. All rights reserved.
using FlaxEditor.GUI.Input;
using FlaxEngine;
using FlaxEngine.GUI;
namespace FlaxEditor.Surface.Elements
{
/// <summary>
/// Unsigned Integer value editing element.
/// </summary>
/// <seealso cref="UIntValueBox" />
/// <seealso cref="ISurfaceNodeElement" />
[HideInEditor]
public sealed class UnsignedIntegerValue : UIntValueBox, ISurfaceNodeElement
{
/// <inheritdoc />
public SurfaceNode ParentNode { get; }
/// <inheritdoc />
public NodeElementArchetype Archetype { get; }
/// <inheritdoc />
public UnsignedIntegerValue(SurfaceNode parentNode, NodeElementArchetype archetype)
: base(Get(parentNode, archetype), archetype.Position.X, archetype.Position.Y, 50, (uint)archetype.ValueMin, (uint)archetype.ValueMax, 0.05f)
{
ParentNode = parentNode;
Archetype = archetype;
ParentNode.ValuesChanged += OnNodeValuesChanged;
}
private void OnNodeValuesChanged()
{
Value = Get(ParentNode, Archetype);
}
/// <inheritdoc />
public override void Draw()
{
base.Draw();
// Draw border
if (!IsFocused)
Render2D.DrawRectangle(new Rectangle(Vector2.Zero, Size), Style.Current.BorderNormal);
}
/// <inheritdoc />
protected override void OnValueChanged()
{
base.OnValueChanged();
Set(ParentNode, Archetype, Value);
}
/// <summary>
/// Gets the unsigned integer value from the specified parent node. Handles type casting and components gather.
/// </summary>
/// <param name="parentNode">The parent node.</param>
/// <param name="arch">The node element archetype.</param>
/// <returns>The result value.</returns>
public static uint Get(SurfaceNode parentNode, NodeElementArchetype arch)
{
if (arch.ValueIndex < 0)
return 0;
uint result;
var value = parentNode.Values[arch.ValueIndex];
// Note: this value box may edit on component of the vector like Vector3.Y, BoxID from Archetype tells which component pick
if (value is int valueInt)
result = (uint)valueInt;
else if (value is uint valueUint)
result = valueUint;
else if (value is long valueLong)
result = (uint)valueLong;
else if (value is ulong valueUlong)
result = (uint)valueUlong;
else if (value is float valueFloat)
result = (uint)valueFloat;
else if (value is Vector2 valueVec2)
result = (uint)(arch.BoxID == 0 ? valueVec2.X : valueVec2.Y);
else if (value is Vector3 valueVec3)
result = (uint)(arch.BoxID == 0 ? valueVec3.X : arch.BoxID == 1 ? valueVec3.Y : valueVec3.Z);
else if (value is Vector4 valueVec4)
result = (uint)(arch.BoxID == 0 ? valueVec4.X : arch.BoxID == 1 ? valueVec4.Y : arch.BoxID == 2 ? valueVec4.Z : valueVec4.W);
else
result = 0;
return result;
}
/// <summary>
/// Sets the unsigned integer value of the specified parent node. Handles type casting and components assignment.
/// </summary>
/// <param name="parentNode">The parent node.</param>
/// <param name="arch">The node element archetype.</param>
/// <param name="toSet">The value to set.</param>
public static void Set(SurfaceNode parentNode, NodeElementArchetype arch, uint toSet)
{
if (arch.ValueIndex < 0)
return;
var value = parentNode.Values[arch.ValueIndex];
float toSetF = (float)toSet;
if (value is int)
value = (int)toSet;
else if (value is uint)
value = toSet;
else if (value is long)
value = (long)toSet;
else if (value is ulong)
value = (ulong)toSet;
else if (value is float)
value = toSetF;
else if (value is Vector2 valueVec2)
{
if (arch.BoxID == 0)
valueVec2.X = toSetF;
else
valueVec2.Y = toSetF;
value = valueVec2;
}
else if (value is Vector3 valueVec3)
{
if (arch.BoxID == 0)
valueVec3.X = toSetF;
else if (arch.BoxID == 1)
valueVec3.Y = toSetF;
else
valueVec3.Z = toSetF;
value = valueVec3;
}
else if (value is Vector4 valueVec4)
{
if (arch.BoxID == 0)
valueVec4.X = toSetF;
else if (arch.BoxID == 1)
valueVec4.Y = toSetF;
else if (arch.BoxID == 2)
valueVec4.Z = toSetF;
else
valueVec4.W = toSetF;
value = valueVec4;
}
else
value = 0;
parentNode.SetValue(arch.ValueIndex, value);
}
}
}

View File

@@ -316,6 +316,16 @@ namespace FlaxEngine
return a <= b ? b : a;
}
/// <summary>
/// Returns the largest of two or more values.
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
public static uint Max(uint a, uint b)
{
return a <= b ? b : a;
}
/// <summary>
/// Returns the largest of two or more values.
/// </summary>
@@ -401,6 +411,16 @@ namespace FlaxEngine
return a >= b ? b : a;
}
/// <summary>
/// Returns the smallest of two or more values.
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
public static uint Min(uint a, uint b)
{
return a >= b ? b : a;
}
/// <summary>
/// Returns the smallest of two or more values.
/// </summary>
@@ -1173,6 +1193,18 @@ namespace FlaxEngine
return value < min ? min : value > max ? max : value;
}
/// <summary>
/// Clamps the specified value.
/// </summary>
/// <param name="value">The value.</param>
/// <param name="min">The min.</param>
/// <param name="max">The max.</param>
/// <returns>The result of clamping a value between min and max</returns>
public static uint Clamp(uint value, uint min, uint max)
{
return value < min ? min : value > max ? max : value;
}
/// <summary>
/// Interpolates between two values using a linear function by a given amount.
/// </summary>