Fix DragInt WrapAround exceeding min/max bounds

This commit is contained in:
swayie 2026-11-04 18:07:23 -03:00
parent 8c8b8fc266
commit d5b6f51cf4

View File

@ -2649,12 +2649,29 @@ bool ImGui::DragBehaviorT(ImGuiDataType data_type, TYPE* v, float v_speed, const
{
if (is_wrapped)
{
// Wrap values
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
{
// Clamp values + handle overflow/wrap-around for integer types.