]>
git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blob - helpers.py
1 # -*- coding: utf-8 -*-
15 default_outer_color
= (120, 120, 120)
16 lighter_outer_color
= (200, 200, 200)
17 default_inner_color
= (255, 255, 255)
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
23 if isinstance(top
, str):
24 self
.top
= self
.row_positions
[top
]
32 self
.bottom
= self
.top
+ self
.height
33 self
.right
= self
.left
+ self
.width
35 self
.rect
= (self
.left
, self
.top
, self
.right
, self
.bottom
)
36 self
.position
= (self
.left
, self
.top
)
39 self
.outer_color
= self
.lighter_outer_color
42 self
.outer_color
= self
.default_outer_color
45 self
.inner_color
= self
.default_inner_color
48 return RoundedRect((0, 0, self
.width
, self
.height
),
49 self
.outer_color
, self
.inner_color
, self
.linewidth
)
51 def collidepoint(self
, position
):
52 return self
.surface
.get_rect().collidepoint(
53 position
[0] - self
.position
[0],
54 position
[1] - self
.position
[1]
57 def draw(self
, background_surface
):
58 self
.surface
= self
.square().surface()
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
)
66 print("coucou " + self
.key_sym
)
70 K_ESCAPE
: Key(K_ESCAPE
, 'ESC', 'first', 0),
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),
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),
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),
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),
102 K_BACKSPACE
: Key(K_BACKSPACE
, '<-', 'second', 650, width
= 98),
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),
119 K_RETURN
: Key(K_RETURN
, 'Enter', 'third', 692, width
= 56, height
= 98),
121 K_CAPSLOCK
: Key(K_CAPSLOCK
, 'CAPS', 'fourth', 0, width
= 88, disabled
= True),
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),
137 K_LSHIFT
: Key(K_LSHIFT
, 'LShift', 'fifth', 0, width
= 63, disabled
= True),
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),
151 K_RSHIFT
: Key(K_RSHIFT
, 'RShift', 'fifth', 615, width
= 133, disabled
= True),
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),
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),
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),
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
185 rectangle
= self
.filledRoundedRect(self
.rect
, self
.outer_color
, self
.radius
)
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
194 inner_rectangle
= self
.filledRoundedRect(inner_rect
, self
.inner_color
, self
.radius
)
196 rectangle
.blit(inner_rectangle
, (self
.linewidth
, self
.linewidth
))
200 def filledRoundedRect(self
, rect
, color
, radius
=0.4):
202 filledRoundedRect(rect,color,radius=0.4)
206 radius : 0 <= radius <= 1
213 rectangle
= Surface(rect
.size
,SRCALPHA
)
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)
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
)
227 rectangle
.fill((0,0,0),rect
.inflate(-radius
.w
,0))
228 rectangle
.fill((0,0,0),rect
.inflate(0,-radius
.h
))
230 rectangle
.fill(color
,special_flags
=BLEND_RGBA_MAX
)
231 rectangle
.fill((255,255,255,alpha
),special_flags
=BLEND_RGBA_MIN
)