fixed dragging GuiColorPanel outside of bounds (#366)

This commit is contained in:
kolunmi 2024-01-20 12:49:27 -07:00 committed by GitHub
parent dd36bff888
commit 45e7f967e6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3379,15 +3379,54 @@ int GuiColorPanel(Rectangle bounds, const char *text, Color *color)
// Update control // Update control
//-------------------------------------------------------------------- //--------------------------------------------------------------------
if ((state != STATE_DISABLED) && !guiLocked && !guiSliderDragging) if ((state != STATE_DISABLED) && !guiLocked)
{ {
Vector2 mousePoint = GetMousePosition(); Vector2 mousePoint = GetMousePosition();
if (CheckCollisionPointRec(mousePoint, bounds)) if (guiSliderDragging)
{
if (IsMouseButtonDown(MOUSE_LEFT_BUTTON))
{
if (CHECK_BOUNDS_ID(bounds, guiSliderActive))
{
pickerSelector = mousePoint;
if (pickerSelector.x < bounds.x) pickerSelector.x = bounds.x;
if (pickerSelector.x > bounds.x + bounds.width) pickerSelector.x = bounds.x + bounds.width;
if (pickerSelector.y < bounds.y) pickerSelector.y = bounds.y;
if (pickerSelector.y > bounds.y + bounds.height) pickerSelector.y = bounds.y + bounds.height;
// Calculate color from picker
Vector2 colorPick = { pickerSelector.x - bounds.x, pickerSelector.y - bounds.y };
colorPick.x /= (float)bounds.width; // Get normalized value on x
colorPick.y /= (float)bounds.height; // Get normalized value on y
hsv.y = colorPick.x;
hsv.z = 1.0f - colorPick.y;
Vector3 rgb = ConvertHSVtoRGB(hsv);
// NOTE: Vector3ToColor() only available on raylib 1.8.1
*color = RAYGUI_CLITERAL(Color){ (unsigned char)(255.0f*rgb.x),
(unsigned char)(255.0f*rgb.y),
(unsigned char)(255.0f*rgb.z),
(unsigned char)(255.0f*(float)color->a/255.0f) };
}
}
else
{
guiSliderDragging = false;
guiSliderActive = RAYGUI_CLITERAL(Rectangle){ 0, 0, 0, 0 };
}
}
else if (CheckCollisionPointRec(mousePoint, bounds))
{ {
if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) if (IsMouseButtonDown(MOUSE_LEFT_BUTTON))
{ {
state = STATE_PRESSED; state = STATE_PRESSED;
guiSliderDragging = true;
guiSliderActive = bounds;
pickerSelector = mousePoint; pickerSelector = mousePoint;
// Calculate color from picker // Calculate color from picker