]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blame - helpers/key.py
Move classes to separate file
[perso/Immae/Projets/Python/MusicSampler.git] / helpers / key.py
CommitLineData
be27763f
IB
1from .rounded_rect import *
2from .action import *
3import time
4import sys
5import pygame
6
7class Key:
8 row_positions = {
9 'first': 0,
10 'second': 50,
11 'third': 100,
12 'fourth': 150,
13 'fifth': 200,
14 'sixth': 250,
15 }
16
17 default_outer_color = (120, 120, 120)
18 lighter_outer_color = (200, 200, 200)
19 default_inner_color = (255, 255, 255)
20 mapped_inner_color = ( 0, 255, 0)
21 mapped_unready_inner_color = (255, 165, 0)
22
23 def __init__(self, mapping, draw_lock, key_name, key_sym, top, left, width = 48, height = 48, disabled = False):
24 self.draw_lock = draw_lock
25 self.mapping = mapping
26 self.key_name = key_name
27 self.key_sym = key_sym
28
29 if isinstance(top, str):
30 self.top = self.row_positions[top]
31 else:
32 self.top = top
33
34 self.left = left
35 self.width = width
36 self.height = height
37
38 self.bottom = self.top + self.height
39 self.right = self.left + self.width
40
41 self.rect = (self.left, self.top, self.right, self.bottom)
42 self.position = (self.left, self.top)
43
44 if disabled:
45 self.outer_color = self.lighter_outer_color
46 self.linewidth = 1
47 else:
48 self.outer_color = self.default_outer_color
49 self.linewidth = 3
50
51 self.inner_color = self.default_inner_color
52 self.actions = []
53
54 def square(self, all_actions_ready):
55 if self.has_actions():
56 if all_actions_ready:
57 self.inner_color = self.mapped_inner_color
58 else:
59 self.inner_color = self.mapped_unready_inner_color
60
61 return RoundedRect((0, 0, self.width, self.height),
62 self.outer_color, self.inner_color, self.linewidth)
63
64 def collidepoint(self, position):
65 return self.surface.get_rect().collidepoint(
66 position[0] - self.position[0],
67 position[1] - self.position[1]
68 )
69
70 def draw(self, background_surface):
71 self.draw_lock.acquire()
72 all_actions_ready = self.all_actions_ready()
73
74 self.surface = self.square(all_actions_ready).surface()
75
76 if getattr(sys, 'frozen', False):
77 police = pygame.font.Font(sys._MEIPASS + "/Ubuntu-Regular.ttf", 14)
78 else:
79 police = pygame.font.Font("Ubuntu-Regular.ttf", 14)
80
81 text = police.render(self.key_sym, True, (0,0,0))
82 self.surface.blit(text, (5,5))
83 background_surface.blit(self.surface, self.position)
84 self.draw_lock.release()
85
86 return not all_actions_ready
87
88 def poll_redraw(self, background):
89 while True:
90 time.sleep(1)
91 if self.all_actions_ready():
92 self.draw(background)
93 self.mapping.blit()
94 break
95
96 def has_actions(self):
97 return len(self.actions) > 0
98
99 def all_actions_ready(self):
100 return all(action.ready() for action in self.actions)
101
102 def add_action(self, action_name, **arguments):
103 self.actions.append(Action(action_name, self, **arguments))
104
105 def do_actions(self):
106 print("running actions for {}".format(self.key_sym))
107 start_time = time.time()
108 self.mapping.start_running(self, start_time)
109 for action in self.actions:
110 if self.mapping.keep_running(self, start_time):
111 action.run()
112
113 self.mapping.finished_running(self, start_time)
114
115 def list_actions(self, surface):
116 # FIXME: Todo
117 print("bouh", self.key_sym)
118 surface.fill((255, 0, 0))
119
120