]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blobdiff - helpers/key.py
Change line cross color depending on state
[perso/Immae/Projets/Python/MusicSampler.git] / helpers / key.py
index c21f9ae2a0bf8ffa60f927e5646a6f65528c8b50..66e792d514ed2ac31a10a273dc95605ff2d5b8ac 100644 (file)
@@ -6,6 +6,7 @@ from kivy.uix.behaviors import ButtonBehavior
 from .action import Action
 from . import debug_print
 import time
+import threading
 from transitions.extensions import HierarchicalMachine as Machine
 
 class Key(ButtonBehavior, Widget):
@@ -17,7 +18,12 @@ class Key(ButtonBehavior, Widget):
         'failed',
         {
             'name': 'loaded',
-            'children': ['no_config', 'no_actions', 'running']
+            'children': [
+                'no_config',
+                'no_actions',
+                'running',
+                'protecting_repeat'
+            ]
         }
     ]
 
@@ -79,15 +85,20 @@ class Key(ButtonBehavior, Widget):
             'source': 'loaded',
             'dest': 'loaded_running',
             'after': ['run_actions', 'finish'],
-            # if a child, like loaded_no_actions, has no transitions, then it is
-            # bubbled to the parent, and we don't want that.
+            # if a child, like loaded_no_actions, has no transitions, then it
+            # is bubbled to the parent, and we don't want that.
             'conditions': ['is_loaded']
         },
         {
             'trigger': 'finish',
             'source': 'loaded_running',
+            'dest': 'loaded_protecting_repeat'
+        },
+        {
+            'trigger': 'repeat_protection_finished',
+            'source': 'loaded_protecting_repeat',
             'dest': 'loaded'
-        }
+        },
     ]
 
     key_sym = StringProperty(None)
@@ -96,6 +107,23 @@ class Key(ButtonBehavior, Widget):
     description = ListProperty([])
     state = StringProperty("")
 
+    def get_alias_line_cross_color(self):
+        if not self.is_failed() and (
+                not self.is_loaded(allow_substates=True)\
+                or self.is_loaded_running()\
+                or self.is_loaded_protecting_repeat()):
+            return [120/255, 120/255, 120/255, 1]
+        else:
+            return [0, 0, 0, 0]
+
+    def set_alias_line_cross_color(self):
+        pass
+
+    line_cross_color = AliasProperty(
+            get_alias_line_cross_color,
+            set_alias_line_cross_color,
+            bind=['state'])
+
     def get_alias_line_color(self):
         if self.is_loaded_running():
             return [0, 0, 0, 1]
@@ -111,6 +139,10 @@ class Key(ButtonBehavior, Widget):
     def get_alias_color(self):
         if self.is_loaded_inactive():
             return [1, 1, 1, 1]
+        elif self.is_loaded_protecting_repeat():
+            return [*self.custom_color, 100/255]
+        elif self.is_loaded_running():
+            return [*self.custom_color, 100/255]
         elif self.is_loaded(allow_substates=True):
             return [*self.custom_color, 1]
         elif self.is_failed():
@@ -178,11 +210,20 @@ class Key(ButtonBehavior, Widget):
         for self.current_action in self.actions:
             if self.parent.keep_running(self, start_time):
                 self.list_actions()
-                self.current_action.run()
+                self.current_action.run(start_time)
         self.list_actions(last_action_finished=True)
 
         self.parent.finished_running(self, start_time)
 
+    def on_enter_loaded_protecting_repeat(self, modifiers):
+        if 'repeat_delay' in self.config['properties']:
+            self.protecting_repeat_timer = threading.Timer(
+                    self.config['properties']['repeat_delay'],
+                    self.repeat_protection_finished)
+            self.protecting_repeat_timer.start()
+        else:
+            self.repeat_protection_finished()
+
     # This one cannot be in the Machine state since it would be queued to run
     # *after* the loop is ended...
     def interrupt(self):