]>
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
, ListProperty
, StringProperty
3 from kivy
.clock
import Clock
4 from kivy
.uix
.behaviors
import ButtonBehavior
7 from . import debug_print
10 class Key(ButtonBehavior
, Widget
):
11 key_sym
= StringProperty(None)
12 custom_color
= ListProperty([0, 1, 0, 1])
13 custom_unready_color
= ListProperty([0, 1, 0, 100/255])
14 description_title
= StringProperty("")
15 description
= ListProperty([])
16 is_key_ready
= BooleanProperty(True)
19 if not self
.has_actions
:
21 elif self
.all_actions_ready
:
22 return self
.custom_color
24 return self
.custom_unready_color
28 color
= AliasProperty(get_color
, set_color
, bind
=['is_key_ready'])
30 def __init__(self
, **kwargs
):
31 super(Key
, self
).__init
__(**kwargs
)
34 def on_key_sym(self
, key
, key_sym
):
35 if key_sym
in self
.parent
.key_config
:
36 self
.is_key_ready
= False
38 self
.config
= self
.parent
.key_config
[key_sym
]
41 for key_action
in self
.config
['actions']:
42 self
.add_action(key_action
[0], **key_action
[1])
44 if 'description' in self
.config
['properties']:
45 key
.set_description(self
.config
['properties']['description'])
46 if 'color' in self
.config
['properties']:
47 key
.set_color(self
.config
['properties']['color'])
49 Clock
.schedule_interval(self
.check_all_active
, 1)
51 def check_all_active(self
, dt
):
52 if self
.all_actions_ready
:
53 self
.is_key_ready
= True
56 def set_description(self
, description
):
57 if description
[0] is not None:
58 self
.description_title
= str(description
[0])
59 for desc
in description
[1:]:
61 self
.description
.append("")
63 self
.description
.append(str(desc
).replace(" ", " "))
65 def set_color(self
, color
):
66 color
= [x
/ 255 for x
in color
]
68 self
.custom_color
= color
70 self
.custom_unready_color
= tuple(color
)
73 def has_actions(self
):
74 return len(self
.actions
) > 0
77 def all_actions_ready(self
):
78 return all(action
.ready() for action
in self
.actions
)
80 def add_action(self
, action_name
, **arguments
):
81 self
.actions
.append(Action(action_name
, self
, **arguments
))
83 def interrupt_action(self
):
84 self
.current_action
.interrupt()
90 self
.parent
.parent
.ids
['KeyList'].append(self
.key_sym
)
91 debug_print("running actions for {}".format(self
.key_sym
))
92 start_time
= time
.time()
93 self
.parent
.start_running(self
, start_time
)
95 for self
.current_action
in self
.actions
:
96 if self
.parent
.keep_running(self
, start_time
):
97 self
.list_actions(action_number
= action_number
+ 0.5)
98 self
.current_action
.run()
100 self
.list_actions(action_number
= action_number
)
102 self
.parent
.finished_running(self
, start_time
)
104 def list_actions(self
, action_number
= 0):
105 self
.parent
.parent
.ids
['ActionList'].update_list(self
, action_number
)