From e4cba7af2a2623b2ef9958d2a9d5d6c03b22427b Mon Sep 17 00:00:00 2001 From: Ilia Demianenko Date: Wed, 9 Aug 2023 15:07:24 -0700 Subject: [PATCH] Fix slider dragging --- milepost/src/ui.c | 18 +++++++++--------- milepost/src/ui.h | 1 + 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/milepost/src/ui.c b/milepost/src/ui.c index 79153b5..1ac5c4d 100644 --- a/milepost/src/ui.c +++ b/milepost/src/ui.c @@ -538,6 +538,9 @@ ui_sig ui_box_sig(ui_box* box) { vec2 mousePos = ui_mouse_position(); + sig.mouse = (vec2){mousePos.x - box->rect.x, mousePos.y - box->rect.y}; + sig.delta = ui_mouse_delta(); + sig.wheel = ui_mouse_wheel(); sig.hovering = ui_box_hovering(box, mousePos); if(box->flags & UI_FLAG_CLICKABLE) @@ -547,6 +550,10 @@ ui_sig ui_box_sig(ui_box* box) sig.pressed = mp_mouse_pressed(input, MP_MOUSE_LEFT); if(sig.pressed) { + if(!box->dragging) + { + box->pressedMouse = sig.mouse; + } box->dragging = true; } sig.doubleClicked = mp_mouse_double_clicked(input, MP_MOUSE_LEFT); @@ -570,9 +577,6 @@ ui_sig ui_box_sig(ui_box* box) sig.dragging = box->dragging; } - sig.mouse = (vec2){mousePos.x - box->rect.x, mousePos.y - box->rect.y}; - sig.delta = ui_mouse_delta(); - sig.wheel = ui_mouse_wheel(); } return(sig); } @@ -1757,18 +1761,14 @@ ui_box* ui_slider(const char* label, f32 thumbRatio, f32* scrollValue) //NOTE: interaction ui_sig thumbSig = ui_box_sig(thumb); + ui_sig trackSig = ui_box_sig(track); if(thumbSig.dragging) { f32 trackExtents = track->rect.c[2+trackAxis] - thumb->rect.c[2+trackAxis]; - f32 delta = thumbSig.delta.c[trackAxis]/trackExtents; - f32 oldValue = *scrollValue; - - *scrollValue += delta; + *scrollValue = (trackSig.mouse.c[trackAxis] - thumb->pressedMouse.c[trackAxis]) / trackExtents; *scrollValue = Clamp(*scrollValue, 0, 1); } - ui_sig trackSig = ui_box_sig(track); - if(ui_box_active(frame)) { //NOTE: activated from outside diff --git a/milepost/src/ui.h b/milepost/src/ui.h index 4c4a843..437bba6 100644 --- a/milepost/src/ui.h +++ b/milepost/src/ui.h @@ -318,6 +318,7 @@ struct ui_box bool hot; bool active; vec2 scroll; + vec2 pressedMouse; // animation data f32 hotTransition; -- 2.25.1