how to create a joystick in pyqt4

Solutions on MaxInterview for how to create a joystick in pyqt4 by the best coders in the world

showing results for - "how to create a joystick in pyqt4"
Johanna
15 Mar 2019
1from PyQt4.QtGui import *
2from PyQt4.QtCore import *
3import sys
4from enum import Enum
5
6class Direction(Enum):
7    Left = 0
8    Right = 1
9    Up = 2
10    Down = 3
11
12class Joystick(QWidget):
13    def __init__(self, parent=None):
14        super(Joystick, self).__init__(parent)
15        self.setMinimumSize(100, 100)
16        self.movingOffset = QPointF(0, 0)
17        self.grabCenter = False
18        self.__maxDistance = 50
19
20    def paintEvent(self, event):
21        painter = QPainter(self)
22        bounds = QRectF(-self.__maxDistance, -self.__maxDistance, self.__maxDistance * 2, self.__maxDistance * 2).translated(self._center())
23        painter.drawEllipse(bounds)
24        painter.setBrush(Qt.black)
25        painter.drawEllipse(self._centerEllipse())
26
27    def _centerEllipse(self):
28        if self.grabCenter:
29            return QRectF(-20, -20, 40, 40).translated(self.movingOffset)
30        return QRectF(-20, -20, 40, 40).translated(self._center())
31
32    def _center(self):
33        return QPointF(self.width()/2, self.height()/2)
34
35
36    def _boundJoystick(self, point):
37        limitLine = QLineF(self._center(), point)
38        if (limitLine.length() > self.__maxDistance):
39            limitLine.setLength(self.__maxDistance)
40        return limitLine.p2()
41
42    def joystickDirection(self):
43        if not self.grabCenter:
44            return 0
45        normVector = QLineF(self._center(), self.movingOffset)
46        currentDistance = normVector.length()
47        angle = normVector.angle()
48
49        distance = min(currentDistance / self.__maxDistance, 1.0)
50        if 45 <= angle < 135:
51            return (Direction.Up, distance)
52        elif 135 <= angle < 225:
53            return (Direction.Left, distance)
54        elif 225 <= angle < 315:
55            return (Direction.Down, distance)
56        return (Direction.Right, distance)
57
58
59    def mousePressEvent(self, ev):
60        self.grabCenter = self._centerEllipse().contains(ev.pos())
61        return super().mousePressEvent(ev)
62
63    def mouseReleaseEvent(self, event):
64        self.grabCenter = False
65        self.movingOffset = QPointF(0, 0)
66        self.update()
67
68    def mouseMoveEvent(self, event):
69        if self.grabCenter:
70            print("Moving")
71            self.movingOffset = self._boundJoystick(event.pos())
72            self.update()
73        print(self.joystickDirection())
74
75if __name__ == '__main__':
76    # Create main application window
77    app = QApplication([])
78    app.setStyle(QStyleFactory.create("Cleanlooks"))
79    mw = QMainWindow()
80    mw.setWindowTitle('Joystick example')
81
82    # Create and set widget layout
83    # Main widget container
84    cw = QWidget()
85    ml = QGridLayout()
86    cw.setLayout(ml)
87    mw.setCentralWidget(cw)
88
89    # Create joystick 
90    joystick = Joystick()
91
92    # ml.addLayout(joystick.get_joystick_layout(),0,0)
93    ml.addWidget(joystick,0,0)
94
95    mw.show()
96
97    ## Start Qt event loop unless running in interactive mode or using pyside.
98    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
99        QApplication.instance().exec_()
similar questions
queries leading to this page
how to create a joystick in pyqt4