1 from kivy
.uix
.widget
import Widget
2 from kivy
.properties
import AliasProperty
, BooleanProperty
, \
3 ListProperty
, StringProperty
4 from kivy
.uix
.behaviors
import ButtonBehavior
6 from .action
import Action
7 from .helpers
import debug_print
10 from transitions
.extensions
import HierarchicalMachine
as Machine
12 class KeyMachine(Widget
):
32 'trigger': 'configure',
38 'source': 'configuring',
40 'after': 'key_loaded_callback'
44 'source': 'configuring',
49 'trigger': 'no_config',
50 'source': 'configuring',
51 'dest': 'loaded_no_config',
52 'after': 'key_loaded_callback'
56 'source': 'configured',
63 'after': 'key_loaded_callback'
69 'after': 'key_loaded_callback'
72 'trigger': 'no_actions',
74 'dest': 'loaded_no_actions',
75 'after': 'key_loaded_callback'
79 'source': ['loaded','failed'],
80 'dest': 'configuring',
81 'after': 'key_loaded_callback'
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']
94 'source': 'loaded_running',
95 'dest': 'loaded_protecting_repeat'
98 'trigger': 'repeat_protection_finished',
99 'source': 'loaded_protecting_repeat',
104 state
= StringProperty("")
106 def __init__(self
, key
, **kwargs
):
109 Machine(model
=self
, states
=self
.STATES
,
110 transitions
=self
.TRANSITIONS
, initial
='initial',
111 ignore_invalid_triggers
=True, queued
=True)
112 super(KeyMachine
, self
).__init
__(**kwargs
)
114 # Machine states / events
115 def is_loaded_or_failed(self
):
116 return self
.is_loaded(allow_substates
=True) or self
.is_failed()
118 def is_loaded_inactive(self
):
119 return self
.is_loaded_no_config() or self
.is_loaded_no_actions()
121 def on_enter_configuring(self
):
122 self
.destroy_actions()
123 self
.key
.unset_description()
124 self
.key
.unset_color()
126 if self
.key
.key_sym
in self
.key
.parent
.key_config
:
127 self
.key
.config
= self
.key
.parent
.key_config
[self
.key
.key_sym
]
129 for key_action
in self
.key
.config
['actions']:
130 self
.key
.add_action(key_action
[0], **key_action
[1])
132 if 'description' in self
.key
.config
['properties']:
133 self
.key
.set_description(self
.key
.config
['properties']['description'])
134 if 'color' in self
.key
.config
['properties']:
135 self
.key
.set_color(self
.key
.config
['properties']['color'])
140 def on_enter_loading(self
):
141 if len(self
.key
.actions
) > 0:
142 for action
in self
.key
.actions
:
147 def destroy_actions(self
):
148 for action
in self
.key
.actions
:
150 self
.key
.actions
= []
152 def run_actions(self
, modifiers
):
153 self
.key
.parent
.parent
.ids
['KeyList'].append(self
.key
.key_sym
)
154 debug_print("running actions for {}".format(self
.key
.key_sym
))
155 start_time
= time
.time()
156 self
.key
.parent
.start_running(self
.key
, start_time
)
157 for self
.key
.current_action
in self
.key
.actions
:
158 if self
.key
.parent
.keep_running(self
.key
, start_time
):
159 self
.key
.list_actions()
160 self
.key
.current_action
.run(start_time
)
161 self
.key
.list_actions(last_action_finished
=True)
163 self
.key
.parent
.finished_running(self
.key
, start_time
)
165 def on_enter_loaded_protecting_repeat(self
, modifiers
):
166 if self
.key
.repeat_delay
> 0:
167 self
.key
.protecting_repeat_timer
= threading
.Timer(
168 self
.key
.repeat_delay
,
169 self
.key
.repeat_protection_finished
)
170 self
.key
.protecting_repeat_timer
.start()
172 self
.key
.repeat_protection_finished()
175 def key_loaded_callback(self
):
176 self
.key
.parent
.key_loaded_callback()
179 class Key(ButtonBehavior
, Widget
):
181 key_sym
= StringProperty(None)
182 custom_color
= ListProperty([0, 1, 0])
183 description_title
= StringProperty("")
184 description
= ListProperty([])
185 machine_state
= StringProperty("")
187 def get_alias_line_cross_color(self
):
188 if not self
.is_failed() and (
189 not self
.is_loaded(allow_substates
=True)\
190 or self
.is_loaded_running()\
191 or self
.is_loaded_protecting_repeat()):
192 return [120/255, 120/255, 120/255, 1]
196 def set_alias_line_cross_color(self
):
199 line_cross_color
= AliasProperty(
200 get_alias_line_cross_color
,
201 set_alias_line_cross_color
,
202 bind
=['machine_state'])
204 def get_alias_line_color(self
):
205 if self
.is_loaded_running():
208 return [120/255, 120/255, 120/255, 1]
210 def set_alias_line_color(self
):
213 line_color
= AliasProperty(get_alias_line_color
, set_alias_line_color
,
214 bind
=['machine_state'])
216 def get_alias_color(self
):
217 if self
.is_loaded_inactive():
219 elif self
.is_loaded_protecting_repeat():
220 return [*self
.custom_color
, 100/255]
221 elif self
.is_loaded_running():
222 return [*self
.custom_color
, 100/255]
223 elif self
.is_loaded(allow_substates
=True):
224 return [*self
.custom_color
, 1]
225 elif self
.is_failed():
228 return [*self
.custom_color
, 100/255]
229 def set_alias_color(self
):
232 color
= AliasProperty(get_alias_color
, set_alias_color
,
233 bind
=['machine_state', 'custom_color'])
235 def __getattr__(self
, name
):
236 if hasattr(self
.machine
, name
):
237 return getattr(self
.machine
, name
)
241 def machine_state_changed(self
, instance
, machine_state
):
242 self
.machine_state
= self
.machine
.state
244 def __init__(self
, **kwargs
):
246 self
.current_action
= None
247 self
.machine
= KeyMachine(self
)
248 self
.machine
.bind(state
=self
.machine_state_changed
)
250 super(Key
, self
).__init
__(**kwargs
)
253 def on_key_sym(self
, key
, key_sym
):
260 # This one cannot be in the Machine state since it would be queued to run
261 # *after* the loop is ended...
263 self
.current_action
.interrupt()
266 def callback_action_ready(self
, action
, success
):
269 elif all(action
.is_loaded_or_failed() for action
in self
.actions
):
273 def set_description(self
, description
):
274 if description
[0] is not None:
275 self
.description_title
= str(description
[0])
276 self
.description
= []
277 for desc
in description
[1 :]:
279 self
.description
.append("")
281 self
.description
.append(str(desc
).replace(" ", " "))
283 def unset_description(self
):
284 self
.description_title
= ""
285 self
.description
= []
287 def set_color(self
, color
):
288 color
= [x
/ 255 for x
in color
]
289 self
.custom_color
= color
291 def unset_color(self
):
292 self
.custom_color
= [0, 1, 0]
296 def repeat_delay(self
):
297 if hasattr(self
, 'config') and\
298 'repeat_delay' in self
.config
['properties']:
299 return self
.config
['properties']['repeat_delay']
304 def add_action(self
, action_name
, **arguments
):
305 self
.actions
.append(Action(action_name
, self
, **arguments
))
307 def list_actions(self
, last_action_finished
=False):
308 not_running
= (not self
.is_loaded_running())
309 current_action_seen
= False
310 action_descriptions
= []
311 for action
in self
.actions
:
314 elif last_action_finished
:
316 elif current_action_seen
:
318 elif action
== self
.current_action
:
319 current_action_seen
= True
323 action_descriptions
.append([action
.description(), state
])
324 self
.parent
.parent
.ids
['ActionList'].update_list(