From df446188b63c14c28aa65cbb047d35f2082d166d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Fri, 1 Dec 2023 23:13:30 +0100 Subject: [PATCH] Add support for strings and BoolX in script engine, show strings as textfields in editor --- GlitchyEngine/src/Scripting/ScriptEngine.bf | 4 +++ .../src/Scripting/ScriptFieldType.bf | 18 +++++++++++-- ScriptCore/Editor/EntityEditor.cs | 27 +++++++++++++++++++ ScriptCore/Editor/TextFieldAttribute.cs | 19 +++++++++++++ 4 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 ScriptCore/Editor/TextFieldAttribute.cs diff --git a/GlitchyEngine/src/Scripting/ScriptEngine.bf b/GlitchyEngine/src/Scripting/ScriptEngine.bf index 59bfc23..d982cc9 100644 --- a/GlitchyEngine/src/Scripting/ScriptEngine.bf +++ b/GlitchyEngine/src/Scripting/ScriptEngine.bf @@ -14,6 +14,8 @@ static sealed class ScriptEngineHelper { private static Dictionary _scriptFieldTypes = new Dictionary() { + ("System.String", .String), + ("System.Boolean", .Bool), ("System.Char", .Char), @@ -382,6 +384,8 @@ static class ScriptEngine // TODO: Handle Structs when copying fields to instance. case .Class: // TODO: Handle Class when copying fields to instance. + case .String: + // TODO: Handle String when copying fields to instance. default: script.Instance.SetFieldValue(scriptField, field._data); } diff --git a/GlitchyEngine/src/Scripting/ScriptFieldType.bf b/GlitchyEngine/src/Scripting/ScriptFieldType.bf index 776c0b5..360fa93 100644 --- a/GlitchyEngine/src/Scripting/ScriptFieldType.bf +++ b/GlitchyEngine/src/Scripting/ScriptFieldType.bf @@ -15,7 +15,9 @@ enum ScriptFieldType case GenericClass; case Array; - case Bool; + case String; + + case Bool, Bool2, Bool3, Bool4; case Char; case SByte; @@ -29,7 +31,7 @@ enum ScriptFieldType case Float, float2, float3, float4; case Double, Double2, Double3, Double4; - + case Entity; case Component; @@ -37,8 +39,20 @@ enum ScriptFieldType { switch(this) { + case .String: + return typeof(String); + case .Bool: return typeof(bool); + case .Bool2: + return typeof(bool2); + case .Bool3: + return typeof(bool3); + case .Bool4: + return typeof(bool4); + + case .Char: + return typeof(char16); case .SByte: return typeof(int8); diff --git a/ScriptCore/Editor/EntityEditor.cs b/ScriptCore/Editor/EntityEditor.cs index 1558a2e..921ef07 100644 --- a/ScriptCore/Editor/EntityEditor.cs +++ b/ScriptCore/Editor/EntityEditor.cs @@ -274,6 +274,10 @@ internal class EntityEditor // TODO: Show component drop target ImGui.Text($"{fieldName} Component ({fieldType.Name})"); } + else if (fieldType == typeof(string)) + { + newValue = ShowStringEditor(reference, fieldType, fieldName, attributes); + } else { ImGui.EndDisabled(); @@ -328,6 +332,29 @@ internal class EntityEditor return newValue; } + private static object ShowStringEditor(object reference, Type fieldType, string fieldName, IEnumerable attributes) + { + object newValue = DidNotChange; + + TextFieldAttribute textField = GetAttribute(attributes); + + string value = reference as string ?? $"{float.MinValue}"; + + if (textField?.Multiline == true) + { + ImGui.Text(fieldName); + if (ImGui.InputTextMultiline($"##{fieldName}", ref value, 1000, new Vector2(-1.0f, ImGui.GetTextLineHeight() * textField.TextFieldLines))) + newValue = value; + } + else + { + if (ImGui.InputText(fieldName, ref value, 1000)) + newValue = value; + } + + return newValue; + } + public static void ShowEditor(Type type, object reference) { T GetValue(FieldInfo fieldInfo) diff --git a/ScriptCore/Editor/TextFieldAttribute.cs b/ScriptCore/Editor/TextFieldAttribute.cs new file mode 100644 index 0000000..a2fa8a8 --- /dev/null +++ b/ScriptCore/Editor/TextFieldAttribute.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace GlitchyEngine.Editor; + +public sealed class TextFieldAttribute : Attribute +{ + /// + /// If the string field will be shown in a multiline text box. + /// + public bool Multiline { get; set; } + + /// + /// The number of lines that the text field has. Only applies if is set to . + /// It does not affect how many lines of text can be stored in the field. Only how many lines the text field itself has. + /// + public int TextFieldLines { get; set; } = 3; +}