From c908f5a0e7f5b3eb4fa9f6ffe9234e3bf0395c5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Thu, 27 Jun 2024 20:50:26 +0200 Subject: [PATCH] A little bit better number formatting --- .../src/EditWindows/ComponentEditWindow.bf | 2 +- GlitchyEngine/src/ImGui/ImGuiExtension.bf | 18 ++++++++++++---- ScriptCore/Editor/EntityEditor.cs | 21 +++++++++++++++++++ 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/GlitchyEditor/src/EditWindows/ComponentEditWindow.bf b/GlitchyEditor/src/EditWindows/ComponentEditWindow.bf index b75834d..f14e8dd 100644 --- a/GlitchyEditor/src/EditWindows/ComponentEditWindow.bf +++ b/GlitchyEditor/src/EditWindows/ComponentEditWindow.bf @@ -353,7 +353,7 @@ namespace GlitchyEditor.EditWindows } ImGui.PropertyTableStartNewProperty("Rotation"); - if (ImGui.Float3Editor("##Rotation", ref rotationEuler, resetValues: .Zero, dragSpeed: 0.1f, componentEnabled: componentEditable, format: .("%.3g°",))) + if (ImGui.Float3Editor("##Rotation", ref rotationEuler, resetValues: .Zero, dragSpeed: 0.1f, componentEnabled: componentEditable, format: .("0.#####°",))) { transform.EditorRotationEuler = MathHelper.ToRadians(rotationEuler); diff --git a/GlitchyEngine/src/ImGui/ImGuiExtension.bf b/GlitchyEngine/src/ImGui/ImGuiExtension.bf index d1e70d4..3d4625a 100644 --- a/GlitchyEngine/src/ImGui/ImGuiExtension.bf +++ b/GlitchyEngine/src/ImGui/ImGuiExtension.bf @@ -324,22 +324,32 @@ namespace ImGui SameLine(); - char8* format = "%.3g"; + StringView format = "0.#####"; if (!numberFormat[i].IsWhiteSpace) { // Look if we have a format for the specific index - format = numberFormat[i].ToScopeCStr!:componentLoop(); + format = numberFormat[i]; } else if (!numberFormat[0].IsWhiteSpace) { // Try to take the first number format - format = numberFormat[0].ToScopeCStr!:componentLoop(); + format = numberFormat[0]; + } + + if (!format.StartsWith("%")) + { + String str = scope:componentLoop .(); + + value[i].ToString(str, scope .(format), null); + + format = str; } PushItemWidth(dragFloatWidth); - if (DragFloat(componentIds[i], &value[i], dragSpeed, minValue[i], maxValue[i], format)) + if (DragFloat(componentIds[i], &value[i], dragSpeed, minValue[i], maxValue[i], + format.ToScopeCStr!:componentLoop(), flags: .NoRoundToFormat)) { changed = true; } diff --git a/ScriptCore/Editor/EntityEditor.cs b/ScriptCore/Editor/EntityEditor.cs index d277386..0f6ff67 100644 --- a/ScriptCore/Editor/EntityEditor.cs +++ b/ScriptCore/Editor/EntityEditor.cs @@ -276,6 +276,27 @@ internal class EntityEditor var value = (T)reference!; + if (format == null && value is float || value is double || value is Half) + { + format = "0.#####"; + } + + if (!format.StartsWith("%")) + { + if (value is float f) + { + format = f.ToString(format); + } + else if (value is double d) + { + format = d.ToString(format); + } + else if (value is Half h) + { + format = h.ToString(format); + } + } + if (range?.Slider == true) { if (ImGui.SliderScalar(fieldId, dataType, (IntPtr)(&value), (IntPtr)(&min), (IntPtr)(&max), format))