Slider now centers correctly and general UX improvements (#368)

* Slider now centers correctly and general UX improvements

* Typo
This commit is contained in:
jon 2024-01-28 04:24:31 -08:00 committed by GitHub
parent 45e7f967e6
commit b2829289f6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2943,22 +2943,9 @@ int GuiSliderPro(Rectangle bounds, const char *textLeft, const char *textRight,
float temp = (maxValue - minValue)/2.0f; float temp = (maxValue - minValue)/2.0f;
if (value == NULL) value = &temp; if (value == NULL) value = &temp;
int sliderValue = (int)(((*value - minValue)/(maxValue - minValue))*(bounds.width - 2*GuiGetStyle(SLIDER, BORDER_WIDTH)));
Rectangle slider = { bounds.x, bounds.y + GuiGetStyle(SLIDER, BORDER_WIDTH) + GuiGetStyle(SLIDER, SLIDER_PADDING), Rectangle slider = { bounds.x, bounds.y + GuiGetStyle(SLIDER, BORDER_WIDTH) + GuiGetStyle(SLIDER, SLIDER_PADDING),
0, bounds.height - 2*GuiGetStyle(SLIDER, BORDER_WIDTH) - 2*GuiGetStyle(SLIDER, SLIDER_PADDING) }; 0, bounds.height - 2*GuiGetStyle(SLIDER, BORDER_WIDTH) - 2*GuiGetStyle(SLIDER, SLIDER_PADDING) };
if (sliderWidth > 0) // Slider
{
slider.x += (sliderValue - sliderWidth/2);
slider.width = (float)sliderWidth;
}
else if (sliderWidth == 0) // SliderBar
{
slider.x += GuiGetStyle(SLIDER, BORDER_WIDTH);
slider.width = (float)sliderValue;
}
// Update control // Update control
//-------------------------------------------------------------------- //--------------------------------------------------------------------
if ((state != STATE_DISABLED) && !guiLocked) if ((state != STATE_DISABLED) && !guiLocked)
@ -2972,9 +2959,8 @@ int GuiSliderPro(Rectangle bounds, const char *textLeft, const char *textRight,
if (CHECK_BOUNDS_ID(bounds, guiSliderActive)) if (CHECK_BOUNDS_ID(bounds, guiSliderActive))
{ {
state = STATE_PRESSED; state = STATE_PRESSED;
// Translate Mouse X to Slider Label Value
// Get equivalent value and slider position from mousePosition.x *value = (maxValue - minValue)*((mousePoint.x - bounds.x - sliderWidth/2)/(bounds.width-sliderWidth)) + minValue;
*value = ((maxValue - minValue)*(mousePoint.x - (float)(bounds.x + sliderWidth/2)))/(float)(bounds.width - sliderWidth) + minValue;
} }
} }
else else
@ -2993,11 +2979,8 @@ int GuiSliderPro(Rectangle bounds, const char *textLeft, const char *textRight,
if (!CheckCollisionPointRec(mousePoint, slider)) if (!CheckCollisionPointRec(mousePoint, slider))
{ {
// Get equivalent value and slider position from mousePosition.x // Translate Mouse X to Slider Label Value
*value = ((maxValue - minValue)*(mousePoint.x - (float)(bounds.x + sliderWidth/2)))/(float)(bounds.width - sliderWidth) + minValue; *value = (maxValue - minValue)*((mousePoint.x - bounds.x - sliderWidth/2)/(bounds.width-sliderWidth)) + minValue;
if (sliderWidth > 0) slider.x = mousePoint.x - slider.width/2; // Slider
else if (sliderWidth == 0) slider.width = (float)sliderValue; // SliderBar
} }
} }
else state = STATE_FOCUSED; else state = STATE_FOCUSED;
@ -3011,16 +2994,21 @@ int GuiSliderPro(Rectangle bounds, const char *textLeft, const char *textRight,
if(oldValue == *value) result = 0; if(oldValue == *value) result = 0;
else result = 1; else result = 1;
// Bar limits check int sliderValue = (int)(((*value - minValue)/(maxValue - minValue))*(bounds.width - sliderWidth - GuiGetStyle(SLIDER, BORDER_WIDTH)));
if (sliderWidth > 0) // Slider if (sliderWidth > 0) // Slider
{ {
slider.x += sliderValue;
slider.width = (float)sliderWidth;
if (slider.x <= (bounds.x + GuiGetStyle(SLIDER, BORDER_WIDTH))) slider.x = bounds.x + GuiGetStyle(SLIDER, BORDER_WIDTH); if (slider.x <= (bounds.x + GuiGetStyle(SLIDER, BORDER_WIDTH))) slider.x = bounds.x + GuiGetStyle(SLIDER, BORDER_WIDTH);
else if ((slider.x + slider.width) >= (bounds.x + bounds.width)) slider.x = bounds.x + bounds.width - slider.width - GuiGetStyle(SLIDER, BORDER_WIDTH); else if ((slider.x + slider.width) >= (bounds.x + bounds.width)) slider.x = bounds.x + bounds.width - slider.width - GuiGetStyle(SLIDER, BORDER_WIDTH);
} }
else if (sliderWidth == 0) // SliderBar else if (sliderWidth == 0) // SliderBar
{ {
slider.x += GuiGetStyle(SLIDER, BORDER_WIDTH);
slider.width = (float)sliderValue;
if (slider.width > bounds.width) slider.width = bounds.width - 2*GuiGetStyle(SLIDER, BORDER_WIDTH); if (slider.width > bounds.width) slider.width = bounds.width - 2*GuiGetStyle(SLIDER, BORDER_WIDTH);
} }
//-------------------------------------------------------------------- //--------------------------------------------------------------------
// Draw control // Draw control