]>
Commit | Line | Data |
---|---|---|
1 | from .rounded_rect import * | |
2 | from .action import * | |
3 | import time | |
4 | import sys | |
5 | import pygame | |
6 | ||
7 | class 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 = ( 0, 255, 0, 100) | |
22 | #mapped_unready_inner_color = (255, 165, 0) | |
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 | self.description = description | |
76 | ||
77 | def set_color(self, color): | |
78 | self.custom_color = tuple(color) | |
79 | color.append(100) | |
80 | self.custom_unready_color = tuple(color) | |
81 | ||
82 | def draw(self, background_surface): | |
83 | self.draw_lock.acquire() | |
84 | all_actions_ready = self.all_actions_ready() | |
85 | ||
86 | self.surface = self.square(all_actions_ready).surface() | |
87 | ||
88 | if getattr(sys, 'frozen', False): | |
89 | police = pygame.font.Font(sys._MEIPASS + "/Ubuntu-Regular.ttf", 14) | |
90 | text_police = pygame.font.Font(sys._MEIPASS + "/Ubuntu-Regular.ttf", 10) | |
91 | else: | |
92 | police = pygame.font.Font("Ubuntu-Regular.ttf", 14) | |
93 | text_police = pygame.font.Font("Ubuntu-Regular.ttf", 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() | |
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, (5, offset)) | |
109 | offset += text_police.get_linesize() | |
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((800, 250)).convert() | |
149 | surface.fill((250, 250, 250)) | |
150 | if getattr(sys, 'frozen', False): | |
151 | police = pygame.font.Font(sys._MEIPASS + "/Ubuntu-Regular.ttf", 14) | |
152 | else: | |
153 | police = pygame.font.Font("Ubuntu-Regular.ttf", 14) | |
154 | ||
155 | offset = 0 | |
156 | police.set_bold(True) | |
157 | text = police.render("actions linked to key {}:".format(self.key_sym), True, (0,0,0)) | |
158 | surface.blit(text, (0, offset)) | |
159 | offset += police.get_linesize() | |
160 | ||
161 | police.set_bold(False) | |
162 | for description in action_descriptions: | |
163 | text = police.render(description, True, (0,0,0)) | |
164 | surface.blit(text, (0, offset)) | |
165 | offset += police.get_linesize() | |
166 | ||
167 | screen.blit(surface, (10, 330)) | |
168 | pygame.display.flip() | |
169 | self.draw_lock.release() | |
170 | ||
171 |