-
Notifications
You must be signed in to change notification settings - Fork 0
/
tank.py
61 lines (47 loc) · 2.05 KB
/
tank.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from PySide2.QtCore import QObject, Property, Signal, Slot
import joysticks
class Tank(QObject):
def __init__(self, index, remote_control):
QObject.__init__(self)
self._index = index
self._remote_control = remote_control
self._connected = False
self._joystick = None
def name(self):
return f"Tank {self._index}"
name_changed = Signal()
name = Property(str, name, notify=name_changed)
def is_connected(self):
return self._connected
connected_changed = Signal()
connected = Property(bool, is_connected, notify=connected_changed)
def joystick(self):
return self._joystick
def is_joystick_assigned(self):
return self._joystick is not None
def set_joystick(self, j):
if self._joystick != j:
if self._joystick is not None:
self._joystick.polled.disconnect(self.on_joystick_polled)
self._joystick.long_press_buttons_pressed.disconnect(self.on_long_press_buttons_pressed)
self._joystick = j
if self._joystick is not None:
self._joystick.polled.connect(self.on_joystick_polled)
self._joystick.long_press_buttons_pressed.connect(self.on_long_press_buttons_pressed)
self.joystick_assigned_changed.emit(self.is_joystick_assigned())
joystick_assigned_changed = Signal(bool)
joystickAssigned = Property(bool, is_joystick_assigned, notify=joystick_assigned_changed)
@Slot(int, int, int)
def on_joystick_polled(self, x, y, buttons):
self._remote_control.send_move_command(self._index, x, y)
@Slot(int)
def on_long_press_buttons_pressed(self, buttons):
if buttons & joysticks.SNES_BUTTON_SELECT:
self.select_pressed.emit(self)
select_pressed = Signal(QObject)
@Slot(list)
def onConnectionsChanged(self, ids):
new_connected_status = self._index in ids
if new_connected_status != self._connected:
self._connected = new_connected_status
self.connected_changed.emit()