]>
Commit | Line | Data |
---|---|---|
be27763f IB |
1 | from .rounded_rect import * |
2 | from .action import * | |
956ce6fd | 3 | from .font import font |
be27763f IB |
4 | import time |
5 | import sys | |
6 | import pygame | |
7 | ||
8 | class 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 | ||
121 | def do_actions(self): | |
122 | print("running actions for {}".format(self.key_sym)) | |
123 | start_time = time.time() | |
124 | self.mapping.start_running(self, start_time) | |
125 | for action in self.actions: | |
126 | if self.mapping.keep_running(self, start_time): | |
127 | action.run() | |
128 | ||
129 | self.mapping.finished_running(self, start_time) | |
130 | ||
ba9ea93a IB |
131 | def list_actions(self, screen): |
132 | action_descriptions = [action.description() for action in self.actions] | |
b86db9f1 IB |
133 | #print("actions linked to key {}:".format(self.key_sym)) |
134 | #print("\t" + "\n\t".join(action_descriptions)) | |
ba9ea93a | 135 | self.draw_lock.acquire() |
9de92b6d | 136 | surface = pygame.Surface((690, 250)).convert() |
ba9ea93a | 137 | surface.fill((250, 250, 250)) |
956ce6fd | 138 | police = font(14) |
ba9ea93a IB |
139 | |
140 | offset = 0 | |
141 | police.set_bold(True) | |
142 | text = police.render("actions linked to key {}:".format(self.key_sym), True, (0,0,0)) | |
143 | surface.blit(text, (0, offset)) | |
144 | offset += police.get_linesize() | |
145 | ||
146 | police.set_bold(False) | |
147 | for description in action_descriptions: | |
148 | text = police.render(description, True, (0,0,0)) | |
149 | surface.blit(text, (0, offset)) | |
150 | offset += police.get_linesize() | |
151 | ||
9de92b6d | 152 | screen.blit(surface, (5, 308)) |
ba9ea93a IB |
153 | pygame.display.flip() |
154 | self.draw_lock.release() | |
be27763f IB |
155 | |
156 |