]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blob - helpers/key.py
Cleanup fonts
[perso/Immae/Projets/Python/MusicSampler.git] / helpers / key.py
1 from .rounded_rect import *
2 from .action import *
3 from .font import font
4 import time
5 import sys
6 import pygame
7
8 class Key:
9 row_positions = {
10 'first': 0,
11 'second': 50,
12 'third': 100,
13 'fourth': 150,
14 'fifth': 200,
15 'sixth': 250,
16 }
17
18 default_outer_color = (120, 120, 120)
19 lighter_outer_color = (200, 200, 200)
20 default_inner_color = (255, 255, 255)
21 mapped_inner_color = ( 0, 255, 0)
22 mapped_unready_inner_color = ( 0, 255, 0, 100)
23
24 def __init__(self, mapping, draw_lock, key_name, key_sym, top, left, width = 48, height = 48, disabled = False):
25 self.draw_lock = draw_lock
26 self.mapping = mapping
27 self.key_name = key_name
28 self.key_sym = key_sym
29
30 if isinstance(top, str):
31 self.top = self.row_positions[top]
32 else:
33 self.top = top
34
35 self.left = left
36 self.width = width
37 self.height = height
38
39 self.bottom = self.top + self.height
40 self.right = self.left + self.width
41
42 self.rect = (self.left, self.top, self.right, self.bottom)
43 self.position = (self.left, self.top)
44
45 if disabled:
46 self.outer_color = self.lighter_outer_color
47 self.linewidth = 1
48 else:
49 self.outer_color = self.default_outer_color
50 self.linewidth = 3
51
52 self.inner_color = self.default_inner_color
53 self.actions = []
54 self.description = []
55 self.custom_color = None
56 self.custom_unready_color = None
57
58 def square(self, all_actions_ready):
59 if self.has_actions():
60 if all_actions_ready:
61 self.inner_color = self.custom_color or self.mapped_inner_color
62 else:
63 self.inner_color = self.custom_unready_color or self.mapped_unready_inner_color
64
65 return RoundedRect((0, 0, self.width, self.height),
66 self.outer_color, self.inner_color, self.linewidth)
67
68 def collidepoint(self, position):
69 return self.surface.get_rect().collidepoint(
70 position[0] - self.position[0],
71 position[1] - self.position[1]
72 )
73
74 def set_description(self, description):
75 for desc in description:
76 if desc is None:
77 self.description.append("")
78 else:
79 self.description.append(str(desc))
80
81 def set_color(self, color):
82 self.custom_color = tuple(color)
83 color.append(100)
84 self.custom_unready_color = tuple(color)
85
86 def draw(self, background_surface):
87 self.draw_lock.acquire()
88 all_actions_ready = self.all_actions_ready()
89
90 self.surface = self.square(all_actions_ready).surface()
91
92 police = font(14)
93 text_police = font(10)
94
95 police.set_bold(True)
96 text = police.render(self.key_sym, True, (0,0,0))
97 self.surface.blit(text, (5,5))
98
99 is_first_line = True
100 offset = 11 + text_police.get_linesize() - 4
101 first_line_offset = 18
102 for description in self.description:
103 text = text_police.render(description, True, (0,0,0))
104 if is_first_line:
105 self.surface.blit(text, (first_line_offset, 9))
106 is_first_line = False
107 else:
108 self.surface.blit(text, (3, offset))
109 offset += text_police.get_linesize() - 4
110
111 background_surface.blit(self.surface, self.position)
112 self.draw_lock.release()
113
114 return not all_actions_ready
115
116 def poll_redraw(self, background):
117 while True:
118 time.sleep(1)
119 if self.all_actions_ready():
120 self.draw(background)
121 self.mapping.blit()
122 break
123
124 def has_actions(self):
125 return len(self.actions) > 0
126
127 def all_actions_ready(self):
128 return all(action.ready() for action in self.actions)
129
130 def add_action(self, action_name, **arguments):
131 self.actions.append(Action(action_name, self, **arguments))
132
133 def do_actions(self):
134 print("running actions for {}".format(self.key_sym))
135 start_time = time.time()
136 self.mapping.start_running(self, start_time)
137 for action in self.actions:
138 if self.mapping.keep_running(self, start_time):
139 action.run()
140
141 self.mapping.finished_running(self, start_time)
142
143 def list_actions(self, screen):
144 action_descriptions = [action.description() for action in self.actions]
145 #print("actions linked to key {}:".format(self.key_sym))
146 #print("\t" + "\n\t".join(action_descriptions))
147 self.draw_lock.acquire()
148 surface = pygame.Surface((690, 250)).convert()
149 surface.fill((250, 250, 250))
150 police = font(14)
151
152 offset = 0
153 police.set_bold(True)
154 text = police.render("actions linked to key {}:".format(self.key_sym), True, (0,0,0))
155 surface.blit(text, (0, offset))
156 offset += police.get_linesize()
157
158 police.set_bold(False)
159 for description in action_descriptions:
160 text = police.render(description, True, (0,0,0))
161 surface.blit(text, (0, offset))
162 offset += police.get_linesize()
163
164 screen.blit(surface, (5, 308))
165 pygame.display.flip()
166 self.draw_lock.release()
167
168