summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Scheibenpflug <zorchenhimer@gmail.com>2016-10-29 16:32:17 (GMT)
committerNick Scheibenpflug <zorchenhimer@gmail.com>2016-10-29 16:32:17 (GMT)
commit6d7b24a5cfb7ab03e57fc31cfd5d8829d6e26e64 (patch)
treed98b88d3a42b6c6b8e14285aeee1fd971caf9a2b
parent7a373a0dae31ba38694abeb343e3e3fbd151967f (diff)
downloadcontroller-keyboard-6d7b24a5cfb7ab03e57fc31cfd5d8829d6e26e64.zip
controller-keyboard-6d7b24a5cfb7ab03e57fc31cfd5d8829d6e26e64.tar.gz
Joysick can type!
Lots of stuff is in need of cleaning up, however. I think I've got too many nested levels of shit.
-rw-r--r--lib/Environment.py40
-rw-r--r--lib/EventHandler.py17
-rw-r--r--lib/Keyboards.py5
-rw-r--r--main.py2
4 files changed, 56 insertions, 8 deletions
diff --git a/lib/Environment.py b/lib/Environment.py
index e44650a..2e4c54f 100644
--- a/lib/Environment.py
+++ b/lib/Environment.py
@@ -4,7 +4,7 @@ import pygame
4import platform 4import platform
5from os import path 5from os import path
6from lib.Utils import load_image, tile_image 6from lib.Utils import load_image, tile_image
7from lib.EventHandler import JoyHandler, KeyHandler 7from lib.EventHandler import JoyHandler, KeyHandler, Sectors
8from lib.Keyboards import ControllerKeyboard 8from lib.Keyboards import ControllerKeyboard
9from lib.HeadsUpDisplay import HUD as HeadsUpDisplay, Locations as HudLocs 9from lib.HeadsUpDisplay import HUD as HeadsUpDisplay, Locations as HudLocs
10from lib.xinput import XInputJoystick 10from lib.xinput import XInputJoystick
@@ -39,11 +39,14 @@ class EnvironmentController(object):
39 39
40 self.shifted = False 40 self.shifted = False
41 self.set_keyboard_keys() 41 self.set_keyboard_keys()
42 self.set_joystick_buttons()
42 43
43 ## Repeat KEYDOWN events after 500 miliseconds. Each subsequent 44 ## Repeat KEYDOWN events after 500 miliseconds. Each subsequent
44 ## KEYDOWN event is 25 miliseconds after the previous. 45 ## KEYDOWN event is 25 miliseconds after the previous.
45 pygame.key.set_repeat(500, 25) 46 pygame.key.set_repeat(500, 25)
46 47
48 #self.controller.handler.dump_bindings()
49
47 def update(self): 50 def update(self):
48 #self.current_handler.update() 51 #self.current_handler.update()
49 self.controller.update() 52 self.controller.update()
@@ -57,6 +60,13 @@ class EnvironmentController(object):
57 def load_layout(self, layout_file): 60 def load_layout(self, layout_file):
58 pass 61 pass
59 62
63 def do_joydown(self, event):
64 #print('joydown: {}'.format(event.button))
65 self.controller.do_joydown(str(event.button))
66
67 def do_joyup(self, event):
68 self.controller.do_joyup(str(event.button))
69
60 def do_keydown(self, event): 70 def do_keydown(self, event):
61 self.keyboard_handler.do_keydown(event.key) 71 self.keyboard_handler.do_keydown(event.key)
62 72
@@ -142,6 +152,34 @@ class EnvironmentController(object):
142 self.keyboard_handler.add_keyup_handle(pygame.K_LSHIFT, self.type_shift, args=(False)) 152 self.keyboard_handler.add_keyup_handle(pygame.K_LSHIFT, self.type_shift, args=(False))
143 self.keyboard_handler.add_keyup_handle(pygame.K_RSHIFT, self.type_shift, args=(False)) 153 self.keyboard_handler.add_keyup_handle(pygame.K_RSHIFT, self.type_shift, args=(False))
144 154
155
156 def set_joystick_buttons(self):
157 self.controller.handler.add_joydown_handle("0", self.button_press, args=('a'))
158 self.controller.handler.add_joydown_handle("1", self.button_press, args=('b'))
159 self.controller.handler.add_joydown_handle("2", self.button_press, args=('x'))
160 self.controller.handler.add_joydown_handle("3", self.button_press, args=('y'))
161
162 self.controller.handler.add_joydown_handle("5", self.type_key, args=('backspace'))
163
164 self.controller.handler.add_joydown_handle("4", self.type_shift, args=(True))
165 self.controller.handler.add_joyup_handle("4", self.type_shift, args=(False))
166
167 def button_press(self, button):
168 sector = self.controller.handler.get_sector()
169 if sector == 'x':
170 return
171
172 if button == 'y': # Y
173 self.type_key(Sectors[sector][0])
174 elif button == 'b': # B
175 self.type_key(Sectors[sector][1])
176 elif button == 'a': # A
177 self.type_key(Sectors[sector][2])
178 elif button == 'x': # X
179 self.type_key(Sectors[sector][3])
180 else: ## All other buttons
181 pass
182
145class Text(object): 183class Text(object):
146 """ Handles drawing text and the screen. """ 184 """ Handles drawing text and the screen. """
147 def __init__(self, text='', enable_cursor = True): 185 def __init__(self, text='', enable_cursor = True):
diff --git a/lib/EventHandler.py b/lib/EventHandler.py
index d1993a6..5feb7c7 100644
--- a/lib/EventHandler.py
+++ b/lib/EventHandler.py
@@ -73,6 +73,15 @@ class KeyHandler(object):
73 def copy(self): 73 def copy(self):
74 return copy.copy(self) 74 return copy.copy(self)
75 75
76 def dump_bindings(self):
77 print("Keydown:")
78 for key, val in self.keydown_assignments.items():
79 print("{}: {}\n".format(key, val))
80
81 print("Keyup:")
82 for key, val in self.keyup_assignments.items():
83 print("{}: {}\n".format(key, val))
84
76## TODO: Analogue sticks 85## TODO: Analogue sticks
77class JoyHandler(object): 86class JoyHandler(object):
78 """ 87 """
@@ -293,17 +302,11 @@ class JoyHandler(object):
293 i = 0 302 i = 0
294 self.num_axes = pygame.joystick.Joystick(jID).get_numaxes() 303 self.num_axes = pygame.joystick.Joystick(jID).get_numaxes()
295 print('Joystick id {} number of axes: {}'.format(jID, self.num_axes)) 304 print('Joystick id {} number of axes: {}'.format(jID, self.num_axes))
305
296 self.LeftStick = JoyHandler.MrStick(jID, 0, 1, self.ButtonHandler) 306 self.LeftStick = JoyHandler.MrStick(jID, 0, 1, self.ButtonHandler)
297 self.RightStick = JoyHandler.MrStick(jID, 4, 3, self.ButtonHandler) 307 self.RightStick = JoyHandler.MrStick(jID, 4, 3, self.ButtonHandler)
298 self.TriggerAxis = JoyHandler.MrAxis(jID, 2, self.ButtonHandler) 308 self.TriggerAxis = JoyHandler.MrAxis(jID, 2, self.ButtonHandler)
299 self.TriggerStick = JoyHandler.MrStick(jID, 2, 5, self.ButtonHandler) 309 self.TriggerStick = JoyHandler.MrStick(jID, 2, 5, self.ButtonHandler)
300 #while i < self.num_axes:
301 # if (i + 2) < num_axes:
302 # self.axes.append(JoyHandler.MrStick(jID, i, i+1, self.ButtonHandler))
303 # i += 2
304 # else:
305 # self.axes.append(JoyHandler.MrAxis(jID, i, self.ButtonHandler))
306 # i += 1
307 310
308 def update(self): 311 def update(self):
309 ## I can't find a controller with more than one hat that isn't the 312 ## I can't find a controller with more than one hat that isn't the
diff --git a/lib/Keyboards.py b/lib/Keyboards.py
index c371df4..2e453fb 100644
--- a/lib/Keyboards.py
+++ b/lib/Keyboards.py
@@ -71,3 +71,8 @@ class ControllerKeyboard(Keyboard):
71 surf_x, surf_y = surf.get_size() 71 surf_x, surf_y = surf.get_size()
72 screen.blit(surf, ((screen_x/2) - (surf_x/2), (screen_y/2) - (surf_y/2))) 72 screen.blit(surf, ((screen_x/2) - (surf_x/2), (screen_y/2) - (surf_y/2)))
73 73
74 def do_joydown(self, key):
75 self.handler.do_joydown(key)
76
77 def do_joyup(self, key):
78 self.handler.do_joyup(key)
diff --git a/main.py b/main.py
index 9decade..275e0d6 100644
--- a/main.py
+++ b/main.py
@@ -53,8 +53,10 @@ class Game(object):
53 ## the controller keyboard which gets passed off to the 53 ## the controller keyboard which gets passed off to the
54 ## JoyHandler. 54 ## JoyHandler.
55 elif event.type == pygame.JOYBUTTONDOWN: 55 elif event.type == pygame.JOYBUTTONDOWN:
56 self.controller.do_joydown(event)
56 pass 57 pass
57 elif event.type == pygame.JOYBUTTONUP: 58 elif event.type == pygame.JOYBUTTONUP:
59 self.controller.do_joyup(event)
58 pass 60 pass
59 61
60 if running == False: 62 if running == False: