-
Notifications
You must be signed in to change notification settings - Fork 2
/
joystickdevice.py
151 lines (119 loc) · 4.53 KB
/
joystickdevice.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# -*- coding: utf-8 -*-
##############################################################
## this module wraps around the pygame joystick event handling
## and holds the information about all connected joystick and
## gamepad devices
##############################################################
import pygame
from direct.directnotify.DirectNotify import DirectNotify
# ---------------------------------------------------------
# ---------------------------------------------------------
# ---------------------------------------------------------
class JoystickDevice(object):
'''
This class represents a joystick and holds the pygame data
'''
def __init__(self, joystick):
'''
@param joystick: the pygame joystick object
'''
self._notify = DirectNotify().newCategory("Input")
self._notify.info("New Joystick-Object created: %s" %(self))
# the pygame joysick object
self.joystick = joystick
self.joystick.init()
#print self.joystick.get_name()
# initialize the buttons axes, and cooliehats
self.buttons = []
self.axes = []
self.hats = []
for i in range(self.joystick.get_numaxes()):
self.axes.append(0.0)
for i in range(self.joystick.get_numbuttons()):
self.buttons.append(False)
for i in range(self.joystick.get_numhats()):
self.hats.append((0,0))
# ---------------------------------------------------------
def getAxisCount(self):
'''
'''
return len(self.axes)
# ---------------------------------------------------------
def getHatCount(self):
'''
'''
return len(self.hats)
# ---------------------------------------------------------
def getButtonCount(self):
'''
'''
return len(self.buttons)
# ---------------------------------------------------------
def getName(self):
'''
'''
return self.joystick.get_name()
# ---------------------------------------------------------
# ---------------------------------------------------------
# ---------------------------------------------------------
class JoystickDevices():
'''
This class holds all connected Joystick and gamepad devices
'''
def __init__(self):
'''
initializes the joysticks
'''
# we need a list for our joysticks
self.joysticks = []
# fill the list
for num in range(pygame.joystick.get_count()):
self.joysticks.append(JoystickDevice(pygame.joystick.Joystick(num)))
# ---------------------------------------------------------
def getJoysticks(self):
'''
'''
return self.joysticks
# ---------------------------------------------------------
def getDeviceCount(self):
'''
@return: (int) count of the connected joystick devices
'''
return len(self.joysticks)
# ---------------------------------------------------------
def getJoystick(self, number):
'''
@return: (pygame.joystick.Joystick) the Joystick with the given number
'''
if number >= len(self.joysticks) or number < 0 or type(number) != int:
raise IndexError("Joystick Device index out of range")
return self.joysticks[number]
# ---------------------------------------------------------
def fetchEvents(self):
'''
this function gets the events from pygame
'''
# catch all joystick events from pygame
for e in pygame.event.get([pygame.JOYAXISMOTION, pygame.JOYBUTTONUP, pygame.JOYBUTTONDOWN, pygame.JOYHATMOTION]): #pygame.JOYBALLMOTION,
# cooliehat moved / for gamepads without an analog stick
if e.type == pygame.JOYHATMOTION:
self.joysticks[e.joy].buttons[e.hat] = e.value
# button pressed
if e.type == pygame.JOYBUTTONDOWN:
self.joysticks[e.joy].buttons[e.button] = True
# button released
if e.type == pygame.JOYBUTTONUP:
self.joysticks[e.joy].buttons[e.button] = False
# axis changed
if e.type == pygame.JOYAXISMOTION:
self.joysticks[e.joy].axes[e.axis] = e.value
# ---------------------------------------------------------
if __name__ == "__main__":
# init pygame and its joystick module
pygame.init()
pygame.joystick.init()
j = JoystickDevices()
while True:
j.fetchEvents()
pygame.joystick.quit()
pygame.quit()