mirror of
https://github.com/raysan5/raygui.git
synced 2026-01-29 18:29:18 -05:00
Create GuiSliderPro() to be usde on all GuiSlider*()
This commit is contained in:
147
src/raygui.h
147
src/raygui.h
@ -1814,7 +1814,7 @@ RAYGUIDEF bool GuiTextBoxMulti(Rectangle bounds, char *text, int textSize, bool
|
|||||||
{
|
{
|
||||||
if ((framesCounter/20)%2 == 0)
|
if ((framesCounter/20)%2 == 0)
|
||||||
{
|
{
|
||||||
char *line;
|
char *line = NULL;
|
||||||
if (currentLine > 0) line = strrchr(text, '\n');
|
if (currentLine > 0) line = strrchr(text, '\n');
|
||||||
else line = text;
|
else line = text;
|
||||||
|
|
||||||
@ -1838,22 +1838,31 @@ RAYGUIDEF bool GuiTextBoxMulti(Rectangle bounds, char *text, int textSize, bool
|
|||||||
return pressed;
|
return pressed;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Slider control, returns selected value
|
// Slider control with pro parameters
|
||||||
RAYGUIDEF float GuiSlider(Rectangle bounds, float value, float minValue, float maxValue)
|
// NOTE: Other GuiSlider*() controls use this one
|
||||||
{
|
RAYGUIDEF float GuiSliderPro(Rectangle bounds, float value, float minValue, float maxValue, int sliderWidth, const char *text, bool showValue)
|
||||||
return GuiSliderEx(bounds, value, minValue, maxValue, NULL, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Slider control extended, returns selected value and has text
|
|
||||||
RAYGUIDEF float GuiSliderEx(Rectangle bounds, float value, float minValue, float maxValue, const char *text, bool showValue)
|
|
||||||
{
|
{
|
||||||
GuiControlState state = guiState;
|
GuiControlState state = guiState;
|
||||||
|
|
||||||
int textWidth = (text == NULL) ? 0: GuiTextWidth(text);
|
int textWidth = (text == NULL) ? 0: GuiTextWidth(text);
|
||||||
|
|
||||||
Rectangle slider = { bounds.x + (int)((value/(maxValue - minValue))*(bounds.width - 2*GuiGetStyle(SLIDER, BORDER_WIDTH))) - GuiGetStyle(SLIDER, SLIDER_WIDTH)/2,
|
Rectangle slider = { 0 };
|
||||||
bounds.y + GuiGetStyle(SLIDER, BORDER_WIDTH), GuiGetStyle(SLIDER, SLIDER_WIDTH), bounds.height - 2*GuiGetStyle(SLIDER, BORDER_WIDTH) };
|
|
||||||
|
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) };
|
||||||
|
}
|
||||||
|
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) };
|
||||||
|
}
|
||||||
|
|
||||||
// Update control
|
// Update control
|
||||||
//--------------------------------------------------------------------
|
//--------------------------------------------------------------------
|
||||||
if ((state != GUI_STATE_DISABLED) && !guiLocked)
|
if ((state != GUI_STATE_DISABLED) && !guiLocked)
|
||||||
@ -1867,21 +1876,35 @@ RAYGUIDEF float GuiSliderEx(Rectangle bounds, float value, float minValue, float
|
|||||||
state = GUI_STATE_PRESSED;
|
state = GUI_STATE_PRESSED;
|
||||||
|
|
||||||
// Get equivalent value and slider position from mousePoint.x
|
// Get equivalent value and slider position from mousePoint.x
|
||||||
value = ((maxValue - minValue)*(mousePoint.x - (float)(bounds.x + slider.width/2)))/(float)(bounds.width - slider.width) + minValue;
|
if (sliderWidth > 0) // Slider
|
||||||
slider.x = mousePoint.x - slider.width/2;
|
{
|
||||||
|
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)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else state = GUI_STATE_FOCUSED;
|
else state = GUI_STATE_FOCUSED;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (value > maxValue) value = maxValue;
|
if (value > maxValue) value = maxValue;
|
||||||
else if (value < minValue) value = minValue;
|
else if (value < minValue) value = minValue;
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
else if (sliderWidth == 0) // SliderBar
|
||||||
|
{
|
||||||
|
if (slider.width > bounds.width) slider.width = bounds.width - 2*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);
|
|
||||||
//--------------------------------------------------------------------
|
//--------------------------------------------------------------------
|
||||||
|
|
||||||
// Draw control
|
// Draw control
|
||||||
//--------------------------------------------------------------------
|
//--------------------------------------------------------------------
|
||||||
switch (state)
|
switch (state)
|
||||||
@ -1925,86 +1948,28 @@ RAYGUIDEF float GuiSliderEx(Rectangle bounds, float value, float minValue, float
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Slider control, returns selected value
|
||||||
|
RAYGUIDEF float GuiSlider(Rectangle bounds, float value, float minValue, float maxValue)
|
||||||
|
{
|
||||||
|
return GuiSliderPro(bounds, value, minValue, maxValue, GuiGetStyle(SLIDER, SLIDER_WIDTH), NULL, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Slider control extended, returns selected value and has text
|
||||||
|
RAYGUIDEF float GuiSliderEx(Rectangle bounds, float value, float minValue, float maxValue, const char *text, bool showValue)
|
||||||
|
{
|
||||||
|
return GuiSliderPro(bounds, value, minValue, maxValue, GuiGetStyle(SLIDER, SLIDER_WIDTH), text, showValue);
|
||||||
|
}
|
||||||
|
|
||||||
// Slider Bar control, returns selected value
|
// Slider Bar control, returns selected value
|
||||||
RAYGUIDEF float GuiSliderBar(Rectangle bounds, float value, float minValue, float maxValue)
|
RAYGUIDEF float GuiSliderBar(Rectangle bounds, float value, float minValue, float maxValue)
|
||||||
{
|
{
|
||||||
return GuiSliderBarEx(bounds, value, minValue, maxValue, NULL, false);
|
return GuiSliderPro(bounds, value, minValue, maxValue, 0, NULL, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Slider Bar control extended, returns selected value
|
// Slider Bar control extended, returns selected value
|
||||||
RAYGUIDEF float GuiSliderBarEx(Rectangle bounds, float value, float minValue, float maxValue, const char *text, bool showValue)
|
RAYGUIDEF float GuiSliderBarEx(Rectangle bounds, float value, float minValue, float maxValue, const char *text, bool showValue)
|
||||||
{
|
{
|
||||||
GuiControlState state = guiState;
|
return GuiSliderPro(bounds, value, minValue, maxValue, 0, text, showValue);
|
||||||
int textWidth = (text == NULL) ? 0: GuiTextWidth(text);
|
|
||||||
|
|
||||||
Rectangle slider = { 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) };
|
|
||||||
|
|
||||||
// Update control
|
|
||||||
//--------------------------------------------------------------------
|
|
||||||
if ((state != GUI_STATE_DISABLED) && !guiLocked)
|
|
||||||
{
|
|
||||||
Vector2 mousePoint = GetMousePosition();
|
|
||||||
|
|
||||||
if (CheckCollisionPointRec(mousePoint, bounds))
|
|
||||||
{
|
|
||||||
if (IsMouseButtonDown(MOUSE_LEFT_BUTTON))
|
|
||||||
{
|
|
||||||
state = GUI_STATE_PRESSED;
|
|
||||||
|
|
||||||
// Get equivalent value and slider width from mousePoint.x
|
|
||||||
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)));
|
|
||||||
}
|
|
||||||
else state = GUI_STATE_FOCUSED;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (slider.width > bounds.width) slider.width = bounds.width - 2*GuiGetStyle(SLIDER, BORDER_WIDTH);
|
|
||||||
}
|
|
||||||
//--------------------------------------------------------------------
|
|
||||||
|
|
||||||
// Draw control
|
|
||||||
//--------------------------------------------------------------------
|
|
||||||
switch (state)
|
|
||||||
{
|
|
||||||
case GUI_STATE_NORMAL:
|
|
||||||
{
|
|
||||||
DrawRectangleLinesEx(bounds, GuiGetStyle(SLIDER, BORDER_WIDTH), Fade(GetColor(GuiGetStyle(SLIDER, BORDER_COLOR_NORMAL)), guiAlpha));
|
|
||||||
DrawRectangle(bounds.x + GuiGetStyle(SLIDER, BORDER_WIDTH), bounds.y + GuiGetStyle(SLIDER, BORDER_WIDTH), bounds.width - 2*GuiGetStyle(SLIDER, BORDER_WIDTH), bounds.height - 2*GuiGetStyle(SLIDER, BORDER_WIDTH), Fade(GetColor(GuiGetStyle(SLIDER, BASE_COLOR_NORMAL)), guiAlpha));
|
|
||||||
DrawRectangleRec(slider, Fade(GetColor(GuiGetStyle(SLIDER, BASE_COLOR_PRESSED)), guiAlpha));
|
|
||||||
if (text != NULL) GuiDrawText(text, bounds.x - GuiGetStyle(SLIDER, EX_TEXT_PADDING) - textWidth, bounds.y + bounds.height/2 - GuiGetStyle(DEFAULT, TEXT_SIZE)/2 + GuiGetStyle(SLIDER, INNER_PADDING), Fade(GetColor(GuiGetStyle(DEFAULT, TEXT_COLOR_NORMAL)), guiAlpha));
|
|
||||||
if (showValue) GuiDrawText(FormatText("%.02f", value), bounds.x + bounds.width + GuiGetStyle(SLIDER, EX_TEXT_PADDING), bounds.y + bounds.height/2 - GuiGetStyle(DEFAULT, TEXT_SIZE)/2 + GuiGetStyle(SLIDER, INNER_PADDING), Fade(GetColor(GuiGetStyle(DEFAULT, TEXT_COLOR_NORMAL)), guiAlpha));
|
|
||||||
} break;
|
|
||||||
case GUI_STATE_FOCUSED:
|
|
||||||
{
|
|
||||||
DrawRectangleLinesEx(bounds, GuiGetStyle(SLIDER, BORDER_WIDTH), Fade(GetColor(GuiGetStyle(SLIDER, BORDER_COLOR_FOCUSED)), guiAlpha));
|
|
||||||
DrawRectangle(bounds.x + GuiGetStyle(SLIDER, BORDER_WIDTH), bounds.y + GuiGetStyle(SLIDER, BORDER_WIDTH), bounds.width - 2*GuiGetStyle(SLIDER, BORDER_WIDTH), bounds.height - 2*GuiGetStyle(SLIDER, BORDER_WIDTH), Fade(GetColor(GuiGetStyle(SLIDER, BASE_COLOR_NORMAL)), guiAlpha));
|
|
||||||
DrawRectangleRec(slider, Fade(GetColor(GuiGetStyle(SLIDER, BASE_COLOR_FOCUSED)), guiAlpha));
|
|
||||||
if (text != NULL) GuiDrawText(text, bounds.x - GuiGetStyle(SLIDER, EX_TEXT_PADDING) - textWidth, bounds.y + bounds.height/2 - GuiGetStyle(DEFAULT, TEXT_SIZE)/2 + GuiGetStyle(SLIDER, INNER_PADDING), Fade(GetColor(GuiGetStyle(DEFAULT, TEXT_COLOR_FOCUSED)), guiAlpha));
|
|
||||||
if (showValue) GuiDrawText(FormatText("%.02f", value), bounds.x + bounds.width + GuiGetStyle(SLIDER, EX_TEXT_PADDING), bounds.y + bounds.height/2 - GuiGetStyle(DEFAULT, TEXT_SIZE)/2 + GuiGetStyle(SLIDER, INNER_PADDING), Fade(GetColor(GuiGetStyle(DEFAULT, TEXT_COLOR_FOCUSED)), guiAlpha));
|
|
||||||
} break;
|
|
||||||
case GUI_STATE_PRESSED:
|
|
||||||
{
|
|
||||||
DrawRectangleLinesEx(bounds, GuiGetStyle(SLIDER, BORDER_WIDTH), Fade(GetColor(GuiGetStyle(SLIDER, BORDER_COLOR_PRESSED)), guiAlpha));
|
|
||||||
DrawRectangle(bounds.x + GuiGetStyle(SLIDER, BORDER_WIDTH), bounds.y + GuiGetStyle(SLIDER, BORDER_WIDTH), bounds.width - 2*GuiGetStyle(SLIDER, BORDER_WIDTH), bounds.height - 2*GuiGetStyle(SLIDER, BORDER_WIDTH), Fade(GetColor(GuiGetStyle(SLIDER, BASE_COLOR_NORMAL)), guiAlpha));
|
|
||||||
DrawRectangleRec(slider, Fade(GetColor(GuiGetStyle(SLIDER, BASE_COLOR_PRESSED)), guiAlpha));
|
|
||||||
if (text != NULL) GuiDrawText(text, bounds.x - GuiGetStyle(SLIDER, EX_TEXT_PADDING) - textWidth, bounds.y + bounds.height/2 - GuiGetStyle(DEFAULT, TEXT_SIZE)/2 + GuiGetStyle(SLIDER, INNER_PADDING), Fade(GetColor(GuiGetStyle(DEFAULT, TEXT_COLOR_PRESSED)), guiAlpha));
|
|
||||||
if (showValue) GuiDrawText(FormatText("%.02f", value), bounds.x + bounds.width + GuiGetStyle(SLIDER, EX_TEXT_PADDING), bounds.y + bounds.height/2 - GuiGetStyle(DEFAULT, TEXT_SIZE)/2 + GuiGetStyle(SLIDER, INNER_PADDING), Fade(GetColor(GuiGetStyle(DEFAULT, TEXT_COLOR_PRESSED)), guiAlpha));
|
|
||||||
} break;
|
|
||||||
case GUI_STATE_DISABLED:
|
|
||||||
{
|
|
||||||
DrawRectangleLinesEx(bounds, GuiGetStyle(SLIDER, BORDER_WIDTH), Fade(GetColor(GuiGetStyle(SLIDER, BORDER_COLOR_DISABLED)), guiAlpha));
|
|
||||||
DrawRectangle(bounds.x + GuiGetStyle(SLIDER, BORDER_WIDTH), bounds.y + GuiGetStyle(SLIDER, BORDER_WIDTH), bounds.width - 2*GuiGetStyle(SLIDER, BORDER_WIDTH), bounds.height - 2*GuiGetStyle(SLIDER, BORDER_WIDTH), Fade(GetColor(GuiGetStyle(SLIDER, BASE_COLOR_DISABLED)), guiAlpha));
|
|
||||||
DrawRectangleRec(slider, Fade(GetColor(GuiGetStyle(DEFAULT, TEXT_COLOR_DISABLED)), guiAlpha));
|
|
||||||
if (text != NULL) GuiDrawText(text, bounds.x - GuiGetStyle(SLIDER, EX_TEXT_PADDING) - textWidth, bounds.y + bounds.height/2 - GuiGetStyle(DEFAULT, TEXT_SIZE)/2 + GuiGetStyle(SLIDER, INNER_PADDING), Fade(GetColor(GuiGetStyle(DEFAULT, TEXT_COLOR_DISABLED)), guiAlpha));
|
|
||||||
if (showValue) GuiDrawText(FormatText("%.02f", value), bounds.x + bounds.width + GuiGetStyle(SLIDER, EX_TEXT_PADDING), bounds.y + bounds.height/2 - GuiGetStyle(DEFAULT, TEXT_SIZE)/2 + GuiGetStyle(SLIDER, INNER_PADDING), Fade(GetColor(GuiGetStyle(DEFAULT, TEXT_COLOR_DISABLED)), guiAlpha));
|
|
||||||
} break;
|
|
||||||
default: break;
|
|
||||||
}
|
|
||||||
//--------------------------------------------------------------------
|
|
||||||
|
|
||||||
return value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Progress Bar control, shows current progress value
|
// Progress Bar control, shows current progress value
|
||||||
|
|||||||
Reference in New Issue
Block a user