From 0c35b15afff0b9a77255336a80e10fbb00a18085 Mon Sep 17 00:00:00 2001 From: 418Cat <94795008+418Cat@users.noreply.github.com> Date: Sat, 23 Sep 2023 20:32:17 +0200 Subject: [PATCH] Color picker cursor not restricted to bounds When picking a color in the Devices tab, the mouse cursor doesn't need to be strictly in the rectangle and circle bounds anymore --- dependencies/ColorWheel/ColorWheel.cpp | 80 +++++++++++++++++++++----- 1 file changed, 65 insertions(+), 15 deletions(-) diff --git a/dependencies/ColorWheel/ColorWheel.cpp b/dependencies/ColorWheel/ColorWheel.cpp index 9a91c0343..39426fb72 100644 --- a/dependencies/ColorWheel/ColorWheel.cpp +++ b/dependencies/ColorWheel/ColorWheel.cpp @@ -222,21 +222,71 @@ void ColorWheel::mouseMoveEvent(QMouseEvent *event) } else { - // TODO: due with cursor out of region after press - // int length = qMin(width(), height()); - // QPoint center(length/2, length/2); - // int R = qSqrt(qPow(qAbs(lastPos.x()), 2) - // + qPow(qAbs(lastPos.y()), 2)); - // if(inWheel){ - // int r = length / 2; - // r += qSqrt(qPow(center.x(), 2) + qPow(center.y(), 2)); - // int x0 = r/R * qAbs(lastPos.x()); - // int y0 = r/R * qAbs(lastPos.y()); - // QColor color = posColor(QPoint(x0, y0)); - // hueChanged(color.hue()); - // }else if(inSquare){ - // // - // } + /*-----------------------------------------------------*\ + | If mouse is outside of wheel region, set lastPos to | + | corresponding values inside the wheel region | + \*-----------------------------------------------------*/ + if(inWheel) + { + QPoint center = wheelRegion.boundingRect().center(); + + int radius = center.x() - wheelRegion.boundingRect().bottomLeft().x(); + + int xInWheel = lastPos.x() - center.x(); + int yInWheel = lastPos.y() - center.y(); + + int centerToMouse = sqrt + ( + qPow((int)xInWheel, 2) + + qPow((int)yInWheel, 2) + +1 //the sqrt function causes a crash when it's 0, so +1 prevents this with no noticeable precision difference and no performance penalty + ); + + xInWheel = radius * xInWheel / centerToMouse; + yInWheel = radius * yInWheel / centerToMouse; + + lastPos.setX(xInWheel + center.x()); + lastPos.setY(yInWheel + center.y()); + + QColor color = posColor(lastPos); + hueChanged(color.hue()); + } + else + + /*-----------------------------------------------------*\ + | If mouse is outside of square region, set lastPos to | + | corresponding values inside the square region | + \*-----------------------------------------------------*/ + if(inSquare) + { + + int xInSquare = lastPos.x() - squareRegion.boundingRect().bottomLeft().x(); + int maxX = squareRegion.boundingRect().topRight().x() - squareRegion.boundingRect().bottomLeft().x(); + + int yInSquare = lastPos.y() - squareRegion.boundingRect().topRight().y(); + int maxY = squareRegion.boundingRect().bottomLeft().y() - squareRegion.boundingRect().topRight().y(); + + if(xInSquare < 0) + { + lastPos.setX(squareRegion.boundingRect().bottomLeft().x()); + } + else if(xInSquare >= maxX) + { + lastPos.setX(squareRegion.boundingRect().topRight().x()); + } + + if(yInSquare < 0) + { + lastPos.setY(squareRegion.boundingRect().topRight().y()); + } + else if (yInSquare >= maxY) + { + lastPos.setY(squareRegion.boundingRect().bottomLeft().y()); + } + + QColor color = posColor(lastPos); + svChanged(color); + } } }