1 from .rounded_rect
import *
18 default_outer_color
= (120, 120, 120)
19 lighter_outer_color
= (200, 200, 200)
20 default_inner_color
= (255, 255, 255)
21 mapped_inner_color
= ( 0, 255, 0)
22 mapped_unready_inner_color
= ( 0, 255, 0, 100)
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
30 if isinstance(top
, str):
31 self
.top
= self
.row_positions
[top
]
39 self
.bottom
= self
.top
+ self
.height
40 self
.right
= self
.left
+ self
.width
42 self
.rect
= (self
.left
, self
.top
, self
.right
, self
.bottom
)
43 self
.position
= (self
.left
, self
.top
)
46 self
.outer_color
= self
.lighter_outer_color
49 self
.outer_color
= self
.default_outer_color
52 self
.inner_color
= self
.default_inner_color
55 self
.custom_color
= None
56 self
.custom_unready_color
= None
58 def square(self
, all_actions_ready
):
59 if self
.has_actions():
61 self
.inner_color
= self
.custom_color
or self
.mapped_inner_color
63 self
.inner_color
= self
.custom_unready_color
or self
.mapped_unready_inner_color
65 return RoundedRect((0, 0, self
.width
, self
.height
),
66 self
.outer_color
, self
.inner_color
, self
.linewidth
)
68 def collidepoint(self
, position
):
69 return self
.surface
.get_rect().collidepoint(
70 position
[0] - self
.position
[0],
71 position
[1] - self
.position
[1]
74 def set_description(self
, description
):
75 for desc
in description
:
77 self
.description
.append("")
79 self
.description
.append(str(desc
))
81 def set_color(self
, color
):
82 self
.custom_color
= tuple(color
)
84 self
.custom_unready_color
= tuple(color
)
86 def draw(self
, background_surface
):
87 self
.draw_lock
.acquire()
88 all_actions_ready
= self
.all_actions_ready()
90 self
.surface
= self
.square(all_actions_ready
).surface()
93 text_police
= font(10)
96 text
= police
.render(self
.key_sym
, True, (0,0,0))
97 self
.surface
.blit(text
, (5,5))
100 offset
= 11 + text_police
.get_linesize() - 4
101 first_line_offset
= 18
102 for description
in self
.description
:
103 text
= text_police
.render(description
, True, (0,0,0))
105 self
.surface
.blit(text
, (first_line_offset
, 9))
106 is_first_line
= False
108 self
.surface
.blit(text
, (3, offset
))
109 offset
+= text_police
.get_linesize() - 4
111 background_surface
.blit(self
.surface
, self
.position
)
112 self
.draw_lock
.release()
114 return not all_actions_ready
116 def poll_redraw(self
, background
):
119 if self
.all_actions_ready():
120 self
.draw(background
)
124 def has_actions(self
):
125 return len(self
.actions
) > 0
127 def all_actions_ready(self
):
128 return all(action
.ready() for action
in self
.actions
)
130 def add_action(self
, action_name
, **arguments
):
131 self
.actions
.append(Action(action_name
, self
, **arguments
))
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
):
141 self
.mapping
.finished_running(self
, start_time
)
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((690, 250)).convert()
149 surface
.fill((250, 250, 250))
153 police
.set_bold(True)
154 text
= police
.render("actions linked to key {}:".format(self
.key_sym
), True, (0,0,0))
155 surface
.blit(text
, (0, offset
))
156 offset
+= police
.get_linesize()
158 police
.set_bold(False)
159 for description
in action_descriptions
:
160 text
= police
.render(description
, True, (0,0,0))
161 surface
.blit(text
, (0, offset
))
162 offset
+= police
.get_linesize()
164 screen
.blit(surface
, (5, 308))
165 pygame
.display
.flip()
166 self
.draw_lock
.release()