mirror of
https://github.com/raysan5/raygui.git
synced 2026-02-02 04:09:18 -05:00
Improved GuiSpinner() responsiveness when buttons are held
This commit is contained in:
33
src/raygui.h
33
src/raygui.h
@ -1476,14 +1476,15 @@ RAYGUIDEF bool GuiDropdownBox(Rectangle bounds, const char *text, int *active, b
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Spinner control, returns selected value
|
// Spinner control, returns selected value
|
||||||
// NOTE: Requires static variables: framesCounter, valueSpeed - ERROR!
|
// NOTE: Requires static variables: timer, valueSpeed - ERROR!
|
||||||
RAYGUIDEF bool GuiSpinner(Rectangle bounds, int *value, int minValue, int maxValue, bool editMode)
|
RAYGUIDEF bool GuiSpinner(Rectangle bounds, int *value, int minValue, int maxValue, bool editMode)
|
||||||
{
|
{
|
||||||
#define GUI_SPINNER_HOLD_SPEED 9
|
#define GUI_SPINNER_HOLD_SPEED 0.2f // min 200ms delay
|
||||||
static int framesCounter = 0;
|
static float timer = 0.0f;
|
||||||
|
|
||||||
bool pressed = false;
|
bool pressed = false, active = GuiTextBoxIsActive(bounds);
|
||||||
int tempValue = *value;
|
int tempValue = *value;
|
||||||
|
const float time = GetTime(); // Get current time
|
||||||
|
|
||||||
Rectangle spinner = { bounds.x + GuiGetStyle(SPINNER, SELECT_BUTTON_WIDTH) + GuiGetStyle(SPINNER, SELECT_BUTTON_PADDING), bounds.y,
|
Rectangle spinner = { bounds.x + GuiGetStyle(SPINNER, SELECT_BUTTON_WIDTH) + GuiGetStyle(SPINNER, SELECT_BUTTON_PADDING), bounds.y,
|
||||||
bounds.width - 2*(GuiGetStyle(SPINNER, SELECT_BUTTON_WIDTH) + GuiGetStyle(SPINNER, SELECT_BUTTON_PADDING)), bounds.height };
|
bounds.width - 2*(GuiGetStyle(SPINNER, SELECT_BUTTON_WIDTH) + GuiGetStyle(SPINNER, SELECT_BUTTON_PADDING)), bounds.height };
|
||||||
@ -1498,10 +1499,20 @@ RAYGUIDEF bool GuiSpinner(Rectangle bounds, int *value, int minValue, int maxVal
|
|||||||
|
|
||||||
if(editMode)
|
if(editMode)
|
||||||
{
|
{
|
||||||
if(!GuiTextBoxIsActive(bounds) && (IsMouseButtonPressed(MOUSE_LEFT_BUTTON) || IsMouseButtonDown(MOUSE_LEFT_BUTTON)) && CheckCollisionPointRec(mouse, bounds))
|
if(!active)
|
||||||
GuiTextBoxSetActive(bounds); // When mouse is pressed or held over the buttons make this becomes the active textbox
|
{
|
||||||
framesCounter++;
|
// This becomes the active textbox when mouse is pressed or held inside bounds
|
||||||
|
if((IsMouseButtonPressed(MOUSE_LEFT_BUTTON) || IsMouseButtonDown(MOUSE_LEFT_BUTTON)) && CheckCollisionPointRec(mouse, bounds))
|
||||||
|
{
|
||||||
|
GuiTextBoxSetActive(bounds);
|
||||||
|
active = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reset timer when one of the buttons is clicked (without this, holding the button down will not behave correctly)
|
||||||
|
if((CheckCollisionPointRec(mouse, leftButtonBound) || CheckCollisionPointRec(mouse, rightButtonBound)) && IsMouseButtonPressed(MOUSE_LEFT_BUTTON) )
|
||||||
|
timer = time;
|
||||||
//--------------------------------------------------------------------
|
//--------------------------------------------------------------------
|
||||||
|
|
||||||
// Draw control
|
// Draw control
|
||||||
@ -1523,7 +1534,7 @@ RAYGUIDEF bool GuiSpinner(Rectangle bounds, int *value, int minValue, int maxVal
|
|||||||
icon = (char*)GuiIconText(RICON_ARROW_LEFT_FILL, NULL);
|
icon = (char*)GuiIconText(RICON_ARROW_LEFT_FILL, NULL);
|
||||||
#endif
|
#endif
|
||||||
if (GuiButton(leftButtonBound, icon) || // NOTE: also decrease value when the button is held down
|
if (GuiButton(leftButtonBound, icon) || // NOTE: also decrease value when the button is held down
|
||||||
(IsMouseButtonDown(MOUSE_LEFT_BUTTON) && CheckCollisionPointRec(mouse, leftButtonBound) && framesCounter % GUI_SPINNER_HOLD_SPEED == 0) )
|
(IsMouseButtonDown(MOUSE_LEFT_BUTTON) && CheckCollisionPointRec(mouse, leftButtonBound) && time - timer > GUI_SPINNER_HOLD_SPEED) )
|
||||||
tempValue--;
|
tempValue--;
|
||||||
|
|
||||||
icon = ">";
|
icon = ">";
|
||||||
@ -1531,7 +1542,7 @@ RAYGUIDEF bool GuiSpinner(Rectangle bounds, int *value, int minValue, int maxVal
|
|||||||
icon = (char*)GuiIconText(RICON_ARROW_RIGHT_FILL, NULL);
|
icon = (char*)GuiIconText(RICON_ARROW_RIGHT_FILL, NULL);
|
||||||
#endif
|
#endif
|
||||||
if (GuiButton(rightButtonBound, icon) || // NOTE: also increase value when the button is held down
|
if (GuiButton(rightButtonBound, icon) || // NOTE: also increase value when the button is held down
|
||||||
(IsMouseButtonDown(MOUSE_LEFT_BUTTON) && CheckCollisionPointRec(mouse, rightButtonBound) && framesCounter % GUI_SPINNER_HOLD_SPEED == 0) )
|
(IsMouseButtonDown(MOUSE_LEFT_BUTTON) && CheckCollisionPointRec(mouse, rightButtonBound) && time - timer > GUI_SPINNER_HOLD_SPEED) )
|
||||||
tempValue++;
|
tempValue++;
|
||||||
|
|
||||||
GuiSetStyle(BUTTON, TEXT_ALIGNMENT, tempTextAlign);
|
GuiSetStyle(BUTTON, TEXT_ALIGNMENT, tempTextAlign);
|
||||||
@ -1541,6 +1552,10 @@ RAYGUIDEF bool GuiSpinner(Rectangle bounds, int *value, int minValue, int maxVal
|
|||||||
if (tempValue < minValue) tempValue = minValue;
|
if (tempValue < minValue) tempValue = minValue;
|
||||||
if (tempValue > maxValue) tempValue = maxValue;
|
if (tempValue > maxValue) tempValue = maxValue;
|
||||||
|
|
||||||
|
// Reset timer
|
||||||
|
if(active && (time - timer > GUI_SPINNER_HOLD_SPEED || timer == 0.0f || timer > time))
|
||||||
|
timer = time;
|
||||||
|
|
||||||
*value = tempValue;
|
*value = tempValue;
|
||||||
return pressed;
|
return pressed;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user