summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Scheibenpflug <zorchenhimer@gmail.com>2016-11-13 15:23:46 (GMT)
committerNick Scheibenpflug <zorchenhimer@gmail.com>2016-11-13 15:23:46 (GMT)
commitab693b6e64b7f2a050354718284193cafe090707 (patch)
treef248367fd021296ee3c2a36d62946c37e95bc0cb
parent23e31e81eafb17c8e143c40a61ea3ddb932586b8 (diff)
downloadcontroller-keyboard-ab693b6e64b7f2a050354718284193cafe090707.zip
controller-keyboard-ab693b6e64b7f2a050354718284193cafe090707.tar.gz
Pre-render sectors; Add shifting to GUI
All sectors are pre-rendered when ControllerKeyboard is initialized. This includes all four states: base, active, shifted base, and shifted active. Also, the list of paired shift characters has been moved to Keyboards.py.
-rw-r--r--lib/Environment.py14
-rw-r--r--lib/Keyboards.py62
2 files changed, 56 insertions, 20 deletions
diff --git a/lib/Environment.py b/lib/Environment.py
index fb37fba..8ef2320 100644
--- a/lib/Environment.py
+++ b/lib/Environment.py
@@ -6,7 +6,7 @@ import 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 8from lib.EventHandler import JoyHandler, KeyHandler
9from lib.Keyboards import ControllerKeyboard, Sectors 9from lib.Keyboards import ControllerKeyboard, Sectors, ShiftPairs
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
@@ -92,18 +92,11 @@ class EnvironmentController(object):
92 92
93 93
94 def type_key(self, key): 94 def type_key(self, key):
95 shift_pairs = {
96 '1': '!', '2': '@', '3': '#', '4': '$', '5': '%',
97 '6': '^', '7': '&', '8': '*', '9': '(', '0': ')',
98 '[': '{', ']': '}', '/': '?', '=': '+', '\\': '|',
99 '-': '_', ';': ':', '\'': '"', ',': '<', '.': '>',
100 '`': '~'
101 }
102 key = key.lower() 95 key = key.lower()
103 if len(key) == 1: 96 if len(key) == 1:
104 if self.shifted: 97 if self.shifted:
105 if key in shift_pairs.keys(): 98 if key in ShiftPairs.keys():
106 key = shift_pairs[key] 99 key = ShiftPairs[key]
107 else: 100 else:
108 key = key.upper() 101 key = key.upper()
109 self.TxtObject.add_text(key) 102 self.TxtObject.add_text(key)
@@ -117,6 +110,7 @@ class EnvironmentController(object):
117 110
118 def type_shift(self, state=False): 111 def type_shift(self, state=False):
119 self.shifted = state 112 self.shifted = state
113 self.controller.set_shift(state)
120 114
121 def set_keyboard_keys(self): 115 def set_keyboard_keys(self):
122 ## Oh god, why. 116 ## Oh god, why.
diff --git a/lib/Keyboards.py b/lib/Keyboards.py
index b0d656d..5f5b254 100644
--- a/lib/Keyboards.py
+++ b/lib/Keyboards.py
@@ -12,10 +12,17 @@ Sectors = {
12 's': ['q', 'r', 's', 't'], 12 's': ['q', 'r', 's', 't'],
13 'sw': ['u', 'v', 'w', 'x'], 13 'sw': ['u', 'v', 'w', 'x'],
14 'w': ['y', 'z', '.', ','], 14 'w': ['y', 'z', '.', ','],
15 'nw': ['"', '-', '!', '?'], 15 'nw': ['\'', '-', '!', '?'],
16 'x': ['*', '*', '*', '*'], 16 'x': ['*', '*', '*', '*'],
17} 17}
18 18
19ShiftPairs = {
20 '1': '!', '2': '@', '3': '#', '4': '$', '5': '%',
21 '6': '^', '7': '&', '8': '*', '9': '(', '0': ')',
22 '[': '{', ']': '}', '/': '?', '=': '+', '\\': '|',
23 '-': '_', ';': ':', '\'': '"', ',': '<', '.': '>',
24 '`': '~', '\'': '"',
25}
19 26
20class Keyboard(object): 27class Keyboard(object):
21 def __init__(self, id, name): 28 def __init__(self, id, name):
@@ -44,10 +51,42 @@ class ControllerKeyboard(Keyboard):
44 self.font_color = (0, 0, 0) 51 self.font_color = (0, 0, 0)
45 self.font = pygame.font.Font(font_path, font_size) 52 self.font = pygame.font.Font(font_path, font_size)
46 53
54 self.shifted = False
55
56 self.rendered_sectors = {}
57 self.rendered_active_sectors = {}
58
59 self.rendered_shifted_sectors = {}
60 self.rendered_shifted_active_sectors = {}
61
62 self.current_sectors = self.rendered_sectors
63 self.current_active_sectors = self.rendered_active_sectors
64
65 for key, val in Sectors.items():
66 self.rendered_sectors[key] = self.populate_sector(key)
67
68 for key, val in Sectors.items():
69 self.rendered_active_sectors[key] = self.populate_sector(key, True)
70
71 for key, val in Sectors.items():
72 self.rendered_shifted_sectors[key] = self.populate_sector(key, active=False, shifted=True)
73
74 for key, val in Sectors.items():
75 self.rendered_shifted_active_sectors[key] = self.populate_sector(key, active=True, shifted=True)
76
47 def update(self): 77 def update(self):
48 self.handler.update() 78 self.handler.update()
49 79
50 def populate_sector(self, sector, active=False): 80 def set_shift(self, state):
81 self.shifted = state
82 if self.shifted:
83 self.current_sectors = self.rendered_shifted_sectors
84 self.current_active_sectors = self.rendered_shifted_active_sectors
85 else:
86 self.current_sectors = self.rendered_sectors
87 self.current_active_sectors = self.rendered_active_sectors
88
89 def populate_sector(self, sector, active=False, shifted=False):
51 # Create a new surface of just this sector 90 # Create a new surface of just this sector
52 surf = pygame.Surface(self.img_sector_base.get_size(), flags=pygame.SRCALPHA) 91 surf = pygame.Surface(self.img_sector_base.get_size(), flags=pygame.SRCALPHA)
53 92
@@ -60,10 +99,15 @@ class ControllerKeyboard(Keyboard):
60 # Draw the font 99 # Draw the font
61 for s in range(4): 100 for s in range(4):
62 # single character 101 # single character
63 font_surf = self.font.render(Sectors[sector][s], True, self.font_color).convert_alpha() 102 char = Sectors[sector][s]
64 103 if shifted:
104 if char in ShiftPairs:
105 char = ShiftPairs[char]
106 else:
107 char = char.upper()
108
109 font_surf = self.font.render(char, True, self.font_color).convert_alpha()
65 val = self.get_font_coord(surf, font_surf, s) 110 val = self.get_font_coord(surf, font_surf, s)
66
67 surf.blit(font_surf, val) 111 surf.blit(font_surf, val)
68 112
69 return surf 113 return surf
@@ -111,12 +155,10 @@ class ControllerKeyboard(Keyboard):
111 155
112 # Draw each sector 156 # Draw each sector
113 for key, val in gui_sectors.items(): 157 for key, val in gui_sectors.items():
114 active = False
115 if sector == key: 158 if sector == key:
116 active = True 159 surf.blit(self.current_active_sectors[key], val)
117 160 else:
118 sector_img = self.populate_sector(key, active) 161 surf.blit(self.current_sectors[key], val)
119 surf.blit(sector_img, val)
120 162
121 return surf 163 return surf
122 164