/
inputdevice.py
271 lines (222 loc) · 9.86 KB
/
inputdevice.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# -*- coding: utf-8 -*-
###########################################################
## this module holds the keyboard and joystick devices
###########################################################
from panda3d.core import *
from direct.directnotify.DirectNotify import DirectNotify
import pygame
import keyboarddevice
import joystickdevice
import wiidevice
import sys
# ---------------------------------------------------------
# ---------------------------------------------------------
# ---------------------------------------------------------
# ---------------------------------------------------------
class InputDevice(object):
'''
'''
def __init__(self, device, settings):
'''
'''
self._notify = DirectNotify().newCategory("Input")
self._notify.info("New Input-Object created: %s" %(self))
self.device = device
self.directions = [0,0] # x and y movement
self.boost = False # Button for boosting
self.use_item = False # Button for using items
self.escape = False
# if this is a Joystick, look if there are Settings for Joysticks with this name
if type(self.device) == joystickdevice.JoystickDevice:
if self.device.getName() in settings["joysticks"]:
self.settings = settings["joysticks"][self.device.getName()]
else:
self.settings = {}
self._setStandardSettings()
# Keyboard settings are always available
elif type(self.device) == keyboarddevice.KeyboardDevice:
#self.settings = settings["keyboard"]
self.settings = {}
self.settings["boost"] = "space"
self.settings["use_item"] = "lalt"
self.settings["up"] = "arrow_up"
self.settings["down"] = "arrow_down"
self.settings["left"] = "arrow_left"
self.settings["right"] = "arrow_right"
self.settings["escape"] = "escape"
self.device.keys[self.settings["up"]] = False
self.device.keys[self.settings["down"]] = False
self.device.keys[self.settings["left"]] = False
self.device.keys[self.settings["right"]] = False
self.device.keys[self.settings["boost"]] = False
self.device.keys[self.settings["use_item"]] = False
self.device.keys[self.settings["escape"]] = False
elif type(self.device) == wiidevice.WiiDevice:
pass #Think isn't necessary by Wiimotes
# maybe load settings here
# ---------------------------------------------------------
def _setStandardSettings(self):
'''
if the input device is a joystick and no settings are available, this method analyzes the
kind of the Joystick and tries to find a senseful configuration
'''
# if we have at least two axes, the player is controlled by them
if self.device.getAxisCount() >= 2:
self.settings["directions"] = ("AXIS", 0)
# if there is only one or less axes we have to use the cooliehat
elif self.device.getHatCount() >= 1:
self.settings["directions"] = ("HAT", 0)
# if there isn't a hat either, the device can't be used
else:
raise StandardError("the Joystick device has no useable axes or hats")
# if four or more buttons are available, use the buttons for breakting and accelerating, too
if self.device.getButtonCount() >= 4:
self.settings["boost"] = ("BUTTON", 0)
self.settings["use_item"] = ("BUTTON", 2)
self.settings["accelerator"] = ("BUTTON", 1)
self.settings["brake"] = ("BUTTON", 3)
# if only two or three buttons and two axes are available, use the y-axis for breakting and accelerating
elif self.device.getButtonCount() >= 2 and self.device.getAxisCount() >= 2:
self.settings["boost"] = ("BUTTON", 0)
self.settings["use_item"] = ("BUTTON", 1)
self.settings["accelerator"] = ("AXIS", 0)
self.settings["brake"] = ("AXIS", 0)
# if only two or three buttons and a hat is available, use the y-axis of it for breakting and accelerating
elif self.device.getButtonCount() >= 2 and self.device.getHatCount() >= 1:
self.settings["boost"] = ("BUTTON", 0)
self.settings["use_item"] = ("BUTTON", 1)
self.settings["accelerator"] = ("HAT", 0)
self.settings["brake"] = ("HAT", 0)
# if the Joystick has less than 2 Buttons or less than 4 and no at or axes, it can't be used
else:
raise StandardError("the Joystick device has not enough buttons")
# ---------------------------------------------------------
def getSettings(self):
'''
@return: (dict) returns the settings of the device
'''
return self.settings
# ---------------------------------------------------------
def noticeAction(self):
'''
'''
if type(self.device) == joystickdevice.JoystickDevice:
## first get the direction:
# from axis
if self.settings["directions"][0] == "AXIS":
self.directions[0] = self.device.axes[self.settings["directions"][1]]
# or from cooliehat
elif self.settings["directions"][0] == "HAT":
self.directions[0] = self.device.hats[self.settings["directions"][1]]
# then get the acceleration
# from buttons
if self.settings["accelerator"][0] == self.settings["brake"][0] == "BUTTON":
value = 0
if self.device.buttons[self.settings["accelerator"][1]]:
value += 1
if self.device.buttons[self.settings["brake"][1]]:
value -= 1
self.directions[1] = value
# from axis
elif self.settings["accelerator"][0] == self.settings["brake"][0] == "AXIS":
self.directions[1] = -self.device.axes[self.settings["accelerator"][1]]
# or from cooliehat
elif self.settings["accelerator"][0] == self.settings["brake"][0] == "HAT":
self.directions[1] = self.device.hats[self.settings["accelerator"][1]]
# then get boost and item button values
self.boost = self.device.buttons[self.settings["boost"][1]]
self.use_item = self.device.buttons[self.settings["use_item"][1]]
elif type(self.device) == keyboarddevice.KeyboardDevice:
acceleration = 0
direction = 0
if self.device.keys[self.settings["up"]]:
acceleration += 1
if self.device.keys[self.settings["down"]]:
acceleration -= 1
if self.device.keys[self.settings["left"]]:
direction -= 1
if self.device.keys[self.settings["right"]]:
direction += 1
self.directions[0] = direction
self.directions[1] = acceleration
self.boost = self.device.keys[self.settings["boost"]]
self.use_item = self.device.keys[self.settings["use_item"]]
if self.device.keys[self.settings["escape"]]:
sys.exit()
elif type(self.device) == wiidevice.WiiDevice:
pass
# ---------------------------------------------------------
# ---------------------------------------------------------
# ---------------------------------------------------------
# ---------------------------------------------------------
# ---------------------------------------------------------
class InputDevices(object):
'''
'''
def __init__(self, settings):
'''
@param keyboard: = (KeyboardDevice) keyboard
@param joysticks: = (JoystickDevices) joysicks
'''
pygame.init()
pygame.joystick.init()
try:
import pywii
self.wii = pywii.Wii()
self.wiis = wiidevice.WiiDevices(self, self.wii)
except:
self.wii = []
self.wiis = None
self.keyboard = keyboarddevice.KeyboardDevice()
self.joysticks = joystickdevice.JoystickDevices()
self.devices = [InputDevice(self.keyboard, settings)]
for joystick in self.joysticks.getJoysticks():
try:
dev = InputDevice(joystick, settings)
except StandardError as e:
print e
continue
self.devices.append(dev)
# ---------------------------------------------------------
def fetchEvents(self, task):
'''
'''
self.joysticks.fetchEvents()
for device in self.devices:
device.noticeAction()
return task.cont
# ---------------------------------------------------------
def closeDevices(self):
'''
'''
pygame.joystick.quit()
pygame.quit()
# ---------------------------------------------------------
def getCount(self):
'''
'''
return pygame.joystick.get_count()
def addWii(self, wiimote):
'''
'''
self.devices.append(InputDevice(wiimote, {'wii':{}}, self.wii))
# ---------------------------------------------------------
# ---------------------------------------------------------
# ---------------------------------------------------------
# ---------------------------------------------------------
if __name__ == "__main__":
# from direct.showbase.ShowBase import ShowBase
# sb = ShowBase()
#
# import time
# import settings
#
# conf = settings.Settings()
# conf.loadSettings("user/config.ini")
#
#
# i = InputDevices(conf.getInputSettings())
#
# taskMgr.add(i.fetchEvents, "fetchEvents")
# run()
import main