]>
git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blob - helpers/__init__.py
1 # -*- coding: utf-8 -*-
14 def __init__(self
, action
, **kwargs
):
15 if action
in self
.action_types
:
18 raise Exception("Unknown action {}".format(action
))
20 self
.arguments
= kwargs
22 def run(self
, callback
):
23 getattr(self
, self
.action
)(callback
, **self
.arguments
)
25 def command(self
, callback
, command
= "", **kwargs
):
28 def pause(self
, callback
, music
= None, **kwargs
):
31 def play(self
, callback
,
34 restart_if_running
= False,
39 def stop(self
, callback
, music
= None, fade_out
= 0, **kwargs
):
43 def volume(self
, callback
, music
= None, value
= 100, **kwargs
):
46 def wait(self
, callback
, time
= 0, **kwargs
):
59 default_outer_color
= (120, 120, 120)
60 lighter_outer_color
= (200, 200, 200)
61 default_inner_color
= (255, 255, 255)
62 mapped_inner_color
= ( 0, 255, 0)
64 def __init__(self
, key_name
, key_sym
, top
, left
, width
= 48, height
= 48, disabled
= False):
65 self
.key_name
= key_name
66 self
.key_sym
= key_sym
68 if isinstance(top
, str):
69 self
.top
= self
.row_positions
[top
]
77 self
.bottom
= self
.top
+ self
.height
78 self
.right
= self
.left
+ self
.width
80 self
.rect
= (self
.left
, self
.top
, self
.right
, self
.bottom
)
81 self
.position
= (self
.left
, self
.top
)
84 self
.outer_color
= self
.lighter_outer_color
87 self
.outer_color
= self
.default_outer_color
90 self
.inner_color
= self
.default_inner_color
95 self
.inner_color
= self
.mapped_inner_color
97 return RoundedRect((0, 0, self
.width
, self
.height
),
98 self
.outer_color
, self
.inner_color
, self
.linewidth
)
100 def collidepoint(self
, position
):
101 return self
.surface
.get_rect().collidepoint(
102 position
[0] - self
.position
[0],
103 position
[1] - self
.position
[1]
106 def draw(self
, background_surface
):
107 self
.surface
= self
.square().surface()
109 police
= font
.Font(None, 20)
110 text
= police
.render(self
.key_sym
, True, (0,0,0))
111 self
.surface
.blit(text
, (5,5))
112 background_surface
.blit(self
.surface
, self
.position
)
114 def has_action(self
):
115 return len(self
.actions
) > 0
117 def add_action(self
, action_name
, **arguments
):
118 self
.actions
.append(Action(action_name
, **arguments
))
120 def next_action(self
):
121 print("running next action")
123 def do_actions(self
):
124 self
.current_action
= 0
125 print("running actions for {}".format(self
.key_sym
))
126 if len(self
.actions
) > 0:
127 self
.actions
[0].run(self
.next_action
)
129 def list_actions(self
, surface
):
130 print("bouh", self
.key_sym
)
131 surface
.fill((255, 0, 0))
133 def find_by_key_num(key_num
):
134 if key_num
in Mapping
.KEYS
:
135 return Mapping
.KEYS
[key_num
]
138 def find_by_collidepoint(position
):
139 for key
in Mapping
.KEYS
:
140 if Mapping
.KEYS
[key
].collidepoint(position
):
141 return Mapping
.KEYS
[key
]
144 def find_by_unicode(key_sym
):
145 for key
in Mapping
.KEYS
:
146 if Mapping
.KEYS
[key
].key_sym
== key_sym
:
147 return Mapping
.KEYS
[key
]
152 K_ESCAPE
: Key(K_ESCAPE
, 'ESC', 'first', 0),
154 K_F1
: Key(K_F1
, 'F1', 'first', 100),
155 K_F2
: Key(K_F2
, 'F2', 'first', 150),
156 K_F3
: Key(K_F3
, 'F3', 'first', 200),
157 K_F4
: Key(K_F4
, 'F4', 'first', 250),
159 K_F5
: Key(K_F5
, 'F5', 'first', 325),
160 K_F6
: Key(K_F6
, 'F6', 'first', 375),
161 K_F7
: Key(K_F7
, 'F7', 'first', 425),
162 K_F8
: Key(K_F8
, 'F8', 'first', 475),
164 K_F9
: Key(K_F9
, 'F9', 'first', 550),
165 K_F10
: Key(K_F10
, 'F10', 'first', 600),
166 K_F11
: Key(K_F11
, 'F11', 'first', 650),
167 K_F12
: Key(K_F12
, 'F12', 'first', 700),
170 178: Key(178, 'ยฒ', 'second', 0),
171 K_AMPERSAND
: Key(K_AMPERSAND
, '&', 'second', 50),
172 233: Key(233, 'รฉ', 'second', 100),
173 K_QUOTEDBL
: Key(K_QUOTEDBL
, '"', 'second', 150),
174 K_QUOTE
: Key(K_QUOTE
, "'", 'second', 200),
175 K_LEFTPAREN
: Key(K_LEFTPAREN
, '(', 'second', 250),
176 K_MINUS
: Key(K_MINUS
, '-', 'second', 300),
177 232: Key(232, 'รจ', 'second', 350),
178 K_UNDERSCORE
: Key(K_UNDERSCORE
, '_', 'second', 400),
179 231: Key(231, 'รง', 'second', 450),
180 224: Key(224, 'ร ', 'second', 500),
181 K_RIGHTPAREN
: Key(K_RIGHTPAREN
, ')', 'second', 550),
182 K_EQUALS
: Key(K_EQUALS
, '=', 'second', 600),
184 K_BACKSPACE
: Key(K_BACKSPACE
, '<-', 'second', 650, width
= 98),
187 K_TAB
: Key(K_TAB
, 'tab', 'third', 0, width
= 73),
188 K_a
: Key(K_a
, 'a', 'third', 75),
189 K_z
: Key(K_z
, 'z', 'third', 125),
190 K_e
: Key(K_e
, 'e', 'third', 175),
191 K_r
: Key(K_r
, 'r', 'third', 225),
192 K_t
: Key(K_t
, 't', 'third', 275),
193 K_y
: Key(K_y
, 'y', 'third', 325),
194 K_u
: Key(K_u
, 'u', 'third', 375),
195 K_i
: Key(K_i
, 'i', 'third', 425),
196 K_o
: Key(K_o
, 'o', 'third', 475),
197 K_p
: Key(K_p
, 'p', 'third', 525),
198 K_CARET
: Key(K_CARET
, '^', 'third', 575),
199 K_DOLLAR
: Key(K_DOLLAR
, '$', 'third', 625),
201 K_RETURN
: Key(K_RETURN
, 'Enter', 'third', 692, width
= 56, height
= 98),
203 K_CAPSLOCK
: Key(K_CAPSLOCK
, 'CAPS', 'fourth', 0, width
= 88, disabled
= True),
205 K_q
: Key(K_q
, 'q', 'fourth', 90),
206 K_s
: Key(K_s
, 's', 'fourth', 140),
207 K_d
: Key(K_d
, 'd', 'fourth', 190),
208 K_f
: Key(K_f
, 'f', 'fourth', 240),
209 K_g
: Key(K_g
, 'g', 'fourth', 290),
210 K_h
: Key(K_h
, 'h', 'fourth', 340),
211 K_j
: Key(K_j
, 'j', 'fourth', 390),
212 K_k
: Key(K_k
, 'k', 'fourth', 440),
213 K_l
: Key(K_l
, 'l', 'fourth', 490),
214 K_m
: Key(K_m
, 'm', 'fourth', 540),
215 249: Key(249, 'รน', 'fourth', 590),
216 K_ASTERISK
: Key(K_ASTERISK
, '*', 'fourth', 640),
219 K_LSHIFT
: Key(K_LSHIFT
, 'LShift', 'fifth', 0, width
= 63, disabled
= True),
221 K_LESS
: Key(K_LESS
, '<', 'fifth', 65),
222 K_w
: Key(K_w
, 'w', 'fifth', 115),
223 K_x
: Key(K_x
, 'x', 'fifth', 165),
224 K_c
: Key(K_c
, 'c', 'fifth', 215),
225 K_v
: Key(K_v
, 'v', 'fifth', 265),
226 K_b
: Key(K_b
, 'b', 'fifth', 315),
227 K_n
: Key(K_n
, 'n', 'fifth', 365),
228 K_COMMA
: Key(K_COMMA
, ',', 'fifth', 415),
229 K_SEMICOLON
: Key(K_SEMICOLON
, ';', 'fifth', 465),
230 K_COLON
: Key(K_COLON
, ':', 'fifth', 515),
231 K_EXCLAIM
: Key(K_EXCLAIM
, '!', 'fifth', 565),
233 K_RSHIFT
: Key(K_RSHIFT
, 'RShift', 'fifth', 615, width
= 133, disabled
= True),
235 K_LCTRL
: Key(K_LCTRL
, 'LCtrl', 'sixth', 0, width
= 63, disabled
= True),
236 K_LSUPER
: Key(K_LSUPER
, 'LSuper', 'sixth', 115, disabled
= True),
237 K_LALT
: Key(K_LALT
, 'LAlt', 'sixth', 165, disabled
= True),
238 K_SPACE
: Key(K_SPACE
, 'Espace', 'sixth', 215, width
= 248),
239 K_MODE
: Key(K_MODE
, 'AltGr', 'sixth', 465, disabled
= True),
240 314: Key(314, 'Compose', 'sixth', 515, disabled
= True),
241 K_RCTRL
: Key(K_RCTRL
, 'RCtrl', 'sixth', 565, width
= 63, disabled
= True),
244 K_INSERT
: Key(K_INSERT
, 'ins', 'second', 755),
245 K_HOME
: Key(K_HOME
, 'home', 'second', 805),
246 K_PAGEUP
: Key(K_PAGEUP
, 'pg_u', 'second', 855),
247 K_DELETE
: Key(K_DELETE
, 'del', 'third', 755),
248 K_END
: Key(K_END
, 'end', 'third', 805),
249 K_PAGEDOWN
: Key(K_PAGEDOWN
, 'pg_d', 'third', 855),
252 K_UP
: Key(K_UP
, 'up', 'fifth', 805),
253 K_DOWN
: Key(K_DOWN
, 'down', 'sixth', 805),
254 K_LEFT
: Key(K_LEFT
, 'left', 'sixth', 755),
255 K_RIGHT
: Key(K_RIGHT
, 'right', 'sixth', 855),
259 def __init__(self
, filename
):
260 self
.filename
= filename
263 def __init__(self
, rect
, outer_color
, inner_color
, linewidth
= 2, radius
= 0.4):
264 self
.rect
= Rect(rect
)
265 self
.outer_color
= Color(*outer_color
)
266 self
.inner_color
= Color(*inner_color
)
267 self
.linewidth
= linewidth
271 rectangle
= self
.filledRoundedRect(self
.rect
, self
.outer_color
, self
.radius
)
274 self
.rect
.left
+ 2 * self
.linewidth
,
275 self
.rect
.top
+ 2 * self
.linewidth
,
276 self
.rect
.right
- 2 * self
.linewidth
,
277 self
.rect
.bottom
- 2 * self
.linewidth
280 inner_rectangle
= self
.filledRoundedRect(inner_rect
, self
.inner_color
, self
.radius
)
282 rectangle
.blit(inner_rectangle
, (self
.linewidth
, self
.linewidth
))
286 def filledRoundedRect(self
, rect
, color
, radius
=0.4):
288 filledRoundedRect(rect,color,radius=0.4)
292 radius : 0 <= radius <= 1
299 rectangle
= Surface(rect
.size
,SRCALPHA
)
301 circle
= Surface([min(rect
.size
)*3]*2,SRCALPHA
)
302 draw
.ellipse(circle
,(0,0,0),circle
.get_rect(),0)
303 circle
= transform
.smoothscale(circle
,[int(min(rect
.size
)*radius
)]*2)
305 radius
= rectangle
.blit(circle
,(0,0))
306 radius
.bottomright
= rect
.bottomright
307 rectangle
.blit(circle
,radius
)
308 radius
.topright
= rect
.topright
309 rectangle
.blit(circle
,radius
)
310 radius
.bottomleft
= rect
.bottomleft
311 rectangle
.blit(circle
,radius
)
313 rectangle
.fill((0,0,0),rect
.inflate(-radius
.w
,0))
314 rectangle
.fill((0,0,0),rect
.inflate(0,-radius
.h
))
316 rectangle
.fill(color
,special_flags
=BLEND_RGBA_MAX
)
317 rectangle
.fill((255,255,255,alpha
),special_flags
=BLEND_RGBA_MIN
)
324 stream
= open("config.yml", "r")
325 config
= yaml
.load(stream
)
328 aliases
= config
['aliases']
331 for mapped_key
in config
['keys']:
332 key
= Key
.find_by_unicode(mapped_key
)
336 for action
in config
['keys'][mapped_key
]:
337 action_name
= list(action
)[0]
339 if action
[action_name
] is None:
340 action
[action_name
] = []
342 for argument
in action
[action_name
]:
343 if argument
== 'include':
344 included
= action
[action_name
]['include']
345 if isinstance(included
, str):
346 action_args
.update(aliases
[included
])
348 for included_
in included
:
349 action_args
.update(aliases
[included_
])
350 elif argument
== 'file':
351 filename
= action
[action_name
]['file']
352 if filename
not in seen_files
:
353 seen_files
[filename
] = MusicFile
.new(filename
)
355 action_args
['music'] = seen_files
[filename
]
358 action_args
[argument
] = action
[action_name
][argument
]
360 key
.add_action(action_name
, **action_args
)