| 1 | from panda3d.core import Texture, TextNode, WindowProperties, LVector2i |
| 2 | from direct.gui.DirectGui import DirectButton, DirectCheckButton, \ |
| 3 | DirectOptionMenu, DirectSlider, DirectCheckButton |
| 4 | from direct.gui.DirectGuiGlobals import FLAT |
| 5 | from direct.gui.OnscreenText import OnscreenText |
| 6 | |
| 7 | |
| 8 | class Menu: |
| 9 | |
| 10 | def __init__(self, fsm, lang_mgr, opt_file, music): |
| 11 | self._fsm = fsm |
| 12 | self._lang_mgr = lang_mgr |
| 13 | self._opt_file = opt_file |
| 14 | self._music = music |
| 15 | self._font = base.loader.load_font('assets/fonts/Hanken-Book.ttf') |
| 16 | self._font.clear() |
| 17 | self._font.set_pixels_per_unit(60) |
| 18 | self._font.set_minfilter(Texture.FTLinearMipmapLinear) |
| 19 | self._font.set_outline((0, 0, 0, 1), .8, .2) |
| 20 | self._widgets = [] |
| 21 | self._common = { |
| 22 | 'scale': .12, |
| 23 | 'text_font': self._font, |
| 24 | 'text_fg': (.9, .9, .9, 1), |
| 25 | 'relief': FLAT, |
| 26 | 'frameColor': (.4, .4, .4, .14), |
| 27 | 'rolloverSound': loader.load_sfx('assets/audio/sfx/rollover.ogg'), |
| 28 | 'clickSound': loader.load_sfx('assets/audio/sfx/click.ogg')} |
| 29 | self._common_btn = {'frameSize': (-3.8, 3.8, -.6, 1.2)} | self._common |
| 30 | hlc = self._common_btn['frameColor'] |
| 31 | hlc = (hlc[0] + .2, hlc[1] + .2, hlc[2] + .2, hlc[3] + .2) |
| 32 | self._common_opt = { |
| 33 | 'item_frameColor': self._common_btn['frameColor'], |
| 34 | 'popupMarker_frameColor': self._common_btn['frameColor'], |
| 35 | 'item_relief': self._common_btn['relief'], |
| 36 | 'item_text_font': self._common_btn['text_font'], |
| 37 | 'item_text_fg': self._common_btn['text_fg'], |
| 38 | 'textMayChange': 1, |
| 39 | 'highlightColor': hlc, |
| 40 | 'text_align': TextNode.A_center, |
| 41 | } | self._common_btn |
| 42 | f_s = self._common_opt['frameSize'] |
| 43 | self._common_opt['frameSize'] = f_s[0], f_s[1] - .56, f_s[2], f_s[3] |
| 44 | self._common_slider = self._common | { |
| 45 | 'range': (0, 1), |
| 46 | 'thumb_frameColor': (.4, .4, .4, .4), |
| 47 | 'thumb_scale': 1.6, |
| 48 | 'scale': .4} |
| 49 | del self._common_slider['rolloverSound'] |
| 50 | del self._common_slider['clickSound'] |
| 51 | self._set_main() |
| 52 | |
| 53 | def _set_main(self): |
| 54 | self._widgets = [] |
| 55 | self._widgets += [DirectButton( |
| 56 | text=_('Play'), pos=(0, 1, .6), command=self.on_play, |
| 57 | **self._common_btn)] |
| 58 | self._widgets += [DirectButton( |
| 59 | text=_('Options'), pos=(0, 1, .3), command=self.on_options, |
| 60 | **self._common_btn)] |
| 61 | self._widgets += [DirectButton( |
| 62 | text=_('Support us'), pos=(0, 1, 0), command=self.on_play, |
| 63 | **self._common_btn)] |
| 64 | self._widgets += [DirectButton( |
| 65 | text=_('Credits'), pos=(0, 1, -.3), command=self.on_play, |
| 66 | **self._common_btn)] |
| 67 | self._widgets += [DirectButton( |
| 68 | text=_('Exit'), pos=(0, 1, -.6), command=self.on_play, |
| 69 | **self._common_btn)] |
| 70 | |
| 71 | def _set_options(self): |
| 72 | self._widgets = [] |
| 73 | self._lang_funcs = [lambda: _('English'), lambda: _('Italian')] |
| 74 | items = [fnc() for fnc in self._lang_funcs] |
| 75 | inititem = { |
| 76 | 'en': _('English'), |
| 77 | 'it': _('Italian') |
| 78 | }[self._opt_file['settings']['language']] |
| 79 | btn = DirectOptionMenu( |
| 80 | text=_('Language'), items=items, initialitem=inititem, |
| 81 | pos=(0, 1, .6), command=self.on_language, **self._common_opt) |
| 82 | btn.popupMenu['frameColor'] = self._common_btn['frameColor'] |
| 83 | btn.popupMenu['relief'] = self._common_btn['relief'] |
| 84 | self._widgets += [btn] |
| 85 | self._widgets += [OnscreenText( |
| 86 | _('Volume'), pos=(-.1, .3), font=self._common['text_font'], |
| 87 | scale=self._common['scale'], fg=self._common['text_fg'], |
| 88 | align=TextNode.A_right)] |
| 89 | self._widgets += [DirectSlider( |
| 90 | pos=(.5, 1, .32), |
| 91 | value=self._opt_file['settings']['volume'], |
| 92 | command=self.on_volume, |
| 93 | **self._common_slider)] |
| 94 | self._slider = self._widgets[-1] |
| 95 | self._widgets += [DirectCheckButton( |
| 96 | text=_('Fullscreen'), pos=(0, 1, 0), command=self.on_fullscreen, |
| 97 | indicator_frameColor=self._common_opt['highlightColor'], |
| 98 | indicator_relief=self._common_btn['relief'], |
| 99 | indicatorValue=self._opt_file['settings']['fullscreen'], |
| 100 | **self._common_btn)] |
| 101 | res = self._opt_file['settings']['resolution'] |
| 102 | d_i = base.pipe.get_display_information() |
| 103 | def _res(idx): |
| 104 | return d_i.get_display_mode_width(idx), \ |
| 105 | d_i.get_display_mode_height(idx) |
| 106 | resolutions = [ |
| 107 | _res(idx) for idx in range(d_i.get_total_display_modes())] |
| 108 | resolutions = list(set(resolutions)) |
| 109 | resolutions = sorted(resolutions) |
| 110 | resolutions = [(str(_res[0]), str(_res[1])) for _res in resolutions] |
| 111 | resolutions = ['x'.join(_res) for _res in resolutions] |
| 112 | if not res: |
| 113 | res = resolutions[-1] |
| 114 | btn = DirectOptionMenu( |
| 115 | text=_('Resolution'), items=resolutions, initialitem=res, |
| 116 | pos=(0, 1, -.3), command=self.on_resolution, **self._common_opt) |
| 117 | btn.popupMenu['frameColor'] = self._common_btn['frameColor'] |
| 118 | btn.popupMenu['relief'] = self._common_btn['relief'] |
| 119 | self._widgets += [btn] |
| 120 | self._widgets += [DirectButton( |
| 121 | text=_('Back'), pos=(0, 1, -.6), command=self.on_back, |
| 122 | **self._common_btn)] |
| 123 | |
| 124 | def on_play(self): |
| 125 | self._fsm.demand('Scene') |
| 126 | |
| 127 | def on_options(self): |
| 128 | self.destroy() |
| 129 | self._set_options() |
| 130 | |
| 131 | def on_language(self, arg): |
| 132 | lang_code = { |
| 133 | _('English'): 'en_EN', |
| 134 | _('Italian'): 'it_IT'}[arg] |
| 135 | self._lang_mgr.set_lang(lang_code) |
| 136 | self._opt_file['settings']['language'] = lang_code[:2] |
| 137 | self._opt_file.store() |
| 138 | self.on_options() |
| 139 | |
| 140 | def on_volume(self): |
| 141 | self._opt_file['settings']['volume'] = self._slider['value'] |
| 142 | self._music.set_volume(self._slider['value']) |
| 143 | |
| 144 | def on_fullscreen(self, arg): |
| 145 | props = WindowProperties() |
| 146 | props.set_fullscreen(arg) |
| 147 | base.win.request_properties(props) |
| 148 | self._opt_file['settings']['fullscreen'] = int(arg) |
| 149 | self._opt_file.store() |
| 150 | |
| 151 | def on_resolution(self, arg): |
| 152 | props = WindowProperties() |
| 153 | props.set_size(LVector2i(*[int(_res) for _res in arg.split('x')])) |
| 154 | base.win.request_properties(props) |
| 155 | self._opt_file['settings']['resolution'] = arg |
| 156 | self._opt_file.store() |
| 157 | |
| 158 | def on_back(self): |
| 159 | self._opt_file.store() |
| 160 | self.destroy() |
| 161 | self._set_main() |
| 162 | |
| 163 | def destroy(self): |
| 164 | [wdg.destroy() for wdg in self._widgets] |