]>
Commit | Line | Data |
---|---|---|
4b2d79ca | 1 | from kivy.uix.relativelayout import RelativeLayout |
30d8796f | 2 | from kivy.properties import NumericProperty, ListProperty |
4b2d79ca | 3 | from kivy.core.window import Window |
30d8796f | 4 | from kivy.clock import Clock |
4b2d79ca | 5 | |
be27763f | 6 | import threading |
4b2d79ca | 7 | import yaml |
b68b4e8f | 8 | import sys |
4b2d79ca | 9 | |
4b2d79ca | 10 | from .music_file import * |
22514f3a | 11 | from .mixer import Mixer |
6c44b231 | 12 | from . import Config, gain, error_print |
a8340c5d | 13 | from .music_effect import GainEffect |
3aaddc9d | 14 | from .action import Action |
4b2d79ca IB |
15 | |
16 | class Mapping(RelativeLayout): | |
17 | expected_keys = NumericProperty(0) | |
1b4b78f5 | 18 | master_volume = NumericProperty(100) |
30d8796f | 19 | ready_color = ListProperty([1, 165/255, 0, 1]) |
4b2d79ca IB |
20 | |
21 | def __init__(self, **kwargs): | |
d6290f14 | 22 | if Config.builtin_mixing: |
af27d782 | 23 | self.mixer = Mixer() |
d6290f14 IB |
24 | else: |
25 | self.mixer = None | |
9c4f705f IB |
26 | |
27 | try: | |
28 | self.key_config, self.open_files = self.parse_config() | |
29 | except Exception as e: | |
30 | error_print("Error while loading configuration: {}".format(e)) | |
31 | sys.exit() | |
32 | ||
4b2d79ca IB |
33 | super(Mapping, self).__init__(**kwargs) |
34 | self._keyboard = Window.request_keyboard(self._keyboard_closed, self) | |
35 | self._keyboard.bind(on_key_down=self._on_keyboard_down) | |
be27763f | 36 | self.running = [] |
3aaddc9d | 37 | self.wait_ids = {} |
30d8796f | 38 | Clock.schedule_interval(self.not_all_keys_ready, 1) |
be27763f | 39 | |
1b4b78f5 IB |
40 | @property |
41 | def master_gain(self): | |
42 | return gain(self.master_volume) | |
43 | ||
a8340c5d | 44 | def set_master_volume(self, value, delta=False, fade=0): |
2e404903 IB |
45 | [db_gain, self.master_volume] = gain( |
46 | value + int(delta) * self.master_volume, | |
47 | self.master_volume) | |
48 | ||
1b4b78f5 | 49 | for music in self.open_files.values(): |
51322669 IB |
50 | if not (music.is_loaded_playing() or music.is_loaded_paused()): |
51 | continue | |
52 | ||
a8340c5d IB |
53 | if fade > 0: |
54 | music.gain_effects.append(GainEffect( | |
55 | "fade", | |
56 | music.current_audio_segment, | |
57 | music.current_loop, | |
58 | music.sound_position, | |
59 | music.sound_position + fade, | |
60 | gain=db_gain)) | |
61 | else: | |
62 | music.set_gain(db_gain) | |
1b4b78f5 | 63 | |
3aaddc9d IB |
64 | def add_wait_id(self, wait_id, action_or_wait): |
65 | self.wait_ids[wait_id] = action_or_wait | |
66 | ||
67 | def interrupt_wait(self, wait_id): | |
68 | if wait_id in self.wait_ids: | |
69 | action_or_wait = self.wait_ids[wait_id] | |
70 | del(self.wait_ids[wait_id]) | |
71 | if isinstance(action_or_wait, Action): | |
72 | action_or_wait.interrupt() | |
73 | else: | |
74 | action_or_wait.set() | |
75 | ||
4b2d79ca IB |
76 | def _keyboard_closed(self): |
77 | self._keyboard.unbind(on_key_down=self._on_keyboard_down) | |
78 | self._keyboard = None | |
79 | ||
80 | def _on_keyboard_down(self, keyboard, keycode, text, modifiers): | |
81 | key = self.find_by_key_code(keycode) | |
b68b4e8f | 82 | if len(modifiers) == 0 and key is not None: |
2e404903 | 83 | threading.Thread(name="MSKeyAction", target=key.do_actions).start() |
b68b4e8f IB |
84 | elif 'ctrl' in modifiers and (keycode[0] == 113 or keycode[0] == '99'): |
85 | for thread in threading.enumerate(): | |
86 | if thread.getName()[0:2] != "MS": | |
87 | continue | |
88 | thread.join() | |
89 | ||
b68b4e8f | 90 | sys.exit() |
4b2d79ca IB |
91 | return True |
92 | ||
93 | def find_by_key_code(self, key_code): | |
94 | if "Key_" + str(key_code[0]) in self.ids: | |
95 | return self.ids["Key_" + str(key_code[0])] | |
be27763f IB |
96 | return None |
97 | ||
30d8796f IB |
98 | def not_all_keys_ready(self, dt): |
99 | for key in self.children: | |
100 | if not type(key).__name__ == "Key": | |
101 | continue | |
102 | if not key.is_key_ready: | |
103 | return True | |
104 | self.ready_color = [0, 1, 0, 1] | |
105 | return False | |
106 | ||
be27763f | 107 | def stop_all_running(self): |
0deb82a5 | 108 | running = self.running |
be27763f | 109 | self.running = [] |
0deb82a5 IB |
110 | for (key, start_time) in running: |
111 | key.interrupt_action() | |
be27763f IB |
112 | |
113 | def start_running(self, key, start_time): | |
114 | self.running.append((key, start_time)) | |
115 | ||
116 | def keep_running(self, key, start_time): | |
117 | return (key, start_time) in self.running | |
118 | ||
119 | def finished_running(self, key, start_time): | |
120 | if (key, start_time) in self.running: | |
121 | self.running.remove((key, start_time)) | |
122 | ||
4b2d79ca | 123 | def parse_config(self): |
75d6cdba | 124 | stream = open(Config.yml_file, "r") |
6c44b231 IB |
125 | try: |
126 | config = yaml.load(stream) | |
aee1334c | 127 | except Exception as e: |
6c44b231 IB |
128 | error_print("Error while loading config file: {}".format(e)) |
129 | sys.exit() | |
4b2d79ca IB |
130 | stream.close() |
131 | ||
132 | aliases = config['aliases'] | |
133 | seen_files = {} | |
134 | ||
4b2d79ca IB |
135 | key_properties = {} |
136 | ||
137 | for key in config['key_properties']: | |
138 | if key not in key_properties: | |
e5edd8b9 IB |
139 | key_prop = config['key_properties'][key] |
140 | if 'include' in key_prop: | |
141 | included = key_prop['include'] | |
142 | del(key_prop['include']) | |
143 | ||
144 | if isinstance(included, str): | |
145 | key_prop.update(aliases[included], **key_prop) | |
146 | else: | |
147 | for included_ in included: | |
148 | key_prop.update(aliases[included_], **key_prop) | |
149 | ||
4b2d79ca IB |
150 | key_properties[key] = { |
151 | "actions": [], | |
e5edd8b9 | 152 | "properties": key_prop, |
4b2d79ca IB |
153 | "files": [] |
154 | } | |
155 | ||
156 | for mapped_key in config['keys']: | |
157 | if mapped_key not in key_properties: | |
158 | key_properties[mapped_key] = { | |
159 | "actions": [], | |
160 | "properties": {}, | |
161 | "files": [] | |
162 | } | |
163 | for action in config['keys'][mapped_key]: | |
164 | action_name = list(action)[0] | |
165 | action_args = {} | |
166 | if action[action_name] is None: | |
167 | action[action_name] = [] | |
168 | ||
169 | if 'include' in action[action_name]: | |
170 | included = action[action_name]['include'] | |
171 | del(action[action_name]['include']) | |
172 | ||
173 | if isinstance(included, str): | |
2e404903 IB |
174 | action[action_name].update( |
175 | aliases[included], | |
176 | **action[action_name]) | |
4b2d79ca IB |
177 | else: |
178 | for included_ in included: | |
2e404903 IB |
179 | action[action_name].update( |
180 | aliases[included_], | |
181 | **action[action_name]) | |
4b2d79ca IB |
182 | |
183 | for argument in action[action_name]: | |
184 | if argument == 'file': | |
185 | filename = action[action_name]['file'] | |
186 | if filename not in seen_files: | |
187 | if filename in config['music_properties']: | |
188 | seen_files[filename] = MusicFile( | |
189 | filename, | |
1b4b78f5 | 190 | self, |
4b2d79ca IB |
191 | **config['music_properties'][filename]) |
192 | else: | |
193 | seen_files[filename] = MusicFile( | |
1b4b78f5 | 194 | self, |
29597680 | 195 | filename) |
4b2d79ca IB |
196 | |
197 | if filename not in key_properties[mapped_key]['files']: | |
2e404903 IB |
198 | key_properties[mapped_key]['files'] \ |
199 | .append(seen_files[filename]) | |
4b2d79ca IB |
200 | |
201 | action_args['music'] = seen_files[filename] | |
202 | ||
203 | else: | |
204 | action_args[argument] = action[action_name][argument] | |
205 | ||
2e404903 IB |
206 | key_properties[mapped_key]['actions'] \ |
207 | .append([action_name, action_args]) | |
4b2d79ca | 208 | |
29597680 | 209 | return (key_properties, seen_files) |
4b2d79ca IB |
210 | |
211 |