Skip to content

Commit 086c392

Browse files
committed
Internals: Fixed DragInt* default format string. InputScalar(), InputScalarN(), removed InputFloatN(), InputInt(). Note that DragInt2/3/4 will %f format strings will currently be broken. (#320, #643, #708, #1011)
1 parent 6c93247 commit 086c392

File tree

5 files changed

+40
-70
lines changed

5 files changed

+40
-70
lines changed

CHANGELOG.txt

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ Other Changes:
6464
- DragFloat, SliderFloat: Fixes to allow input of scientific notation numbers when using CTRL+Click to input the value. (~#648, #1011)
6565
- DragFloat, SliderFloat: Rounding-on-write uses the provided format string instead of parsing the precision from the string, which allows for finer uses of %e %g etc. (#648, #642)
6666
- DragFloat: Improved computation when using the power curve. Improved lost of input precision with very small steps. Added an assert than power-curve requires a min/max range. (~#642)
67+
- DragFloat: The 'power' parameter is only honored if the min/max parameter are also setup.
6768
- Nav: Fixed hovering a Selectable() with the mouse so that it update the navigation cursor (as it happened in the pre 1.60 navigation branch). (#787)
6869
- Style: Changed default style.DisplaySafeAreaPadding values from (4,4) to (3,3) so it is smaller than FramePadding and has no effect on main menu bar on a computer. (#1439)
6970
- Misc: Added IMGUI_CHECKVERSION() macro to compare version string and data structure sizes in order to catch issues with mismatching compilation unit settings. (#1695, #1769)

imgui.cpp

+31-61
Original file line numberDiff line numberDiff line change
@@ -8533,7 +8533,7 @@ static const ImGuiDataTypeInfo GDataTypeInfo[ImGuiDataType_COUNT] =
85338533

85348534
// User can input math operators (e.g. +100) to edit a numerical values.
85358535
// NB: This is _not_ a full expression evaluator. We should probably add one and replace this dumb mess..
8536-
static bool DataTypeApplyOpFromText(const char* buf, const char* initial_value_buf, ImGuiDataType data_type, void* data_ptr, const char* scalar_format)
8536+
static bool DataTypeApplyOpFromText(const char* buf, const char* initial_value_buf, ImGuiDataType data_type, void* data_ptr, const char* format)
85378537
{
85388538
while (ImCharIsBlankA(*buf))
85398539
buf++;
@@ -8560,38 +8560,38 @@ static bool DataTypeApplyOpFromText(const char* buf, const char* initial_value_b
85608560
IM_ASSERT(GDataTypeInfo[data_type].Size <= sizeof(data_backup));
85618561
memcpy(data_backup, data_ptr, GDataTypeInfo[data_type].Size);
85628562

8563-
if (scalar_format == NULL)
8564-
scalar_format = GDataTypeInfo[data_type].ScanFmt;
8563+
if (format == NULL)
8564+
format = GDataTypeInfo[data_type].ScanFmt;
85658565

85668566
int arg1i = 0;
85678567
if (data_type == ImGuiDataType_S32)
85688568
{
85698569
int* v = (int*)data_ptr;
85708570
int arg0i = *v;
85718571
float arg1f = 0.0f;
8572-
if (op && sscanf(initial_value_buf, scalar_format, &arg0i) < 1)
8572+
if (op && sscanf(initial_value_buf, format, &arg0i) < 1)
85738573
return false;
85748574
// Store operand in a float so we can use fractional value for multipliers (*1.1), but constant always parsed as integer so we can fit big integers (e.g. 2000000003) past float precision
85758575
if (op == '+') { if (sscanf(buf, "%d", &arg1i)) *v = (int)(arg0i + arg1i); } // Add (use "+-" to subtract)
85768576
else if (op == '*') { if (sscanf(buf, "%f", &arg1f)) *v = (int)(arg0i * arg1f); } // Multiply
85778577
else if (op == '/') { if (sscanf(buf, "%f", &arg1f) && arg1f != 0.0f) *v = (int)(arg0i / arg1f); } // Divide
8578-
else { if (sscanf(buf, scalar_format, &arg1i) == 1) *v = arg1i; } // Assign constant
8578+
else { if (sscanf(buf, format, &arg1i) == 1) *v = arg1i; } // Assign constant
85798579
}
85808580
else if (data_type == ImGuiDataType_U32 || data_type == ImGuiDataType_S64 || data_type == ImGuiDataType_U64)
85818581
{
85828582
// Assign constant
85838583
// FIXME: We don't bother handling support for legacy operators since they are a little too crappy. Instead we may implement a proper expression evaluator in the future.
8584-
sscanf(buf, scalar_format, data_ptr);
8584+
sscanf(buf, format, data_ptr);
85858585
}
85868586
else if (data_type == ImGuiDataType_Float)
85878587
{
85888588
// For floats we have to ignore format with precision (e.g. "%.2f") because sscanf doesn't take them in
8589-
scalar_format = "%f";
8589+
format = "%f";
85908590
float* v = (float*)data_ptr;
85918591
float arg0f = *v, arg1f = 0.0f;
8592-
if (op && sscanf(initial_value_buf, scalar_format, &arg0f) < 1)
8592+
if (op && sscanf(initial_value_buf, format, &arg0f) < 1)
85938593
return false;
8594-
if (sscanf(buf, scalar_format, &arg1f) < 1)
8594+
if (sscanf(buf, format, &arg1f) < 1)
85958595
return false;
85968596
if (op == '+') { *v = arg0f + arg1f; } // Add (use "+-" to subtract)
85978597
else if (op == '*') { *v = arg0f * arg1f; } // Multiply
@@ -8600,12 +8600,12 @@ static bool DataTypeApplyOpFromText(const char* buf, const char* initial_value_b
86008600
}
86018601
else if (data_type == ImGuiDataType_Double)
86028602
{
8603-
scalar_format = "%lf"; // scanf differentiate float/double unlike printf which forces everything to double because of ellipsis
8603+
format = "%lf"; // scanf differentiate float/double unlike printf which forces everything to double because of ellipsis
86048604
double* v = (double*)data_ptr;
86058605
double arg0f = *v, arg1f = 0.0;
8606-
if (op && sscanf(initial_value_buf, scalar_format, &arg0f) < 1)
8606+
if (op && sscanf(initial_value_buf, format, &arg0f) < 1)
86078607
return false;
8608-
if (sscanf(buf, scalar_format, &arg1f) < 1)
8608+
if (sscanf(buf, format, &arg1f) < 1)
86098609
return false;
86108610
if (op == '+') { *v = arg0f + arg1f; } // Add (use "+-" to subtract)
86118611
else if (op == '*') { *v = arg0f * arg1f; } // Multiply
@@ -9406,12 +9406,7 @@ bool ImGui::DragFloatRange2(const char* label, float* v_current_min, float* v_cu
94069406
// NB: v_speed is float to allow adjusting the drag speed with more precision
94079407
bool ImGui::DragInt(const char* label, int* v, float v_speed, int v_min, int v_max, const char* format)
94089408
{
9409-
if (!format)
9410-
format = "%.0f";
9411-
float v_f = (float)*v;
9412-
bool value_changed = DragFloat(label, &v_f, v_speed, (float)v_min, (float)v_max, format);
9413-
*v = (int)v_f;
9414-
return value_changed;
9409+
return DragScalar(label, ImGuiDataType_S32, v, v_speed, &v_min, &v_max, format);
94159410
}
94169411

94179412
bool ImGui::DragInt2(const char* label, int v[2], float v_speed, int v_min, int v_max, const char* format)
@@ -10647,8 +10642,8 @@ bool ImGui::InputTextMultiline(const char* label, char* buf, size_t buf_size, co
1064710642
return InputTextEx(label, buf, (int)buf_size, size, flags | ImGuiInputTextFlags_Multiline, callback, user_data);
1064810643
}
1064910644

10650-
// NB: scalar_format here must be a simple "%xx" format string with no prefix/suffix (unlike the Drag/Slider functions "format" argument)
10651-
bool ImGui::InputScalar(const char* label, ImGuiDataType data_type, void* data_ptr, void* step_ptr, void* step_fast_ptr, const char* scalar_format, ImGuiInputTextFlags extra_flags)
10645+
// NB: format here must be a simple "%xx" format string with no prefix/suffix (unlike the Drag/Slider functions "format" argument)
10646+
bool ImGui::InputScalar(const char* label, ImGuiDataType data_type, void* data_ptr, void* step_ptr, void* step_fast_ptr, const char* format, ImGuiInputTextFlags extra_flags)
1065210647
{
1065310648
ImGuiWindow* window = GetCurrentWindow();
1065410649
if (window->SkipItems)
@@ -10658,7 +10653,7 @@ bool ImGui::InputScalar(const char* label, ImGuiDataType data_type, void* data_p
1065810653
const ImGuiStyle& style = g.Style;
1065910654

1066010655
char buf[64];
10661-
DataTypeFormatString(buf, IM_ARRAYSIZE(buf), data_type, data_ptr, scalar_format);
10656+
DataTypeFormatString(buf, IM_ARRAYSIZE(buf), data_type, data_ptr, format);
1066210657

1066310658
bool value_changed = false;
1066410659
if ((extra_flags & (ImGuiInputTextFlags_CharsHexadecimal | ImGuiInputTextFlags_CharsScientific)) == 0)
@@ -10673,7 +10668,7 @@ bool ImGui::InputScalar(const char* label, ImGuiDataType data_type, void* data_p
1067310668
PushID(label);
1067410669
PushItemWidth(ImMax(1.0f, CalcItemWidth() - (button_size + style.ItemInnerSpacing.x) * 2));
1067510670
if (InputText("", buf, IM_ARRAYSIZE(buf), extra_flags)) // PushId(label) + "" gives us the expected ID from outside point of view
10676-
value_changed = DataTypeApplyOpFromText(buf, g.InputTextState.InitialText.Data, data_type, data_ptr, scalar_format);
10671+
value_changed = DataTypeApplyOpFromText(buf, g.InputTextState.InitialText.Data, data_type, data_ptr, format);
1067710672
PopItemWidth();
1067810673

1067910674
// Step buttons
@@ -10698,7 +10693,7 @@ bool ImGui::InputScalar(const char* label, ImGuiDataType data_type, void* data_p
1069810693
else
1069910694
{
1070010695
if (InputText(label, buf, IM_ARRAYSIZE(buf), extra_flags))
10701-
value_changed = DataTypeApplyOpFromText(buf, g.InputTextState.InitialText.Data, data_type, data_ptr, scalar_format);
10696+
value_changed = DataTypeApplyOpFromText(buf, g.InputTextState.InitialText.Data, data_type, data_ptr, format);
1070210697
}
1070310698

1070410699
return value_changed;
@@ -10723,7 +10718,7 @@ bool ImGui::InputInt(const char* label, int* v, int step, int step_fast, ImGuiIn
1072310718
return InputScalar(label, ImGuiDataType_S32, (void*)v, (void*)(step>0 ? &step : NULL), (void*)(step_fast>0 ? &step_fast : NULL), format, extra_flags);
1072410719
}
1072510720

10726-
bool ImGui::InputFloatN(const char* label, float* v, int components, const char* format, ImGuiInputTextFlags extra_flags)
10721+
bool ImGui::InputScalarN(const char* label, ImGuiDataType data_type, void* v, int components, void* step_ptr, void* step_fast_ptr, const char* format, ImGuiInputTextFlags extra_flags)
1072710722
{
1072810723
ImGuiWindow* window = GetCurrentWindow();
1072910724
if (window->SkipItems)
@@ -10734,13 +10729,15 @@ bool ImGui::InputFloatN(const char* label, float* v, int components, const char*
1073410729
BeginGroup();
1073510730
PushID(label);
1073610731
PushMultiItemsWidths(components);
10732+
size_t type_size = GDataTypeInfo[data_type].Size;
1073710733
for (int i = 0; i < components; i++)
1073810734
{
1073910735
PushID(i);
10740-
value_changed |= InputFloat("##v", &v[i], 0, 0, format, extra_flags);
10736+
value_changed |= InputScalar("##v", data_type, v, step_ptr, step_fast_ptr, format, extra_flags);
1074110737
SameLine(0, g.Style.ItemInnerSpacing.x);
1074210738
PopID();
1074310739
PopItemWidth();
10740+
v = (void*)((char*)v + type_size);
1074410741
}
1074510742
PopID();
1074610743

@@ -10752,17 +10749,17 @@ bool ImGui::InputFloatN(const char* label, float* v, int components, const char*
1075210749

1075310750
bool ImGui::InputFloat2(const char* label, float v[2], const char* format, ImGuiInputTextFlags extra_flags)
1075410751
{
10755-
return InputFloatN(label, v, 2, format, extra_flags);
10752+
return InputScalarN(label, ImGuiDataType_Float, v, 2, NULL, NULL, format, extra_flags);
1075610753
}
1075710754

1075810755
bool ImGui::InputFloat3(const char* label, float v[3], const char* format, ImGuiInputTextFlags extra_flags)
1075910756
{
10760-
return InputFloatN(label, v, 3, format, extra_flags);
10757+
return InputScalarN(label, ImGuiDataType_Float, v, 3, NULL, NULL, format, extra_flags);
1076110758
}
1076210759

1076310760
bool ImGui::InputFloat4(const char* label, float v[4], const char* format, ImGuiInputTextFlags extra_flags)
1076410761
{
10765-
return InputFloatN(label, v, 4, format, extra_flags);
10762+
return InputScalarN(label, ImGuiDataType_Float, v, 4, NULL, NULL, format, extra_flags);
1076610763
}
1076710764

1076810765
// Prefer using "const char* format" directly, which is more flexible and consistent with other API.
@@ -10780,66 +10777,39 @@ bool ImGui::InputFloat2(const char* label, float v[2], int decimal_precision, Im
1078010777
char format[16] = "%f";
1078110778
if (decimal_precision >= 0)
1078210779
ImFormatString(format, IM_ARRAYSIZE(format), "%%.%df", decimal_precision);
10783-
return InputFloatN(label, v, 2, format, extra_flags);
10780+
return InputScalarN(label, ImGuiDataType_Float, v, 2, NULL, NULL, format, extra_flags);
1078410781
}
1078510782

1078610783
bool ImGui::InputFloat3(const char* label, float v[3], int decimal_precision, ImGuiInputTextFlags extra_flags)
1078710784
{
1078810785
char format[16] = "%f";
1078910786
if (decimal_precision >= 0)
1079010787
ImFormatString(format, IM_ARRAYSIZE(format), "%%.%df", decimal_precision);
10791-
return InputFloatN(label, v, 3, format, extra_flags);
10788+
return InputScalarN(label, ImGuiDataType_Float, v, 3, NULL, NULL, format, extra_flags);
1079210789
}
1079310790

1079410791
bool ImGui::InputFloat4(const char* label, float v[4], int decimal_precision, ImGuiInputTextFlags extra_flags)
1079510792
{
1079610793
char format[16] = "%f";
1079710794
if (decimal_precision >= 0)
1079810795
ImFormatString(format, IM_ARRAYSIZE(format), "%%.%df", decimal_precision);
10799-
return InputFloatN(label, v, 4, format, extra_flags);
10796+
return InputScalarN(label, ImGuiDataType_Float, v, 4, NULL, NULL, format, extra_flags);
1080010797
}
1080110798
#endif // IMGUI_DISABLE_OBSOLETE_FUNCTIONS
1080210799

10803-
bool ImGui::InputIntN(const char* label, int* v, int components, ImGuiInputTextFlags extra_flags)
10804-
{
10805-
ImGuiWindow* window = GetCurrentWindow();
10806-
if (window->SkipItems)
10807-
return false;
10808-
10809-
ImGuiContext& g = *GImGui;
10810-
bool value_changed = false;
10811-
BeginGroup();
10812-
PushID(label);
10813-
PushMultiItemsWidths(components);
10814-
for (int i = 0; i < components; i++)
10815-
{
10816-
PushID(i);
10817-
value_changed |= InputInt("##v", &v[i], 0, 0, extra_flags);
10818-
SameLine(0, g.Style.ItemInnerSpacing.x);
10819-
PopID();
10820-
PopItemWidth();
10821-
}
10822-
PopID();
10823-
10824-
TextUnformatted(label, FindRenderedTextEnd(label));
10825-
EndGroup();
10826-
10827-
return value_changed;
10828-
}
10829-
1083010800
bool ImGui::InputInt2(const char* label, int v[2], ImGuiInputTextFlags extra_flags)
1083110801
{
10832-
return InputIntN(label, v, 2, extra_flags);
10802+
return InputScalarN(label, ImGuiDataType_S32, v, 2, NULL, NULL, "%d", extra_flags);
1083310803
}
1083410804

1083510805
bool ImGui::InputInt3(const char* label, int v[3], ImGuiInputTextFlags extra_flags)
1083610806
{
10837-
return InputIntN(label, v, 3, extra_flags);
10807+
return InputScalarN(label, ImGuiDataType_S32, v, 3, NULL, NULL, "%d", extra_flags);
1083810808
}
1083910809

1084010810
bool ImGui::InputInt4(const char* label, int v[4], ImGuiInputTextFlags extra_flags)
1084110811
{
10842-
return InputIntN(label, v, 4, extra_flags);
10812+
return InputScalarN(label, ImGuiDataType_S32, v, 4, NULL, NULL, "%d", extra_flags);
1084310813
}
1084410814

1084510815
static float CalcMaxPopupHeightFromItemCount(int items_count)

imgui.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -347,11 +347,11 @@ namespace ImGui
347347
IMGUI_API bool DragFloat3(const char* label, float v[3], float v_speed = 1.0f, float v_min = 0.0f, float v_max = 0.0f, const char* format = "%.3f", float power = 1.0f);
348348
IMGUI_API bool DragFloat4(const char* label, float v[4], float v_speed = 1.0f, float v_min = 0.0f, float v_max = 0.0f, const char* format = "%.3f", float power = 1.0f);
349349
IMGUI_API bool DragFloatRange2(const char* label, float* v_current_min, float* v_current_max, float v_speed = 1.0f, float v_min = 0.0f, float v_max = 0.0f, const char* format = "%.3f", const char* format_max = NULL, float power = 1.0f);
350-
IMGUI_API bool DragInt(const char* label, int* v, float v_speed = 1.0f, int v_min = 0, int v_max = 0, const char* format = "%.0f"); // If v_min >= v_max we have no bound
351-
IMGUI_API bool DragInt2(const char* label, int v[2], float v_speed = 1.0f, int v_min = 0, int v_max = 0, const char* format = "%.0f");
352-
IMGUI_API bool DragInt3(const char* label, int v[3], float v_speed = 1.0f, int v_min = 0, int v_max = 0, const char* format = "%.0f");
353-
IMGUI_API bool DragInt4(const char* label, int v[4], float v_speed = 1.0f, int v_min = 0, int v_max = 0, const char* format = "%.0f");
354-
IMGUI_API bool DragIntRange2(const char* label, int* v_current_min, int* v_current_max, float v_speed = 1.0f, int v_min = 0, int v_max = 0, const char* format = "%.0f", const char* format_max = NULL);
350+
IMGUI_API bool DragInt(const char* label, int* v, float v_speed = 1.0f, int v_min = 0, int v_max = 0, const char* format = "%d"); // If v_min >= v_max we have no bound
351+
IMGUI_API bool DragInt2(const char* label, int v[2], float v_speed = 1.0f, int v_min = 0, int v_max = 0, const char* format = "%d");
352+
IMGUI_API bool DragInt3(const char* label, int v[3], float v_speed = 1.0f, int v_min = 0, int v_max = 0, const char* format = "%d");
353+
IMGUI_API bool DragInt4(const char* label, int v[4], float v_speed = 1.0f, int v_min = 0, int v_max = 0, const char* format = "%d");
354+
IMGUI_API bool DragIntRange2(const char* label, int* v_current_min, int* v_current_max, float v_speed = 1.0f, int v_min = 0, int v_max = 0, const char* format = "%d", const char* format_max = NULL);
355355

356356
// Widgets: Input with Keyboard
357357
IMGUI_API bool InputText(const char* label, char* buf, size_t buf_size, ImGuiInputTextFlags flags = 0, ImGuiTextEditCallback callback = NULL, void* user_data = NULL);

imgui_demo.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1008,16 +1008,16 @@ void ImGui::ShowDemoWindow(bool* p_open)
10081008
ImGui::InputFloat2("input float2", vec4f);
10091009
ImGui::DragFloat2("drag float2", vec4f, 0.01f, 0.0f, 1.0f);
10101010
ImGui::SliderFloat2("slider float2", vec4f, 0.0f, 1.0f);
1011-
ImGui::DragInt2("drag int2", vec4i, 1, 0, 255);
10121011
ImGui::InputInt2("input int2", vec4i);
1012+
ImGui::DragInt2("drag int2", vec4i, 1, 0, 255);
10131013
ImGui::SliderInt2("slider int2", vec4i, 0, 255);
10141014
ImGui::Spacing();
10151015

10161016
ImGui::InputFloat3("input float3", vec4f);
10171017
ImGui::DragFloat3("drag float3", vec4f, 0.01f, 0.0f, 1.0f);
10181018
ImGui::SliderFloat3("slider float3", vec4f, 0.0f, 1.0f);
1019-
ImGui::DragInt3("drag int3", vec4i, 1, 0, 255);
10201019
ImGui::InputInt3("input int3", vec4i);
1020+
ImGui::DragInt3("drag int3", vec4i, 1, 0, 255);
10211021
ImGui::SliderInt3("slider int3", vec4i, 0, 255);
10221022
ImGui::Spacing();
10231023

imgui_internal.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -1105,9 +1105,8 @@ namespace ImGui
11051105
IMGUI_API bool DragScalarN(const char* label, ImGuiDataType data_type, void* v, int components, float v_speed, const void* v_min, const void* v_max, const char* format, float power = 1.0f);
11061106

11071107
IMGUI_API bool InputScalar(const char* label, ImGuiDataType data_type, void* v, void* step_ptr, void* step_fast_ptr, const char* format, ImGuiInputTextFlags extra_flags = 0);
1108+
IMGUI_API bool InputScalarN(const char* label, ImGuiDataType data_type, void* v, int components, void* step_ptr, void* step_fast_ptr, const char* format, ImGuiInputTextFlags extra_flags = 0);
11081109
IMGUI_API bool InputTextEx(const char* label, char* buf, int buf_size, const ImVec2& size_arg, ImGuiInputTextFlags flags, ImGuiTextEditCallback callback = NULL, void* user_data = NULL);
1109-
IMGUI_API bool InputFloatN(const char* label, float* v, int components, const char* format, ImGuiInputTextFlags extra_flags);
1110-
IMGUI_API bool InputIntN(const char* label, int* v, int components, ImGuiInputTextFlags extra_flags);
11111110
IMGUI_API bool InputScalarAsWidgetReplacement(const ImRect& bb, ImGuiID id, const char* label, ImGuiDataType data_type, void* data_ptr, const char* format);
11121111

11131112
IMGUI_API void ColorTooltip(const char* text, const float* col, ImGuiColorEditFlags flags);

0 commit comments

Comments
 (0)