From 09624e4e59a9146ee0a83ef14282b2f299836a76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Sat, 9 Mar 2024 01:00:59 +0100 Subject: [PATCH] ScriptCore: Added NumberFormatAttribute --- ScriptCore/Editor/EntityEditor.cs | 8 ++++-- ScriptCore/Editor/NumberFormatAttribute.cs | 30 ++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 ScriptCore/Editor/NumberFormatAttribute.cs diff --git a/ScriptCore/Editor/EntityEditor.cs b/ScriptCore/Editor/EntityEditor.cs index eaa98bf..05ba826 100644 --- a/ScriptCore/Editor/EntityEditor.cs +++ b/ScriptCore/Editor/EntityEditor.cs @@ -216,6 +216,10 @@ internal class EntityEditor } } + NumberFormatAttribute? numberFormat = GetAttribute(attributes); + + string? format = numberFormat?.Format; + RangeAttribute? range = GetAttribute(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; } diff --git a/ScriptCore/Editor/NumberFormatAttribute.cs b/ScriptCore/Editor/NumberFormatAttribute.cs new file mode 100644 index 0000000..ce786b8 --- /dev/null +++ b/ScriptCore/Editor/NumberFormatAttribute.cs @@ -0,0 +1,30 @@ +using System; + +namespace GlitchyEngine.Editor; + +/// +/// Specifies a custom number format for the input field in the editor. +/// +/// +/// This attribute is only applicable to numerical fields like , , etc. +/// +[AttributeUsage(AttributeTargets.Field)] +public sealed class NumberFormatAttribute : Attribute +{ + /// + /// The number format for the input field. + /// + /// + /// This doesn't use C# number formatting, but rather the C-style printf number formatting. + /// + public string Format { get; private set; } + + /// + /// Initializes a new instance of the with a custom number format. + /// + /// + public NumberFormatAttribute(string format) + { + Format = format; + } +}