summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Scheibenpflug <zorchenhimer@gmail.com>2016-11-12 18:36:36 (GMT)
committerNick Scheibenpflug <zorchenhimer@gmail.com>2016-11-12 18:36:36 (GMT)
commita7c872c5c8aa4376ac6c3fb96d13065b6fb954af (patch)
tree9c0fe4edc5aa07275779435ba7325694ccb1dd37
parent738a03336bb2b86ecfd9a0317458d89928b6c638 (diff)
downloadcontroller-keyboard-a7c872c5c8aa4376ac6c3fb96d13065b6fb954af.zip
controller-keyboard-a7c872c5c8aa4376ac6c3fb96d13065b6fb954af.tar.gz
Add a proper GUI for the sectors
All the available sectors are now displayed on screen at the same time, arranged according to their angle on the joystick. Also removed the debugging HUD elements (sans FPS counter) as the are no longer needed.
-rw-r--r--lib/Environment.py4
-rw-r--r--lib/EventHandler.py12
-rw-r--r--lib/Keyboards.py131
-rw-r--r--pics/bg_sector.pngbin0 -> 11636 bytes
-rw-r--r--pics/bg_sector_active.pngbin0 -> 11921 bytes
5 files changed, 103 insertions, 44 deletions
diff --git a/lib/Environment.py b/lib/Environment.py
index 049dd96..b460c71 100644
--- a/lib/Environment.py
+++ b/lib/Environment.py
@@ -5,8 +5,8 @@ import platform
5import math 5import math
6from os import path 6from os import path
7from lib.Utils import load_image, tile_image 7from lib.Utils import load_image, tile_image
8from lib.EventHandler import JoyHandler, KeyHandler, Sectors 8from lib.EventHandler import JoyHandler, KeyHandler
9from lib.Keyboards import ControllerKeyboard 9from lib.Keyboards import ControllerKeyboard, Sectors
10from lib.HeadsUpDisplay import HUD as HeadsUpDisplay, Locations as HudLocs 10from lib.HeadsUpDisplay import HUD as HeadsUpDisplay, Locations as HudLocs
11from lib.xinput import XInputJoystick 11from lib.xinput import XInputJoystick
12 12
diff --git a/lib/EventHandler.py b/lib/EventHandler.py
index 5feb7c7..535a58a 100644
--- a/lib/EventHandler.py
+++ b/lib/EventHandler.py
@@ -8,18 +8,6 @@ import math
8import pygame 8import pygame
9import random 9import random
10 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
23class KeyHandler(object): 11class KeyHandler(object):
24 def __init__(self, name=None): 12 def __init__(self, name=None):
25 self.keydown_assignments = {} 13 self.keydown_assignments = {}
diff --git a/lib/Keyboards.py b/lib/Keyboards.py
index 2e453fb..b0d656d 100644
--- a/lib/Keyboards.py
+++ b/lib/Keyboards.py
@@ -1,8 +1,22 @@
1 1
2import pygame 2import pygame
3from lib.EventHandler import JoyHandler, Sectors 3from lib.Utils import load_image
4from lib.EventHandler import JoyHandler
4from lib.HeadsUpDisplay import Locations as HudLocs 5from lib.HeadsUpDisplay import Locations as HudLocs
5 6
7Sectors = {
8 'n': ['a', 'b', 'c', 'd'],
9 'ne': ['e', 'f', 'g', 'h'],
10 'e': ['i', 'j', 'k', 'l'],
11 'se': ['m', 'n', 'o', 'p'],
12 's': ['q', 'r', 's', 't'],
13 'sw': ['u', 'v', 'w', 'x'],
14 'w': ['y', 'z', '.', ','],
15 'nw': ['"', '-', '!', '?'],
16 'x': ['*', '*', '*', '*'],
17}
18
19
6class Keyboard(object): 20class Keyboard(object):
7 def __init__(self, id, name): 21 def __init__(self, id, name):
8 self.handler = None 22 self.handler = None
@@ -13,11 +27,6 @@ class Keyboard(object):
13 def draw(self, screen): 27 def draw(self, screen):
14 raise NotImplementedError 28 raise NotImplementedError
15 29
16class TraditionalKeyboard(Keyboard):
17 def __init__(self, id=0, name='[noname]'):
18 ## Keydow/up callbacks?
19 pass
20
21class ControllerKeyboard(Keyboard): 30class ControllerKeyboard(Keyboard):
22 def __init__(self, js, Name='[j-noname]', hud=None): 31 def __init__(self, js, Name='[j-noname]', hud=None):
23 self.js = js 32 self.js = js
@@ -27,26 +36,91 @@ class ControllerKeyboard(Keyboard):
27 self.line_color = (200, 10, 10) 36 self.line_color = (200, 10, 10)
28 self.line_color_2 = (10, 10, 200) 37 self.line_color_2 = (10, 10, 200)
29 38
39 self.img_sector_base = pygame.transform.scale(load_image('pics/bg_sector.png'), (150, 150))
40 self.img_sector_active = pygame.transform.scale(load_image('pics/bg_sector_active.png'), (150, 150))
41
42 font_path = 'data/profont.ttf'
43 font_size = 15
44 self.font_color = (0, 0, 0)
45 self.font = pygame.font.Font(font_path, font_size)
46
30 def update(self): 47 def update(self):
31 self.handler.update() 48 self.handler.update()
32 49
33 def draw(self, screen): 50 def populate_sector(self, sector, active=False):
34 surf = pygame.Surface((300, 300), flags=pygame.SRCALPHA) 51 # Create a new surface of just this sector
35 val_x, val_y = self.handler.get_stick_value() 52 surf = pygame.Surface(self.img_sector_base.get_size(), flags=pygame.SRCALPHA)
36 val_x = int(val_x * 100) + 150 53
37 val_y = int(val_y * 100) + 150 54 # Draw the correct background
55 if active:
56 surf.blit(self.img_sector_active, (0, 0))
57 else:
58 surf.blit(self.img_sector_base, (0, 0))
59
60 # Draw the font
61 for s in range(4):
62 # single character
63 font_surf = self.font.render(Sectors[sector][s], True, self.font_color).convert_alpha()
64
65 val = self.get_font_coord(surf, font_surf, s)
66
67 surf.blit(font_surf, val)
68
69 return surf
38 70
39 rval_x, rval_y = self.handler.get_stick_value('right') 71 def get_font_coord(self, dest_surf, font_surf, font_sector):
40 rval_x = int(rval_x * 100) + 150 72 (w, h) = dest_surf.get_size()
41 rval_y = int(rval_y * 100) + 150 73 (fw, fh) = font_surf.get_size()
74 font_sects = [
75 # North
76 (w / 2 - fw / 2, fh),
42 77
43 tval = self.handler.get_trigger_axis_value() 78 # East
44 tval = int(tval * 100) + 150 79 (w - fw * 3.25, h / 2 - fh / 2),
45 80
46 tval_x, tval_y = self.handler.get_stick_value('trigger') 81 # South
47 tval_x = int(tval_x * 100) + 150 82 (w / 2 - fw / 2, h - fh * 2),
48 tval_y = int(tval_y * 100) + 150
49 83
84 # West
85 (fw * 2.5, h / 2 - fh / 2),
86 ]
87
88 return font_sects[font_sector]
89
90
91 def get_base_gui(self, sector='x'):
92 """ Draw the base of the sector gui, taking into account the active sector. """
93 surf = pygame.Surface((600, 600), flags=pygame.SRCALPHA)
94
95 # Image width, height
96 (iw, ih) = self.img_sector_base.get_size()
97 # Surface width, height
98 (w, h) = surf.get_size()
99
100 gui_sectors = {
101 'x': (w / 2 - iw / 2, h / 2 - ih / 2),
102 'n': (w / 2 - iw / 2, 0),
103 's': (w / 2 - iw / 2, h - ih),
104 'w': (0, h / 2 - ih / 2),
105 'e': (w - iw, h / 2 - ih / 2),
106 'nw': (w / 4 - iw / 2, h / 4 - ih / 2),
107 'sw': (w / 4 - iw / 2, (h / 4) * 3 - ih / 2),
108 'ne': ((w / 4) * 3 - iw / 2, h / 4 - ih / 2),
109 'se': ((w / 4) * 3 - iw / 2, (h / 4) * 3 - ih / 2),
110 }
111
112 # Draw each sector
113 for key, val in gui_sectors.items():
114 active = False
115 if sector == key:
116 active = True
117
118 sector_img = self.populate_sector(key, active)
119 surf.blit(sector_img, val)
120
121 return surf
122
123 def draw(self, screen):
50 #js = pygame.joystick.Joystick(self.controller_id) 124 #js = pygame.joystick.Joystick(self.controller_id)
51 # FIXME: hardcoded 125 # FIXME: hardcoded
52 js = pygame.joystick.Joystick(0) 126 js = pygame.joystick.Joystick(0)
@@ -54,22 +128,19 @@ class ControllerKeyboard(Keyboard):
54 for n in range(0, js.get_numaxes()): 128 for n in range(0, js.get_numaxes()):
55 a_lst.append('{}: {:.3f}'.format(n, js.get_axis(n))) 129 a_lst.append('{}: {:.3f}'.format(n, js.get_axis(n)))
56 130
57 self.hud.set_text(HudLocs.BOTTOMLEFT, '\n'.join(a_lst)) 131 #self.hud.set_text(HudLocs.BOTTOMLEFT, '\n'.join(a_lst))
58 132
59 pygame.draw.line(surf, self.line_color, [150, 150], [val_x, val_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])
62 pygame.draw.line(surf, (0, 0, 0), [150, 150], [tval_x, tval_y])
63 sector = self.handler.get_sector() 133 sector = self.handler.get_sector()
134 gui_surf = self.get_base_gui(sector)
135 (val_x, val_y) = self.handler.get_stick_value()
64 136
65 if self.hud is not None: 137 #if self.hud is not None:
66 self.hud.set_text(HudLocs.BOTTOMCENTER, "(x, y): {}\nintensity: {}".format((val_x, val_y), self.handler.get_intensity())) 138 # self.hud.set_text(HudLocs.BOTTOMCENTER, "(x, y): ({:.2f}, {:.2f})\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)) 139 # self.hud.set_text(HudLocs.BOTTOMRIGHT, "sector: {1:2s}\n{0[0]} \n{0[3]} {0[1]}\n{0[2]} ".format(Sectors[sector], sector))
68 self.hud.draw(screen)
69 140
70 screen_x, screen_y = screen.get_size() 141 screen_x, screen_y = screen.get_size()
71 surf_x, surf_y = surf.get_size() 142 surf_x, surf_y = gui_surf.get_size()
72 screen.blit(surf, ((screen_x/2) - (surf_x/2), (screen_y/2) - (surf_y/2))) 143 screen.blit(gui_surf, ((screen_x/2) - (surf_x/2), (screen_y/2) - (surf_y/2)))
73 144
74 def do_joydown(self, key): 145 def do_joydown(self, key):
75 self.handler.do_joydown(key) 146 self.handler.do_joydown(key)
diff --git a/pics/bg_sector.png b/pics/bg_sector.png
new file mode 100644
index 0000000..0f31290
--- /dev/null
+++ b/pics/bg_sector.png
Binary files differ
diff --git a/pics/bg_sector_active.png b/pics/bg_sector_active.png
new file mode 100644
index 0000000..8f7ad6b
--- /dev/null
+++ b/pics/bg_sector_active.png
Binary files differ