ya2 · news · projects · code · about

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