Fix GuiSlider jumping to mouse position on click#526
Fix GuiSlider jumping to mouse position on click#526grerfou wants to merge 1 commit intoraysan5:masterfrom
Conversation
|
@grerfou I'm afraid this is not the desired approach, it's an immediate-mode UI so the priority is avoidinng any kind of static data storage, like this one: static float dragOffsetX = 0.0f;If the only possible solution implies storing data, I'm afraid this is not a solution. |
|
@raysan5 You're totally right about the static variable... I've been thinking about other ways to approach this. What if we use
Here's what I'm thinking for the key change: if (guiControlExclusiveMode) // Dragging in progress
{
if (IsMouseButtonDown(MOUSE_LEFT_BUTTON))
{
if (CHECK_BOUNDS_ID(bounds, guiControlExclusiveRec))
{
state = STATE_PRESSED;
// Use mouse delta to update value incrementally (no jump on click)
Vector2 mouseDelta = GetMouseDelta();
float deltaValue = (maxValue - minValue) * (mouseDelta.x / (bounds.width - sliderWidth));
*value += deltaValue;
}
}
else
{
guiControlExclusiveMode = false;
guiControlExclusiveRec = RAYGUI_CLITERAL(Rectangle){ 0, 0, 0, 0 };
}
}
else if (CheckCollisionPointRec(mousePoint, bounds))
{
if (IsMouseButtonDown(MOUSE_LEFT_BUTTON))
{
state = STATE_PRESSED;
// Start dragging and apply delta
guiControlExclusiveMode = true;
guiControlExclusiveRec = bounds;
Vector2 mouseDelta = GetMouseDelta();
float deltaValue = (maxValue - minValue) * (mouseDelta.x / (bounds.width - sliderWidth));
*value += deltaValue;
}
else state = STATE_FOCUSED;
}I tested it and it seems to work - no jump on click, smooth dragging, no static storage. One thing I noticed: if someone clicks and moves super fast in the same frame, there could be a tiny movement. But that's pretty rare and still way better than jumping to the mouse position. Does this approach make sense to you? Happy to try something else if you think there's a better way! |
|
@raysan5 Hey ! Wondering if you had thoughts on the If it doesn't feel right for raygui, I can try something else, like only dragging |
|
@grerfou Using I don't know if there is any simpler solution to this problem... |
Fixes #474
Problem
When clicking on the slider track (not on the handle), the slider would immediately jump to the mouse position before dragging could begin.
Solution
Testing
Created a test program that demonstrates the fix works for both
GuiSlider()andGuiSliderBar().Before: Clicking anywhere on track caused slider to jump to that position
After: Clicking starts dragging from current slider position, no jump occurs
Note: Opened as draft pending confirmation from @raysan5 on the expected behavior (see issue discussion). Will mark as ready for review once confirmed.