| 1 | from panda3d.core import Texture |
| 2 | from direct.gui.DirectGui import DirectButton, DirectCheckButton, \ |
| 3 | DirectOptionMenu |
| 4 | from direct.gui.DirectGuiGlobals import FLAT |
| 5 | |
| 6 | |
| 7 | class Menu: |
| 8 | |
| 9 | def __init__(self, fsm, lang_mgr, opt_file): |
| 10 | self._fsm = fsm |
| 11 | self._lang_mgr = lang_mgr |
| 12 | self._opt_file = opt_file |
| 13 | self._font = base.loader.load_font('assets/fonts/Hanken-Book.ttf') |
| 14 | self._font.clear() |
| 15 | self._font.set_pixels_per_unit(60) |
| 16 | self._font.set_minfilter(Texture.FTLinearMipmapLinear) |
| 17 | self._font.set_outline((0, 0, 0, 1), .8, .2) |
| 18 | self._buttons = [] |
| 19 | self._common = { |
| 20 | 'scale': .12, |
| 21 | 'text_font': self._font, |
| 22 | 'text_fg': (.9, .9, .9, 1), |
| 23 | 'relief': FLAT, |
| 24 | 'frameColor': (.4, .4, .4, .14), |
| 25 | 'rolloverSound': loader.load_sfx('assets/audio/sfx/rollover.ogg'), |
| 26 | 'clickSound': loader.load_sfx('assets/audio/sfx/click.ogg')} |
| 27 | self._common_btn = {'frameSize': (-3.8, 3.8, -.6, 1.2)} | self._common |
| 28 | self._set_main() |
| 29 | |
| 30 | def _set_main(self): |
| 31 | self._buttons = [] |
| 32 | self._buttons += [DirectButton( |
| 33 | text=_('Play'), pos=(0, 1, .6), command=self.on_play, |
| 34 | **self._common_btn)] |
| 35 | self._buttons += [DirectButton( |
| 36 | text=_('Options'), pos=(0, 1, .3), command=self.on_options, |
| 37 | **self._common_btn)] |
| 38 | self._buttons += [DirectButton( |
| 39 | text=_('Support us'), pos=(0, 1, 0), command=self.on_play, |
| 40 | **self._common_btn)] |
| 41 | self._buttons += [DirectButton( |
| 42 | text=_('Credits'), pos=(0, 1, -.3), command=self.on_play, |
| 43 | **self._common_btn)] |
| 44 | self._buttons += [DirectButton( |
| 45 | text=_('Exit'), pos=(0, 1, -.6), command=self.on_play, |
| 46 | **self._common_btn)] |
| 47 | |
| 48 | def _set_options(self): |
| 49 | self._buttons = [] |
| 50 | self._lang_funcs = [lambda: _('English'), lambda: _('Italian')] |
| 51 | items = [fnc() for fnc in self._lang_funcs] |
| 52 | inititem = { |
| 53 | 'en': _('English'), |
| 54 | 'it': _('Italian') |
| 55 | }[self._opt_file['settings']['language']] |
| 56 | hlc = self._common_btn['frameColor'] |
| 57 | hlc = (hlc[0] + .2, hlc[1] + .2, hlc[2] + .2, hlc[3] + .2) |
| 58 | btn = DirectOptionMenu( |
| 59 | text=_('Language'), items=items, initialitem=inititem, |
| 60 | pos=(0, 1, .6), command=self.on_language, |
| 61 | item_frameColor=self._common_btn['frameColor'], |
| 62 | popupMarker_frameColor=self._common_btn['frameColor'], |
| 63 | item_relief=self._common_btn['relief'], |
| 64 | item_text_font=self._common_btn['text_font'], |
| 65 | item_text_fg=self._common_btn['text_fg'], |
| 66 | textMayChange=1, highlightColor=hlc, |
| 67 | **self._common_btn) |
| 68 | btn.popupMenu['frameColor'] = self._common_btn['frameColor'] |
| 69 | btn.popupMenu['relief'] = self._common_btn['relief'] |
| 70 | self._buttons += [btn] |
| 71 | self._buttons += [DirectButton( |
| 72 | text=_('Back'), pos=(0, 1, -.6), command=self.on_back, |
| 73 | **self._common_btn)] |
| 74 | |
| 75 | def on_play(self): |
| 76 | self._fsm.demand('Scene') |
| 77 | |
| 78 | def on_options(self): |
| 79 | self.destroy() |
| 80 | self._set_options() |
| 81 | |
| 82 | def on_language(self, arg): |
| 83 | lang_code = { |
| 84 | _('English'): 'en_EN', |
| 85 | _('Italian'): 'it_IT'}[arg] |
| 86 | self._lang_mgr.set_lang(lang_code) |
| 87 | self._opt_file['settings']['language'] = lang_code[:2] |
| 88 | self._opt_file.store() |
| 89 | self.on_options() |
| 90 | |
| 91 | def on_back(self): |
| 92 | self.destroy() |
| 93 | self._set_main() |
| 94 | |
| 95 | def destroy(self): |
| 96 | [btn.destroy() for btn in self._buttons] |