]>
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 |
e55b29bb | 9 | from transitions.extensions import HierarchicalMachine as Machine |
4b2d79ca IB |
10 | |
11 | class Key(ButtonBehavior, Widget): | |
e55b29bb IB |
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 | }, | |
35 | { | |
36 | 'trigger': 'success', | |
37 | 'source': 'configuring', | |
38 | 'dest': 'configured', | |
39 | 'after': 'load' | |
40 | }, | |
41 | { | |
42 | 'trigger': 'no_config', | |
43 | 'source': 'configuring', | |
44 | 'dest': 'loaded_no_config', | |
45 | }, | |
46 | { | |
47 | 'trigger': 'load', | |
48 | 'source': 'configured', | |
49 | 'dest': 'loading' | |
50 | }, | |
51 | { | |
52 | 'trigger': 'fail', | |
53 | 'source': 'loading', | |
54 | 'dest': 'failed' | |
55 | }, | |
56 | { | |
57 | 'trigger': 'success', | |
58 | 'source': 'loading', | |
59 | 'dest': 'loaded' | |
60 | }, | |
61 | { | |
62 | 'trigger': 'no_actions', | |
63 | 'source': 'loading', | |
64 | 'dest': 'loaded_no_actions', | |
65 | }, | |
66 | { | |
67 | 'trigger': 'reload', | |
68 | 'source': 'loaded', | |
69 | 'dest': 'configuring' | |
70 | }, | |
71 | { | |
72 | 'trigger': 'run', | |
73 | 'source': 'loaded', | |
74 | 'dest': 'loaded_running', | |
75 | 'after': 'finish', | |
76 | # if a child, like loaded_no_actions, has no transitions, then it is | |
77 | # bubbled to the parent, and we don't want that. | |
78 | 'conditions': ['is_loaded'] | |
79 | }, | |
80 | { | |
81 | 'trigger': 'finish', | |
82 | 'source': 'loaded_running', | |
83 | 'dest': 'loaded' | |
84 | } | |
85 | ] | |
86 | ||
4b2d79ca | 87 | key_sym = StringProperty(None) |
e55b29bb | 88 | custom_color = ListProperty([0, 1, 0]) |
4b2d79ca IB |
89 | description_title = StringProperty("") |
90 | description = ListProperty([]) | |
e55b29bb | 91 | state = StringProperty("") |
4b2d79ca | 92 | |
e55b29bb IB |
93 | def get_alias_color(self): |
94 | if self.is_loaded_inactive(): | |
4b2d79ca | 95 | return [1, 1, 1, 1] |
e55b29bb IB |
96 | elif self.is_loaded(allow_substates=True): |
97 | return [*self.custom_color, 1] | |
98 | elif self.is_failed(): | |
99 | return [0, 0, 0, 1] | |
be27763f | 100 | else: |
e55b29bb IB |
101 | return [*self.custom_color, 100/255] |
102 | def set_alias_color(self): | |
4b2d79ca IB |
103 | pass |
104 | ||
e55b29bb IB |
105 | color = AliasProperty(get_alias_color, set_alias_color, |
106 | bind=['state', 'custom_color']) | |
be27763f | 107 | |
4b2d79ca | 108 | def __init__(self, **kwargs): |
be27763f | 109 | self.actions = [] |
e55b29bb IB |
110 | Machine(model=self, states=self.STATES, |
111 | transitions=self.TRANSITIONS, initial='initial', | |
112 | ignore_invalid_triggers=True, queued=True) | |
113 | super(Key, self).__init__(**kwargs) | |
be27763f | 114 | |
e55b29bb | 115 | # Kivy events |
4b2d79ca | 116 | def on_key_sym(self, key, key_sym): |
e55b29bb IB |
117 | if key_sym != "": |
118 | self.configure() | |
119 | ||
120 | def on_press(self): | |
121 | self.list_actions() | |
4b2d79ca | 122 | |
e55b29bb IB |
123 | # Machine states / events |
124 | def is_loaded_or_failed(self): | |
125 | return self.is_loaded(allow_substates=True) or self.is_failed() | |
126 | ||
127 | def is_loaded_inactive(self): | |
128 | return self.is_loaded_no_config() or self.is_loaded_no_actions() | |
129 | ||
130 | def on_enter_configuring(self): | |
131 | if self.key_sym in self.parent.key_config: | |
132 | self.config = self.parent.key_config[self.key_sym] | |
be27763f | 133 | |
4b2d79ca IB |
134 | self.actions = [] |
135 | for key_action in self.config['actions']: | |
136 | self.add_action(key_action[0], **key_action[1]) | |
137 | ||
138 | if 'description' in self.config['properties']: | |
e55b29bb | 139 | self.set_description(self.config['properties']['description']) |
4b2d79ca | 140 | if 'color' in self.config['properties']: |
e55b29bb IB |
141 | self.set_color(self.config['properties']['color']) |
142 | self.success() | |
143 | else: | |
144 | self.no_config() | |
4b2d79ca | 145 | |
e55b29bb IB |
146 | def on_enter_loading(self): |
147 | if len(self.actions) > 0: | |
148 | for action in self.actions: | |
149 | action.load() | |
150 | else: | |
151 | self.no_actions() | |
152 | ||
153 | def on_enter_loaded_running(self): | |
154 | self.parent.parent.ids['KeyList'].append(self.key_sym) | |
155 | debug_print("running actions for {}".format(self.key_sym)) | |
156 | start_time = time.time() | |
157 | self.parent.start_running(self, start_time) | |
158 | action_number = 0 | |
159 | for self.current_action in self.actions: | |
160 | if self.parent.keep_running(self, start_time): | |
161 | self.list_actions(action_number=action_number + 0.5) | |
162 | self.current_action.run() | |
163 | action_number += 1 | |
164 | self.list_actions(action_number=action_number) | |
4b2d79ca | 165 | |
e55b29bb | 166 | self.parent.finished_running(self, start_time) |
be27763f | 167 | |
e55b29bb IB |
168 | # This one cannot be in the Machine state since it would be queued to run |
169 | # *after* the loop is ended... | |
170 | def interrupt(self): | |
171 | self.current_action.interrupt() | |
172 | ||
173 | # Callbacks | |
174 | def callback_action_ready(self, action, success): | |
175 | if not success: | |
176 | self.fail() | |
177 | elif all(action.is_loaded_or_failed() for action in self.actions): | |
178 | self.success() | |
179 | ||
180 | # Setters | |
b86db9f1 | 181 | def set_description(self, description): |
4b2d79ca IB |
182 | if description[0] is not None: |
183 | self.description_title = str(description[0]) | |
2e404903 | 184 | for desc in description[1 :]: |
d479af33 IB |
185 | if desc is None: |
186 | self.description.append("") | |
187 | else: | |
4b2d79ca | 188 | self.description.append(str(desc).replace(" ", " ")) |
b86db9f1 IB |
189 | |
190 | def set_color(self, color): | |
4b2d79ca | 191 | color = [x / 255 for x in color] |
4b2d79ca | 192 | self.custom_color = color |
be27763f | 193 | |
e55b29bb | 194 | # Actions handling |
be27763f IB |
195 | def add_action(self, action_name, **arguments): |
196 | self.actions.append(Action(action_name, self, **arguments)) | |
197 | ||
2e404903 | 198 | def list_actions(self, action_number=0): |
4b2d79ca | 199 | self.parent.parent.ids['ActionList'].update_list(self, action_number) |
be27763f | 200 |