]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/commitdiff
Cleanup some constants
authorIsmaël Bouya <ismael.bouya@normalesup.org>
Mon, 20 Jun 2016 22:08:27 +0000 (00:08 +0200)
committerIsmaël Bouya <ismael.bouya@normalesup.org>
Mon, 20 Jun 2016 22:08:27 +0000 (00:08 +0200)
helpers/key.py
helpers/mapping.py
music_sampler.py

index e643dc240f030042831000c6a890d85b6bfc961f..612758f5e4f01f7e489ce7b01f64ca5cec573652 100644 (file)
@@ -6,15 +6,6 @@ import sys
 import pygame
 
 class Key:
-    row_positions = {
-        'first':    0,
-        'second':  50,
-        'third':  100,
-        'fourth': 150,
-        'fifth':  200,
-        'sixth':  250,
-    }
-
     default_outer_color = (120, 120, 120)
     lighter_outer_color = (200, 200, 200)
     default_inner_color = (255, 255, 255)
@@ -27,11 +18,7 @@ class Key:
         self.key_name = key_name
         self.key_sym  = key_sym
 
-        if isinstance(top, str):
-            self.top = self.row_positions[top]
-        else:
-            self.top = top
-
+        self.top = top
         self.left   = left
         self.width  = width
         self.height = height
@@ -41,6 +28,7 @@ class Key:
 
         self.rect     = (self.left, self.top, self.right, self.bottom)
         self.position = (self.left, self.top)
+        self.disabled = disabled
 
         if disabled:
             self.outer_color = self.lighter_outer_color
index 84e99410874a979b7252329c245f152877582586..58aaf3d698461fed22e2913a5ffdb757e33dc597 100644 (file)
@@ -7,6 +7,15 @@ class Mapping:
     HEIGHT = 298
     SIZE   = WIDTH, HEIGHT
 
+    ROW_POSITIONS = {
+        'first':    0,
+        'second':  50,
+        'third':  100,
+        'fourth': 150,
+        'fifth':  200,
+        'sixth':  250,
+    }
+
     KEYS = [
         (pygame.K_ESCAPE, 'ESC', 'first',   0, {}),
 
@@ -26,17 +35,17 @@ class Mapping:
         (pygame.K_F12,    'F12', 'first', 700, {}),
 
 
-        (178,          '²', 'second',   0, {}),
+        (178,                 '²', 'second',   0, {}),
         (pygame.K_AMPERSAND,  '&', 'second',  50, {}),
-        (233,          'é', 'second', 100, {}),
+        (233,                 'é', 'second', 100, {}),
         (pygame.K_QUOTEDBL,   '"', 'second', 150, {}),
         (pygame.K_QUOTE,      "'", 'second', 200, {}),
         (pygame.K_LEFTPAREN,  '(', 'second', 250, {}),
         (pygame.K_MINUS,      '-', 'second', 300, {}),
-        (232,          'è', 'second', 350, {}),
+        (232,                 'è', 'second', 350, {}),
         (pygame.K_UNDERSCORE, '_', 'second', 400, {}),
-        (231,          'ç', 'second', 450, {}),
-        (224,          'à', 'second', 500, {}),
+        (231,                 'ç', 'second', 450, {}),
+        (224,                 'à', 'second', 500, {}),
         (pygame.K_RIGHTPAREN, ')', 'second', 550, {}),
         (pygame.K_EQUALS,     '=', 'second', 600, {}),
 
@@ -57,9 +66,9 @@ class Mapping:
         (pygame.K_CARET,      '^',   'third', 575, {}),
         (pygame.K_DOLLAR,     '$',   'third', 625, {}),
 
-        (pygame.K_RETURN, 'Enter', 'third', 692, { 'width': 56, 'height': 98 }),
+        (pygame.K_RETURN,   'Enter', 'third', 692, { 'width': 56, 'height': 98 }),
 
-        (pygame.K_CAPSLOCK,    'CAPS', 'fourth',  0, { 'width': 88, 'disabled': True }),
+        (pygame.K_CAPSLOCK, 'CAPS', 'fourth',  0, { 'width': 88, 'disabled': True }),
 
         (pygame.K_q,        'q', 'fourth',  90, {}),
         (pygame.K_s,        's', 'fourth', 140, {}),
@@ -71,7 +80,7 @@ class Mapping:
         (pygame.K_k,        'k', 'fourth', 440, {}),
         (pygame.K_l,        'l', 'fourth', 490, {}),
         (pygame.K_m,        'm', 'fourth', 540, {}),
-        (249,        'ù', 'fourth', 590, {}),
+        (249,               'ù', 'fourth', 590, {}),
         (pygame.K_ASTERISK, '*', 'fourth', 640, {}),
 
 
@@ -96,7 +105,7 @@ class Mapping:
         (pygame.K_LALT,     'LAlt',    'sixth', 165, { 'disabled': True }),
         (pygame.K_SPACE,    'Espace',  'sixth', 215, { 'width': 248 }),
         (pygame.K_MODE,     'AltGr',   'sixth', 465, { 'disabled': True }),
-        (314,        'Compose', 'sixth', 515, { 'disabled': True }),
+        (314,               'Compose', 'sixth', 515, { 'disabled': True }),
         (pygame.K_RCTRL,    'RCtrl',   'sixth', 565, { 'width': 63, 'disabled': True }),
 
 
@@ -122,7 +131,14 @@ class Mapping:
         self.keys = {}
         self.running = []
         for key in self.KEYS:
-            self.keys[key[0]] = Key(self, self.draw_lock, *key[0:4], **key[4])
+            if key[2] in self.ROW_POSITIONS:
+                position = self.ROW_POSITIONS[key[2]]
+            else:
+                position = key[2]
+            self.keys[key[0]] = Key(self,
+                    self.draw_lock,
+                    key[0], key[1], position, key[3],
+                    **key[4])
 
     def draw(self):
         for key_name in self.keys:
index 8ebfaa3e664f9c57eb951e57682dffd4e565eb74..c17f7bff98d1764704ee542ef9a1004aa1151f8b 100644 (file)
@@ -47,7 +47,7 @@ while 1:
     if context == 'normal':
         if event.type == pygame.KEYDOWN:
             key = mapping.find_by_key_num(event.key)
-            if key is not None:
+            if key is not None and not key.disabled:
                 threading.Thread(name = "MSKeyAction", target=key.do_actions).start()
                 threading.Thread(name = "MSClic", target=key.list_actions, args = [screen]).start()
         elif event.type == pygame.MOUSEBUTTONUP:
@@ -65,10 +65,10 @@ while 1:
     for music_file in open_files.values():
         police.set_bold(False)
         if music_file.is_playing():
-            icon = icon_police.render("⏵", True, (0,0,0))
             if music_file.is_paused():
                 icon = icon_police.render("⏸", True, (0,0,0))
             else:
+                icon = icon_police.render("⏵", True, (0,0,0))
                 police.set_bold(True)
             text = police.render(music_file.name, True, (0,0,0))
             surface.blit(icon, (0, offset))