-
Notifications
You must be signed in to change notification settings - Fork 2
/
joystick.py
109 lines (93 loc) · 2.9 KB
/
joystick.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#-----------------------------------------------------
# Filename: joystick.py
#
# Author: Ron Klose
#
# Description: JoystickHandler object uses pygame to
# enumerates game devices connected to
# the system and poll for events from
# the joystick. Any events caught will
# send a custom event to the panda3D
# event handler.
#-----------------------------------------------------
import pygame
from pygame.locals import *
class JoystickHandler:
'''
A handler for game devices. Initializes pygame,
enumerates the game devices, and adds pollInputDevice
to the task manager.
'''
def __init__(self):
# Initializes all imported pygame modules.
# Calls pygame.joystick.init()
pygame.init()
self.joy = []
# Enumerate all game devices recognized
self.enumerateDevices()
taskMgr.add(self.pollInputDevice, 'JoystickTask')
def enumerateDevices(self):
'''
Creates and initializes a Joystick object for each game
connected to the system.
Inputs:
None
Outputs:
None
'''
# Enumerate joysticks
for device in range(pygame.joystick.get_count()):
joystick = pygame.joystick.Joystick(device)
joystick.init()
self.joy.append(joystick)
def getDevices(self):
'''
Returns a list of the enumerated devices
Inputs:
None
Outputs:
self.joy - List of Joystick objects
'''
return self.joy
def getDeviceName(self, joystick_id):
'''
Returns the device name for a given
joystick ID
Inputs:
joystick_id - The ID of the joystick to get
the name of
Outputs:
deviceName - The name of the device based
on the given ID
'''
deviceName = self.joy[joystick_id].get_name()
return deviceName
def pollInputDevice(self, task):
'''
Polls for pygame joystick events and sends
custom signals to panda3D for events to be handled.
Inputs:
task
Outputs:
task.cont
'''
for event in pygame.event.get():
if event.type is JOYBUTTONDOWN:
name = 'joystick%d-button%d' % (event.joy, event.button)
messenger.send(name)
elif event.type is JOYBUTTONUP:
name = 'joystick%d-button%d-up' % (event.joy, event.button)
messenger.send(name)
elif event.type is JOYHATMOTION:
name = 'joystick%d-hat%d' % (event.joy, event.hat)
messenger.send(name, [event.value])
elif event.type is JOYAXISMOTION:
name = 'joystick%d-axis%d' % (event.joy, event.axis)
messenger.send(name, [event.value])
elif event.type is JOYBALLMOTION:
name = 'joystick%d-ball%d' % (event.joy, event.hat)
messenger.send(name, [event.rel])
# Left in for debug purposes until a menu is implemented
# to map the buttons to actions
#print name
return task.cont