diff options
author | Ismaël Bouya <ismael.bouya@normalesup.org> | 2016-06-13 10:48:04 +0200 |
---|---|---|
committer | Ismaël Bouya <ismael.bouya@normalesup.org> | 2016-06-13 10:48:04 +0200 |
commit | 189bf90cfc9401b4bc0f6f36c152a439626cef3a (patch) | |
tree | 39e5a90c0bde95cabc6c6ab228d8b8ad2a0737aa /helpers.py | |
download | MusicSampler-189bf90cfc9401b4bc0f6f36c152a439626cef3a.tar.gz MusicSampler-189bf90cfc9401b4bc0f6f36c152a439626cef3a.tar.zst MusicSampler-189bf90cfc9401b4bc0f6f36c152a439626cef3a.zip |
Initial commit
Diffstat (limited to 'helpers.py')
-rw-r--r-- | helpers.py | 232 |
1 files changed, 232 insertions, 0 deletions
diff --git a/helpers.py b/helpers.py new file mode 100644 index 0000000..6a2ed6f --- /dev/null +++ b/helpers.py | |||
@@ -0,0 +1,232 @@ | |||
1 | from pygame import * | ||
2 | from math import pi | ||
3 | |||
4 | class Key: | ||
5 | row_positions = { | ||
6 | 'first': 5, | ||
7 | 'second': 55, | ||
8 | 'third': 105, | ||
9 | 'fourth': 155, | ||
10 | 'fifth': 205, | ||
11 | 'sixth': 255, | ||
12 | } | ||
13 | |||
14 | default_outer_color = (120, 120, 120) | ||
15 | lighter_outer_color = (200, 200, 200) | ||
16 | default_inner_color = (255, 255, 255) | ||
17 | |||
18 | def __init__(self, key_name, key_sym, top, left, width = 48, height = 48, disabled = False): | ||
19 | self.key_name = key_name | ||
20 | self.key_sym = key_sym | ||
21 | |||
22 | if isinstance(top, str): | ||
23 | self.top = self.row_positions[top] | ||
24 | else: | ||
25 | self.top = top | ||
26 | |||
27 | self.left = left | ||
28 | self.width = width | ||
29 | self.height = height | ||
30 | |||
31 | self.bottom = self.top + self.height | ||
32 | self.right = self.left + self.width | ||
33 | |||
34 | self.rect = (self.left, self.top, self.right, self.bottom) | ||
35 | self.position = (self.left, self.top) | ||
36 | |||
37 | if disabled: | ||
38 | self.outer_color = self.lighter_outer_color | ||
39 | self.linewidth = 1 | ||
40 | else: | ||
41 | self.outer_color = self.default_outer_color | ||
42 | self.linewidth = 3 | ||
43 | |||
44 | self.inner_color = self.default_inner_color | ||
45 | |||
46 | def square(self): | ||
47 | return RoundedRect((0, 0, self.width, self.height), | ||
48 | self.outer_color, self.inner_color, self.linewidth) | ||
49 | |||
50 | def collidepoint(self, position): | ||
51 | return self.surface.get_rect().collidepoint( | ||
52 | position[0] - self.position[0], | ||
53 | position[1] - self.position[1] | ||
54 | ) | ||
55 | |||
56 | def draw(self, background_surface): | ||
57 | self.surface = self.square().surface() | ||
58 | |||
59 | police = font.Font(None, 20) | ||
60 | text = police.render(self.key_sym, True, (0,0,0)) | ||
61 | self.surface.blit(text, (5,5)) | ||
62 | background_surface.blit(self.surface, self.position) | ||
63 | |||
64 | def do_actions(self): | ||
65 | print("coucou" + self.key_sym) | ||
66 | pass | ||
67 | |||
68 | keys = { | ||
69 | K_ESCAPE: Key(K_ESCAPE, 'ESC', 'first', 0), | ||
70 | |||
71 | K_F1: Key(K_F1, 'F1', 'first', 100), | ||
72 | K_F2: Key(K_F2, 'F2', 'first', 150), | ||
73 | K_F3: Key(K_F3, 'F3', 'first', 200), | ||
74 | K_F4: Key(K_F4, 'F4', 'first', 250), | ||
75 | |||
76 | K_F5: Key(K_F5, 'F5', 'first', 325), | ||
77 | K_F6: Key(K_F6, 'F6', 'first', 375), | ||
78 | K_F7: Key(K_F7, 'F7', 'first', 425), | ||
79 | K_F8: Key(K_F8, 'F8', 'first', 475), | ||
80 | |||
81 | K_F9: Key(K_F9, 'F9', 'first', 550), | ||
82 | K_F10: Key(K_F10, 'F10', 'first', 600), | ||
83 | K_F11: Key(K_F11, 'F11', 'first', 650), | ||
84 | K_F12: Key(K_F12, 'F12', 'first', 700), | ||
85 | |||
86 | |||
87 | 178: Key(178, '²', 'second', 0), | ||
88 | K_AMPERSAND: Key(K_AMPERSAND, '&', 'second', 50), | ||
89 | 233: Key(233, 'é', 'second', 100), | ||
90 | K_QUOTEDBL: Key(K_QUOTEDBL, '"', 'second', 150), | ||
91 | K_QUOTE: Key(K_QUOTE, "'", 'second', 200), | ||
92 | K_LEFTPAREN: Key(K_LEFTPAREN, '(', 'second', 250), | ||
93 | K_MINUS: Key(K_MINUS, '-', 'second', 300), | ||
94 | 232: Key(232, 'è', 'second', 350), | ||
95 | K_UNDERSCORE: Key(K_UNDERSCORE, '_', 'second', 400), | ||
96 | 231: Key(231, 'ç', 'second', 450), | ||
97 | 224: Key(224, 'à', 'second', 500), | ||
98 | K_RIGHTPAREN: Key(K_RIGHTPAREN, ')', 'second', 550), | ||
99 | K_EQUALS: Key(K_EQUALS, '=', 'second', 600), | ||
100 | |||
101 | K_BACKSPACE: Key(K_BACKSPACE, '<-', 'second', 650, width = 98), | ||
102 | |||
103 | |||
104 | K_TAB: Key(K_TAB, 'tab', 'third', 0, width = 73), | ||
105 | K_a: Key(K_a, 'a', 'third', 75), | ||
106 | K_z: Key(K_z, 'z', 'third', 125), | ||
107 | K_e: Key(K_e, 'e', 'third', 175), | ||
108 | K_r: Key(K_r, 'r', 'third', 225), | ||
109 | K_t: Key(K_t, 't', 'third', 275), | ||
110 | K_y: Key(K_y, 'y', 'third', 325), | ||
111 | K_u: Key(K_u, 'u', 'third', 375), | ||
112 | K_i: Key(K_i, 'i', 'third', 425), | ||
113 | K_o: Key(K_o, 'o', 'third', 475), | ||
114 | K_p: Key(K_p, 'p', 'third', 525), | ||
115 | K_CARET: Key(K_CARET, '^', 'third', 575), | ||
116 | K_DOLLAR: Key(K_DOLLAR, '$', 'third', 625), | ||
117 | |||
118 | K_RETURN: Key(K_RETURN, 'Enter', 'third', 692, width = 56, height = 98), | ||
119 | |||
120 | K_CAPSLOCK: Key(K_CAPSLOCK, 'CAPS', 'fourth', 0, width = 88, disabled = True), | ||
121 | |||
122 | K_q: Key(K_q, 'q', 'fourth', 90), | ||
123 | K_s: Key(K_s, 's', 'fourth', 140), | ||
124 | K_d: Key(K_d, 'd', 'fourth', 190), | ||
125 | K_f: Key(K_f, 'f', 'fourth', 240), | ||
126 | K_g: Key(K_g, 'g', 'fourth', 290), | ||
127 | K_h: Key(K_h, 'h', 'fourth', 340), | ||
128 | K_j: Key(K_j, 'j', 'fourth', 390), | ||
129 | K_k: Key(K_k, 'k', 'fourth', 440), | ||
130 | K_l: Key(K_l, 'l', 'fourth', 490), | ||
131 | K_m: Key(K_m, 'm', 'fourth', 540), | ||
132 | 249: Key(249, 'ù', 'fourth', 590), | ||
133 | K_ASTERISK: Key(K_ASTERISK, '*', 'fourth', 640), | ||
134 | |||
135 | |||
136 | K_LSHIFT: Key(K_LSHIFT, 'LShift', 'fifth', 0, width = 63, disabled = True), | ||
137 | |||
138 | K_LESS: Key(K_LESS, '<', 'fifth', 65), | ||
139 | K_w: Key(K_w, 'w', 'fifth', 115), | ||
140 | K_x: Key(K_x, 'x', 'fifth', 165), | ||
141 | K_c: Key(K_c, 'c', 'fifth', 215), | ||
142 | K_v: Key(K_v, 'v', 'fifth', 265), | ||
143 | K_b: Key(K_b, 'b', 'fifth', 315), | ||
144 | K_n: Key(K_n, 'n', 'fifth', 365), | ||
145 | K_COMMA: Key(K_COMMA, ',', 'fifth', 415), | ||
146 | K_SEMICOLON: Key(K_SEMICOLON, ';', 'fifth', 465), | ||
147 | K_COLON: Key(K_COLON, ':', 'fifth', 515), | ||
148 | K_EXCLAIM: Key(K_EXCLAIM, '!', 'fifth', 565), | ||
149 | |||
150 | K_RSHIFT: Key(K_RSHIFT, 'RShift', 'fifth', 615, width = 133, disabled = True), | ||
151 | |||
152 | K_LCTRL: Key(K_LCTRL, 'LCtrl', 'sixth', 0, width = 63, disabled = True), | ||
153 | K_LSUPER: Key(K_LSUPER, 'LSuper', 'sixth', 115, disabled = True), | ||
154 | K_LALT: Key(K_LALT, 'LAlt', 'sixth', 165, disabled = True), | ||
155 | K_SPACE: Key(K_SPACE, 'Espace', 'sixth', 215, width = 248), | ||
156 | K_MODE: Key(K_MODE, 'AltGr', 'sixth', 465, disabled = True), | ||
157 | 314: Key(314, 'Compose', 'sixth', 515, disabled = True), | ||
158 | K_RCTRL: Key(K_RCTRL, 'RCtrl', 'sixth', 565, width = 63, disabled = True), | ||
159 | |||
160 | |||
161 | K_INSERT: Key(K_INSERT, 'ins', 'second', 755), | ||
162 | K_HOME: Key(K_HOME, 'home', 'second', 805), | ||
163 | K_PAGEUP: Key(K_PAGEUP, 'pg_u', 'second', 855), | ||
164 | K_DELETE: Key(K_DELETE, 'del', 'third', 755), | ||
165 | K_END: Key(K_END, 'end', 'third', 805), | ||
166 | K_PAGEDOWN: Key(K_PAGEDOWN, 'pg_d', 'third', 855), | ||
167 | |||
168 | |||
169 | K_UP: Key(K_UP, 'up', 'fifth', 805), | ||
170 | K_DOWN: Key(K_DOWN, 'down', 'sixth', 805), | ||
171 | K_LEFT: Key(K_LEFT, 'left', 'sixth', 755), | ||
172 | K_RIGHT: Key(K_RIGHT, 'right', 'sixth', 855), | ||
173 | } | ||
174 | |||
175 | class RoundedRect: | ||
176 | def __init__(self, rect, outer_color, inner_color, linewidth = 2, radius = 0.4): | ||
177 | self.rect = Rect(rect) | ||
178 | self.outer_color = Color(*outer_color) | ||
179 | self.inner_color = Color(*inner_color) | ||
180 | self.linewidth = linewidth | ||
181 | self.radius = radius | ||
182 | |||
183 | def surface(self): | ||
184 | rectangle = filledRoundedRect(self.rect, self.outer_color, self.radius) | ||
185 | |||
186 | inner_rect = Rect(( | ||
187 | self.rect.left + 2 * self.linewidth, | ||
188 | self.rect.top + 2 * self.linewidth, | ||
189 | self.rect.right - 2 * self.linewidth, | ||
190 | self.rect.bottom - 2 * self.linewidth | ||
191 | )) | ||
192 | |||
193 | inner_rectangle = self.filledRoundedRect(inner_rect, self.inner_color, self.radius) | ||
194 | |||
195 | rectangle.blit(inner_rectangle, (self.linewidth, self.linewidth)) | ||
196 | |||
197 | return rectangle | ||
198 | |||
199 | def filledRoundedRect(self, rect, color, radius=0.4): | ||
200 | """ | ||
201 | filledRoundedRect(rect,color,radius=0.4) | ||
202 | |||
203 | rect : rectangle | ||
204 | color : rgb or rgba | ||
205 | radius : 0 <= radius <= 1 | ||
206 | """ | ||
207 | |||
208 | alpha = color.a | ||
209 | color.a = 0 | ||
210 | pos = rect.topleft | ||
211 | rect.topleft = 0,0 | ||
212 | rectangle = Surface(rect.size,SRCALPHA) | ||
213 | |||
214 | circle = Surface([min(rect.size)*3]*2,SRCALPHA) | ||
215 | draw.ellipse(circle,(0,0,0),circle.get_rect(),0) | ||
216 | circle = transform.smoothscale(circle,[int(min(rect.size)*radius)]*2) | ||
217 | |||
218 | radius = rectangle.blit(circle,(0,0)) | ||
219 | radius.bottomright = rect.bottomright | ||
220 | rectangle.blit(circle,radius) | ||
221 | radius.topright = rect.topright | ||
222 | rectangle.blit(circle,radius) | ||
223 | radius.bottomleft = rect.bottomleft | ||
224 | rectangle.blit(circle,radius) | ||
225 | |||
226 | rectangle.fill((0,0,0),rect.inflate(-radius.w,0)) | ||
227 | rectangle.fill((0,0,0),rect.inflate(0,-radius.h)) | ||
228 | |||
229 | rectangle.fill(color,special_flags=BLEND_RGBA_MAX) | ||
230 | rectangle.fill((255,255,255,alpha),special_flags=BLEND_RGBA_MIN) | ||
231 | |||
232 | return rectangle | ||