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