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