1 from .rounded_rect
import *
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)
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
26 self
.bottom
= self
.top
+ self
.height
27 self
.right
= self
.left
+ self
.width
29 self
.rect
= (self
.left
, self
.top
, self
.right
, self
.bottom
)
30 self
.position
= (self
.left
, self
.top
)
31 self
.disabled
= disabled
34 self
.outer_color
= self
.lighter_outer_color
37 self
.outer_color
= self
.default_outer_color
40 self
.inner_color
= self
.default_inner_color
43 self
.custom_color
= self
.mapped_inner_color
44 self
.custom_unready_color
= self
.mapped_unready_inner_color
46 def square(self
, all_actions_ready
):
47 if self
.has_actions():
49 self
.inner_color
= self
.custom_color
51 self
.inner_color
= self
.custom_unready_color
53 return RoundedRect((0, 0, self
.width
, self
.height
),
54 self
.outer_color
, self
.inner_color
, self
.linewidth
)
56 def collidepoint(self
, position
):
57 return self
.surface
.get_rect().collidepoint(
58 position
[0] - self
.position
[0],
59 position
[1] - self
.position
[1]
62 def set_description(self
, description
):
63 for desc
in description
:
65 self
.description
.append("")
67 self
.description
.append(str(desc
))
69 def set_color(self
, color
):
70 self
.custom_color
= tuple(color
)
72 self
.custom_unready_color
= tuple(color
)
74 def draw(self
, background_surface
):
75 self
.draw_lock
.acquire()
76 all_actions_ready
= self
.all_actions_ready()
78 self
.surface
= self
.square(all_actions_ready
).surface()
81 text_police
= font(10)
84 text
= police
.render(self
.key_sym
, True, (0,0,0))
85 self
.surface
.blit(text
, (5,5))
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))
93 self
.surface
.blit(text
, (first_line_offset
, 9))
96 self
.surface
.blit(text
, (3, offset
))
97 offset
+= text_police
.get_linesize() - 4
99 background_surface
.blit(self
.surface
, self
.position
)
100 self
.draw_lock
.release()
102 return not all_actions_ready
104 def poll_redraw(self
, background
):
107 if self
.all_actions_ready():
108 self
.draw(background
)
112 def has_actions(self
):
113 return len(self
.actions
) > 0
115 def all_actions_ready(self
):
116 return all(action
.ready() for action
in self
.actions
)
118 def add_action(self
, action_name
, **arguments
):
119 self
.actions
.append(Action(action_name
, self
, **arguments
))
121 def do_actions(self
, screen
):
122 print("running actions for {}".format(self
.key_sym
))
123 start_time
= time
.time()
124 self
.mapping
.start_running(self
, start_time
)
126 for action
in self
.actions
:
127 if self
.mapping
.keep_running(self
, start_time
):
128 self
.list_actions(screen
, action_number
= action_number
+ 0.5)
131 self
.list_actions(screen
, action_number
= action_number
)
133 self
.mapping
.finished_running(self
, start_time
)
135 def list_actions(self
, screen
, action_number
= 0):
136 action_descriptions
= [action
.description() for action
in self
.actions
]
137 #print("actions linked to key {}:".format(self.key_sym))
138 #print("\t" + "\n\t".join(action_descriptions))
139 self
.draw_lock
.acquire()
140 surface
= pygame
.Surface((690, 250)).convert()
141 surface
.fill((250, 250, 250))
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()
150 police
.set_bold(False)
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))
158 icon
= icon_police
.render(" ", True, (0,0,0))
160 text
= police
.render(description
, True, (0,0,0))
161 surface
.blit(icon
, (0, offset
))
162 surface
.blit(text
, (10, offset
))
163 offset
+= police
.get_linesize()
165 screen
.blit(surface
, (5, 308))
166 pygame
.display
.flip()
167 self
.draw_lock
.release()