From d5b6f51cf489765a79e4e915fdc4b8cb238a975f Mon Sep 17 00:00:00 2001 From: swayie Date: Wed, 4 Nov 2026 18:07:23 -0300 Subject: [PATCH] Fix DragInt WrapAround exceeding min/max bounds --- imgui_widgets.cpp | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/imgui_widgets.cpp b/imgui_widgets.cpp index f36a888c2..1ab338a58 100644 --- a/imgui_widgets.cpp +++ b/imgui_widgets.cpp @@ -2649,11 +2649,28 @@ bool ImGui::DragBehaviorT(ImGuiDataType data_type, TYPE* v, float v_speed, const { if (is_wrapped) { - // Wrap values - if (v_cur < v_min) - v_cur += v_max - v_min + (is_floating_point ? 0 : 1); - if (v_cur > v_max) - v_cur -= v_max - v_min + (is_floating_point ? 0 : 1); + if (!is_floating_point && sizeof(TYPE) <= 4) + { + // Handle multiple wraps using wider arithmetic. + const ImS64 min_value = (ImS64)v_min; + const ImS64 max_value = (ImS64)v_max; + const ImS64 range = max_value - min_value + 1; + + ImS64 offset = ((ImS64)v_cur - min_value) % range; + if (offset < 0) + offset += range; + + v_cur = (TYPE)(min_value + offset); + } + else + { + // Preserve original behavior for floating-point + // and 64-bit integer types. + if (v_cur < v_min) + v_cur += v_max - v_min + (is_floating_point ? 0 : 1); + if (v_cur > v_max) + v_cur -= v_max - v_min + (is_floating_point ? 0 : 1); + } } else { @@ -11190,4 +11207,4 @@ void ImGui::TabItemLabelAndCloseButton(ImDrawList* draw_list, const ImRect& bb, } -#endif // #ifndef IMGUI_DISABLE +#endif // #ifndef IMGUI_DISABLE \ No newline at end of file