From 9c90b6bb3f50f58ffd75fbfee562b3ebaf49996e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Fri, 23 Oct 2026 22:11:38 +0200 Subject: [PATCH] ImGui extension for InputText that works on a mutable String directly --- GlitchyEditor/src/ImGui/ImGuiExtension.bf | 29 +++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/GlitchyEditor/src/ImGui/ImGuiExtension.bf b/GlitchyEditor/src/ImGui/ImGuiExtension.bf index 32d2e90..e794e3e 100644 --- a/GlitchyEditor/src/ImGui/ImGuiExtension.bf +++ b/GlitchyEditor/src/ImGui/ImGuiExtension.bf @@ -535,6 +535,35 @@ namespace ImGui ImGui.EndTooltip(); } + /// Provides a text input field that allows to edit the given string directly. The string will grow as needed. + /// Returns true if the text was changed. + public static bool InputText(char8* label, String str, InputTextFlags flags = .None) + { + return ImGui.InputText(label, str.CStr(), (.)str.AllocSize, flags | .CallbackResize, => StringResizeCallback, Internal.UnsafeCastToPtr(str)); + } + + private static int StringResizeCallback(InputTextCallbackData* data) + { + if (data.EventFlag != .CallbackResize) + return 0; + + String str = (String)Internal.UnsafeCastToObject(data.UserData); + + int newLength = data.BufTextLen; + + // PrepareBuffer increases the length and reserves room for the null terminator. + if (newLength > str.Length) + str.PrepareBuffer(newLength - str.Length); + else + str.Length = newLength; + + // The string might have been reallocated. + data.Buf = str.Ptr; + data.BufSize = (.)str.AllocSize; + + return 0; + } + [Comptime] private static DataType GetDataType() {