aboutsummaryrefslogtreecommitdiff
path: root/music_sampler/key.py
diff options
context:
space:
mode:
Diffstat (limited to 'music_sampler/key.py')
-rw-r--r--music_sampler/key.py35
1 files changed, 20 insertions, 15 deletions
diff --git a/music_sampler/key.py b/music_sampler/key.py
index e524c35..e05bb16 100644
--- a/music_sampler/key.py
+++ b/music_sampler/key.py
@@ -13,7 +13,7 @@ from transitions.extensions import HierarchicalMachine as Machine
13# https://github.com/kivy/kivy/wiki/Working-with-Python-threads-inside-a-Kivy-application 13# https://github.com/kivy/kivy/wiki/Working-with-Python-threads-inside-a-Kivy-application
14from kivy.clock import mainthread 14from kivy.clock import mainthread
15 15
16class KeyMachine(Widget): 16class KeyMachine():
17 STATES = [ 17 STATES = [
18 'initial', 18 'initial',
19 'configuring', 19 'configuring',
@@ -101,11 +101,15 @@ class KeyMachine(Widget):
101 { 101 {
102 'trigger': 'repeat_protection_finished', 102 'trigger': 'repeat_protection_finished',
103 'source': 'loaded_protecting_repeat', 103 'source': 'loaded_protecting_repeat',
104 'dest': 'loaded' 104 'dest': 'loaded',
105 'after': 'callback_action_state_changed'
105 }, 106 },
106 ] 107 ]
107 108
108 state = StringProperty("") 109 def __setattr__(self, name, value):
110 if hasattr(self, 'initialized') and name == 'state':
111 self.key.update_state(value)
112 super().__setattr__(name, value)
109 113
110 def __init__(self, key, **kwargs): 114 def __init__(self, key, **kwargs):
111 self.key = key 115 self.key = key
@@ -113,7 +117,8 @@ class KeyMachine(Widget):
113 Machine(model=self, states=self.STATES, 117 Machine(model=self, states=self.STATES,
114 transitions=self.TRANSITIONS, initial='initial', 118 transitions=self.TRANSITIONS, initial='initial',
115 ignore_invalid_triggers=True, queued=True) 119 ignore_invalid_triggers=True, queued=True)
116 super(KeyMachine, self).__init__(**kwargs) 120
121 self.initialized = True
117 122
118 # Machine states / events 123 # Machine states / events
119 def is_loaded_or_failed(self): 124 def is_loaded_or_failed(self):
@@ -181,6 +186,17 @@ class KeyMachine(Widget):
181 def key_loaded_callback(self): 186 def key_loaded_callback(self):
182 self.key.parent.key_loaded_callback() 187 self.key.parent.key_loaded_callback()
183 188
189 def callback_action_state_changed(self):
190 if self.state not in ['failed', 'loading', 'loaded']:
191 return
192
193 if any(action.is_failed() for action in self.key.actions):
194 self.to_failed()
195 elif any(action.is_loading() for action in self.key.actions):
196 self.to_loading()
197 else:
198 self.to_loaded()
199 self.key_loaded_callback()
184 200
185class Key(ButtonBehavior, Widget): 201class Key(ButtonBehavior, Widget):
186 202
@@ -244,14 +260,10 @@ class Key(ButtonBehavior, Widget):
244 else: 260 else:
245 raise AttributeError 261 raise AttributeError
246 262
247 def machine_state_changed(self, instance, machine_state):
248 self.machine_state = self.machine.state
249
250 def __init__(self, **kwargs): 263 def __init__(self, **kwargs):
251 self.actions = [] 264 self.actions = []
252 self.current_action = None 265 self.current_action = None
253 self.machine = KeyMachine(self) 266 self.machine = KeyMachine(self)
254 self.machine.bind(state=self.machine_state_changed)
255 267
256 super(Key, self).__init__(**kwargs) 268 super(Key, self).__init__(**kwargs)
257 269
@@ -272,13 +284,6 @@ class Key(ButtonBehavior, Widget):
272 def interrupt(self): 284 def interrupt(self):
273 self.current_action.interrupt() 285 self.current_action.interrupt()
274 286
275 # Callbacks
276 def callback_action_ready(self, action, success):
277 if not success:
278 self.fail()
279 elif all(action.is_loaded_or_failed() for action in self.actions):
280 self.success()
281
282 # Setters 287 # Setters
283 def set_description(self, description): 288 def set_description(self, description):
284 if description[0] is not None: 289 if description[0] is not None: