ScriptCore: Added NumberFormatAttribute

This commit is contained in:
Simon Lübeß
2024-03-09 01:00:59 +01:00
parent 5dbe1d7e50
commit 09624e4e59
2 changed files with 36 additions and 2 deletions
+6 -2
View File
@@ -216,6 +216,10 @@ internal class EntityEditor
}
}
NumberFormatAttribute? numberFormat = GetAttribute<NumberFormatAttribute>(attributes);
string? format = numberFormat?.Format;
RangeAttribute? range = GetAttribute<RangeAttribute>(attributes);
// Get Min and Max Values from Type T
@@ -255,14 +259,14 @@ internal class EntityEditor
if (range?.Slider == true)
{
if (ImGui.SliderScalar(fieldId, dataType, (IntPtr)(&value), (IntPtr)(&min), (IntPtr)(&max)))
if (ImGui.SliderScalar(fieldId, dataType, (IntPtr)(&value), (IntPtr)(&min), (IntPtr)(&max), format))
{
newValue = value;
}
}
else
{
if (ImGui.DragScalar(fieldId, dataType, (IntPtr)(&value), dragSpeed, (IntPtr)(&min), (IntPtr)(&max)))
if (ImGui.DragScalar(fieldId, dataType, (IntPtr)(&value), dragSpeed, (IntPtr)(&min), (IntPtr)(&max), format))
{
newValue = value;
}
@@ -0,0 +1,30 @@
using System;
namespace GlitchyEngine.Editor;
/// <summary>
/// Specifies a custom number format for the input field in the editor.
/// </summary>
/// <remarks>
/// This attribute is only applicable to numerical fields like <see cref="float"/>, <see cref="int"/>, etc.
/// </remarks>
[AttributeUsage(AttributeTargets.Field)]
public sealed class NumberFormatAttribute : Attribute
{
/// <summary>
/// The number format for the input field.
/// </summary>
/// <remarks>
/// This doesn't use C# number formatting, but rather the C-style printf number formatting.
/// </remarks>
public string Format { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="NumberFormatAttribute"/> with a custom number format.
/// </summary>
/// <param name="format"><inheritdoc cref="Format"/></param>
public NumberFormatAttribute(string format)
{
Format = format;
}
}