Commit | Line | Data |
---|---|---|
94a18c21 | 1 | from logging import info, debug |
2612dfc8 FC |
2 | from sys import platform, exit |
3 | from os import environ, system | |
8c9bf90e FC |
4 | from glob import glob |
5 | from importlib import import_module | |
6 | from inspect import isclass | |
2612dfc8 FC |
7 | from webbrowser import open_new_tab |
8 | from panda3d.core import Texture, TextNode, WindowProperties, LVector2i, \ | |
9 | TextProperties, TextPropertiesManager | |
4071c6d8 | 10 | from direct.gui.DirectGui import DirectButton, DirectCheckButton, \ |
6fff1464 | 11 | DirectOptionMenu, DirectSlider, DirectCheckButton |
4071c6d8 | 12 | from direct.gui.DirectGuiGlobals import FLAT |
e1e44d5c | 13 | from direct.gui.OnscreenText import OnscreenText |
407412a5 | 14 | from lib.engine.gui.cursor import MouseCursor |
a0462193 | 15 | from game.scene import Scene |
0d5a5427 | 16 | from panda3d.bullet import BulletWorld |
4071c6d8 FC |
17 | |
18 | ||
19 | class Menu: | |
20 | ||
6168d0c2 | 21 | def __init__(self, fsm, lang_mgr, opt_file, music, pipeline, scenes): |
4071c6d8 FC |
22 | self._fsm = fsm |
23 | self._lang_mgr = lang_mgr | |
24 | self._opt_file = opt_file | |
e1e44d5c | 25 | self._music = music |
a9aba267 | 26 | self._pipeline = pipeline |
6168d0c2 | 27 | self._scenes = scenes |
407412a5 | 28 | self._cursor = MouseCursor( |
7e487769 | 29 | 'assets/images/buttons/arrowUpLeft.dds', (.04, 1, .04), (.5, .5, .5, 1), |
407412a5 | 30 | (.01, .01)) |
4071c6d8 FC |
31 | self._font = base.loader.load_font('assets/fonts/Hanken-Book.ttf') |
32 | self._font.clear() | |
33 | self._font.set_pixels_per_unit(60) | |
34 | self._font.set_minfilter(Texture.FTLinearMipmapLinear) | |
35 | self._font.set_outline((0, 0, 0, 1), .8, .2) | |
e1e44d5c | 36 | self._widgets = [] |
4071c6d8 FC |
37 | self._common = { |
38 | 'scale': .12, | |
39 | 'text_font': self._font, | |
40 | 'text_fg': (.9, .9, .9, 1), | |
41 | 'relief': FLAT, | |
42 | 'frameColor': (.4, .4, .4, .14), | |
43 | 'rolloverSound': loader.load_sfx('assets/audio/sfx/rollover.ogg'), | |
44 | 'clickSound': loader.load_sfx('assets/audio/sfx/click.ogg')} | |
75b89b83 | 45 | self._common_btn = {'frameSize': (-4.8, 4.8, -.6, 1.2)} | self._common |
6fff1464 FC |
46 | hlc = self._common_btn['frameColor'] |
47 | hlc = (hlc[0] + .2, hlc[1] + .2, hlc[2] + .2, hlc[3] + .2) | |
48 | self._common_opt = { | |
49 | 'item_frameColor': self._common_btn['frameColor'], | |
50 | 'popupMarker_frameColor': self._common_btn['frameColor'], | |
51 | 'item_relief': self._common_btn['relief'], | |
52 | 'item_text_font': self._common_btn['text_font'], | |
53 | 'item_text_fg': self._common_btn['text_fg'], | |
54 | 'textMayChange': 1, | |
55 | 'highlightColor': hlc, | |
56 | 'text_align': TextNode.A_center, | |
57 | } | self._common_btn | |
58 | f_s = self._common_opt['frameSize'] | |
59 | self._common_opt['frameSize'] = f_s[0], f_s[1] - .56, f_s[2], f_s[3] | |
e1e44d5c FC |
60 | self._common_slider = self._common | { |
61 | 'range': (0, 1), | |
62 | 'thumb_frameColor': (.4, .4, .4, .4), | |
63 | 'thumb_scale': 1.6, | |
64 | 'scale': .4} | |
65 | del self._common_slider['rolloverSound'] | |
66 | del self._common_slider['clickSound'] | |
4071c6d8 FC |
67 | self._set_main() |
68 | ||
69 | def _set_main(self): | |
e1e44d5c FC |
70 | self._widgets = [] |
71 | self._widgets += [DirectButton( | |
4071c6d8 FC |
72 | text=_('Play'), pos=(0, 1, .6), command=self.on_play, |
73 | **self._common_btn)] | |
e1e44d5c | 74 | self._widgets += [DirectButton( |
2612dfc8 | 75 | text=_('Options'), pos=(0, 1, .2), command=self.on_options, |
4071c6d8 | 76 | **self._common_btn)] |
e1e44d5c | 77 | self._widgets += [DirectButton( |
2612dfc8 | 78 | text=_('Credits'), pos=(0, 1, -.2), command=self.on_credits, |
4071c6d8 | 79 | **self._common_btn)] |
e1e44d5c | 80 | self._widgets += [DirectButton( |
2612dfc8 | 81 | text=_('Exit'), pos=(0, 1, -.6), command=lambda: exit(), |
4071c6d8 | 82 | **self._common_btn)] |
75b89b83 | 83 | self._rearrange_width() |
4071c6d8 FC |
84 | |
85 | def _set_options(self): | |
e1e44d5c | 86 | self._widgets = [] |
4071c6d8 FC |
87 | self._lang_funcs = [lambda: _('English'), lambda: _('Italian')] |
88 | items = [fnc() for fnc in self._lang_funcs] | |
89 | inititem = { | |
90 | 'en': _('English'), | |
91 | 'it': _('Italian') | |
92 | }[self._opt_file['settings']['language']] | |
4071c6d8 FC |
93 | btn = DirectOptionMenu( |
94 | text=_('Language'), items=items, initialitem=inititem, | |
a9aba267 | 95 | pos=(0, 1, .8), command=self.on_language, **self._common_opt) |
4071c6d8 FC |
96 | btn.popupMenu['frameColor'] = self._common_btn['frameColor'] |
97 | btn.popupMenu['relief'] = self._common_btn['relief'] | |
e1e44d5c FC |
98 | self._widgets += [btn] |
99 | self._widgets += [OnscreenText( | |
a9aba267 | 100 | _('Volume'), pos=(-.1, .55), font=self._common['text_font'], |
e1e44d5c FC |
101 | scale=self._common['scale'], fg=self._common['text_fg'], |
102 | align=TextNode.A_right)] | |
103 | self._widgets += [DirectSlider( | |
a9aba267 | 104 | pos=(.5, 1, .57), |
e1e44d5c FC |
105 | value=self._opt_file['settings']['volume'], |
106 | command=self.on_volume, | |
107 | **self._common_slider)] | |
108 | self._slider = self._widgets[-1] | |
6fff1464 | 109 | self._widgets += [DirectCheckButton( |
a9aba267 | 110 | text=_('Fullscreen'), pos=(0, 1, .3), command=self.on_fullscreen, |
6fff1464 FC |
111 | indicator_frameColor=self._common_opt['highlightColor'], |
112 | indicator_relief=self._common_btn['relief'], | |
113 | indicatorValue=self._opt_file['settings']['fullscreen'], | |
114 | **self._common_btn)] | |
115 | res = self._opt_file['settings']['resolution'] | |
116 | d_i = base.pipe.get_display_information() | |
117 | def _res(idx): | |
118 | return d_i.get_display_mode_width(idx), \ | |
119 | d_i.get_display_mode_height(idx) | |
120 | resolutions = [ | |
121 | _res(idx) for idx in range(d_i.get_total_display_modes())] | |
122 | resolutions = list(set(resolutions)) | |
123 | resolutions = sorted(resolutions) | |
124 | resolutions = [(str(_res[0]), str(_res[1])) for _res in resolutions] | |
125 | resolutions = ['x'.join(_res) for _res in resolutions] | |
126 | if not res: | |
127 | res = resolutions[-1] | |
128 | btn = DirectOptionMenu( | |
129 | text=_('Resolution'), items=resolutions, initialitem=res, | |
a9aba267 | 130 | pos=(0, 1, .05), command=self.on_resolution, **self._common_opt) |
6fff1464 FC |
131 | btn.popupMenu['frameColor'] = self._common_btn['frameColor'] |
132 | btn.popupMenu['relief'] = self._common_btn['relief'] | |
133 | self._widgets += [btn] | |
a9aba267 FC |
134 | self._widgets += [DirectCheckButton( |
135 | text=_('Antialiasing'), pos=(0, 1, -.2), command=self.on_aa, | |
136 | indicator_frameColor=self._common_opt['highlightColor'], | |
137 | indicator_relief=self._common_btn['relief'], | |
138 | indicatorValue=self._opt_file['settings']['antialiasing'], | |
139 | **self._common_btn)] | |
5fdf77d0 FC |
140 | self._widgets += [DirectCheckButton( |
141 | text=_('Shadows'), pos=(0, 1, -.45), command=self.on_shadows, | |
142 | indicator_frameColor=self._common_opt['highlightColor'], | |
143 | indicator_relief=self._common_btn['relief'], | |
144 | indicatorValue=self._opt_file['settings']['shadows'], | |
145 | **self._common_btn)] | |
e1e44d5c | 146 | self._widgets += [DirectButton( |
a9aba267 | 147 | text=_('Back'), pos=(0, 1, -.8), command=self.on_back, |
4071c6d8 FC |
148 | **self._common_btn)] |
149 | ||
2612dfc8 FC |
150 | def _set_credits(self): |
151 | self._widgets = [] | |
152 | tp_scale = TextProperties() | |
153 | tp_scale.set_text_scale(.64) | |
154 | TextPropertiesManager.getGlobalPtr().setProperties('scale', tp_scale) | |
155 | self._widgets += [OnscreenText( | |
156 | _('Code and gfx\n \1scale\1Flavio Calva\2\n\n\nMusic\n \1scale\1Stefan Grossmann\2'), | |
157 | pos=(-.9, .55), font=self._common['text_font'], | |
158 | scale=self._common['scale'], fg=self._common['text_fg'], | |
159 | align=TextNode.A_left)] | |
160 | self._widgets += [DirectButton( | |
161 | text=_('Website'), pos=(-.6, 1, .29), command=self.on_website, | |
162 | **self._common_btn | {'scale': .08})] | |
163 | self._widgets += [OnscreenText( | |
e67dbe53 | 164 | _('Special thanks to:\n \1scale\1rdb\2\n \1scale\1Luisa Tenuta\2\n \1scale\1Damiana Ercolani\2'), |
2612dfc8 FC |
165 | pos=(.1, .55), font=self._common['text_font'], |
166 | scale=self._common['scale'], fg=self._common['text_fg'], | |
167 | align=TextNode.A_left)] | |
168 | self._widgets += [DirectButton( | |
169 | text=_('Back'), pos=(0, 1, -.8), command=self.on_back, | |
170 | **self._common_btn)] | |
171 | ||
4071c6d8 | 172 | def on_play(self): |
0d5a5427 | 173 | self.destroy() |
638deddf | 174 | self._cursor = MouseCursor( |
7e487769 | 175 | 'assets/images/buttons/arrowUpLeft.dds', (.04, 1, .04), (.5, .5, .5, 1), |
638deddf | 176 | (.01, .01)) |
0d5a5427 | 177 | self._widgets = [] |
8c9bf90e FC |
178 | cmn = self._common_btn.copy() | { |
179 | 'frameSize': (-2.4, 2.4, -2.4, 2.4), | |
bb956c26 FC |
180 | 'frameColor': (1, 1, 1, .8), |
181 | 'text_scale': .64} | |
6168d0c2 FC |
182 | left = - (dx := .8) * (min(4, len(self._scenes)) - 1) / 2 |
183 | for i, cls in enumerate(self._scenes): | |
184 | top = .1 if len(self._scenes) < 5 else .6 | |
a5fddddc | 185 | row = 0 if i < 4 else 1 |
28acea3a | 186 | self._widgets += [DirectButton( |
a5fddddc | 187 | text=cls.name(), pos=(left + dx * (i % 4), 1, top - dx * row), |
bb956c26 | 188 | command=self.start, extraArgs=[cls], text_wordwrap=6, |
420ce99a | 189 | frameTexture='assets/images/scenes/%s.dds' % cls.__name__, |
28acea3a | 190 | **cmn)] |
bb956c26 FC |
191 | for j in range(4): |
192 | tnode = self._widgets[-1].component('text%s' % j).textNode | |
193 | height = - tnode.getLineHeight() / 2 | |
194 | height += (tnode.get_height() - tnode.get_line_height()) / 2 | |
195 | self._widgets[-1].component('text%s' % j).set_pos(0, 0, height) | |
0d5a5427 FC |
196 | self._widgets += [DirectButton( |
197 | text=_('Back'), pos=(0, 1, -.8), command=self.on_back, | |
198 | **self._common_btn)] | |
199 | ||
8c9bf90e FC |
200 | def start(self, cls): |
201 | self._fsm.demand('Scene', cls) | |
4071c6d8 FC |
202 | |
203 | def on_options(self): | |
204 | self.destroy() | |
638deddf | 205 | self._cursor = MouseCursor( |
7e487769 | 206 | 'assets/images/buttons/arrowUpLeft.dds', (.04, 1, .04), (.5, .5, .5, 1), |
638deddf | 207 | (.01, .01)) |
4071c6d8 FC |
208 | self._set_options() |
209 | ||
2612dfc8 FC |
210 | def on_credits(self): |
211 | self.destroy() | |
638deddf | 212 | self._cursor = MouseCursor( |
7e487769 | 213 | 'assets/images/buttons/arrowUpLeft.dds', (.04, 1, .04), (.5, .5, .5, 1), |
638deddf | 214 | (.01, .01)) |
2612dfc8 FC |
215 | self._set_credits() |
216 | ||
75b89b83 FC |
217 | def _rearrange_width(self): |
218 | max_width = 0 | |
219 | for wdg in self._widgets: | |
220 | t_n = wdg.component('text0') | |
221 | u_l = t_n.textNode.get_upper_left_3d() | |
222 | l_r = t_n.textNode.get_lower_right_3d() | |
223 | max_width = max(l_r[0] - u_l[0], max_width) | |
224 | for wdg in self._widgets: | |
225 | m_w = max_width / 2 + .8 | |
226 | wdg['frameSize'] = -m_w, m_w, wdg['frameSize'][2], wdg['frameSize'][3] | |
227 | ||
4071c6d8 FC |
228 | def on_language(self, arg): |
229 | lang_code = { | |
230 | _('English'): 'en_EN', | |
231 | _('Italian'): 'it_IT'}[arg] | |
232 | self._lang_mgr.set_lang(lang_code) | |
233 | self._opt_file['settings']['language'] = lang_code[:2] | |
234 | self._opt_file.store() | |
235 | self.on_options() | |
236 | ||
e1e44d5c FC |
237 | def on_volume(self): |
238 | self._opt_file['settings']['volume'] = self._slider['value'] | |
239 | self._music.set_volume(self._slider['value']) | |
240 | ||
6fff1464 FC |
241 | def on_fullscreen(self, arg): |
242 | props = WindowProperties() | |
243 | props.set_fullscreen(arg) | |
244 | base.win.request_properties(props) | |
245 | self._opt_file['settings']['fullscreen'] = int(arg) | |
246 | self._opt_file.store() | |
247 | ||
248 | def on_resolution(self, arg): | |
249 | props = WindowProperties() | |
250 | props.set_size(LVector2i(*[int(_res) for _res in arg.split('x')])) | |
251 | base.win.request_properties(props) | |
252 | self._opt_file['settings']['resolution'] = arg | |
253 | self._opt_file.store() | |
254 | ||
a9aba267 FC |
255 | def on_aa(self, arg): |
256 | self._pipeline.msaa_samples = 4 if arg else 1 | |
94a18c21 | 257 | debug(f'msaa: {self._pipeline.msaa_samples}') |
a9aba267 FC |
258 | self._opt_file['settings']['antialiasing'] = int(arg) |
259 | self._opt_file.store() | |
260 | ||
5fdf77d0 FC |
261 | def on_shadows(self, arg): |
262 | self._pipeline.enable_shadows = int(arg) | |
94a18c21 | 263 | debug(f'shadows: {self._pipeline.enable_shadows}') |
5fdf77d0 FC |
264 | self._opt_file['settings']['shadows'] = int(arg) |
265 | self._opt_file.store() | |
266 | ||
2612dfc8 FC |
267 | def on_website(self): |
268 | if platform.startswith('linux'): | |
269 | environ['LD_LIBRARY_PATH'] = '' | |
270 | system('xdg-open https://www.ya2.it') | |
271 | else: | |
272 | open_new_tab('https://www.ya2.it') | |
273 | ||
4071c6d8 | 274 | def on_back(self): |
e1e44d5c | 275 | self._opt_file.store() |
4071c6d8 | 276 | self.destroy() |
638deddf | 277 | self._cursor = MouseCursor( |
7e487769 | 278 | 'assets/images/buttons/arrowUpLeft.dds', (.04, 1, .04), (.5, .5, .5, 1), |
638deddf | 279 | (.01, .01)) |
4071c6d8 FC |
280 | self._set_main() |
281 | ||
282 | def destroy(self): | |
e1e44d5c | 283 | [wdg.destroy() for wdg in self._widgets] |
407412a5 | 284 | self._cursor.destroy() |