]>
Commit | Line | Data |
---|---|---|
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 | 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) | |
13 | mapped_unready_inner_color = ( 0, 255, 0, 100) | |
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 | ||
21 | self.top = top | |
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) | |
31 | self.disabled = disabled | |
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 = [] | |
42 | self.description = [] | |
43 | self.custom_color = self.mapped_inner_color | |
44 | self.custom_unready_color = self.mapped_unready_inner_color | |
45 | ||
46 | def square(self, all_actions_ready): | |
47 | if self.has_actions(): | |
48 | if all_actions_ready: | |
49 | self.inner_color = self.custom_color | |
50 | else: | |
51 | self.inner_color = self.custom_unready_color | |
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 | ||
62 | def set_description(self, description): | |
63 | for desc in description: | |
64 | if desc is None: | |
65 | self.description.append("") | |
66 | else: | |
67 | self.description.append(str(desc)) | |
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 | ||
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 | ||
80 | police = font(14) | |
81 | text_police = font(10) | |
82 | ||
83 | police.set_bold(True) | |
84 | text = police.render(self.key_sym, True, (0,0,0)) | |
85 | self.surface.blit(text, (5,5)) | |
86 | ||
87 | is_first_line = True | |
88 | offset = 11 + text_police.get_linesize() - 4 | |
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: | |
96 | self.surface.blit(text, (3, offset)) | |
97 | offset += text_police.get_linesize() - 4 | |
98 | ||
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 | ||
131 | def list_actions(self, screen): | |
132 | action_descriptions = [action.description() for action in self.actions] | |
133 | #print("actions linked to key {}:".format(self.key_sym)) | |
134 | #print("\t" + "\n\t".join(action_descriptions)) | |
135 | self.draw_lock.acquire() | |
136 | surface = pygame.Surface((690, 250)).convert() | |
137 | surface.fill((250, 250, 250)) | |
138 | police = font(14) | |
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 | ||
152 | screen.blit(surface, (5, 308)) | |
153 | pygame.display.flip() | |
154 | self.draw_lock.release() | |
155 | ||
156 |