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
):
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
):
129 self
.mapping
.finished_running(self
, start_time
)
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))
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()
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()
152 screen
.blit(surface
, (5, 308))
153 pygame
.display
.flip()
154 self
.draw_lock
.release()