-
Notifications
You must be signed in to change notification settings - Fork 0
/
Menu.py
234 lines (191 loc) · 11.2 KB
/
Menu.py
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
'''
Created on Aug 17, 2012
@author: Jami
'''
from MissionMenu import MissionMenu
from ProfileMenu import ProfileMenu
from gui.basicmenu import BasicMenu, BasicTextButton, BasicImageButton, \
BasicTextInput, Label, ImageLabel, BasicCheckbox
from gui.gui import GUI, Frame, Element
import Utils
import consts
import pygame
class SpaceDominationGUI(GUI):
'''GUI implentation for Space Domination'''
mission_results = None
def __init__(self, parent, **kwargs):
super(SpaceDominationGUI, self).__init__(parent, **kwargs)
# set up the main menu
self.main_menu = BasicMenu(self, h_pad = 5, v_pad = 5)
self.add_child(self.main_menu)
self.main_menu.add_child(BasicTextButton(self.main_menu, text = 'Training Missions', select_fxn = self.main_menu.mouse_over_callback, callback = self.mission_menu_click))
self.main_menu.add_child(BasicTextButton(self.main_menu, text = 'Campaigns', select_fxn = self.main_menu.mouse_over_callback, callback = self.campaign_menu_click))
self.main_menu.add_child(BasicTextButton(self.main_menu, text = 'Profiles', select_fxn = self.main_menu.mouse_over_callback, callback = self.profile_menu_click))
self.main_menu.add_child(BasicTextButton(self.main_menu, text = 'Options', select_fxn = self.main_menu.mouse_over_callback, callback = self.options_menu_click))
self.main_menu.add_child(BasicTextButton(self.main_menu, text = 'Exit', select_fxn = self.main_menu.mouse_over_callback, callback = self.exit_click))
# TODO add/set up the mission menu
self.mission_menu = MissionMenu(self, self.parent.startMission, missions = self.parent.missionList)
self.add_child(self.mission_menu)
# add/set up the profile menu
#self.profile_menu = ProfileMenu(self, self.parent.currentProfile, self.parent.profiles, self.parent.setActiveProfile, shiplist = self.parent.shipList)
#self.add_child(self.profile_menu)
# add/set up the options menu
self.options_menu = BasicMenu(self, h_pad = 5, v_pad = 5, on_close = self.main_menu_click)
self.add_child(self.options_menu)
BasicTextInput(self.options_menu, label = 'Screen Width', value = self.parent.currentProfile['width'], numbers_only = True, select_fxn = self.options_menu.mouse_over_callback)
BasicTextInput(self.options_menu, label = 'Screen Height', value = self.parent.currentProfile['height'], numbers_only = True, select_fxn = self.options_menu.mouse_over_callback)
BasicCheckbox(self.options_menu, text = 'Fullscreen', value = int(self.parent.currentProfile['fullscreen']), select_fxn = self.options_menu.mouse_over_callback)
BasicTextButton(self.options_menu, text = 'Apply Changes', select_fxn = self.options_menu.mouse_over_callback, callback = self.apply_options)
BasicTextButton(self.options_menu, text = 'Back to Main Menu', select_fxn = self.options_menu.mouse_over_callback, callback = self.main_menu_click)
# add/set up the pause menu
self.pause_menu = PauseMenu(self, self.parent.unpause_game, h_pad = 5, v_pad = 5)
self.add_child(self.pause_menu)
BasicTextButton(self.pause_menu, text = 'Resume', select_fxn = self.pause_menu.mouse_over_callback, callback = self.parent.unpause_game)
BasicTextButton(self.pause_menu, text = 'Quit Mission', select_fxn = self.pause_menu.mouse_over_callback, callback = self.parent.quit_mission)
BasicTextButton(self.pause_menu, text = 'Options', select_fxn = self.pause_menu.mouse_over_callback, callback = self.options_menu_click)
BasicTextButton(self.pause_menu, text = 'Exit', select_fxn = self.pause_menu.mouse_over_callback, callback = self.exit_click)
def apply_options(self):
'''apply the height/width options'''
self.parent.currentProfile['width'] = int(self.options_menu.children[0].value)
self.parent.currentProfile['height'] = int(self.options_menu.children[1].value)
self.parent.currentProfile['fullscreen'] = int(self.options_menu.children[2].value)
self.parent.createDisplay()
self.parent.saveProfiles()
self.options_menu.children[0].value = self.parent.currentProfile['width']
self.options_menu.children[1].value = self.parent.currentProfile['height']
self.options_menu.children[0].set_unselected_image()
self.options_menu.children[1].set_unselected_image()
def profile_menu_click(self):
if self.profile_menu and self.profile_menu in self.children:
self.children.remove(self.profile_menu)
self.profile_menu = ProfileMenu(self, self.parent.currentProfile, self.parent.profiles, self.parent.setActiveProfile, self.parent.saveProfiles, shiplist = self.parent.shipList, weaponlist = self.parent.weaponList, upgradelist = self.parent.upgradeList)
self.add_child(self.profile_menu)
super(SpaceDominationGUI, self).profile_menu_click()
def campaign_menu_click(self):
if self.parent.campaignMgr:
for child in self.children:
child.set_active(False)
self.parent.campaignMgr.show_display(self)
def mission_results_show(self, result, mission):
if not self.mission_results:
self.mission_results = MissionResultsMenu(self, self.main_menu_click, self.campaign_menu_click, mission, self.parent.largefont, self.parent.defaultfont)
self.add_child(self.mission_results)
self.mission_results.init(result, mission)
self.generic_click(target_id = self.children.index(self.mission_results))
class PauseMenu(BasicMenu):
unpause_fxn = None
def __init__(self, parent, unpause_fxn, **kwargs):
super(PauseMenu, self).__init__(parent, **kwargs)
self.unpause_fxn = unpause_fxn
def update(self, event):
super(PauseMenu, self).update(event)
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
if self.unpause_fxn: self.unpause_fxn()
class MissionResultsMenu(Frame):
default_back_fxn = None
campaign_back_fxn = None
mission = None
largefont = None
smallfont = None
resultLabel = None
backBtn = None
def __init__(self, parent, default_back_fxn, campaign_back_fxn, mission, largefont, smallfont, **kwargs):
super(MissionResultsMenu, self).__init__(parent, **kwargs)
self.default_back_fxn = default_back_fxn
self.campaign_back_fxn = campaign_back_fxn
self.mission = mission
self.largefont = largefont
self.smallfont = smallfont
def init(self, results, mission = None):
if mission:
self.mission = mission
if 'win' in results and results['win']:
text = "MISSION COMPLETE"
color = consts.COLOR_GREEN
else:
text = "MISSION FAILED"
color = consts.COLOR_RED
if not self.resultLabel:
self.resultLabel = Label(self, text, font = self.largefont, color = color)
else:
self.resultLabel.color = color
self.resultLabel.set_text(text)
self.resultLabel.rect.center = (pygame.display.get_surface().get_width() * 0.5, 150)
if self.children:
self.children = []
self.add_child(self.resultLabel)
maxwidth, h = self.smallfont.size("Friendly Losses:")
x = 100
y = 200
lb = Label(self, "Friendly Losses:", font = self.smallfont, color = (255, 255, 255))
lb.rect.topleft = (x, y)
ally = results['labels']['ally']
ally_labels = []
ally_labels.append(lb)
y += 20
for i in ally:
y += h + 2
text = str(ally[i]['num']) + "x " + ally[i]['label']
if not ally[i]['image']:
ally[i]['image'] = pygame.surface.Surface(25, 25)
img = ally[i]['image']
img = pygame.transform.scale(img, (25, img.get_height() / (img.get_width() / 25)))
w, h = self.smallfont.size(text)
if img.get_height() > h: h = img.get_height()
w += img.get_width() + 2
surf = pygame.surface.Surface((w, h))
surf.set_colorkey(surf.get_at((0,0)))
surf.blit(img, (0,0))
surf.blit(self.smallfont.render(text, 1, (255,255,255)), (img.get_width() + 2, (h - self.smallfont.size(text)[1]) * 0.5))
if w > maxwidth: maxwidth = w
lb = ImageLabel(self, surf)
ally_labels.append(lb)
lb.rect.top = y
x = pygame.display.get_surface().get_width() * 0.5 - maxwidth - 100
for lb in ally_labels:
lb.rect.left = x
y1 = y + h
x += maxwidth + 200
y = 200
w, h = self.smallfont.size("Enemy Losses:")
lb = Label(self, "Enemy Losses:", font = self.smallfont, color = (255, 255, 255))
lb.rect.topleft = x, y
enemy = results['labels']['enemy']
y += 20
for i in enemy:
y += h + 2
text = str(enemy[i]['num']) + "x " + enemy[i]['label']
if not enemy[i]['image']:
enemy[i]['image'] = pygame.surface.Surface(25, 25)
img = enemy[i]['image']
img = pygame.transform.scale(img, (25, img.get_height() / (img.get_width() / 25)))
w, h = self.smallfont.size(text)
if img.get_height() > h: h = img.get_height()
w += img.get_width() + 2
surf = pygame.surface.Surface((w, h))
surf.set_colorkey(surf.get_at((0,0)))
surf.blit(img, (0,0))
surf.blit(self.smallfont.render(text, 1, (255,255,255)), (img.get_width() + 2, (h - self.smallfont.size(text)[1]) * 0.5))
if w > maxwidth: maxwidth = w
lb = ImageLabel(self, surf)
lb.rect.topleft = x, y
if self.mission.isCampaignMission:
self.backBtn = BasicTextButton(self, text = "Back to Campaign", callback = self.parent.campaign_menu_click)
else:
self.backBtn = BasicTextButton(self, text = "Back to Main Menu", callback = self.parent.main_menu_click)
y2 = y + h
maxy = y1
if y2 > maxy: maxy = y2
self.backBtn.rect.top = maxy + 20
self.backBtn.rect.centerx = pygame.display.get_surface().get_width() * 0.5
def update(self, event):
super(MissionResultsMenu, self).update(event)
if self.resultLabel:
self.resultLabel.rect.center = (pygame.display.get_surface().get_width() * 0.5, 150)
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
if self.mission and self.mission.isCampaignMission:
if self.campaign_back_fxn: self.campaign_back_fxn()
return True
else:
if self.default_back_fxn: self.default_back_fxn()
return True