Skip to content

Commit

Permalink
🚧 InputText: Tweak ImGuiInputTextFlags_EscapeClearsAll handling so de…
Browse files Browse the repository at this point in the history
…cision is taken on input buffer + Showcase a few more InputText() flags. (#5688, #2620)

               This makes is more obvious that value_change==true when apply_new_text != NULL.
  • Loading branch information
elect86 committed Sep 5, 2023
1 parent 339a118 commit 5babe81
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
11 changes: 11 additions & 0 deletions core/src/main/kotlin/imgui/demo/ShowDemoWindowWidgets.kt
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ import imgui.ImGui.treeNodeToLabelSpacing
import imgui.ImGui.treePop
import imgui.ImGui.unindent
import imgui.ImGui.windowDrawList
import imgui.InputTextFlag
import imgui.api.*
import imgui.api.demoDebugInformations.Companion.helpMarker
import imgui.classes.Color
Expand Down Expand Up @@ -1065,6 +1066,8 @@ object ShowDemoWindowWidgets {
var buf2 = ByteArray(64)
var buf3 = ByteArray(64)
var editCount = 0
val buf4 = ByteArray(16)
var flags1: InputTextSingleFlags = InputTextFlag.EscapeClearsAll
operator fun invoke() {
// To wire InputText() with std::string or any other custom string type,
// see the "Text Input > Resize Callback" section of this demo, and the misc/cpp/imgui_stdlib.h file.
Expand Down Expand Up @@ -1125,6 +1128,14 @@ object ShowDemoWindowWidgets {
Funcs2.MyInputTextMultiline("##MyStr", myStr, Vec2(-Float.MIN_VALUE, textLineHeight * 16), none)
text("Data: ${myStr.hashCode()}\nSize: ${myStr.strlen()}\nCapacity: ${myStr.size}")
}

// IMGUI_DEMO_MARKER("Widgets/Text Input/Miscellaneous");
treeNode("Miscellaneous") {
ImGui.checkboxFlags("ImGuiInputTextFlags_EscapeClearsAll", ::flags1, InputTextFlag.EscapeClearsAll)
ImGui.checkboxFlags("ImGuiInputTextFlags_ReadOnly", ::flags1, InputTextFlag.ReadOnly)
ImGui.checkboxFlags("ImGuiInputTextFlags_NoUndoRedo", ::flags1, InputTextFlag.NoUndoRedo)
ImGui.inputText("Hello", buf1, flags1)
}
}
}
}
Expand Down
12 changes: 8 additions & 4 deletions core/src/main/kotlin/imgui/internal/api/inputText.kt
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ internal interface inputText {
}
isCancel ->
if (flags has Itf.EscapeClearsAll) {
if (state.curLenA > 0)
if (buf[0] != 0.b)
revertEdit = true
else {
renderCursor = false; renderSelection = false
Expand Down Expand Up @@ -576,9 +576,10 @@ internal interface inputText {
if (revertEdit && !isReadOnly) {
if (flags has Itf.EscapeClearsAll) {
// Clear input
assert(buf[0] != 0.b)
applyNewText = ByteArray(0)
applyNewTextLength = 0
valueChanged /= buf[0] != 0.b
valueChanged = true
val emptyString = CharArray(0)
state.replace(emptyString, 0)
} else if (buf.strcmp(state.initialTextA) != 0) {
Expand Down Expand Up @@ -606,9 +607,12 @@ internal interface inputText {
textStrToUtf8(state.textA, state.textW)
}

// When using 'ImGuiInputTextFlags_EnterReturnsTrue' as a special case we reapply the live buffer back to the input buffer before clearing ActiveId, even though strictly speaking it wasn't modified on this frame.
// When using 'ImGuiInputTextFlags_EnterReturnsTrue' as a special case we reapply the live buffer back to the input buffer
// before clearing ActiveId, even though strictly speaking it wasn't modified on this frame.
// If we didn't do that, code like InputInt() with ImGuiInputTextFlags_EnterReturnsTrue would fail.
// This also allows the user to use InputText() with ImGuiInputTextFlags_EnterReturnsTrue without maintaining any user-side storage (please note that if you use this property along ImGuiInputTextFlags_CallbackResize you can end up with your temporary string object unnecessarily allocating once a frame, either store your string data, either if you don't then don't use ImGuiInputTextFlags_CallbackResize).
// This also allows the user to use InputText() with ImGuiInputTextFlags_EnterReturnsTrue without maintaining any user-side storage
// (please note that if you use this property along ImGuiInputTextFlags_CallbackResize you can end up with your temporary string object
// unnecessarily allocating once a frame, either store your string data, either if you don't then don't use ImGuiInputTextFlags_CallbackResize).
val applyEditBackToUserBuffer = !revertEdit || (validated && flags hasnt Itf.EnterReturnsTrue)
if (applyEditBackToUserBuffer) {
// Apply new value immediately - copy modified buffer back
Expand Down

0 comments on commit 5babe81

Please sign in to comment.