summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Scheibenpflug <zorchenhimer@gmail.com>2016-10-29 03:29:25 (GMT)
committerNick Scheibenpflug <zorchenhimer@gmail.com>2016-10-29 03:29:25 (GMT)
commit39e72c981e3d5867431ef60f825a88f0cb2e55a4 (patch)
tree2d0f17c346688f4d8d96a373c16077e0c15e09bb
parent04a92b85e4da324325846a983dbf518ecb38ab92 (diff)
downloadcontroller-keyboard-39e72c981e3d5867431ef60f825a88f0cb2e55a4.zip
controller-keyboard-39e72c981e3d5867431ef60f825a88f0cb2e55a4.tar.gz
Add sector detection
The joystick can now determine which sector it is in. There are currently eight sectors plus one "off" sector. The sectors are 45 degree slices offset by 22.5 degrees to line up with n, ne, e, se, s, sw, w, and nw.
-rw-r--r--lib/EventHandler.py70
-rw-r--r--lib/Keyboards.py8
2 files changed, 75 insertions, 3 deletions
diff --git a/lib/EventHandler.py b/lib/EventHandler.py
index e4580e7..795ba3f 100644
--- a/lib/EventHandler.py
+++ b/lib/EventHandler.py
@@ -4,9 +4,22 @@
4## eg: on "you died" do function_x() 4## eg: on "you died" do function_x()
5 5
6import copy 6import copy
7import math
7import pygame 8import pygame
8import random 9import random
9 10
11Sectors = {
12 'n': ['a', 'b', 'c', 'd'],
13 'ne': ['e', 'f', 'g', 'h'],
14 'e': ['i', 'j', 'k', 'l'],
15 'se': ['m', 'n', 'o', 'p'],
16 's': ['q', 'r', 's', 't'],
17 'sw': ['u', 'v', 'w', 'x'],
18 'w': ['y', 'z', '.', ','],
19 'nw': ['"', '-', '!', '?'],
20 'x': ['*', '*', '*', '*'],
21}
22
10class KeyHandler(object): 23class KeyHandler(object):
11 def __init__(self, name=None): 24 def __init__(self, name=None):
12 self.keydown_assignments = {} 25 self.keydown_assignments = {}
@@ -218,6 +231,45 @@ class JoyHandler(object):
218 def get_value(self): 231 def get_value(self):
219 return (self.__X.get_value(), self.__Y.get_value()) 232 return (self.__X.get_value(), self.__Y.get_value())
220 233
234 def get_intensity(self):
235 norm_x = self.__X.get_value() * 100 + 0.00001
236 norm_y = self.__Y.get_value() * 100 + 0.00001
237 return math.floor(math.sqrt(math.pow(norm_x, 2) + math.pow(norm_y, 2)))
238
239 def get_sector(self):
240 angle = 0
241 norm_x = self.__X.get_value() + 0.00001
242 norm_y = self.__Y.get_value() + 0.00001
243
244 intensity = self.get_intensity()
245
246 if norm_x != 0:
247 angle = math.atan(norm_y/norm_x) * (180 / math.pi)
248 if norm_x > 0:
249 angle += 180
250 elif norm_y > 0:
251 angle += 360
252
253 if intensity > 25:
254 if angle < 22.5:
255 return 'w'
256 elif angle < 67.5:
257 return 'nw'
258 elif angle < 112.5:
259 return 'n'
260 elif angle < 157.5:
261 return 'ne'
262 elif angle < 202.5:
263 return 'e'
264 elif angle < 247.5:
265 return 'se'
266 elif angle < 292.5:
267 return 's'
268 elif angle < 337.5:
269 return 'sw'
270 return 'w'
271 return 'x'
272
221 def update(self): 273 def update(self):
222 self.__X.update() 274 self.__X.update()
223 self.__Y.update() 275 self.__Y.update()
@@ -282,6 +334,16 @@ class JoyHandler(object):
282 else: 334 else:
283 return self.RightStick.get_value() 335 return self.RightStick.get_value()
284 336
337 def get_sector(self, stick):
338 if stick == 'right':
339 return self.RightStick.get_sector()
340 return self.LeftStick.get_sector()
341
342 def get_intensity(self, stick):
343 if stick == 'right':
344 return self.RightStick.get_intensity()
345 return self.LeftStick.get_intensity()
346
285 def get_trigger_axis_value(self): 347 def get_trigger_axis_value(self):
286 return self.TriggerAxis.get_value() 348 return self.TriggerAxis.get_value()
287 349
@@ -365,6 +427,14 @@ class JoyHandler(object):
365 if self.real_handle is not None: 427 if self.real_handle is not None:
366 return self.real_handle.get_stick_value(stick) 428 return self.real_handle.get_stick_value(stick)
367 429
430 def get_sector(self, stick='left'):
431 if self.real_handle is not None:
432 return self.real_handle.get_sector(stick)
433
434 def get_intensity(self, stick='left'):
435 if self.real_handle is not None:
436 return self.real_handle.get_intensity(stick)
437
368 def do_joydown(self, button, joy=0): 438 def do_joydown(self, button, joy=0):
369 if self.real_handle is not None: 439 if self.real_handle is not None:
370 self.real_handle.do_joydown(button, joy) 440 self.real_handle.do_joydown(button, joy)
diff --git a/lib/Keyboards.py b/lib/Keyboards.py
index 9edcbc4..4cabd2c 100644
--- a/lib/Keyboards.py
+++ b/lib/Keyboards.py
@@ -1,6 +1,6 @@
1 1
2import pygame 2import pygame
3from lib.EventHandler import JoyHandler 3from lib.EventHandler import JoyHandler, Sectors
4from lib.HeadsUpDisplay import Locations as HudLocs 4from lib.HeadsUpDisplay import Locations as HudLocs
5 5
6class Keyboard(object): 6class Keyboard(object):
@@ -52,7 +52,7 @@ class ControllerKeyboard(Keyboard):
52 js = pygame.joystick.Joystick(0) 52 js = pygame.joystick.Joystick(0)
53 a_lst = [] 53 a_lst = []
54 for n in range(0, js.get_numaxes()): 54 for n in range(0, js.get_numaxes()):
55 a_lst.append('{}: {}'.format(n, js.get_axis(n))) 55 a_lst.append('{}: {:.3f}'.format(n, js.get_axis(n)))
56 56
57 self.hud.set_text(HudLocs.BOTTOMLEFT, '\n'.join(a_lst)) 57 self.hud.set_text(HudLocs.BOTTOMLEFT, '\n'.join(a_lst))
58 58
@@ -60,9 +60,11 @@ class ControllerKeyboard(Keyboard):
60 pygame.draw.line(surf, self.line_color_2, [150, 150], [rval_x, rval_y]) 60 pygame.draw.line(surf, self.line_color_2, [150, 150], [rval_x, rval_y])
61 pygame.draw.line(surf, (0, 0, 0), [150, 10], [tval, 10]) 61 pygame.draw.line(surf, (0, 0, 0), [150, 10], [tval, 10])
62 pygame.draw.line(surf, (0, 0, 0), [150, 150], [tval_x, tval_y]) 62 pygame.draw.line(surf, (0, 0, 0), [150, 150], [tval_x, tval_y])
63 sector = self.handler.get_sector()
63 64
64 if self.hud is not None: 65 if self.hud is not None:
65 self.hud.set_text(HudLocs.BOTTOMCENTER, '(x, y): {}'.format((val_x, val_y))) 66 self.hud.set_text(HudLocs.BOTTOMCENTER, "(x, y): {}\nintensity: {}".format((val_x, val_y), self.handler.get_intensity()))
67 self.hud.set_text(HudLocs.BOTTOMRIGHT, "sector: {1:2s}\n{0[0]} \n{0[3]} {0[1]}\n{0[2]} ".format(Sectors[sector], sector))
66 self.hud.draw(screen) 68 self.hud.draw(screen)
67 69
68 screen_x, screen_y = screen.get_size() 70 screen_x, screen_y = screen.get_size()