]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/commitdiff
enhancing locks
authorIsmaël Bouya <ismael.bouya@normalesup.org>
Sat, 18 Jun 2016 23:13:41 +0000 (01:13 +0200)
committerIsmaël Bouya <ismael.bouya@normalesup.org>
Sat, 18 Jun 2016 23:13:41 +0000 (01:13 +0200)
helpers/__init__.py
helpers/action.py
helpers/key.py
helpers/lock.py [new file with mode: 0644]
music_sampler.py

index 6935342cc34a74f0304f4bd83bb42a335a7a1cb2..b1723ee8e51c169ef5ccd922169a0e4ecefb6340 100644 (file)
@@ -1,12 +1,10 @@
 # -*- coding: utf-8 -*-
-import threading
 from .music_file import *
 from .mapping import *
-
-draw_lock = threading.RLock()
+from .lock import *
+import yaml
 
 def parse_config(mapping):
-    import yaml
     stream = open("config.yml", "r")
     config = yaml.load(stream)
     stream.close()
@@ -14,7 +12,7 @@ def parse_config(mapping):
     aliases = config['aliases']
     seen_files = {}
 
-    file_lock = threading.RLock()
+    file_lock = Lock("file")
 
     for mapped_key in config['keys']:
         key = mapping.find_by_unicode(mapped_key)
index 8a69fae3a967f4dd020f7d037e89415a7de072e3..97873a0029d8e24e8a93b239174b305ab76bdd65 100644 (file)
@@ -28,9 +28,12 @@ class Action:
             return True
 
     def run(self):
-        print(getattr(self, self.action + "_print")(**self.arguments))
+        print(self.description())
         return getattr(self, self.action)(**self.arguments)
 
+    def description(self):
+        return getattr(self, self.action + "_print")(**self.arguments)
+
     def command(self, command = "", **kwargs):
         pass
 
index 2e4a313b39f28abb324cb1689e433621b95d1c3d..6997e702d75a78040e391e77e225a02a40b08d76 100644 (file)
@@ -112,9 +112,32 @@ class Key:
 
         self.mapping.finished_running(self, start_time)
 
-    def list_actions(self, surface):
-        # FIXME: Todo
-        print("bouh", self.key_sym)
-        surface.fill((255, 0, 0))
+    def list_actions(self, screen):
+        action_descriptions = [action.description() for action in self.actions]
+        print("actions linked to key {}:".format(self.key_sym))
+        print("\t" + "\n\t".join(action_descriptions))
+        self.draw_lock.acquire()
+        surface = pygame.Surface((800, 250)).convert()
+        surface.fill((250, 250, 250))
+        if getattr(sys, 'frozen', False):
+            police = pygame.font.Font(sys._MEIPASS + "/Ubuntu-Regular.ttf", 14)
+        else:
+            police = pygame.font.Font("Ubuntu-Regular.ttf", 14)
+
+        offset = 0
+        police.set_bold(True)
+        text = police.render("actions linked to key {}:".format(self.key_sym), True, (0,0,0))
+        surface.blit(text, (0, offset))
+        offset += police.get_linesize()
+
+        police.set_bold(False)
+        for description in action_descriptions:
+            text = police.render(description, True, (0,0,0))
+            surface.blit(text, (0, offset))
+            offset += police.get_linesize()
+
+        screen.blit(surface, (10, 330))
+        pygame.display.flip()
+        self.draw_lock.release()
 
 
diff --git a/helpers/lock.py b/helpers/lock.py
new file mode 100644 (file)
index 0000000..dff8b1f
--- /dev/null
@@ -0,0 +1,15 @@
+import threading
+
+class Lock:
+    def __init__(self, lock_type):
+        self.type = lock_type
+        self.lock = threading.RLock()
+
+    def acquire(self, *args, **kwargs):
+        #print("acquiring lock for {}".format(self.type))
+        self.lock.acquire(*args, **kwargs)
+
+    def release(self, *args, **kwargs):
+        #print("releasing lock for {}".format(self.type))
+        self.lock.release(*args, **kwargs)
+
index 9f14d561fe127c419f4b0955f5f05ad78ee1b1ca..fd03009495f72cddfaa635558190d70badeb05ae 100644 (file)
@@ -7,23 +7,19 @@ pygame.mixer.pre_init(frequency = 44100)
 pygame.init()
 
 size = width, height = 1024, 600
-
-helpers.draw_lock.acquire()
 screen = pygame.display.set_mode(size)
-mapping = helpers.Mapping(screen, helpers.draw_lock)
+screen.fill((250, 250, 250))
+
+draw_lock = helpers.Lock("draw")
 
-action_surface = pygame.Surface((600, 250)).convert()
-action_surface.fill((0,0,0))
+mapping = helpers.Mapping(screen, draw_lock)
 helpers.parse_config(mapping)
-helpers.draw_lock.release()
 
 mapping.draw()
 
-helpers.draw_lock.acquire()
-screen.blit(action_surface, (10, 330))
-
+draw_lock.acquire()
 pygame.display.flip()
-helpers.draw_lock.release()
+draw_lock.release()
 
 contexts = [
     'normal'
@@ -54,7 +50,9 @@ while 1:
         elif event.type == pygame.MOUSEBUTTONUP:
             key = mapping.find_by_collidepoint(pygame.mouse.get_pos())
             if key is not None:
-                key.list_actions(action_surface)
+                threading.Thread(target=key.list_actions, args = [screen]).start()
 
+    draw_lock.acquire()
     pygame.display.flip()
+    draw_lock.release()