29 #include <QVBoxLayout>
33 #include <QSizePolicy>
45 QGraphicsView {scene, parent}
70 onlyQuadrant2and1 {!useQuadrant3and4},
75 std::unique_ptr<QGraphicsScene> scene{
new QGraphicsScene{}};
76 QPen pen{Qt::black, 0};
78 scene->addEllipse(-1, -1, 2, 2, pen);
80 scene->addEllipse(-0.13, -0.13, 0.26, 0.26, pen);
82 scene->addLine(0, -1, 0, 1, pen);
83 scene->addLine(-1, 0, 1, 0, pen);
85 QPen penRed{Qt::darkRed};
86 penRed.setCosmetic(
true);
87 nibble = scene->addEllipse(-0.1, -0.1, 0.2, 0.2, penRed, QBrush{Qt::darkRed});
90 std::unique_ptr<JoystickControlWidgetQGraphicsView>
91 viewPtr{
new JoystickControlWidgetQGraphicsView{scene.release()}};
94 view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
95 view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
96 view->setSceneRect(-1, -1, 2, (onlyQuadrant2and1 ? 1 : 2));
99 connect(viewPtr.get(), SIGNAL(positionChanged(QPointF)),
this, SLOT(setNibble(QPointF)));
100 connect(viewPtr.get(), SIGNAL(released()),
this, SLOT(mouseReleased()));
101 connect(viewPtr.get(), SIGNAL(pressed()),
this, SIGNAL(pressed()));
104 std::unique_ptr<QVBoxLayout> layout{
new QVBoxLayout{}};
105 layout->setSpacing(0);
106 layout->setContentsMargins(0, 0, 0, 0);
107 layout->addWidget(viewPtr.release());
108 setLayout(layout.release());
114 return nibble->pos();
122 auto length = std::hypot(pos.x(), pos.y());
123 auto normalized = pos / length;
131 if (normalized.x() < 0)
133 return -std::acos(-normalized.y());
137 return std::acos(-normalized.y());
142 void JoystickControlWidget::mouseReleased()
150 if (onlyQuadrant2and1 && (pos.y() > 0))
156 auto length = std::hypot(pos.x(), pos.y());
157 auto normalized = pos / length;
165 pos = (length > 1) ? normalized : pos;
170 double stepSize = 1.0 / steps;
173 double newLength =
static_cast<unsigned int>(length * steps + 0.3 * stepSize) * stepSize;
174 pos = std::fmin(1, newLength) * normalized;
200 if (onlyQuadrant2and1)
202 height =
std::min(contentsRect().width() / 2, contentsRect().height());
207 width =
std::min(contentsRect().width(), contentsRect().height());
212 view->setMinimumSize(0.9 * width, 0.9 * height);
213 view->setMaximumSize(width, height);
214 auto viewScaleFactor = 0.49 * width;
215 view->setTransform(QTransform::fromScale(viewScaleFactor, viewScaleFactor));
221 steps = (stepCount < 0) ? 0 : stepCount;