]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blob - helpers/key.py
9099f00925b28e0764aa4eb6f6afa1743a595993
[perso/Immae/Projets/Python/MusicSampler.git] / helpers / key.py
1 from kivy.uix.widget import Widget
2 from kivy.properties import AliasProperty, BooleanProperty, \
3 ListProperty, StringProperty
4 from kivy.uix.behaviors import ButtonBehavior
5
6 from .action import Action
7 from . import debug_print
8 import time
9 from transitions.extensions import HierarchicalMachine as Machine
10
11 class Key(ButtonBehavior, Widget):
12 STATES = [
13 'initial',
14 'configuring',
15 'configured',
16 'loading',
17 'failed',
18 {
19 'name': 'loaded',
20 'children': ['no_config', 'no_actions', 'running']
21 }
22 ]
23
24 TRANSITIONS = [
25 {
26 'trigger': 'configure',
27 'source': 'initial',
28 'dest': 'configuring'
29 },
30 {
31 'trigger': 'fail',
32 'source': 'configuring',
33 'dest': 'failed',
34 'after': 'key_loaded_callback'
35 },
36 {
37 'trigger': 'success',
38 'source': 'configuring',
39 'dest': 'configured',
40 'after': 'load'
41 },
42 {
43 'trigger': 'no_config',
44 'source': 'configuring',
45 'dest': 'loaded_no_config',
46 'after': 'key_loaded_callback'
47 },
48 {
49 'trigger': 'load',
50 'source': 'configured',
51 'dest': 'loading'
52 },
53 {
54 'trigger': 'fail',
55 'source': 'loading',
56 'dest': 'failed',
57 'after': 'key_loaded_callback'
58 },
59 {
60 'trigger': 'success',
61 'source': 'loading',
62 'dest': 'loaded',
63 'after': 'key_loaded_callback'
64 },
65 {
66 'trigger': 'no_actions',
67 'source': 'loading',
68 'dest': 'loaded_no_actions',
69 'after': 'key_loaded_callback'
70 },
71 {
72 'trigger': 'reload',
73 'source': ['loaded','failed'],
74 'dest': 'configuring',
75 'after': 'key_loaded_callback'
76 },
77 {
78 'trigger': 'run',
79 'source': 'loaded',
80 'dest': 'loaded_running',
81 'after': 'finish',
82 # if a child, like loaded_no_actions, has no transitions, then it is
83 # bubbled to the parent, and we don't want that.
84 'conditions': ['is_loaded']
85 },
86 {
87 'trigger': 'finish',
88 'source': 'loaded_running',
89 'dest': 'loaded'
90 }
91 ]
92
93 key_sym = StringProperty(None)
94 custom_color = ListProperty([0, 1, 0])
95 description_title = StringProperty("")
96 description = ListProperty([])
97 state = StringProperty("")
98
99 def get_alias_line_color(self):
100 if self.is_loaded_running():
101 return [0, 0, 0, 1]
102 else:
103 return [120/255, 120/255, 120/255, 1]
104
105 def set_alias_line_color(self):
106 pass
107
108 line_color = AliasProperty(get_alias_line_color, set_alias_line_color,
109 bind=['state'])
110
111 def get_alias_color(self):
112 if self.is_loaded_inactive():
113 return [1, 1, 1, 1]
114 elif self.is_loaded(allow_substates=True):
115 return [*self.custom_color, 1]
116 elif self.is_failed():
117 return [0, 0, 0, 1]
118 else:
119 return [*self.custom_color, 100/255]
120 def set_alias_color(self):
121 pass
122
123 color = AliasProperty(get_alias_color, set_alias_color,
124 bind=['state', 'custom_color'])
125
126 def __init__(self, **kwargs):
127 self.actions = []
128 Machine(model=self, states=self.STATES,
129 transitions=self.TRANSITIONS, initial='initial',
130 ignore_invalid_triggers=True, queued=True)
131 super(Key, self).__init__(**kwargs)
132
133 # Kivy events
134 def on_key_sym(self, key, key_sym):
135 if key_sym != "":
136 self.configure()
137
138 def on_press(self):
139 self.list_actions()
140
141 # Machine states / events
142 def is_loaded_or_failed(self):
143 return self.is_loaded(allow_substates=True) or self.is_failed()
144
145 def is_loaded_inactive(self):
146 return self.is_loaded_no_config() or self.is_loaded_no_actions()
147
148 def on_enter_configuring(self):
149 if self.key_sym in self.parent.key_config:
150 self.config = self.parent.key_config[self.key_sym]
151
152 self.actions = []
153 for key_action in self.config['actions']:
154 self.add_action(key_action[0], **key_action[1])
155
156 if 'description' in self.config['properties']:
157 self.set_description(self.config['properties']['description'])
158 if 'color' in self.config['properties']:
159 self.set_color(self.config['properties']['color'])
160 self.success()
161 else:
162 self.no_config()
163
164 def on_enter_loading(self):
165 if len(self.actions) > 0:
166 for action in self.actions:
167 action.load()
168 else:
169 self.no_actions()
170
171 def on_enter_loaded_running(self, modifiers):
172 self.parent.parent.ids['KeyList'].append(self.key_sym)
173 debug_print("running actions for {}".format(self.key_sym))
174 start_time = time.time()
175 self.parent.start_running(self, start_time)
176 action_number = 0
177 for self.current_action in self.actions:
178 if self.parent.keep_running(self, start_time):
179 self.list_actions(action_number=action_number + 0.5)
180 self.current_action.run()
181 action_number += 1
182 self.list_actions(action_number=action_number)
183
184 self.parent.finished_running(self, start_time)
185
186 # This one cannot be in the Machine state since it would be queued to run
187 # *after* the loop is ended...
188 def interrupt(self):
189 self.current_action.interrupt()
190
191 # Callbacks
192 def key_loaded_callback(self):
193 self.parent.key_loaded_callback()
194
195 def callback_action_ready(self, action, success):
196 if not success:
197 self.fail()
198 elif all(action.is_loaded_or_failed() for action in self.actions):
199 self.success()
200
201 # Setters
202 def set_description(self, description):
203 if description[0] is not None:
204 self.description_title = str(description[0])
205 self.description = []
206 for desc in description[1 :]:
207 if desc is None:
208 self.description.append("")
209 else:
210 self.description.append(str(desc).replace(" ", " "))
211
212 def set_color(self, color):
213 color = [x / 255 for x in color]
214 self.custom_color = color
215
216 # Actions handling
217 def add_action(self, action_name, **arguments):
218 self.actions.append(Action(action_name, self, **arguments))
219
220 def list_actions(self, action_number=0):
221 self.parent.parent.ids['ActionList'].update_list(self, action_number)
222