]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/commitdiff
Move running keeper to Mapping
authorIsmaël Bouya <ismael.bouya@normalesup.org>
Fri, 17 Jun 2016 23:03:07 +0000 (01:03 +0200)
committerIsmaël Bouya <ismael.bouya@normalesup.org>
Fri, 17 Jun 2016 23:03:07 +0000 (01:03 +0200)
helpers/__init__.py

index ec7da22f4397f44fe28d2333c29dac223b317e3d..8570008f265efe7e86ae1f21be28c9c68213cae8 100644 (file)
@@ -18,12 +18,13 @@ class Action:
         'wait',
     ]
 
-    def __init__(self, action, **kwargs):
+    def __init__(self, action, key, **kwargs):
         if action in self.action_types:
             self.action = action
         else:
             raise Exception("Unknown action {}".format(action))
 
+        self.key = key
         self.arguments = kwargs
 
     def ready(self):
@@ -59,7 +60,7 @@ class Action:
             mixer.stop()
 
     def stop_all_actions(self, **kwargs):
-        Key.running = []
+        self.key.mapping.stop_all_running()
 
     def volume(self, music = None, value = 100, **kwargs):
         pass
@@ -136,7 +137,6 @@ class Key:
     default_inner_color = (255, 255, 255)
     mapped_inner_color  = (  0, 255,   0)
     mapped_unready_inner_color = (255, 165, 0)
-    running = []
 
     def __init__(self, mapping, key_name, key_sym, top, left, width = 48, height = 48, disabled = False):
         self.mapping = mapping
@@ -217,19 +217,17 @@ class Key:
         return all(action.ready() for action in self.actions)
 
     def add_action(self, action_name, **arguments):
-        self.actions.append(Action(action_name, **arguments))
+        self.actions.append(Action(action_name, self, **arguments))
 
     def do_actions(self):
         print("running actions for {}".format(self.key_sym))
-        Key.running.append(self)
+        start_time = time.time()
+        self.mapping.start_running(self, start_time)
         for action in self.actions:
-            #FIXME: si on stop_all_actions et qu'on relance, "self" est de
-            #nouveau dans Key.running
-            if self in Key.running:
+            if self.mapping.keep_running(self, start_time):
                 action.run()
 
-        if self in Key.running:
-            Key.running.remove(self)
+        self.mapping.finished_running(self, start_time)
 
     def list_actions(self, surface):
         # FIXME: Todo
@@ -354,6 +352,7 @@ class Mapping:
         self.background = Surface(self.SIZE).convert()
         self.background.fill((250, 250, 250))
         self.keys = {}
+        self.running = []
         for key in self.KEYS:
             self.keys[key[0]] = Key(self, *key[0:4], **key[4])
 
@@ -389,6 +388,18 @@ class Mapping:
                 return self.keys[key]
         return None
 
+    def stop_all_running(self):
+        self.running = []
+
+    def start_running(self, key, start_time):
+        self.running.append((key, start_time))
+
+    def keep_running(self, key, start_time):
+        return (key, start_time) in self.running
+
+    def finished_running(self, key, start_time):
+        if (key, start_time) in self.running:
+            self.running.remove((key, start_time))
 
 
 class MusicFile: