From 009913c408fdb265fa7128e27485a331dd8c892e Mon Sep 17 00:00:00 2001 From: Ray Date: Mon, 26 Nov 2018 00:58:48 +0100 Subject: [PATCH] Some code tweaks --- .../controls_test_suite/controls_test_suite.c | 2 +- src/raygui.h | 35 ++++++++----------- 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/examples/controls_test_suite/controls_test_suite.c b/examples/controls_test_suite/controls_test_suite.c index 10363a5..02c338d 100644 --- a/examples/controls_test_suite/controls_test_suite.c +++ b/examples/controls_test_suite/controls_test_suite.c @@ -142,7 +142,7 @@ int main() if (GuiTextBoxMulti((Rectangle){ 320, 25, 225, 140 }, multiTextBoxText, 141, multiTextBoxEditMode)) multiTextBoxEditMode = !multiTextBoxEditMode; colorPickerValue = GuiColorPicker((Rectangle){ 320, 185, 196, 192 }, colorPickerValue); - sliderValue = GuiSliderEx((Rectangle){ 320, 400, 200, 20 }, sliderValue, 0, 100, "SLIDER", true); + sliderValue = GuiSliderEx((Rectangle){ 320, 400, 200, 20 }, sliderValue, -50, 100, "SLIDER", true); sliderBarValue = GuiSliderBarEx((Rectangle){ 320, 430, 200, 20 }, sliderBarValue, 0, 100, "SLIDERBAR", true); progressValue = GuiProgressBarEx((Rectangle){ 320, 460, 200, 20 }, progressValue, 0, 1, true); diff --git a/src/raygui.h b/src/raygui.h index 0f7482b..f70bb84 100644 --- a/src/raygui.h +++ b/src/raygui.h @@ -1845,22 +1845,20 @@ RAYGUIDEF float GuiSliderPro(Rectangle bounds, float value, float minValue, floa GuiControlState state = guiState; int textWidth = (text == NULL) ? 0: GuiTextWidth(text); + int sliderValue = (int)(((value - minValue)/(maxValue - minValue))*(bounds.width - 2*GuiGetStyle(SLIDER, BORDER_WIDTH))); - Rectangle slider = { 0 }; + Rectangle slider = { bounds.x, bounds.y + GuiGetStyle(SLIDER, BORDER_WIDTH) + GuiGetStyle(SLIDER, INNER_PADDING), + 0, bounds.height - 2*GuiGetStyle(SLIDER, BORDER_WIDTH) - 2*GuiGetStyle(SLIDER, INNER_PADDING) }; if (sliderWidth > 0) // Slider { - slider = (Rectangle){ bounds.x + (int)((value/(maxValue - minValue))*(bounds.width - 2*GuiGetStyle(SLIDER, BORDER_WIDTH))) - GuiGetStyle(SLIDER, SLIDER_WIDTH)/2, - bounds.y + GuiGetStyle(SLIDER, BORDER_WIDTH), - sliderWidth, - bounds.height - 2*GuiGetStyle(SLIDER, BORDER_WIDTH) }; + slider.x += (sliderValue - sliderWidth/2); + slider.width = sliderWidth; } else if (sliderWidth == 0) // SliderBar { - slider = (Rectangle){ bounds.x + GuiGetStyle(SLIDER, BORDER_WIDTH), - bounds.y + GuiGetStyle(SLIDER, BORDER_WIDTH) + GuiGetStyle(SLIDER, INNER_PADDING), - (int)(((value - minValue)/(maxValue - minValue))*(bounds.width - 2*GuiGetStyle(SLIDER, BORDER_WIDTH))), - bounds.height - 2*GuiGetStyle(SLIDER, BORDER_WIDTH) - 2*GuiGetStyle(SLIDER, INNER_PADDING) }; + slider.x += GuiGetStyle(SLIDER, BORDER_WIDTH); + slider.width = sliderValue; } // Update control @@ -1876,16 +1874,10 @@ RAYGUIDEF float GuiSliderPro(Rectangle bounds, float value, float minValue, floa state = GUI_STATE_PRESSED; // Get equivalent value and slider position from mousePoint.x - if (sliderWidth > 0) // Slider - { - value = ((maxValue - minValue)*(mousePoint.x - (float)(bounds.x + slider.width/2)))/(float)(bounds.width - slider.width) + minValue; - slider.x = mousePoint.x - slider.width/2; - } - else if (sliderWidth == 0) // SliderBar - { - value = (maxValue - minValue)*((mousePoint.x - (float)bounds.x)/(float)bounds.width) + minValue; - slider.width = (int)(((value - minValue)/(maxValue - minValue))*(bounds.width - 2*GuiGetStyle(SLIDER, BORDER_WIDTH))); - } + value = ((maxValue - minValue)*(mousePoint.x - (float)(bounds.x + sliderWidth/2)))/(float)(bounds.width - sliderWidth) + minValue; + + if (sliderWidth > 0) slider.x = mousePoint.x - slider.width/2; // Slider + else if (sliderWidth == 0) slider.width = sliderValue; // SliderBar } else state = GUI_STATE_FOCUSED; } @@ -1893,10 +1885,11 @@ RAYGUIDEF float GuiSliderPro(Rectangle bounds, float value, float minValue, floa if (value > maxValue) value = maxValue; else if (value < minValue) value = minValue; + // Bar limits check if (sliderWidth > 0) // Slider { - 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); + 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 (sliderWidth == 0) // SliderBar {