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