From c5fb92955524204d4aff7c9fe850645b8a37fa2a Mon Sep 17 00:00:00 2001 From: omar Date: Fri, 4 May 2018 21:02:26 +0200 Subject: [PATCH] Data types: Fixed empty format string (or no % specifier) breaking the parsing back of values. --- imgui.cpp | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index e9eb8b9a..9251b47c 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -8744,6 +8744,21 @@ static float GetMinimumStepAtDecimalPrecision(int decimal_precision) return (decimal_precision >= 0 && decimal_precision < 10) ? min_steps[decimal_precision] : ImPow(10.0f, (float)-decimal_precision); } +template +static inline TYPE RoundScalarWithFormat(const char* format, ImGuiDataType data_type, TYPE v) +{ + const char* fmt_start = ImParseFormatFindStart(format); + if (fmt_start[0] != '%' || fmt_start[1] == '%') // Don't apply if the value is not visible in the format string + return v; + char v_str[64]; + ImFormatString(v_str, IM_ARRAYSIZE(v_str), fmt_start, v); + if (data_type == ImGuiDataType_Float || data_type == ImGuiDataType_Double) + v = (TYPE)atof(v_str); + else + ImAtoi(v_str, (SIGNEDTYPE*)&v); + return v; +} + template static inline float SliderBehaviorCalcRatioFromValue(ImGuiDataType data_type, TYPE v, TYPE v_min, TYPE v_max, float power, float linear_zero_pos) { @@ -8914,12 +8929,7 @@ static bool ImGui::SliderBehaviorT(const ImRect& bb, ImGuiID id, ImGuiDataType d } // Round to user desired precision based on format string - char v_str[64]; - ImFormatString(v_str, IM_ARRAYSIZE(v_str), ImParseFormatFindStart(format), v_new); - if (data_type == ImGuiDataType_Float || data_type == ImGuiDataType_Double) - v_new = (TYPE)atof(v_str); - else - ImAtoi(v_str, (SIGNEDTYPE*)&v_new); + v_new = RoundScalarWithFormat(format, data_type, v_new); // Apply result if (*v != v_new) @@ -9262,12 +9272,7 @@ static bool ImGui::DragBehaviorT(ImGuiDataType data_type, TYPE* v, float v_speed } // Round to user desired precision based on format string - char v_str[64]; - ImFormatString(v_str, IM_ARRAYSIZE(v_str), ImParseFormatFindStart(format), v_cur); - if (data_type == ImGuiDataType_Float || data_type == ImGuiDataType_Double) - v_cur = (TYPE)atof(v_str); - else - ImAtoi(v_str, (SIGNEDTYPE*)&v_cur); + v_cur = RoundScalarWithFormat(format, data_type, v_cur); // Preserve remainder after rounding has been applied. This also allow slow tweaking of values. g.DragCurrentAccumDirty = false;