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 Key(ButtonBehavior
, 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 key_sym
= StringProperty(None)
105 custom_color
= ListProperty([0, 1, 0])
106 description_title
= StringProperty("")
107 description
= ListProperty([])
108 state
= StringProperty("")
110 def get_alias_line_cross_color(self
):
111 if not self
.is_failed() and (
112 not self
.is_loaded(allow_substates
=True)\
113 or self
.is_loaded_running()\
114 or self
.is_loaded_protecting_repeat()):
115 return [120/255, 120/255, 120/255, 1]
119 def set_alias_line_cross_color(self
):
122 line_cross_color
= AliasProperty(
123 get_alias_line_cross_color
,
124 set_alias_line_cross_color
,
127 def get_alias_line_color(self
):
128 if self
.is_loaded_running():
131 return [120/255, 120/255, 120/255, 1]
133 def set_alias_line_color(self
):
136 line_color
= AliasProperty(get_alias_line_color
, set_alias_line_color
,
139 def get_alias_color(self
):
140 if self
.is_loaded_inactive():
142 elif self
.is_loaded_protecting_repeat():
143 return [*self
.custom_color
, 100/255]
144 elif self
.is_loaded_running():
145 return [*self
.custom_color
, 100/255]
146 elif self
.is_loaded(allow_substates
=True):
147 return [*self
.custom_color
, 1]
148 elif self
.is_failed():
151 return [*self
.custom_color
, 100/255]
152 def set_alias_color(self
):
155 color
= AliasProperty(get_alias_color
, set_alias_color
,
156 bind
=['state', 'custom_color'])
158 def __init__(self
, **kwargs
):
160 self
.current_action
= None
162 Machine(model
=self
, states
=self
.STATES
,
163 transitions
=self
.TRANSITIONS
, initial
='initial',
164 ignore_invalid_triggers
=True, queued
=True)
165 super(Key
, self
).__init
__(**kwargs
)
168 def on_key_sym(self
, key
, key_sym
):
175 # Machine states / events
176 def is_loaded_or_failed(self
):
177 return self
.is_loaded(allow_substates
=True) or self
.is_failed()
179 def is_loaded_inactive(self
):
180 return self
.is_loaded_no_config() or self
.is_loaded_no_actions()
182 def on_enter_configuring(self
):
183 if self
.key_sym
in self
.parent
.key_config
:
184 self
.config
= self
.parent
.key_config
[self
.key_sym
]
187 for key_action
in self
.config
['actions']:
188 self
.add_action(key_action
[0], **key_action
[1])
190 if 'description' in self
.config
['properties']:
191 self
.set_description(self
.config
['properties']['description'])
193 self
.unset_description()
194 if 'color' in self
.config
['properties']:
195 self
.set_color(self
.config
['properties']['color'])
202 def on_enter_loading(self
):
203 if len(self
.actions
) > 0:
204 for action
in self
.actions
:
209 def run_actions(self
, modifiers
):
210 self
.parent
.parent
.ids
['KeyList'].append(self
.key_sym
)
211 debug_print("running actions for {}".format(self
.key_sym
))
212 start_time
= time
.time()
213 self
.parent
.start_running(self
, start_time
)
214 for self
.current_action
in self
.actions
:
215 if self
.parent
.keep_running(self
, start_time
):
217 self
.current_action
.run(start_time
)
218 self
.list_actions(last_action_finished
=True)
220 self
.parent
.finished_running(self
, start_time
)
222 def on_enter_loaded_protecting_repeat(self
, modifiers
):
223 if 'repeat_delay' in self
.config
['properties']:
224 self
.protecting_repeat_timer
= threading
.Timer(
225 self
.config
['properties']['repeat_delay'],
226 self
.repeat_protection_finished
)
227 self
.protecting_repeat_timer
.start()
229 self
.repeat_protection_finished()
231 # This one cannot be in the Machine state since it would be queued to run
232 # *after* the loop is ended...
234 self
.current_action
.interrupt()
237 def key_loaded_callback(self
):
238 self
.parent
.key_loaded_callback()
240 def callback_action_ready(self
, action
, success
):
243 elif all(action
.is_loaded_or_failed() for action
in self
.actions
):
247 def set_description(self
, description
):
248 if description
[0] is not None:
249 self
.description_title
= str(description
[0])
250 self
.description
= []
251 for desc
in description
[1 :]:
253 self
.description
.append("")
255 self
.description
.append(str(desc
).replace(" ", " "))
257 def unset_description(self
):
258 self
.description_title
= ""
259 self
.description
= []
261 def set_color(self
, color
):
262 color
= [x
/ 255 for x
in color
]
263 self
.custom_color
= color
265 def unset_color(self
):
266 self
.custom_color
= [0, 1, 0]
269 def add_action(self
, action_name
, **arguments
):
270 self
.actions
.append(Action(action_name
, self
, **arguments
))
272 def list_actions(self
, last_action_finished
=False):
273 not_running
= (not self
.is_loaded_running())
274 current_action_seen
= False
275 action_descriptions
= []
276 for action
in self
.actions
:
279 elif last_action_finished
:
281 elif current_action_seen
:
283 elif action
== self
.current_action
:
284 current_action_seen
= True
288 action_descriptions
.append([action
.description(), state
])
289 self
.parent
.parent
.ids
['ActionList'].update_list(