From 3c0302b06c3aad65aeb8d6ad42e303109a7b97b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Sat, 17 Feb 2024 22:53:58 +0100 Subject: [PATCH] Editor: Show pretty script field names --- ScriptCore/Editor/EntityEditor.cs | 4 +- ScriptCore/Extensions/StringExtension.cs | 73 ++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 ScriptCore/Extensions/StringExtension.cs diff --git a/ScriptCore/Editor/EntityEditor.cs b/ScriptCore/Editor/EntityEditor.cs index d57927e..a6dea52 100644 --- a/ScriptCore/Editor/EntityEditor.cs +++ b/ScriptCore/Editor/EntityEditor.cs @@ -852,7 +852,9 @@ internal class EntityEditor IEnumerable attributes = field.GetCustomAttributes(); - object? newValue = ShowFieldEditor(value, value?.GetType() ?? field.FieldType, field.Name, attributes); + string prettyName = field.Name.ToPrettyName(); + + object? newValue = ShowFieldEditor(value, value?.GetType() ?? field.FieldType, prettyName, attributes); if (newValue != DidNotChange) { diff --git a/ScriptCore/Extensions/StringExtension.cs b/ScriptCore/Extensions/StringExtension.cs new file mode 100644 index 0000000..85db751 --- /dev/null +++ b/ScriptCore/Extensions/StringExtension.cs @@ -0,0 +1,73 @@ +using System.Text; + +namespace GlitchyEngine.Extensions; + +public static class StringExtension +{ + /// + /// Converts a variable name to a pretty name as good as reasonably possible. + /// + /// The name of a variable to prettify. + /// The pretty string. + public static string ToPrettyName(this string uglyName) + { + StringBuilder sb = new StringBuilder(uglyName.Length); + + bool wasUpper = false; + bool inWord = false; + bool inNumber = false; + + foreach (char c in uglyName) + { + if (char.IsLetter(c)) + { + if (inNumber) + { + sb.Append(' '); + inNumber = false; + } + + bool newWord = !inWord; + + if (char.IsUpper(c) && !wasUpper) + { + newWord = true; + wasUpper = true; + + if (inWord) + { + sb.Append(' '); + } + } + else if (char.IsLower(c)) + { + wasUpper = false; + } + + sb.Append(newWord ? char.ToUpper(c) : char.ToLower(c)); + + inWord = true; + } + else if (char.IsDigit(c)) + { + if (inWord) + { + sb.Append(' '); + inWord = false; + inNumber = true; + } + + sb.Append(c); + } + else if (inWord || inNumber) + { + sb.Append(' '); + inWord = false; + inNumber = false; + wasUpper = false; + } + } + + return sb.ToString(); + } +}