diff options
-rw-r--r-- | helpers/__init__.py | 8 | ||||
-rw-r--r-- | helpers/action.py | 5 | ||||
-rw-r--r-- | helpers/key.py | 31 | ||||
-rw-r--r-- | helpers/lock.py | 15 | ||||
-rw-r--r-- | music_sampler.py | 20 |
5 files changed, 58 insertions, 21 deletions
diff --git a/helpers/__init__.py b/helpers/__init__.py index 6935342..b1723ee 100644 --- a/helpers/__init__.py +++ b/helpers/__init__.py | |||
@@ -1,12 +1,10 @@ | |||
1 | # -*- coding: utf-8 -*- | 1 | # -*- coding: utf-8 -*- |
2 | import threading | ||
3 | from .music_file import * | 2 | from .music_file import * |
4 | from .mapping import * | 3 | from .mapping import * |
5 | 4 | from .lock import * | |
6 | draw_lock = threading.RLock() | 5 | import yaml |
7 | 6 | ||
8 | def parse_config(mapping): | 7 | def parse_config(mapping): |
9 | import yaml | ||
10 | stream = open("config.yml", "r") | 8 | stream = open("config.yml", "r") |
11 | config = yaml.load(stream) | 9 | config = yaml.load(stream) |
12 | stream.close() | 10 | stream.close() |
@@ -14,7 +12,7 @@ def parse_config(mapping): | |||
14 | aliases = config['aliases'] | 12 | aliases = config['aliases'] |
15 | seen_files = {} | 13 | seen_files = {} |
16 | 14 | ||
17 | file_lock = threading.RLock() | 15 | file_lock = Lock("file") |
18 | 16 | ||
19 | for mapped_key in config['keys']: | 17 | for mapped_key in config['keys']: |
20 | key = mapping.find_by_unicode(mapped_key) | 18 | key = mapping.find_by_unicode(mapped_key) |
diff --git a/helpers/action.py b/helpers/action.py index 8a69fae..97873a0 100644 --- a/helpers/action.py +++ b/helpers/action.py | |||
@@ -28,9 +28,12 @@ class Action: | |||
28 | return True | 28 | return True |
29 | 29 | ||
30 | def run(self): | 30 | def run(self): |
31 | print(getattr(self, self.action + "_print")(**self.arguments)) | 31 | print(self.description()) |
32 | return getattr(self, self.action)(**self.arguments) | 32 | return getattr(self, self.action)(**self.arguments) |
33 | 33 | ||
34 | def description(self): | ||
35 | return getattr(self, self.action + "_print")(**self.arguments) | ||
36 | |||
34 | def command(self, command = "", **kwargs): | 37 | def command(self, command = "", **kwargs): |
35 | pass | 38 | pass |
36 | 39 | ||
diff --git a/helpers/key.py b/helpers/key.py index 2e4a313..6997e70 100644 --- a/helpers/key.py +++ b/helpers/key.py | |||
@@ -112,9 +112,32 @@ class Key: | |||
112 | 112 | ||
113 | self.mapping.finished_running(self, start_time) | 113 | self.mapping.finished_running(self, start_time) |
114 | 114 | ||
115 | def list_actions(self, surface): | 115 | def list_actions(self, screen): |
116 | # FIXME: Todo | 116 | action_descriptions = [action.description() for action in self.actions] |
117 | print("bouh", self.key_sym) | 117 | print("actions linked to key {}:".format(self.key_sym)) |
118 | surface.fill((255, 0, 0)) | 118 | print("\t" + "\n\t".join(action_descriptions)) |
119 | self.draw_lock.acquire() | ||
120 | surface = pygame.Surface((800, 250)).convert() | ||
121 | surface.fill((250, 250, 250)) | ||
122 | if getattr(sys, 'frozen', False): | ||
123 | police = pygame.font.Font(sys._MEIPASS + "/Ubuntu-Regular.ttf", 14) | ||
124 | else: | ||
125 | police = pygame.font.Font("Ubuntu-Regular.ttf", 14) | ||
126 | |||
127 | offset = 0 | ||
128 | police.set_bold(True) | ||
129 | text = police.render("actions linked to key {}:".format(self.key_sym), True, (0,0,0)) | ||
130 | surface.blit(text, (0, offset)) | ||
131 | offset += police.get_linesize() | ||
132 | |||
133 | police.set_bold(False) | ||
134 | for description in action_descriptions: | ||
135 | text = police.render(description, True, (0,0,0)) | ||
136 | surface.blit(text, (0, offset)) | ||
137 | offset += police.get_linesize() | ||
138 | |||
139 | screen.blit(surface, (10, 330)) | ||
140 | pygame.display.flip() | ||
141 | self.draw_lock.release() | ||
119 | 142 | ||
120 | 143 | ||
diff --git a/helpers/lock.py b/helpers/lock.py new file mode 100644 index 0000000..dff8b1f --- /dev/null +++ b/helpers/lock.py | |||
@@ -0,0 +1,15 @@ | |||
1 | import threading | ||
2 | |||
3 | class Lock: | ||
4 | def __init__(self, lock_type): | ||
5 | self.type = lock_type | ||
6 | self.lock = threading.RLock() | ||
7 | |||
8 | def acquire(self, *args, **kwargs): | ||
9 | #print("acquiring lock for {}".format(self.type)) | ||
10 | self.lock.acquire(*args, **kwargs) | ||
11 | |||
12 | def release(self, *args, **kwargs): | ||
13 | #print("releasing lock for {}".format(self.type)) | ||
14 | self.lock.release(*args, **kwargs) | ||
15 | |||
diff --git a/music_sampler.py b/music_sampler.py index 9f14d56..fd03009 100644 --- a/music_sampler.py +++ b/music_sampler.py | |||
@@ -7,23 +7,19 @@ pygame.mixer.pre_init(frequency = 44100) | |||
7 | pygame.init() | 7 | pygame.init() |
8 | 8 | ||
9 | size = width, height = 1024, 600 | 9 | size = width, height = 1024, 600 |
10 | |||
11 | helpers.draw_lock.acquire() | ||
12 | screen = pygame.display.set_mode(size) | 10 | screen = pygame.display.set_mode(size) |
13 | mapping = helpers.Mapping(screen, helpers.draw_lock) | 11 | screen.fill((250, 250, 250)) |
12 | |||
13 | draw_lock = helpers.Lock("draw") | ||
14 | 14 | ||
15 | action_surface = pygame.Surface((600, 250)).convert() | 15 | mapping = helpers.Mapping(screen, draw_lock) |
16 | action_surface.fill((0,0,0)) | ||
17 | helpers.parse_config(mapping) | 16 | helpers.parse_config(mapping) |
18 | helpers.draw_lock.release() | ||
19 | 17 | ||
20 | mapping.draw() | 18 | mapping.draw() |
21 | 19 | ||
22 | helpers.draw_lock.acquire() | 20 | draw_lock.acquire() |
23 | screen.blit(action_surface, (10, 330)) | ||
24 | |||
25 | pygame.display.flip() | 21 | pygame.display.flip() |
26 | helpers.draw_lock.release() | 22 | draw_lock.release() |
27 | 23 | ||
28 | contexts = [ | 24 | contexts = [ |
29 | 'normal' | 25 | 'normal' |
@@ -54,7 +50,9 @@ while 1: | |||
54 | elif event.type == pygame.MOUSEBUTTONUP: | 50 | elif event.type == pygame.MOUSEBUTTONUP: |
55 | key = mapping.find_by_collidepoint(pygame.mouse.get_pos()) | 51 | key = mapping.find_by_collidepoint(pygame.mouse.get_pos()) |
56 | if key is not None: | 52 | if key is not None: |
57 | key.list_actions(action_surface) | 53 | threading.Thread(target=key.list_actions, args = [screen]).start() |
58 | 54 | ||
55 | draw_lock.acquire() | ||
59 | pygame.display.flip() | 56 | pygame.display.flip() |
57 | draw_lock.release() | ||
60 | 58 | ||