16 #include <QMouseEvent>
18 #include <QStyleOption>
25 : QWidget(parent), _x(0), _y(0), _animation(new QParallelAnimationGroup(this)),
26 _xAnimation(new QPropertyAnimation(this,
"x")), _yAnimation(new QPropertyAnimation(this,
"y")),
27 _alignment(Qt::AlignTop | Qt::AlignLeft)
100 QPointF position = QPointF(xOffset * radius +
_bounds.center().x(), yOffset * radius +
_bounds.center().y());
101 QPointF fromCenterToKnop = position -
_bounds.center();
103 fromCenterToKnop.setX(
checkValue(fromCenterToKnop.x(), -radius, radius));
104 fromCenterToKnop.setY(
checkValue(fromCenterToKnop.y(), -radius, radius));
109 if(radius == 0)
return;
128 return (value < min) ? min : (value > max) ? max : value;
135 float a = qMin(width(), height());
143 else if(
_alignment.testFlag(Qt::AlignVCenter))
145 topleft.setY(((height() - a) / 2));
149 topleft.setY(height() + a);
156 else if(
_alignment.testFlag(Qt::AlignHCenter))
158 topleft.setX((width() - a) / 2);
162 topleft.setX(width() - a);
165 _bounds = QRectF(topleft, QSize(a, a));
177 QPainter painter(
this);
178 painter.setRenderHint(QPainter::Antialiasing);
182 background.setFocalRadius(
_bounds.width() * 0.3);
183 background.setCenterRadius(
_bounds.width() * 0.7);
184 background.setColorAt(0, Qt::white);
185 background.setColorAt(1, Qt::lightGray);
187 painter.setPen(QPen(QBrush(Qt::gray),
_bounds.width() * 0.005));
188 painter.setBrush(QBrush(background));
192 painter.setPen(QPen(QBrush(Qt::gray),
_bounds.width() * 0.005));
203 if(!this->isEnabled())
return;
206 painter.setPen(QPen(Qt::lightGray,
_knopBounds.width() / 2.5, Qt::SolidLine, Qt::RoundCap));
210 knob.setColorAt(0, Qt::gray);
211 knob.setColorAt(1, Qt::darkGray);
215 painter.setPen(QPen(QBrush(Qt::darkGray),
_bounds.width() * 0.005));
216 painter.setBrush(QBrush(knob));
const float _midPoint
Middle value of the raw analog joystick coordinates.
float checkValue(const float &value, const float &min, const float &max) const
Check if the coordinate value is in proper range.
void addYAnimation()
Add an animation for the joystick to return to the y-axis zero position.
void yChange(float value)
Signal emitted when the y coordinate value changed.
Qt::Alignment _alignment
Widget alignment.
QPropertyAnimation * _xAnimation
Animation on the the x axis.
void removeYAnimation()
Remove an animation for the joystick to return to the y-axis zero position.
QPropertyAnimation * _yAnimation
Animation on the the y axis.
void movePos(const QPoint &coordinates)
Change the knop coordinates.
QRectF _knopBounds
Knop bounds.
void setX(const float &value)
Set the value of the x coordinate.
void setY(const float &value)
Set the value of the y coordinate.
float _y
Value of the the y coordinate.
float _x
Value of the the x coordinate.
void removeXAnimation()
Remove an animation for the joystick to return to the x-axis zero position.
virtual void paintEvent(QPaintEvent *event) override
Draw an analog joystick with its' background.
QParallelAnimationGroup * _animation
Animation group for two axis.
void addXAnimation()
Add an animation for the joystick to return to the x-axis zero position.
void setAlignment(Qt::Alignment align)
Set the widget alignment.
void resizeEvent(QResizeEvent *event) override
Event handler to change size of widget.
Joystick(QWidget *parent=nullptr)
Construct a new Joystick object.
void xChange(float value)
Signal emitted when the x coordinate value changed.
QRectF _bounds
Background bounds.
The Joystick class declaration.