From 2b4d558d318164706df9dc638e2f99d920da14c7 Mon Sep 17 00:00:00 2001 From: Furu <54323028+furudbat@users.noreply.github.com> Date: Mon, 7 Aug 2023 19:03:47 +0200 Subject: [PATCH] Add Toggle Slider (#305) (#307) * Add Toggle Slider (#305) * Style Toggle Slider * Add GuiToggleSliderGroup * Minimize API for GuiToggleSlider --- .../controls_test_suite/controls_test_suite.c | 3 + src/raygui.h | 66 +++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/examples/controls_test_suite/controls_test_suite.c b/examples/controls_test_suite/controls_test_suite.c index 21ed5cc..d88cef8 100644 --- a/examples/controls_test_suite/controls_test_suite.c +++ b/examples/controls_test_suite/controls_test_suite.c @@ -104,6 +104,7 @@ int main() int prevVisualStyleActive = 0; int toggleGroupActive = 0; + bool toggleSlider = false; Vector2 viewScroll = { 0, 0 }; //---------------------------------------------------------------------------------- @@ -231,6 +232,8 @@ int main() //GuiToggle((Rectangle){ 165, 400, 140, 25 }, "#1#ONE", &toggleGroupActive); GuiToggleGroup((Rectangle){ 165, 400, 140, 25 }, "#1#ONE\n#3#TWO\n#8#THREE\n#23#", &toggleGroupActive); + //GuiDisable(); + GuiToggleSlider((Rectangle){ 165, 510, 140, 25 }, (toggleSlider)? "ON" : "OFF", &toggleSlider); // Third GUI column GuiPanel((Rectangle){ 320, 25, 225, 140 }, "Panel Info"); diff --git a/src/raygui.h b/src/raygui.h index 0b17c85..e6c95bb 100644 --- a/src/raygui.h +++ b/src/raygui.h @@ -628,6 +628,7 @@ RAYGUIAPI int GuiToggle(Rectangle bounds, const char *text, bool *active); RAYGUIAPI int GuiToggleGroup(Rectangle bounds, const char *text, int *active); // Toggle Group control, returns active toggle index RAYGUIAPI int GuiCheckBox(Rectangle bounds, const char *text, bool *checked); // Check Box control, returns true when active RAYGUIAPI int GuiComboBox(Rectangle bounds, const char *text, int *active); // Combo Box control, returns selected item index +RAYGUIAPI int GuiToggleSlider(Rectangle bounds, const char *text, bool *active); // Toggle Slider control, returns true when clicked RAYGUIAPI int GuiDropdownBox(Rectangle bounds, const char *text, int *active, bool editMode); // Dropdown Box control, returns selected item RAYGUIAPI int GuiSpinner(Rectangle bounds, const char *text, int *value, int minValue, int maxValue, bool editMode); // Spinner control, returns selected value @@ -2821,6 +2822,71 @@ int GuiSliderBar(Rectangle bounds, const char *textLeft, const char *textRight, return GuiSliderPro(bounds, textLeft, textRight, value, minValue, maxValue, 0); } +// Toggle Slider control extended, returns true when clicked +int GuiToggleSlider(Rectangle bounds, const char *text, bool *value) +{ + int result = 0; + GuiState state = guiState; + + 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) }; + + if (*value) + { + slider.x += bounds.width/2; + slider.width = bounds.width/2 - 2*GuiGetStyle(SLIDER, BORDER_WIDTH); + } + else + { + slider.x += GuiGetStyle(SLIDER, BORDER_WIDTH); + slider.width = bounds.width/2 - 2*GuiGetStyle(SLIDER, BORDER_WIDTH); + } + + // Update control + //-------------------------------------------------------------------- + if ((state != STATE_DISABLED) && !guiLocked) + { + Vector2 mousePoint = GetMousePosition(); + + if (CheckCollisionPointRec(mousePoint, bounds)) + { + if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON)) + { + state = STATE_PRESSED; + *value = !(*value); + result = 1; + } + else state = STATE_FOCUSED; + } + else if (*value) state = STATE_PRESSED; + } + + // Bar limits check + if (slider.x <= (bounds.x + GuiGetStyle(SLIDER, BORDER_WIDTH))) slider.x = bounds.x + GuiGetStyle(SLIDER, BORDER_WIDTH) + 1; + else if ((slider.x + slider.width) >= (bounds.x + bounds.width)) slider.x = bounds.x + bounds.width - slider.width - GuiGetStyle(SLIDER, BORDER_WIDTH); + //-------------------------------------------------------------------- + + // Draw control + //-------------------------------------------------------------------- + GuiDrawRectangle(bounds, GuiGetStyle(SLIDER, BORDER_WIDTH), Fade(GetColor(GuiGetStyle(BUTTON, BORDER + (state*3))), guiAlpha), Fade(GetColor(GuiGetStyle(BUTTON, (*value || state == STATE_FOCUSED)? BASE_COLOR_NORMAL : BASE_COLOR_DISABLED)), guiAlpha)); + GuiDrawRectangle(slider, 0, BLANK, Fade(GetColor(GuiGetStyle(BUTTON, BASE + (state*3))), guiAlpha)); + + // Draw text in slider + if (text != NULL) + { + Rectangle textBounds = { 0 }; + textBounds.width = (float)GetTextWidth(text); + textBounds.height = (float)GuiGetStyle(DEFAULT, TEXT_SIZE); + textBounds.x = slider.x + slider.width/2 - textBounds.width/2; + textBounds.y = bounds.y + bounds.height/2 - GuiGetStyle(DEFAULT, TEXT_SIZE)/2; + + GuiDrawText(text, textBounds, GuiGetStyle(TOGGLE, TEXT_ALIGNMENT), Fade(GetColor(GuiGetStyle(BUTTON, TEXT + (state*3))), guiAlpha)); + } + //-------------------------------------------------------------------- + + return result; +} + // Progress Bar control extended, shows current progress value int GuiProgressBar(Rectangle bounds, const char *textLeft, const char *textRight, float *value, float minValue, float maxValue) {