Skip to content

Commit

Permalink
EmptyFieldToZero ocornut#2
Browse files Browse the repository at this point in the history
  • Loading branch information
supermerill authored Feb 12, 2024
1 parent e14d3d0 commit fe3d5b2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
9 changes: 6 additions & 3 deletions ImGuiWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions imgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fe3d5b2

Please # to comment.