1 from panda3d
.core
import InputDevice
8 self
.curr_vibration
= {}
9 self
.__is
_vibrating
= [False, False, False, False]
11 def init_joystick(self
):
12 for i
, dev
in enumerate(base
.devices
.getDevices(InputDevice
.DeviceClass
.gamepad
)):
13 base
.attachInputDevice(dev
, prefix
='joypad%s' % i
)
14 taskMgr
.add(self
._update
, 'update joysticks')
18 # joystick.Joystick(idx) for idx in range(joystick.get_count())]
19 # list(map(lambda joystick: joystick.init(), self.joysticks))
22 def num_joysticks(self
):
23 return len(base
.devices
.getDevices(InputDevice
.DeviceClass
.gamepad
))
26 def get_joystick(player_idx
):
27 devices
= base
.devices
.getDevices(InputDevice
.DeviceClass
.gamepad
)
28 if player_idx
> len(devices
) - 1:
29 return 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
30 gamepad
= devices
[player_idx
]
31 face_a
= gamepad
.findButton('face_a')
32 face_b
= gamepad
.findButton('face_b')
33 face_x
= gamepad
.findButton('face_x')
34 face_y
= gamepad
.findButton('face_y')
35 dpad_l
= gamepad
.findButton('dpad_left')
36 dpad_r
= gamepad
.findButton('dpad_right')
37 dpad_u
= gamepad
.findButton('dpad_up')
38 dpad_d
= gamepad
.findButton('dpad_down')
39 trigger_l
= gamepad
.findButton('ltrigger')
40 trigger_r
= gamepad
.findButton('rtrigger')
41 shoulder_l
= gamepad
.findButton('lshoulder')
42 shoulder_r
= gamepad
.findButton('rshoulder')
43 stick_l
= gamepad
.findButton('lstick')
44 stick_r
= gamepad
.findButton('rstick')
45 left_x
= gamepad
.findAxis(InputDevice
.Axis
.left_x
)
46 left_y
= gamepad
.findAxis(InputDevice
.Axis
.left_y
)
47 trigger_l_axis
= gamepad
.findAxis(InputDevice
.Axis
.left_trigger
)
48 trigger_r_axis
= gamepad
.findAxis(InputDevice
.Axis
.right_trigger
)
49 trigger_l_known
= trigger_l
.known
50 trigger_r_known
= trigger_r
.known
51 return (left_x
.value
, -left_y
.value
, face_a
.pressed
, face_b
.pressed
,
52 face_x
.pressed
, face_y
.pressed
,
53 dpad_l
.pressed
, dpad_r
.pressed
, dpad_u
.pressed
, dpad_d
.pressed
,
54 trigger_l
.pressed
or trigger_l_axis
.value
> .5,
55 trigger_r
.pressed
or trigger_r_axis
.value
> .5,
56 shoulder_l
.pressed
, shoulder_r
.pressed
, stick_l
.pressed
,
57 stick_r
.pressed
, trigger_l_known
, trigger_r_known
)
58 # for _ in pygame.event.get(): pass
59 # if not self.joysticks: return 0, 0, 0, 0
60 # jstick = self.joysticks[0]
61 # axis, btn = jstick.get_axis, jstick.get_button
62 # return axis(0), axis(1), btn(0), btn(1)
64 def set_vibration(self
, player_idx
, code
, time
=-1):
65 devices
= base
.devices
.getDevices(InputDevice
.DeviceClass
.gamepad
)
66 if player_idx
< 0 or player_idx
> len(devices
) - 1: return
67 if player_idx
in self
.curr_vibration
and \
68 code
in self
.curr_vibration
[player_idx
]: return
69 if player_idx
not in self
.curr_vibration
:
70 self
.curr_vibration
[player_idx
] = {}
71 self
.curr_vibration
[player_idx
][code
] = time
73 def clear_vibration(self
, player_idx
, code
=None):
74 devices
= base
.devices
.getDevices(InputDevice
.DeviceClass
.gamepad
)
75 if player_idx
< 0 or player_idx
> len(devices
) - 1: return
76 if player_idx
not in self
.curr_vibration
or \
77 code
not in self
.curr_vibration
[player_idx
]: return
78 if code
is None: del self
.curr_vibration
[player_idx
]
79 else: del self
.curr_vibration
[player_idx
][code
]
81 def _update(self
, task
):
82 devices
= base
.devices
.getDevices(InputDevice
.DeviceClass
.gamepad
)
83 for player_idx
in self
.curr_vibration
:
84 for code
in self
.curr_vibration
[player_idx
]:
85 if self
.curr_vibration
[player_idx
][code
] != -1:
86 dt
= globalClock
.getDt()
87 self
.curr_vibration
[player_idx
][code
] -= dt
88 for player_idx
in self
.curr_vibration
:
89 for code
in list(self
.curr_vibration
[player_idx
])[:]:
90 if self
.curr_vibration
[player_idx
][code
] != -1:
91 if self
.curr_vibration
[player_idx
][code
] < 0:
92 del self
.curr_vibration
[player_idx
][code
]
93 for player_idx
in list(self
.curr_vibration
)[:]:
94 if not self
.curr_vibration
[player_idx
]:
95 del self
.curr_vibration
[player_idx
]
96 for player_idx
, dev
in enumerate(devices
):
97 gamepad
= devices
[player_idx
]
98 if player_idx
in self
.curr_vibration
and \
99 not self
.__is
_vibrating
[player_idx
]:
100 gamepad
.set_vibration(.2, .4)
101 self
.__is
_vibrating
[player_idx
] = True
102 elif player_idx
not in self
.curr_vibration
:
103 gamepad
.set_vibration(0, 0)
104 self
.__is
_vibrating
[player_idx
] = False
111 # self.joysticks = []