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 # All drawing operations should happen in the main thread
13 # https://github.com/kivy/kivy/wiki/Working-with-Python-threads-inside-a-Kivy-application
14 from kivy
.clock
import mainthread
16 class KeyMachine(Widget
):
36 'trigger': 'configure',
42 'source': 'configuring',
44 'after': 'key_loaded_callback'
48 'source': 'configuring',
53 'trigger': 'no_config',
54 'source': 'configuring',
55 'dest': 'loaded_no_config',
56 'after': 'key_loaded_callback'
60 'source': 'configured',
67 'after': 'key_loaded_callback'
73 'after': 'key_loaded_callback'
76 'trigger': 'no_actions',
78 'dest': 'loaded_no_actions',
79 'after': 'key_loaded_callback'
83 'source': ['loaded','failed'],
84 'dest': 'configuring',
85 'after': 'key_loaded_callback'
90 'dest': 'loaded_running',
91 'after': ['run_actions', 'finish'],
92 # if a child, like loaded_no_actions, has no transitions, then it
93 # is bubbled to the parent, and we don't want that.
94 'conditions': ['is_loaded']
98 'source': 'loaded_running',
99 'dest': 'loaded_protecting_repeat'
102 'trigger': 'repeat_protection_finished',
103 'source': 'loaded_protecting_repeat',
108 state
= StringProperty("")
110 def __init__(self
, key
, **kwargs
):
113 Machine(model
=self
, states
=self
.STATES
,
114 transitions
=self
.TRANSITIONS
, initial
='initial',
115 ignore_invalid_triggers
=True, queued
=True)
116 super(KeyMachine
, self
).__init
__(**kwargs
)
118 # Machine states / events
119 def is_loaded_or_failed(self
):
120 return self
.is_loaded(allow_substates
=True) or self
.is_failed()
122 def is_loaded_inactive(self
):
123 return self
.is_loaded_no_config() or self
.is_loaded_no_actions()
126 def on_enter_configuring(self
):
127 self
.destroy_actions()
128 self
.key
.unset_description()
129 self
.key
.unset_color()
131 if self
.key
.key_sym
in self
.key
.parent
.key_config
:
132 self
.key
.config
= self
.key
.parent
.key_config
[self
.key
.key_sym
]
134 for key_action
in self
.key
.config
['actions']:
135 self
.key
.add_action(key_action
[0], **key_action
[1])
137 if 'description' in self
.key
.config
['properties']:
138 self
.key
.set_description(self
.key
.config
['properties']['description'])
139 if 'color' in self
.key
.config
['properties']:
140 self
.key
.set_color(self
.key
.config
['properties']['color'])
145 def on_enter_loading(self
):
146 if len(self
.key
.actions
) > 0:
147 for action
in self
.key
.actions
:
152 def destroy_actions(self
):
153 for action
in self
.key
.actions
:
155 self
.key
.actions
= []
157 def run_actions(self
, modifiers
):
158 self
.key
.parent
.parent
.ids
['KeyList'].append(self
.key
.key_sym
)
159 debug_print("running actions for {}".format(self
.key
.key_sym
))
160 start_time
= time
.time()
161 self
.key
.parent
.start_running(self
.key
, start_time
)
162 for self
.key
.current_action
in self
.key
.actions
:
163 if self
.key
.parent
.keep_running(self
.key
, start_time
):
164 self
.key
.list_actions()
165 self
.key
.current_action
.run(start_time
)
166 self
.key
.list_actions(last_action_finished
=True)
168 self
.key
.parent
.finished_running(self
.key
, start_time
)
170 def on_enter_loaded_protecting_repeat(self
, modifiers
):
171 if self
.key
.repeat_delay
> 0:
172 self
.key
.protecting_repeat_timer
= threading
.Timer(
173 self
.key
.repeat_delay
,
174 self
.key
.repeat_protection_finished
)
175 self
.key
.protecting_repeat_timer
.start()
177 self
.key
.repeat_protection_finished()
181 def key_loaded_callback(self
):
182 self
.key
.parent
.key_loaded_callback()
185 class Key(ButtonBehavior
, Widget
):
187 key_sym
= StringProperty(None)
188 custom_color
= ListProperty([0, 1, 0])
189 description_title
= StringProperty("")
190 description
= ListProperty([])
191 machine_state
= StringProperty("")
193 def get_alias_line_cross_color(self
):
194 if not self
.is_failed() and (
195 not self
.is_loaded(allow_substates
=True)\
196 or self
.is_loaded_running()\
197 or self
.is_loaded_protecting_repeat()):
198 return [120/255, 120/255, 120/255, 1]
202 def set_alias_line_cross_color(self
):
205 line_cross_color
= AliasProperty(
206 get_alias_line_cross_color
,
207 set_alias_line_cross_color
,
208 bind
=['machine_state'])
210 def get_alias_line_color(self
):
211 if self
.is_loaded_running():
214 return [120/255, 120/255, 120/255, 1]
216 def set_alias_line_color(self
):
219 line_color
= AliasProperty(get_alias_line_color
, set_alias_line_color
,
220 bind
=['machine_state'])
222 def get_alias_color(self
):
223 if self
.is_loaded_inactive():
225 elif self
.is_loaded_protecting_repeat():
226 return [*self
.custom_color
, 100/255]
227 elif self
.is_loaded_running():
228 return [*self
.custom_color
, 100/255]
229 elif self
.is_loaded(allow_substates
=True):
230 return [*self
.custom_color
, 1]
231 elif self
.is_failed():
234 return [*self
.custom_color
, 100/255]
235 def set_alias_color(self
):
238 color
= AliasProperty(get_alias_color
, set_alias_color
,
239 bind
=['machine_state', 'custom_color'])
241 def __getattr__(self
, name
):
242 if hasattr(self
.machine
, name
):
243 return getattr(self
.machine
, name
)
247 def machine_state_changed(self
, instance
, machine_state
):
248 self
.machine_state
= self
.machine
.state
250 def __init__(self
, **kwargs
):
252 self
.current_action
= None
253 self
.machine
= KeyMachine(self
)
254 self
.machine
.bind(state
=self
.machine_state_changed
)
256 super(Key
, self
).__init
__(**kwargs
)
260 def update_state(self
, value
):
261 self
.machine_state
= value
263 def on_key_sym(self
, key
, key_sym
):
270 # This one cannot be in the Machine state since it would be queued to run
271 # *after* the loop is ended...
273 self
.current_action
.interrupt()
276 def callback_action_ready(self
, action
, success
):
279 elif all(action
.is_loaded_or_failed() for action
in self
.actions
):
283 def set_description(self
, description
):
284 if description
[0] is not None:
285 self
.description_title
= str(description
[0])
286 self
.description
= []
287 for desc
in description
[1 :]:
289 self
.description
.append("")
291 self
.description
.append(str(desc
).replace(" ", " "))
293 def unset_description(self
):
294 self
.description_title
= ""
295 self
.description
= []
297 def set_color(self
, color
):
298 color
= [x
/ 255 for x
in color
]
299 self
.custom_color
= color
301 def unset_color(self
):
302 self
.custom_color
= [0, 1, 0]
306 def repeat_delay(self
):
307 if hasattr(self
, 'config') and\
308 'repeat_delay' in self
.config
['properties']:
309 return self
.config
['properties']['repeat_delay']
314 def add_action(self
, action_name
, **arguments
):
315 self
.actions
.append(Action(action_name
, self
, **arguments
))
317 def list_actions(self
, last_action_finished
=False):
318 not_running
= (not self
.is_loaded_running())
319 current_action_seen
= False
320 action_descriptions
= []
321 for action
in self
.actions
:
324 elif last_action_finished
:
326 elif current_action_seen
:
328 elif action
== self
.current_action
:
329 current_action_seen
= True
333 action_descriptions
.append([action
.description(), state
])
334 self
.parent
.parent
.ids
['ActionList'].update_list(