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