Added basic decimal editor

This commit is contained in:
Simon Lübeß
2024-01-11 22:55:18 +01:00
parent 99a12a118c
commit 7cdc398494
+39 -4
View File
@@ -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,8 +101,9 @@ internal class EntityEditor
{
// TODO: Call custom editors here!
ScriptGlue.Entity_GetScriptInstance(entityId, out object instance);
ScriptGlue.Entity_GetScriptInstance(entityId, out object? instance);
if (instance != null)
ShowEditor(entityType, instance);
}
@@ -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<Attribute>? 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<Attribute>? 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,7 +410,8 @@ internal class EntityEditor
if (ImGui.Checkbox($"##{fieldName}Y", ref value.Y))
newValue = value;
}
else
{
ImGui.EndDisabled();
// Struct
@@ -396,6 +430,7 @@ internal class EntityEditor
ImGui.BeginDisabled(readonlyAttribute != null);
}
}
}
else if (fieldType.IsSubclassOf(typeof(Entity)) || fieldType == typeof(Entity))
{
newValue = ShowEntityDropTarget(fieldName, fieldType, reference);