Use QPointF instead of QPoint for float input

This commit is contained in:
Ogi Moore 2021-02-12 13:30:06 -08:00
parent f853d8b520
commit 955f1c37a6
1 changed files with 17 additions and 9 deletions

View File

@ -11,7 +11,7 @@ class JoystickButton(QtGui.QPushButton):
self.radius = 200
self.setCheckable(True)
self.state = None
self.setState(0,0)
self.setState(0, 0)
self.setFixedWidth(50)
self.setFixedHeight(50)
@ -42,21 +42,24 @@ class JoystickButton(QtGui.QPushButton):
def setState(self, *xy):
xy = list(xy)
d = (xy[0]**2 + xy[1]**2)**0.5
nxy = [0,0]
nxy = [0, 0]
for i in [0,1]:
if xy[i] == 0:
nxy[i] = 0
else:
nxy[i] = xy[i]/d
nxy[i] = xy[i] / d
if d > self.radius:
d = self.radius
d = (d/self.radius)**2
xy = [nxy[0]*d, nxy[1]*d]
d = (d / self.radius) ** 2
xy = [nxy[0] * d, nxy[1] * d]
w2 = self.width()/2.
h2 = self.height()/2
self.spotPos = QtCore.QPoint(w2*(1+xy[0]), h2*(1-xy[1]))
w2 = self.width() / 2
h2 = self.height() / 2
self.spotPos = QtCore.QPoint(
int(w2 * (1 + xy[0])),
int(h2 * (1 - xy[1]))
)
self.update()
if self.state == xy:
return
@ -67,7 +70,12 @@ class JoystickButton(QtGui.QPushButton):
super().paintEvent(ev)
p = QtGui.QPainter(self)
p.setBrush(QtGui.QBrush(QtGui.QColor(0,0,0)))
p.drawEllipse(self.spotPos.x()-3,self.spotPos.y()-3,6,6)
p.drawEllipse(
self.spotPos.x() - 3,
self.spotPos.y() - 3,
6,
6
)
def resizeEvent(self, ev):
self.setState(*self.state)