1 from .rounded_rect
import *
17 default_outer_color
= (120, 120, 120)
18 lighter_outer_color
= (200, 200, 200)
19 default_inner_color
= (255, 255, 255)
20 mapped_inner_color
= ( 0, 255, 0)
21 mapped_unready_inner_color
= ( 0, 255, 0, 100)
22 #mapped_unready_inner_color = (255, 165, 0)
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 self
.description
= [str(desc
) for desc
in description
]
77 def set_color(self
, color
):
78 self
.custom_color
= tuple(color
)
80 self
.custom_unready_color
= tuple(color
)
82 def draw(self
, background_surface
):
83 self
.draw_lock
.acquire()
84 all_actions_ready
= self
.all_actions_ready()
86 self
.surface
= self
.square(all_actions_ready
).surface()
88 if getattr(sys
, 'frozen', False):
89 police
= pygame
.font
.Font(sys
._MEIPASS
+ "/Ubuntu-Regular.ttf", 14)
90 text_police
= pygame
.font
.Font(sys
._MEIPASS
+ "/Ubuntu-Regular.ttf", 10)
92 police
= pygame
.font
.Font("Ubuntu-Regular.ttf", 14)
93 text_police
= pygame
.font
.Font("Ubuntu-Regular.ttf", 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((800, 250)).convert()
149 surface
.fill((250, 250, 250))
150 if getattr(sys
, 'frozen', False):
151 police
= pygame
.font
.Font(sys
._MEIPASS
+ "/Ubuntu-Regular.ttf", 14)
153 police
= pygame
.font
.Font("Ubuntu-Regular.ttf", 14)
156 police
.set_bold(True)
157 text
= police
.render("actions linked to key {}:".format(self
.key_sym
), True, (0,0,0))
158 surface
.blit(text
, (0, offset
))
159 offset
+= police
.get_linesize()
161 police
.set_bold(False)
162 for description
in action_descriptions
:
163 text
= police
.render(description
, True, (0,0,0))
164 surface
.blit(text
, (0, offset
))
165 offset
+= police
.get_linesize()
167 screen
.blit(surface
, (10, 330))
168 pygame
.display
.flip()
169 self
.draw_lock
.release()