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