From 7cdc398494deb8c5e03703dc7609d18ffd4ac3f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Thu, 11 Jan 2024 22:55:18 +0100 Subject: [PATCH] Added basic decimal editor --- ScriptCore/Editor/EntityEditor.cs | 73 +++++++++++++++++++++++-------- 1 file changed, 54 insertions(+), 19 deletions(-) diff --git a/ScriptCore/Editor/EntityEditor.cs b/ScriptCore/Editor/EntityEditor.cs index 08df4ca..b516128 100644 --- a/ScriptCore/Editor/EntityEditor.cs +++ b/ScriptCore/Editor/EntityEditor.cs @@ -2,6 +2,7 @@ using System.Collections; using System.Collections.Generic; using System.Diagnostics; +using System.Globalization; using System.Linq; using System.Numerics; using System.Reflection; @@ -100,9 +101,10 @@ internal class EntityEditor { // TODO: Call custom editors here! - ScriptGlue.Entity_GetScriptInstance(entityId, out object instance); + ScriptGlue.Entity_GetScriptInstance(entityId, out object? instance); - ShowEditor(entityType, instance); + if (instance != null) + ShowEditor(entityType, instance); } private static T? GetAttribute(IEnumerable? attributes) where T : Attribute @@ -110,6 +112,34 @@ internal class EntityEditor return (T?)attributes?.FirstOrDefault(a => a is T); } + private static object ShowDecimalEditor(object? reference, Type fieldType, string fieldName, IEnumerable? attributes) + { + if (reference is not decimal value) + return DidNotChange; + + string stringValue = value.ToString(CultureInfo.InvariantCulture); + + byte[] bytes = new byte[30 * 4]; + + Encoding.UTF8.GetBytes(stringValue, 0, stringValue.Length, bytes, 0); + + ImGui.PushID("Decimal"); + + if (ImGui.InputText(fieldName, bytes, (uint)bytes.Length, ImGuiInputTextFlags.EnterReturnsTrue | ImGuiInputTextFlags.CharsDecimal)) + { + string text = Encoding.UTF8.GetString(bytes); + + if (decimal.TryParse(text, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out decimal newValue)) + { + ImGui.PopID(); + return newValue; + } + } + ImGui.PopID(); + + return DidNotChange; + } + private static object? ShowPrimitiveEditor(object? reference, Type fieldType, string fieldName, IEnumerable? attributes) { object newValue = DidNotChange; @@ -184,7 +214,6 @@ internal class EntityEditor } } - // TODO: allow editing in edit-mode if (reference != null) { if (fieldType == typeof(bool)) @@ -361,7 +390,11 @@ internal class EntityEditor } else if (fieldType.IsValueType) { - if (fieldType == typeof(bool2)) + if (fieldType == typeof(decimal)) + { + newValue = ShowDecimalEditor(reference, fieldType, fieldName, attributes); + } + else if (fieldType == typeof(bool2)) { bool2 value = (bool2)reference; @@ -377,23 +410,25 @@ internal class EntityEditor if (ImGui.Checkbox($"##{fieldName}Y", ref value.Y)) newValue = value; } - - ImGui.EndDisabled(); - - // Struct - if (ImGui.TreeNode(fieldName)) - { - ImGui.BeginDisabled(readonlyAttribute != null); - - ShowEditor(fieldType, reference); - - ImGui.TreePop(); - - newValue = reference; - } else { - ImGui.BeginDisabled(readonlyAttribute != null); + ImGui.EndDisabled(); + + // Struct + if (ImGui.TreeNode(fieldName)) + { + ImGui.BeginDisabled(readonlyAttribute != null); + + ShowEditor(fieldType, reference); + + ImGui.TreePop(); + + newValue = reference; + } + else + { + ImGui.BeginDisabled(readonlyAttribute != null); + } } } else if (fieldType.IsSubclassOf(typeof(Entity)) || fieldType == typeof(Entity))