16 #include <QMouseEvent>
18 #include <QStyleOption>
23 : QWidget(parent), _x(0), _y(0), _animation(new QParallelAnimationGroup(this)),
24 _xAnimation(new QPropertyAnimation(this,
"x")), _yAnimation(new QPropertyAnimation(this,
"y")),
25 _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 raw analog joystick coordinates.
float checkValue(const float &value, const float &min, const float &max) const
Check if coordinate value is in proper range.
void addYAnimation()
Add animation for y axis to return knop to home position.
void yChange(float value)
Signal emitted in order to realize value change of y coordinate.
Qt::Alignment _alignment
Alignment used in resizeEvent.
~Joystick()
Delete Joystick object.
QPropertyAnimation * _xAnimation
Animation on x axis when knop is returning home position.
void removeYAnimation()
Remove animation for y axis to return knop to home position.
QPropertyAnimation * _yAnimation
Animation on y axis when knop is returning home position.
void movePos(const QPoint &coordinates)
Change knop coordinates.
QRectF _knopBounds
Bounds of knop.
void setX(const float &value)
Set value of x coordinate.
void setY(const float &value)
Set value of y coordinate.
float _y
Value of y coordinate.
float _x
Value of x coordinate.
void removeXAnimation()
Remove animation for x axis to return knop to home position.
virtual void paintEvent(QPaintEvent *event) override
Draw analog joystick with it's background.
QParallelAnimationGroup * _animation
Animation group used to animate knop returning to home position.
void addXAnimation()
Add animation for x axis to return knop to home position.
void setAlignment(Qt::Alignment align)
Set alignment needed for resizeEvent to work.
void resizeEvent(QResizeEvent *event) override
Event handler to change size of widget.
Joystick(QWidget *parent=nullptr)
Construct new Joystick object.
void xChange(float value)
Signal emitted in order to realize value change of x coordinate.
QRectF _bounds
Bounds of background.
Header file for Joystick class.