]>
git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blob - helpers/key.py
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 . import debug_print
9 from transitions
.extensions
import HierarchicalMachine
as Machine
11 class Key(ButtonBehavior
, Widget
):
20 'children': ['no_config', 'no_actions', 'running']
26 'trigger': 'configure',
32 'source': 'configuring',
34 'after': 'key_loaded_callback'
38 'source': 'configuring',
43 'trigger': 'no_config',
44 'source': 'configuring',
45 'dest': 'loaded_no_config',
46 'after': 'key_loaded_callback'
50 'source': 'configured',
57 'after': 'key_loaded_callback'
63 'after': 'key_loaded_callback'
66 'trigger': 'no_actions',
68 'dest': 'loaded_no_actions',
69 'after': 'key_loaded_callback'
73 'source': ['loaded','failed'],
74 'dest': 'configuring',
75 'after': 'key_loaded_callback'
80 'dest': 'loaded_running',
81 'after': ['run_actions', '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']
88 'source': 'loaded_running',
93 key_sym
= StringProperty(None)
94 custom_color
= ListProperty([0, 1, 0])
95 description_title
= StringProperty("")
96 description
= ListProperty([])
97 state
= StringProperty("")
99 def get_alias_line_color(self
):
100 if self
.is_loaded_running():
103 return [120/255, 120/255, 120/255, 1]
105 def set_alias_line_color(self
):
108 line_color
= AliasProperty(get_alias_line_color
, set_alias_line_color
,
111 def get_alias_color(self
):
112 if self
.is_loaded_inactive():
114 elif self
.is_loaded(allow_substates
=True):
115 return [*self
.custom_color
, 1]
116 elif self
.is_failed():
119 return [*self
.custom_color
, 100/255]
120 def set_alias_color(self
):
123 color
= AliasProperty(get_alias_color
, set_alias_color
,
124 bind
=['state', 'custom_color'])
126 def __init__(self
, **kwargs
):
128 self
.current_action
= None
130 Machine(model
=self
, states
=self
.STATES
,
131 transitions
=self
.TRANSITIONS
, initial
='initial',
132 ignore_invalid_triggers
=True, queued
=True)
133 super(Key
, self
).__init
__(**kwargs
)
136 def on_key_sym(self
, key
, key_sym
):
143 # Machine states / events
144 def is_loaded_or_failed(self
):
145 return self
.is_loaded(allow_substates
=True) or self
.is_failed()
147 def is_loaded_inactive(self
):
148 return self
.is_loaded_no_config() or self
.is_loaded_no_actions()
150 def on_enter_configuring(self
):
151 if self
.key_sym
in self
.parent
.key_config
:
152 self
.config
= self
.parent
.key_config
[self
.key_sym
]
155 for key_action
in self
.config
['actions']:
156 self
.add_action(key_action
[0], **key_action
[1])
158 if 'description' in self
.config
['properties']:
159 self
.set_description(self
.config
['properties']['description'])
160 if 'color' in self
.config
['properties']:
161 self
.set_color(self
.config
['properties']['color'])
166 def on_enter_loading(self
):
167 if len(self
.actions
) > 0:
168 for action
in self
.actions
:
173 def run_actions(self
, modifiers
):
174 self
.parent
.parent
.ids
['KeyList'].append(self
.key_sym
)
175 debug_print("running actions for {}".format(self
.key_sym
))
176 start_time
= time
.time()
177 self
.parent
.start_running(self
, start_time
)
178 for self
.current_action
in self
.actions
:
179 if self
.parent
.keep_running(self
, start_time
):
181 self
.current_action
.run()
182 self
.list_actions(last_action_finished
=True)
184 self
.parent
.finished_running(self
, start_time
)
186 # This one cannot be in the Machine state since it would be queued to run
187 # *after* the loop is ended...
189 self
.current_action
.interrupt()
192 def key_loaded_callback(self
):
193 self
.parent
.key_loaded_callback()
195 def callback_action_ready(self
, action
, success
):
198 elif all(action
.is_loaded_or_failed() for action
in self
.actions
):
202 def set_description(self
, description
):
203 if description
[0] is not None:
204 self
.description_title
= str(description
[0])
205 self
.description
= []
206 for desc
in description
[1 :]:
208 self
.description
.append("")
210 self
.description
.append(str(desc
).replace(" ", " "))
212 def set_color(self
, color
):
213 color
= [x
/ 255 for x
in color
]
214 self
.custom_color
= color
217 def add_action(self
, action_name
, **arguments
):
218 self
.actions
.append(Action(action_name
, self
, **arguments
))
220 def list_actions(self
, last_action_finished
=False):
221 not_running
= (not self
.is_loaded_running())
222 current_action_seen
= False
223 action_descriptions
= []
224 for action
in self
.actions
:
227 elif last_action_finished
:
229 elif current_action_seen
:
231 elif action
== self
.current_action
:
232 current_action_seen
= True
236 action_descriptions
.append([action
.description(), state
])
237 self
.parent
.parent
.ids
['ActionList'].update_list(