diff options
author | Ismaël Bouya <ismael.bouya@normalesup.org> | 2016-06-18 22:13:19 +0200 |
---|---|---|
committer | Ismaël Bouya <ismael.bouya@normalesup.org> | 2016-06-18 22:13:19 +0200 |
commit | be27763f8be0f647cbe17ecee8c782901ce2cede (patch) | |
tree | 30e02392fa946f92755c83454f85c4572f2d4e77 /helpers/mapping.py | |
parent | 98fee7ff51eac3660f05aefcb18bbd9c2d32c35c (diff) | |
download | MusicSampler-be27763f8be0f647cbe17ecee8c782901ce2cede.tar.gz MusicSampler-be27763f8be0f647cbe17ecee8c782901ce2cede.tar.zst MusicSampler-be27763f8be0f647cbe17ecee8c782901ce2cede.zip |
Move classes to separate file
Diffstat (limited to 'helpers/mapping.py')
-rw-r--r-- | helpers/mapping.py | 171 |
1 files changed, 171 insertions, 0 deletions
diff --git a/helpers/mapping.py b/helpers/mapping.py new file mode 100644 index 0000000..ee5623a --- /dev/null +++ b/helpers/mapping.py | |||
@@ -0,0 +1,171 @@ | |||
1 | import threading | ||
2 | import pygame | ||
3 | from .key import * | ||
4 | |||
5 | class Mapping: | ||
6 | WIDTH = 903 | ||
7 | HEIGHT = 298 | ||
8 | SIZE = WIDTH, HEIGHT | ||
9 | |||
10 | KEYS = [ | ||
11 | (pygame.K_ESCAPE, 'ESC', 'first', 0, {}), | ||
12 | |||
13 | (pygame.K_F1, 'F1', 'first', 100, {}), | ||
14 | (pygame.K_F2, 'F2', 'first', 150, {}), | ||
15 | (pygame.K_F3, 'F3', 'first', 200, {}), | ||
16 | (pygame.K_F4, 'F4', 'first', 250, {}), | ||
17 | |||
18 | (pygame.K_F5, 'F5', 'first', 325, {}), | ||
19 | (pygame.K_F6, 'F6', 'first', 375, {}), | ||
20 | (pygame.K_F7, 'F7', 'first', 425, {}), | ||
21 | (pygame.K_F8, 'F8', 'first', 475, {}), | ||
22 | |||
23 | (pygame.K_F9, 'F9', 'first', 550, {}), | ||
24 | (pygame.K_F10, 'F10', 'first', 600, {}), | ||
25 | (pygame.K_F11, 'F11', 'first', 650, {}), | ||
26 | (pygame.K_F12, 'F12', 'first', 700, {}), | ||
27 | |||
28 | |||
29 | (178, '²', 'second', 0, {}), | ||
30 | (pygame.K_AMPERSAND, '&', 'second', 50, {}), | ||
31 | (233, 'é', 'second', 100, {}), | ||
32 | (pygame.K_QUOTEDBL, '"', 'second', 150, {}), | ||
33 | (pygame.K_QUOTE, "'", 'second', 200, {}), | ||
34 | (pygame.K_LEFTPAREN, '(', 'second', 250, {}), | ||
35 | (pygame.K_MINUS, '-', 'second', 300, {}), | ||
36 | (232, 'è', 'second', 350, {}), | ||
37 | (pygame.K_UNDERSCORE, '_', 'second', 400, {}), | ||
38 | (231, 'ç', 'second', 450, {}), | ||
39 | (224, 'à', 'second', 500, {}), | ||
40 | (pygame.K_RIGHTPAREN, ')', 'second', 550, {}), | ||
41 | (pygame.K_EQUALS, '=', 'second', 600, {}), | ||
42 | |||
43 | (pygame.K_BACKSPACE, '<-', 'second', 650, { 'width': 98 }), | ||
44 | |||
45 | |||
46 | (pygame.K_TAB, 'tab', 'third', 0, { 'width' : 73 }), | ||
47 | (pygame.K_a, 'a', 'third', 75, {}), | ||
48 | (pygame.K_z, 'z', 'third', 125, {}), | ||
49 | (pygame.K_e, 'e', 'third', 175, {}), | ||
50 | (pygame.K_r, 'r', 'third', 225, {}), | ||
51 | (pygame.K_t, 't', 'third', 275, {}), | ||
52 | (pygame.K_y, 'y', 'third', 325, {}), | ||
53 | (pygame.K_u, 'u', 'third', 375, {}), | ||
54 | (pygame.K_i, 'i', 'third', 425, {}), | ||
55 | (pygame.K_o, 'o', 'third', 475, {}), | ||
56 | (pygame.K_p, 'p', 'third', 525, {}), | ||
57 | (pygame.K_CARET, '^', 'third', 575, {}), | ||
58 | (pygame.K_DOLLAR, '$', 'third', 625, {}), | ||
59 | |||
60 | (pygame.K_RETURN, 'Enter', 'third', 692, { 'width': 56, 'height': 98 }), | ||
61 | |||
62 | (pygame.K_CAPSLOCK, 'CAPS', 'fourth', 0, { 'width': 88, 'disabled': True }), | ||
63 | |||
64 | (pygame.K_q, 'q', 'fourth', 90, {}), | ||
65 | (pygame.K_s, 's', 'fourth', 140, {}), | ||
66 | (pygame.K_d, 'd', 'fourth', 190, {}), | ||
67 | (pygame.K_f, 'f', 'fourth', 240, {}), | ||
68 | (pygame.K_g, 'g', 'fourth', 290, {}), | ||
69 | (pygame.K_h, 'h', 'fourth', 340, {}), | ||
70 | (pygame.K_j, 'j', 'fourth', 390, {}), | ||
71 | (pygame.K_k, 'k', 'fourth', 440, {}), | ||
72 | (pygame.K_l, 'l', 'fourth', 490, {}), | ||
73 | (pygame.K_m, 'm', 'fourth', 540, {}), | ||
74 | (249, 'ù', 'fourth', 590, {}), | ||
75 | (pygame.K_ASTERISK, '*', 'fourth', 640, {}), | ||
76 | |||
77 | |||
78 | (pygame.K_LSHIFT, 'LShift', 'fifth', 0, { 'width': 63, 'disabled': True }), | ||
79 | |||
80 | (pygame.K_LESS, '<', 'fifth', 65, {}), | ||
81 | (pygame.K_w, 'w', 'fifth', 115, {}), | ||
82 | (pygame.K_x, 'x', 'fifth', 165, {}), | ||
83 | (pygame.K_c, 'c', 'fifth', 215, {}), | ||
84 | (pygame.K_v, 'v', 'fifth', 265, {}), | ||
85 | (pygame.K_b, 'b', 'fifth', 315, {}), | ||
86 | (pygame.K_n, 'n', 'fifth', 365, {}), | ||
87 | (pygame.K_COMMA, ',', 'fifth', 415, {}), | ||
88 | (pygame.K_SEMICOLON, ';', 'fifth', 465, {}), | ||
89 | (pygame.K_COLON, ':', 'fifth', 515, {}), | ||
90 | (pygame.K_EXCLAIM, '!', 'fifth', 565, {}), | ||
91 | |||
92 | (pygame.K_RSHIFT, 'RShift', 'fifth', 615, { 'width': 133, 'disabled': True }), | ||
93 | |||
94 | (pygame.K_LCTRL, 'LCtrl', 'sixth', 0, { 'width': 63, 'disabled': True }), | ||
95 | (pygame.K_LSUPER, 'LSuper', 'sixth', 115, { 'disabled': True }), | ||
96 | (pygame.K_LALT, 'LAlt', 'sixth', 165, { 'disabled': True }), | ||
97 | (pygame.K_SPACE, 'Espace', 'sixth', 215, { 'width': 248 }), | ||
98 | (pygame.K_MODE, 'AltGr', 'sixth', 465, { 'disabled': True }), | ||
99 | (314, 'Compose', 'sixth', 515, { 'disabled': True }), | ||
100 | (pygame.K_RCTRL, 'RCtrl', 'sixth', 565, { 'width': 63, 'disabled': True }), | ||
101 | |||
102 | |||
103 | (pygame.K_INSERT, 'ins', 'second', 755, {}), | ||
104 | (pygame.K_HOME, 'home', 'second', 805, {}), | ||
105 | (pygame.K_PAGEUP, 'pg_u', 'second', 855, {}), | ||
106 | (pygame.K_DELETE, 'del', 'third', 755, {}), | ||
107 | (pygame.K_END, 'end', 'third', 805, {}), | ||
108 | (pygame.K_PAGEDOWN, 'pg_d', 'third', 855, {}), | ||
109 | |||
110 | |||
111 | (pygame.K_UP, 'up', 'fifth', 805, {}), | ||
112 | (pygame.K_DOWN, 'down', 'sixth', 805, {}), | ||
113 | (pygame.K_LEFT, 'left', 'sixth', 755, {}), | ||
114 | (pygame.K_RIGHT, 'right', 'sixth', 855, {}), | ||
115 | ] | ||
116 | |||
117 | def __init__(self, screen, draw_lock): | ||
118 | self.draw_lock = draw_lock | ||
119 | self.screen = screen | ||
120 | self.background = pygame.Surface(self.SIZE).convert() | ||
121 | self.background.fill((250, 250, 250)) | ||
122 | self.keys = {} | ||
123 | self.running = [] | ||
124 | for key in self.KEYS: | ||
125 | self.keys[key[0]] = Key(self, self.draw_lock, *key[0:4], **key[4]) | ||
126 | |||
127 | def draw(self): | ||
128 | for key_name in self.keys: | ||
129 | key = self.keys[key_name] | ||
130 | should_redraw_key = key.draw(self.background) | ||
131 | |||
132 | if should_redraw_key: | ||
133 | threading.Thread(target = key.poll_redraw, args = [self.background]).start() | ||
134 | self.blit() | ||
135 | |||
136 | def blit(self): | ||
137 | self.draw_lock.acquire() | ||
138 | self.screen.blit(self.background, (5, 5)) | ||
139 | pygame.display.flip() | ||
140 | self.draw_lock.release() | ||
141 | |||
142 | def find_by_key_num(self, key_num): | ||
143 | if key_num in self.keys: | ||
144 | return self.keys[key_num] | ||
145 | return None | ||
146 | |||
147 | def find_by_collidepoint(self, position): | ||
148 | for key in self.keys: | ||
149 | if self.keys[key].collidepoint(position): | ||
150 | return self.keys[key] | ||
151 | return None | ||
152 | |||
153 | def find_by_unicode(self, key_sym): | ||
154 | for key in self.keys: | ||
155 | if self.keys[key].key_sym == key_sym: | ||
156 | return self.keys[key] | ||
157 | return None | ||
158 | |||
159 | def stop_all_running(self): | ||
160 | self.running = [] | ||
161 | |||
162 | def start_running(self, key, start_time): | ||
163 | self.running.append((key, start_time)) | ||
164 | |||
165 | def keep_running(self, key, start_time): | ||
166 | return (key, start_time) in self.running | ||
167 | |||
168 | def finished_running(self, key, start_time): | ||
169 | if (key, start_time) in self.running: | ||
170 | self.running.remove((key, start_time)) | ||
171 | |||