From 98fee7ff51eac3660f05aefcb18bbd9c2d32c35c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Isma=C3=ABl=20Bouya?= Date: Sat, 18 Jun 2016 01:03:07 +0200 Subject: [PATCH] Move running keeper to Mapping --- helpers/__init__.py | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/helpers/__init__.py b/helpers/__init__.py index ec7da22..8570008 100644 --- a/helpers/__init__.py +++ b/helpers/__init__.py @@ -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: -- 2.41.0