32 #include <QSizePolicy>
33 #include <QVBoxLayout>
45 QGraphicsView{scene, parent}
72 QWidget{parent}, onlyQuadrant2and1{!useQuadrant3and4}, nibble{}, steps{0}
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> viewPtr{
91 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();
123 auto length = std::hypot(pos.x(), pos.y());
124 auto normalized = pos / length;
132 if (normalized.x() < 0)
134 return -std::acos(-normalized.y());
138 return std::acos(-normalized.y());
144 JoystickControlWidget::mouseReleased()
153 if (onlyQuadrant2and1 && (pos.y() > 0))
159 auto length = std::hypot(pos.x(), pos.y());
160 auto normalized = pos / length;
168 pos = (length > 1) ? normalized : pos;
173 double stepSize = 1.0 / steps;
177 static_cast<unsigned int>(length * steps + 0.3 * stepSize) * stepSize;
178 pos = std::fmin(1, newLength) * normalized;
204 if (onlyQuadrant2and1)
206 height =
std::min(contentsRect().width() / 2, contentsRect().height());
211 width =
std::min(contentsRect().width(), contentsRect().height());
216 view->setMinimumSize(0.9 * width, 0.9 * height);
217 view->setMaximumSize(width, height);
218 auto viewScaleFactor = 0.49 * width;
219 view->setTransform(QTransform::fromScale(viewScaleFactor, viewScaleFactor));
225 steps = (stepCount < 0) ? 0 : stepCount;