]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blobdiff - helpers/mapping.py
Add possibility to reload YML config file
[perso/Immae/Projets/Python/MusicSampler.git] / helpers / mapping.py
index 9c059725dcb54bd4beda9fc151298c051c531e3f..1256696a8968bfe58ea843ff8a0cfc1874fa0471 100644 (file)
@@ -1,5 +1,5 @@
 from kivy.uix.relativelayout import RelativeLayout
-from kivy.properties import NumericProperty, ListProperty
+from kivy.properties import NumericProperty, ListProperty, StringProperty
 from kivy.core.window import Window
 from kivy.clock import Clock
 
@@ -56,13 +56,34 @@ class Mapping(RelativeLayout):
             'trigger': 'success',
             'source': 'loading',
             'dest': 'loaded'
+        },
+        {
+            'trigger': 'reload',
+            'source': 'loaded',
+            'dest': 'configuring'
         }
     ]
 
     master_volume = NumericProperty(100)
     ready_color = ListProperty([1, 165/255, 0, 1])
+    state = StringProperty("")
 
     def __init__(self, **kwargs):
+        self.keys = []
+        self.running = []
+        self.wait_ids = {}
+        self.open_files = {}
+
+        Machine(model=self, states=self.STATES,
+                transitions=self.TRANSITIONS, initial='initial',
+                ignore_invalid_triggers=True, queued=True)
+        super(Mapping, self).__init__(**kwargs)
+        self.keyboard = Window.request_keyboard(self.on_keyboard_closed, self)
+        self.keyboard.bind(on_key_down=self.on_keyboard_down)
+
+        self.configure()
+
+    def on_enter_configuring(self):
         if Config.builtin_mixing:
             self.mixer = Mixer()
         else:
@@ -74,14 +95,13 @@ class Mapping(RelativeLayout):
             error_print("Error while loading configuration: {}".format(e),
                     with_trace=True)
             sys.exit()
+        else:
+            self.success()
 
-        self.keys = []
-        self.running = []
-        self.wait_ids = {}
-
-        super(Mapping, self).__init__(**kwargs)
-        self.keyboard = Window.request_keyboard(self.on_keyboard_closed, self)
-        self.keyboard.bind(on_key_down=self.on_keyboard_down)
+    def on_enter_loading(self):
+        for key in self.keys:
+            key.reload()
+        self.success()
 
     # Kivy events
     def add_widget(self, widget, index=0):
@@ -100,8 +120,10 @@ class Mapping(RelativeLayout):
 
     def on_keyboard_down(self, keyboard, keycode, text, modifiers):
         key = self.find_by_key_code(keycode)
-        if len(modifiers) == 0 and key is not None:
-            threading.Thread(name="MSKeyAction", target=key.run).start()
+        if self.allowed_modifiers(modifiers) and key is not None:
+            modifiers.sort()
+            threading.Thread(name="MSKeyAction", target=key.run,
+                    args=['-'.join(modifiers)]).start()
         elif 'ctrl' in modifiers and (keycode[0] == 113 or keycode[0] == '99'):
             self.stop_all_running()
             for thread in threading.enumerate():
@@ -110,8 +132,15 @@ class Mapping(RelativeLayout):
                 thread.join()
 
             sys.exit()
+        elif 'ctrl' in modifiers and keycode[0] == 114:
+            threading.Thread(name="MSReload", target=self.reload).start()
         return True
 
+    # Helpers
+    def allowed_modifiers(self, modifiers):
+        allowed = []
+        return len([a for a in modifiers if a not in allowed]) == 0
+
     def find_by_key_code(self, key_code):
         if "Key_" + str(key_code[0]) in self.ids:
             return self.ids["Key_" + str(key_code[0])]
@@ -136,6 +165,8 @@ class Mapping(RelativeLayout):
             self.ready_color = [0, 1, 0, 1]
         elif result == "partial":
             self.ready_color = [1, 0, 0, 1]
+        else:
+            self.ready_color = [1, 165/255, 0, 1]
 
     ## Some global actions
     def stop_all_running(self):
@@ -340,8 +371,14 @@ class Mapping(RelativeLayout):
                                     music_properties[filename],
                                     filename)
 
-                            seen_files[filename] = MusicFile(
-                                    filename, self, **music_property)
+                            if filename in self.open_files:
+                                self.open_files[filename]\
+                                        .reload_properties(**music_property)
+
+                                seen_files[filename] = self.open_files[filename]
+                            else:
+                                seen_files[filename] = MusicFile(
+                                        filename, self, **music_property)
 
                         if filename not in key_properties[mapped_key]['files']:
                             key_properties[mapped_key]['files'] \