[Simulator] Asynchronous SimulatorInterface & a few new features. (#4738)
[opentx.git] / companion / src / simulation / widgets / virtualjoystickwidget.h
blob5507d46e2e853f9e8ae1c8a23e0de5d287dda827
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #ifndef VIRTUALJOYSTICKWIDGET_H
22 #define VIRTUALJOYSTICKWIDGET_H
24 #include "radiowidget.h"
26 #include <QWidget>
27 #include <QResizeEvent>
28 #include <QBoxLayout>
29 #include <QGridLayout>
30 #include <QGraphicsView>
31 #include <QGraphicsScene>
32 #include <QToolButton>
33 #include <QTimer>
34 #include <QLabel>
36 class CustomGraphicsScene;
37 class Node;
38 class RadioTrimWidget;
40 class VirtualJoystickWidget : public QWidget
42 Q_OBJECT
44 public:
45 enum ConstraintTypes {
46 HOLD_X = 0x01,
47 HOLD_Y = 0x02,
48 FIX_X = 0x04,
49 FIX_Y = 0x08
52 explicit VirtualJoystickWidget(QWidget * parent = NULL, QChar side = 'L', bool showTrims = true, bool showBtns = true, bool showValues = true, QSize size = QSize(125, 125));
54 qreal getStickX();
55 qreal getStickY();
56 QPointF getStickPos();
57 int getTrimValue(int which);
58 virtual QSize sizeHint() const;
59 virtual void resizeEvent(QResizeEvent *event);
61 int getStickScale() const;
62 void setStickScale(int stickScale);
64 public slots:
65 void setStickX(qreal x);
66 void setStickY(qreal y);
67 void setStickPos(QPointF xy);
68 void setStickAxisValue(int index, int value);
69 void centerStick();
70 void setTrimsRange(int min, int max);
71 void setTrimsRange(int rng);
72 void setTrimRange(int index, int min, int max);
73 void setTrimValue(int index, int value);
74 void setWidgetValue(const RadioWidget::RadioWidgetType type, const int index, const int value);
76 void setStickIndices(int xIdx = -1, int yIdx = -1);
77 void setStickConstraint(quint8 index, bool active);
78 void setStickColor(const QColor & color);
79 void loadDefaultsForMode(const unsigned mode);
81 signals:
82 void valueChange(const RadioWidget::RadioWidgetType type, const int index, int value);
84 protected slots:
85 void onNodeXChanged();
86 void onNodeYChanged();
87 void onButtonChange(bool checked);
88 void updateNodeValueLabels();
89 void onGsMouseEvent(QGraphicsSceneMouseEvent * event);
91 protected:
92 void setSize(const QSize & size, const QSize &);
93 RadioTrimWidget * createTrimWidget(QChar type);
94 QToolButton * createButtonWidget(int type);
95 QLayout * createNodeValueLayout(QChar type, QLabel *& valLabel);
96 int getStickIndex(QChar type);
97 int getTrimSliderType(QChar type);
98 int getTrimButtonType(QChar type, int pos);
99 RadioTrimWidget * getTrimWidget(int which);
101 QChar stickSide;
102 QSize prefSize;
103 QGridLayout * layout;
104 QGraphicsView * gv;
105 CustomGraphicsScene * scene;
106 Node * node;
107 RadioTrimWidget * hTrimWidget;
108 RadioTrimWidget * vTrimWidget;
109 QToolButton * btnHoldX, * btnHoldY;
110 QToolButton * btnFixX, * btnFixY;
111 QLabel * nodeLabelX, * nodeLabelY;
112 QSize extraSize;
113 QTimer centerStickTimer;
114 float ar; // aspect ratio
115 int m_xIndex;
116 int m_yIndex;
117 int m_stickScale;
118 bool m_stickPressed;
123 * Custom GraphicsScene for mouse handling
125 class CustomGraphicsScene : public QGraphicsScene
127 Q_OBJECT
129 public:
130 CustomGraphicsScene(QObject *parent = Q_NULLPTR) :
131 QGraphicsScene(parent)
134 signals:
135 void mouseEvent(QGraphicsSceneMouseEvent * event);
137 protected:
138 virtual void mousePressEvent(QGraphicsSceneMouseEvent * event);
139 virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent * event);
140 virtual void mouseMoveEvent(QGraphicsSceneMouseEvent * event);
143 #endif // VIRTUALJOYSTICKWIDGET_H