]>
Commit | Line | Data |
---|---|---|
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 .helpers import debug_print | |
8 | import time | |
9 | import threading | |
10 | from transitions.extensions import HierarchicalMachine as Machine | |
11 | ||
12 | # All drawing operations should happen in the main thread | |
13 | # https://github.com/kivy/kivy/wiki/Working-with-Python-threads-inside-a-Kivy-application | |
14 | from kivy.clock import mainthread | |
15 | ||
16 | class KeyMachine(Widget): | |
17 | STATES = [ | |
18 | 'initial', | |
19 | 'configuring', | |
20 | 'configured', | |
21 | 'loading', | |
22 | 'failed', | |
23 | { | |
24 | 'name': 'loaded', | |
25 | 'children': [ | |
26 | 'no_config', | |
27 | 'no_actions', | |
28 | 'running', | |
29 | 'protecting_repeat' | |
30 | ] | |
31 | } | |
32 | ] | |
33 | ||
34 | TRANSITIONS = [ | |
35 | { | |
36 | 'trigger': 'configure', | |
37 | 'source': 'initial', | |
38 | 'dest': 'configuring' | |
39 | }, | |
40 | { | |
41 | 'trigger': 'fail', | |
42 | 'source': 'configuring', | |
43 | 'dest': 'failed', | |
44 | 'after': 'key_loaded_callback' | |
45 | }, | |
46 | { | |
47 | 'trigger': 'success', | |
48 | 'source': 'configuring', | |
49 | 'dest': 'configured', | |
50 | 'after': 'load' | |
51 | }, | |
52 | { | |
53 | 'trigger': 'no_config', | |
54 | 'source': 'configuring', | |
55 | 'dest': 'loaded_no_config', | |
56 | 'after': 'key_loaded_callback' | |
57 | }, | |
58 | { | |
59 | 'trigger': 'load', | |
60 | 'source': 'configured', | |
61 | 'dest': 'loading' | |
62 | }, | |
63 | { | |
64 | 'trigger': 'fail', | |
65 | 'source': 'loading', | |
66 | 'dest': 'failed', | |
67 | 'after': 'key_loaded_callback' | |
68 | }, | |
69 | { | |
70 | 'trigger': 'success', | |
71 | 'source': 'loading', | |
72 | 'dest': 'loaded', | |
73 | 'after': 'key_loaded_callback' | |
74 | }, | |
75 | { | |
76 | 'trigger': 'no_actions', | |
77 | 'source': 'loading', | |
78 | 'dest': 'loaded_no_actions', | |
79 | 'after': 'key_loaded_callback' | |
80 | }, | |
81 | { | |
82 | 'trigger': 'reload', | |
83 | 'source': ['loaded','failed'], | |
84 | 'dest': 'configuring', | |
85 | 'after': 'key_loaded_callback' | |
86 | }, | |
87 | { | |
88 | 'trigger': 'run', | |
89 | 'source': 'loaded', | |
90 | 'dest': 'loaded_running', | |
91 | 'after': ['run_actions', 'finish'], | |
92 | # if a child, like loaded_no_actions, has no transitions, then it | |
93 | # is bubbled to the parent, and we don't want that. | |
94 | 'conditions': ['is_loaded'] | |
95 | }, | |
96 | { | |
97 | 'trigger': 'finish', | |
98 | 'source': 'loaded_running', | |
99 | 'dest': 'loaded_protecting_repeat' | |
100 | }, | |
101 | { | |
102 | 'trigger': 'repeat_protection_finished', | |
103 | 'source': 'loaded_protecting_repeat', | |
104 | 'dest': 'loaded' | |
105 | }, | |
106 | ] | |
107 | ||
108 | state = StringProperty("") | |
109 | ||
110 | def __init__(self, key, **kwargs): | |
111 | self.key = key | |
112 | ||
113 | Machine(model=self, states=self.STATES, | |
114 | transitions=self.TRANSITIONS, initial='initial', | |
115 | ignore_invalid_triggers=True, queued=True) | |
116 | super(KeyMachine, self).__init__(**kwargs) | |
117 | ||
118 | # Machine states / events | |
119 | def is_loaded_or_failed(self): | |
120 | return self.is_loaded(allow_substates=True) or self.is_failed() | |
121 | ||
122 | def is_loaded_inactive(self): | |
123 | return self.is_loaded_no_config() or self.is_loaded_no_actions() | |
124 | ||
125 | @mainthread | |
126 | def on_enter_configuring(self): | |
127 | self.destroy_actions() | |
128 | self.key.unset_description() | |
129 | self.key.unset_color() | |
130 | ||
131 | if self.key.key_sym in self.key.parent.key_config: | |
132 | self.key.config = self.key.parent.key_config[self.key.key_sym] | |
133 | ||
134 | for key_action in self.key.config['actions']: | |
135 | self.key.add_action(key_action[0], **key_action[1]) | |
136 | ||
137 | if 'description' in self.key.config['properties']: | |
138 | self.key.set_description(self.key.config['properties']['description']) | |
139 | if 'color' in self.key.config['properties']: | |
140 | self.key.set_color(self.key.config['properties']['color']) | |
141 | self.success() | |
142 | else: | |
143 | self.no_config() | |
144 | ||
145 | def on_enter_loading(self): | |
146 | if len(self.key.actions) > 0: | |
147 | for action in self.key.actions: | |
148 | action.load() | |
149 | else: | |
150 | self.no_actions() | |
151 | ||
152 | def destroy_actions(self): | |
153 | for action in self.key.actions: | |
154 | action.destroy() | |
155 | self.key.actions = [] | |
156 | ||
157 | def run_actions(self, modifiers): | |
158 | self.key.parent.parent.ids['KeyList'].append(self.key.key_sym) | |
159 | debug_print("running actions for {}".format(self.key.key_sym)) | |
160 | start_time = time.time() | |
161 | self.key.parent.start_running(self.key, start_time) | |
162 | for self.key.current_action in self.key.actions: | |
163 | if self.key.parent.keep_running(self.key, start_time): | |
164 | self.key.list_actions() | |
165 | self.key.current_action.run(start_time) | |
166 | self.key.list_actions(last_action_finished=True) | |
167 | ||
168 | self.key.parent.finished_running(self.key, start_time) | |
169 | ||
170 | def on_enter_loaded_protecting_repeat(self, modifiers): | |
171 | if self.key.repeat_delay > 0: | |
172 | self.key.protecting_repeat_timer = threading.Timer( | |
173 | self.key.repeat_delay, | |
174 | self.key.repeat_protection_finished) | |
175 | self.key.protecting_repeat_timer.start() | |
176 | else: | |
177 | self.key.repeat_protection_finished() | |
178 | ||
179 | # Callbacks | |
180 | @mainthread | |
181 | def key_loaded_callback(self): | |
182 | self.key.parent.key_loaded_callback() | |
183 | ||
184 | ||
185 | class Key(ButtonBehavior, Widget): | |
186 | ||
187 | key_sym = StringProperty(None) | |
188 | custom_color = ListProperty([0, 1, 0]) | |
189 | description_title = StringProperty("") | |
190 | description = ListProperty([]) | |
191 | machine_state = StringProperty("") | |
192 | ||
193 | def get_alias_line_cross_color(self): | |
194 | if not self.is_failed() and ( | |
195 | not self.is_loaded(allow_substates=True)\ | |
196 | or self.is_loaded_running()\ | |
197 | or self.is_loaded_protecting_repeat()): | |
198 | return [120/255, 120/255, 120/255, 1] | |
199 | else: | |
200 | return [0, 0, 0, 0] | |
201 | ||
202 | def set_alias_line_cross_color(self): | |
203 | pass | |
204 | ||
205 | line_cross_color = AliasProperty( | |
206 | get_alias_line_cross_color, | |
207 | set_alias_line_cross_color, | |
208 | bind=['machine_state']) | |
209 | ||
210 | def get_alias_line_color(self): | |
211 | if self.is_loaded_running(): | |
212 | return [0, 0, 0, 1] | |
213 | else: | |
214 | return [120/255, 120/255, 120/255, 1] | |
215 | ||
216 | def set_alias_line_color(self): | |
217 | pass | |
218 | ||
219 | line_color = AliasProperty(get_alias_line_color, set_alias_line_color, | |
220 | bind=['machine_state']) | |
221 | ||
222 | def get_alias_color(self): | |
223 | if self.is_loaded_inactive(): | |
224 | return [1, 1, 1, 1] | |
225 | elif self.is_loaded_protecting_repeat(): | |
226 | return [*self.custom_color, 100/255] | |
227 | elif self.is_loaded_running(): | |
228 | return [*self.custom_color, 100/255] | |
229 | elif self.is_loaded(allow_substates=True): | |
230 | return [*self.custom_color, 1] | |
231 | elif self.is_failed(): | |
232 | return [0, 0, 0, 1] | |
233 | else: | |
234 | return [*self.custom_color, 100/255] | |
235 | def set_alias_color(self): | |
236 | pass | |
237 | ||
238 | color = AliasProperty(get_alias_color, set_alias_color, | |
239 | bind=['machine_state', 'custom_color']) | |
240 | ||
241 | def __getattr__(self, name): | |
242 | if hasattr(self.machine, name): | |
243 | return getattr(self.machine, name) | |
244 | else: | |
245 | raise AttributeError | |
246 | ||
247 | def machine_state_changed(self, instance, machine_state): | |
248 | self.machine_state = self.machine.state | |
249 | ||
250 | def __init__(self, **kwargs): | |
251 | self.actions = [] | |
252 | self.current_action = None | |
253 | self.machine = KeyMachine(self) | |
254 | self.machine.bind(state=self.machine_state_changed) | |
255 | ||
256 | super(Key, self).__init__(**kwargs) | |
257 | ||
258 | # Kivy events | |
259 | @mainthread | |
260 | def update_state(self, value): | |
261 | self.machine_state = value | |
262 | ||
263 | def on_key_sym(self, key, key_sym): | |
264 | if key_sym != "": | |
265 | self.configure() | |
266 | ||
267 | def on_press(self): | |
268 | self.list_actions() | |
269 | ||
270 | # This one cannot be in the Machine state since it would be queued to run | |
271 | # *after* the loop is ended... | |
272 | def interrupt(self): | |
273 | self.current_action.interrupt() | |
274 | ||
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 | |
283 | def set_description(self, description): | |
284 | if description[0] is not None: | |
285 | self.description_title = str(description[0]) | |
286 | self.description = [] | |
287 | for desc in description[1 :]: | |
288 | if desc is None: | |
289 | self.description.append("") | |
290 | else: | |
291 | self.description.append(str(desc).replace(" ", " ")) | |
292 | ||
293 | def unset_description(self): | |
294 | self.description_title = "" | |
295 | self.description = [] | |
296 | ||
297 | def set_color(self, color): | |
298 | color = [x / 255 for x in color] | |
299 | self.custom_color = color | |
300 | ||
301 | def unset_color(self): | |
302 | self.custom_color = [0, 1, 0] | |
303 | ||
304 | # Helpers | |
305 | @property | |
306 | def repeat_delay(self): | |
307 | if hasattr(self, 'config') and\ | |
308 | 'repeat_delay' in self.config['properties']: | |
309 | return self.config['properties']['repeat_delay'] | |
310 | else: | |
311 | return 0 | |
312 | ||
313 | # Actions handling | |
314 | def add_action(self, action_name, **arguments): | |
315 | self.actions.append(Action(action_name, self, **arguments)) | |
316 | ||
317 | def list_actions(self, last_action_finished=False): | |
318 | not_running = (not self.is_loaded_running()) | |
319 | current_action_seen = False | |
320 | action_descriptions = [] | |
321 | for action in self.actions: | |
322 | if not_running: | |
323 | state = "inactive" | |
324 | elif last_action_finished: | |
325 | state = "done" | |
326 | elif current_action_seen: | |
327 | state = "pending" | |
328 | elif action == self.current_action: | |
329 | current_action_seen = True | |
330 | state = "current" | |
331 | else: | |
332 | state = "done" | |
333 | action_descriptions.append([action.description(), state]) | |
334 | self.parent.parent.ids['ActionList'].update_list( | |
335 | self, | |
336 | action_descriptions) |