Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear InputText box after pressing Enter #7430

Closed
lostburn opened this issue Mar 23, 2024 · 2 comments
Closed

Clear InputText box after pressing Enter #7430

lostburn opened this issue Mar 23, 2024 · 2 comments

Comments

@lostburn
Copy link

Version/Branch of Dear ImGui:

Version 1.90, Branch: docking

Back-ends:

imgui_impl_dx12.cpp + imgui_impl_win32.cpp

Compiler, OS:

Windows 10 - Rider

Full config/build information:

Dear ImGui 1.90.5 WIP (19044)
--------------------------------
sizeof(size_t): 8, sizeof(ImDrawIdx): 2, sizeof(ImDrawVert): 20
define: __cplusplus=199711
define: _WIN32
define: _WIN64
define: _MSC_VER=1939
define: _MSVC_LANG=202002
define: IMGUI_HAS_VIEWPORT
define: IMGUI_HAS_DOCK
--------------------------------
io.BackendPlatformName: imgui_impl_win32
io.BackendRendererName: imgui_impl_dx12
io.ConfigFlags: 0x00000441
 NavEnableKeyboard
 DockingEnable
 ViewportsEnable
io.ConfigViewportsNoDecoration
io.ConfigInputTextCursorBlink
io.ConfigWindowsResizeFromEdges
io.ConfigMemoryCompactTimer = 60.0
io.BackendFlags: 0x00001C0E
 HasMouseCursors
 HasSetMousePos
 PlatformHasViewports
 HasMouseHoveredViewport
 RendererHasVtxOffset
 RendererHasViewports
--------------------------------
io.Fonts: 1 fonts, Flags: 0x00000000, TexSize: 512,64
io.DisplaySize: 1264.00,761.00
io.DisplayFramebufferScale: 1.00,1.00
--------------------------------
style.WindowPadding: 8.00,8.00
style.WindowBorderSize: 1.00
style.FramePadding: 4.00,3.00
style.FrameRounding: 0.00
style.FrameBorderSize: 0.00
style.ItemSpacing: 8.00,4.00
style.ItemInnerSpacing: 4.00,4.00

Details:

I'm trying to create a simple input mechanic that the player can type words and commit them to do some actions.
My idea is that every time the player press enter, it will send that text to another widget (a history box) and check if that word should do anything.

However, whenever I press enter, the text gets selected and I can't clear it. I tried with different flags, no flags, and with the
ImGui::IsItemDeactivatedAfterEdit() as well.

Basically, I want to be able to have a similar functionality as the ESC key with the ENTER key.
Any help is appreciated, I am stuck in this for a couple days now.

Screenshots/Video:

Eternal.Ascent.2024-03-23.00-01-11.mp4

Minimal, Complete and Verifiable Example code:

// In the loop Update()

static std::string myInput;
if(ImGui::InputText("Input", &myInput, ImGuiInputTextFlags_EscapeClearsAll | ImGuiInputTextFlags_EnterReturnsTrue)) {
    // Send this text to another widget
    // clear the current text.
    myInput.clear(); 
}
@cfillion
Copy link
Contributor

cfillion commented Mar 23, 2024

Assuming you still want ImGui::GetIO().ConfigInputTextEnterKeepActive enabled: v1.90.2 added functions to imgui_internal.h for this:

myInput.clear();
if(ImGuiInputTextState* state { ImGui::GetInputTextState(ImGui::GetItemID()) })
  state->ReloadUserBufAndSelectAll();

In older versions InputText's internal buffer may still be cleared while active using ImGuiInputTextCallback.

static std::string myInput;
static bool clearBuffer;
ImGuiInputTextFlags flags { ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_EscapeClearsAll };
ImGuiInputTextCallback callback {};
if(clearBuffer) {
  callback = [](ImGuiInputTextCallbackData *data) { data->DeleteChars(0, data->BufTextLen); return 0; };
  flags |= ImGuiInputTextFlags_CallbackAlways;
  clearBuffer = false;
}
if(ImGui::InputText("Input", &myInput, flags, callback)) {
  myInput.clear();
  clearBuffer = true;
}

@lostburn
Copy link
Author

lostburn commented Mar 23, 2024

Assuming you still want ImGui::GetIO().ConfigInputTextEnterKeepActive enabled: v1.90.2 added functions to imgui_internal.h for this:

myInput.clear();
if(ImGuiInputTextState* state { ImGui::GetInputTextState(ImGui::GetItemID()) })
  state->ReloadUserBufAndSelectAll();

That was exactly what I was looking! Thank you for the help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants