-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext_menu.py
More file actions
252 lines (227 loc) · 10.8 KB
/
context_menu.py
File metadata and controls
252 lines (227 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# python 3.7
## TODO
# add option to show icons next to menu items
# pass in kwargs to action-functions somehow
if __name__ == '__main__':
# TESTING ONLY -- not used if imported
# globals
import pygame
pygame.init()
clock = pygame.time.Clock()
# COLORS
BLACK = (0,0,0)
WHITE = (222,222,222)
GREY = (23,23,23)
BORDER = (190, 190, 190)
LIGHT_GREY = (220,220,220)
GREEN_TEXT = (110,180,110)
GREEN = (60,160,60)
# SCREEN RESOLUTIONS
SCREEN_RES = (480, 512)
# screen = pygame.display.set_mode((500, 500), HWSURFACE | DOUBLEBUF | RESIZABLE)
screen = pygame.display.set_mode(SCREEN_RES, 0, 32, pygame.RESIZABLE)
pygame.display.set_caption('Box Test')
image = pygame.image.load("stellar-nebulae/stellar-black-hole-1.jpg")
SIZE = image.get_rect().size
print(SIZE)
screen.blit(image, (0,0), image.get_rect())
pygame.display.flip()
# font
FONT20 = pygame.font.Font('Candara.ttf', 20)
MARGIN = 0 # BUGGY PART! -- should control the whitespace around menu items but doesn't work with highlighter
## DEMO parameters
def dummy_action():
print('1')
menu = [('test test test', dummy_action),
('more more more', dummy_action),
('third', dummy_action)]
panel = None
class ContextMenu(object):
""" creates the list, then removes the list and replaces what was on screen previously. """
def __init__(self, screen, event_pos, menu_obj, font=None):
if not font:
self.font = pygame.font.SysFont('Arial', 25)
else:
self.font = font
self.screen = screen
self.x = event_pos[0]
self.y = event_pos[1]
self.menu = menu_obj # a list of tuples: ('text', event_or_function_to_call)
self.height = 0
self.width = []
for (text, action) in self.menu:
text_width, text_height = self.font.size(text)
self.width.append( text_width )
self.height += text_height
self.width = max(self.width) + 2 * self.font.size(' ')[0]
#self.height = len(self.menu) * FONT20.size("Tg")[1] # estimated
#self.width = max([len(item) for item in self.menu]) * # estimated
## SAVE original screen stuff - to restore later.
self.orig = pygame.image.tostring(self.screen, 'RGBA')
self.events_map = []
self.draw_menu()
self.highlighted_item = None
def draw_menu(self):
self.menu_width = self.width + MARGIN
self.menu_height = self.height + MARGIN
self.menu_size = (self.menu_width, self.menu_height)
this_surface = pygame.Surface(self.menu_size)
this_surface.fill(LIGHT_GREY)
pygame.draw.rect(this_surface, BORDER, (0, 0, self.menu_width, self.menu_height), 3)
self.this = this_surface
new_event_map = [] # (box) list
for lindex, (text, action) in enumerate(self.menu):
textSurface = self.font.render(' '+text, True, GREY)
(topleftx, toplefty, width, height) = textSurface.get_rect()
#toplefty += (lindex * self.font.size("Tg")[1])
topleftx += MARGIN
toplefty += MARGIN + (lindex * height)
self.this.blit(textSurface, (topleftx, toplefty, width, height))
### here, clicking this region now triggers the action:
### update event map, with absolute screen borders for this text.
#print(self.x, self.y, (self.x + topleftx, self.y + toplefty, self.x + width, self.y + toplefty + height))
new_event_map.append((action,(self.x + topleftx,
self.y + toplefty,
self.x + topleftx + width,
self.y + toplefty + height)))
self.screen.blit(self.this, (self.x, self.y))
pygame.display.update()
self.events_map = new_event_map
def draw_carrot_menu_item(self, menu_obj_index, highlightit=True):
""" highlightit: True turns on; False turns off """
text = self.menu[menu_obj_index][0]
textSurface = self.font.render(' '+text, True, GREY)
(topleftx, toplefty, width, height) = textSurface.get_rect()
# assuming one line per menu item; no wrap yet
#highlight = pygame.Surface((10,10)) #(width, height))
#COPY = pygame.Surface((10,10)) #(width, height))
if highlightit:
pygame.draw.polygon(textSurface, GREEN_TEXT, [(0,5), (5,10), (0,15)], 3)
#highlight.fill(BORDER)
#COPY.fill(BORDER)
else:
pygame.draw.polygon(textSurface, LIGHT_GREY, [(0,3), (5,10), (0,15)], 3)
#highlight.fill(LIGHT_GREY)
#COPY.fill(LIGHT_GREY)
topleftx += MARGIN
toplefty += MARGIN + (menu_obj_index * height)
self.this.blit(textSurface, (topleftx, toplefty, width, height))
self.screen.blit(self.this, (self.x, self.y))
pygame.display.update()
#return (action, (self.x + topleftx, self.y + toplefty, self.x + width, self.y + toplefty + height))
def draw_highlight_menu_item(self, menu_obj_index, highlightit=True):
""" highlightit: True turns on; False turns off.
assuming one line per menu item; no wrap yet.
BUGGY -- won't redraw the text after unhighlighting."""
text = self.menu[menu_obj_index][0]
textSurface = self.font.render(' '+text, True, GREY)
(topleftx, toplefty, width, height) = textSurface.get_rect()
highlight = pygame.Surface((width, height))
if highlightit:
highlight.fill(BORDER)
else:
highlight.fill(LIGHT_GREY)
topleftx += MARGIN/2
toplefty += MARGIN/2 + (menu_obj_index * height)
highlight.blit(textSurface, (topleftx, toplefty, width, height))
self.this.blit(highlight, (topleftx, toplefty, width, height))
self.this.blit(textSurface, (topleftx, toplefty, width, height))
self.screen.blit(self.this, (self.x, self.y))
pygame.display.update()
def clear(self):
""" DEBUG just puts a black box where menu used to be"""
self.this = pygame.Surface((self.width, self.height))
self.this.fill((BLACK))
self.screen.blit(self.this, (self.x, self.y))
pygame.display.update()
def close(self):
""" restores the image under the menu by restoring the whole screen """
surf = pygame.image.fromstring(self.orig, self.screen.get_size(), 'RGBA')
screen.blit(surf, (0,0), surf.get_rect())
pygame.display.update()
def check_menu_events(self, event_pos):
(x,y) = event_pos
print((x,y))
for action, (minx, miny, maxx, maxy) in self.events_map:
if minx <= x <= maxx and miny <= y <= maxy:
return action
def mouseover_highlight(self):
(x,y) = pygame.mouse.get_pos()
for lindex, (action, (minx, miny, maxx, maxy)) in enumerate(self.events_map):
# IF mouse outside a box, and that box is highlighted, off it.
if (self.highlighted_item == lindex and
((x < minx or x > maxx) or
(y < miny or y > maxy))):
self.highlighted_item = None
#print ( ('outside', x, y, (minx, miny, maxx, maxy)) )
self.draw_highlight_menu_item(lindex, highlightit=False)
# if mouse inside a box, highlight it.
if minx <= x <= maxx and miny <= y <= maxy:
# if another index is highlighted, turn that off now.
if self.highlighted_item and self.highlighted_item != lindex:
self.draw_highlight_menu_item(self.highlighted_item, highlightit=False)
self.draw_carrot_menu_item(self.highlighted_item, highlightit=False)
elif self.highlighted_item == lindex:
# still in bounds of box
#print ( ('pass', (minx, miny, maxx, maxy)) )
return
# in bounds of a new box
self.draw_highlight_menu_item(lindex)
self.draw_carrot_menu_item(lindex)
self.highlighted_item = lindex
def menu_controller(panel, event):
""" pass in game loop events and this should parse/route them.
has to be a function outside of ContextMenu class, because it creates/destroys instances of panel."""
if event.type == pygame.MOUSEBUTTONUP:
# prints on the console the button released and its position at that moment
print( u'button {} released in the position {}'.format(event.button, event.pos) )
if event.button == 3: # right click -- creates a new menu
if panel:
panel.close()
panel = None
panel = ContextMenu(screen, event.pos, menu, FONT20)
if event.button == 1: # left click chooses option, or erases menu.
if panel != None:
if panel.events_map:
action = panel.check_menu_events(event.pos)
if action:
action() # menus have the functions they call
panel.close()
panel = None
else: # left click anywhere else cancels the right-click ContextMenu.
panel.close()
panel = None
if panel: # and panel.this.get_rect().collidepoint(pygame.mouse.get_pos()):
#mouseover_highlight(panel)
panel.mouseover_highlight()
# GAME LOOP
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.MOUSEBUTTONUP:
# prints on the console the button released and its position at that moment
print( u'button {} released in the position {}'.format(event.button, event.pos) )
if event.button == 3: # right click
if panel:
panel.close()
panel = None
panel = ContextMenu(screen, event.pos, menu, FONT20)
if event.button == 1:
if panel != None:
if panel.events_map:
action = panel.check_menu_events(event.pos)
if action:
action()
panel.close()
panel = None
else: # left click anywhere else cancels the right-click ContextMenu.
panel.close()
panel = None
if panel: # and panel.this.get_rect().collidepoint(pygame.mouse.get_pos()):
#mouseover_highlight(panel)
panel.mouseover_highlight()
# systems_test
clock.tick() # slow it to 30 FPS
pygame.quit()