From fe3d5b2cccf14d869c76379ebb7973a968fe29f1 Mon Sep 17 00:00:00 2001 From: Merill Date: Mon, 12 Feb 2024 18:08:26 +0100 Subject: [PATCH] EmptyFieldToZero ocornut#2 ocornut/imgui#7305 --- ImGuiWrapper.cpp | 9 ++++++--- imgui.h | 1 + 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/ImGuiWrapper.cpp b/ImGuiWrapper.cpp index ede63bdfe507..458c7d757c3f 100644 --- a/ImGuiWrapper.cpp +++ b/ImGuiWrapper.cpp @@ -3535,7 +3535,8 @@ bool ImGui::InputScalar(const char* label, ImGuiDataType data_type, void* p_data char buf[64]; DataTypeFormatString(buf, IM_ARRAYSIZE(buf), data_type, p_data, format); - if ((flags & ImGuiInputTextFlags_EmptyZero) && IsZero(data_type, p_data)) + // If the current value is the default (0), then empty the input. + if ((flags & ImGuiInputTextFlags_EmptyDefaultToZero) && IsZero(data_type, p_data)) buf[0] = 0; flags |= ImGuiInputTextFlags_AutoSelectAll | (ImGuiInputTextFlags)ImGuiInputTextFlags_NoMarkEdited; // We call MarkItemEdited() ourselves by comparing the actual data rather than the string. @@ -3544,7 +3545,8 @@ bool ImGui::InputScalar(const char* label, ImGuiDataType data_type, void* p_data if (p_step == NULL) { if (InputText(label, buf, IM_ARRAYSIZE(buf), flags)) - if (buf[0] == 0 && (flags & ImGuiInputTextFlags_EmptyZero)) { + if (buf[0] == 0 && (flags & ImGuiInputTextFlags_EmptyDefaultToZero)) { + // The input is empty, set its value to the default (0) SetZero(data_type, p_data); value_changed = memcmp(&data_backup, p_data, DataTypeGetInfo(data_type)->Size) != 0; } else @@ -3558,7 +3560,8 @@ bool ImGui::InputScalar(const char* label, ImGuiDataType data_type, void* p_data PushID(label); SetNextItemWidth(ImMax(1.0f, CalcItemWidth() - (button_size + style.ItemInnerSpacing.x) * 2)); if (InputText("", buf, IM_ARRAYSIZE(buf), flags)) { // PushId(label) + "" gives us the expected ID from outside point of view - if (buf[0] == 0 && (flags & ImGuiInputTextFlags_EmptyZero)) { + if (buf[0] == 0 && (flags & ImGuiInputTextFlags_EmptyDefaultToZero)) { + // The input is empty, set its value to the default (0) SetZero(data_type, p_data); value_changed = memcmp(&data_backup, p_data, DataTypeGetInfo(data_type)->Size) != 0; } else diff --git a/imgui.h b/imgui.h index f6740ca35a06..a5ada0032e72 100644 --- a/imgui.h +++ b/imgui.h @@ -1074,6 +1074,7 @@ enum ImGuiInputTextFlags_ ImGuiInputTextFlags_CallbackResize = 1 << 18, // Callback on buffer capacity changes request (beyond 'buf_size' parameter value), allowing the string to grow. Notify when the string wants to be resized (for string types which hold a cache of their Size). You will be provided a new BufSize in the callback and NEED to honor it. (see misc/cpp/imgui_stdlib.h for an example of using this) ImGuiInputTextFlags_CallbackEdit = 1 << 19, // Callback on any edit (note that InputText() already returns true on edit, the callback is useful mainly to manipulate the underlying buffer while focus is active) ImGuiInputTextFlags_EscapeClearsAll = 1 << 20, // Escape key clears content if not empty, and deactivate otherwise (contrast to default behavior of Escape to revert) + ImGuiInputTextFlags_EmptyDefaultToZero = 1 << 21, // When the value is zero it isn't displayed, and when the input is cleared by the user, the value is set to zero. // Obsolete names //ImGuiInputTextFlags_AlwaysInsertMode = ImGuiInputTextFlags_AlwaysOverwrite // [renamed in 1.82] name was not matching behavior