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