From cb166876416ebe68c2677ac159fd84adca2ed5f6 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Wed, 2 Oct 2024 20:55:12 -0500 Subject: [PATCH] [studio] Add variant of InputText that returns an IString --- .../modlib/include/studio/imguiutil.hpp | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/olympic/studio/modlib/include/studio/imguiutil.hpp b/src/olympic/studio/modlib/include/studio/imguiutil.hpp index e0053901..cc343bf1 100644 --- a/src/olympic/studio/modlib/include/studio/imguiutil.hpp +++ b/src/olympic/studio/modlib/include/studio/imguiutil.hpp @@ -128,9 +128,35 @@ void centerNextWindow(turbine::Context &ctx) noexcept; bool PushButton(ox::CStringView lbl, ImVec2 const&btnSz = BtnSz) noexcept; +template +struct TextInput { + bool changed{}; + Str text; + explicit constexpr operator ox::String() const noexcept { return ox::String{text}; } + constexpr operator ox::CStringView() const noexcept { return text; } + constexpr operator ox::StringView() const noexcept { return text; } + constexpr operator bool() const noexcept { return changed; } +}; + +template +TextInput> InputText( + ox::CStringViewCR label, + ox::StringViewCR currentText, + ImGuiInputTextFlags const flags = 0, + ImGuiInputTextCallback const callback = nullptr, + void *user_data = nullptr) noexcept { + TextInput> out = {.text = currentText}; + out.changed = ImGui::InputText( + label.c_str(), out.text.data(), MaxChars + 1, flags, callback, user_data); + if (out.changed) { + std::ignore = out.text.unsafeResize(ox::strlen(out.text.c_str())); + } + return out; +} + template bool InputText( - ox::CStringView label, + ox::CStringViewCR label, ox::IString &text, ImGuiInputTextFlags const flags = 0, ImGuiInputTextCallback const callback = nullptr,