ya2 · news · projects · code · about

aa option
[pmachines.git] / pmachines / menu.py
1 from logging import info
2 from panda3d.core import Texture, TextNode, WindowProperties, LVector2i
3 from direct.gui.DirectGui import DirectButton, DirectCheckButton, \
4 DirectOptionMenu, DirectSlider, DirectCheckButton
5 from direct.gui.DirectGuiGlobals import FLAT
6 from direct.gui.OnscreenText import OnscreenText
7
8
9 class Menu:
10
11 def __init__(self, fsm, lang_mgr, opt_file, music, pipeline):
12 self._fsm = fsm
13 self._lang_mgr = lang_mgr
14 self._opt_file = opt_file
15 self._music = music
16 self._pipeline = pipeline
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)
22 self._widgets = []
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
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]
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']
53 self._set_main()
54
55 def _set_main(self):
56 self._widgets = []
57 self._widgets += [DirectButton(
58 text=_('Play'), pos=(0, 1, .6), command=self.on_play,
59 **self._common_btn)]
60 self._widgets += [DirectButton(
61 text=_('Options'), pos=(0, 1, .3), command=self.on_options,
62 **self._common_btn)]
63 self._widgets += [DirectButton(
64 text=_('Support us'), pos=(0, 1, 0), command=self.on_play,
65 **self._common_btn)]
66 self._widgets += [DirectButton(
67 text=_('Credits'), pos=(0, 1, -.3), command=self.on_play,
68 **self._common_btn)]
69 self._widgets += [DirectButton(
70 text=_('Exit'), pos=(0, 1, -.6), command=self.on_play,
71 **self._common_btn)]
72
73 def _set_options(self):
74 self._widgets = []
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']]
81 btn = DirectOptionMenu(
82 text=_('Language'), items=items, initialitem=inititem,
83 pos=(0, 1, .8), command=self.on_language, **self._common_opt)
84 btn.popupMenu['frameColor'] = self._common_btn['frameColor']
85 btn.popupMenu['relief'] = self._common_btn['relief']
86 self._widgets += [btn]
87 self._widgets += [OnscreenText(
88 _('Volume'), pos=(-.1, .55), font=self._common['text_font'],
89 scale=self._common['scale'], fg=self._common['text_fg'],
90 align=TextNode.A_right)]
91 self._widgets += [DirectSlider(
92 pos=(.5, 1, .57),
93 value=self._opt_file['settings']['volume'],
94 command=self.on_volume,
95 **self._common_slider)]
96 self._slider = self._widgets[-1]
97 self._widgets += [DirectCheckButton(
98 text=_('Fullscreen'), pos=(0, 1, .3), command=self.on_fullscreen,
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,
118 pos=(0, 1, .05), command=self.on_resolution, **self._common_opt)
119 btn.popupMenu['frameColor'] = self._common_btn['frameColor']
120 btn.popupMenu['relief'] = self._common_btn['relief']
121 self._widgets += [btn]
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)]
128 self._widgets += [DirectButton(
129 text=_('Back'), pos=(0, 1, -.8), command=self.on_back,
130 **self._common_btn)]
131
132 def on_play(self):
133 self._fsm.demand('Scene')
134
135 def on_options(self):
136 self.destroy()
137 self._set_options()
138
139 def on_language(self, arg):
140 lang_code = {
141 _('English'): 'en_EN',
142 _('Italian'): 'it_IT'}[arg]
143 self._lang_mgr.set_lang(lang_code)
144 self._opt_file['settings']['language'] = lang_code[:2]
145 self._opt_file.store()
146 self.on_options()
147
148 def on_volume(self):
149 self._opt_file['settings']['volume'] = self._slider['value']
150 self._music.set_volume(self._slider['value'])
151
152 def on_fullscreen(self, arg):
153 props = WindowProperties()
154 props.set_fullscreen(arg)
155 base.win.request_properties(props)
156 self._opt_file['settings']['fullscreen'] = int(arg)
157 self._opt_file.store()
158
159 def on_resolution(self, arg):
160 props = WindowProperties()
161 props.set_size(LVector2i(*[int(_res) for _res in arg.split('x')]))
162 base.win.request_properties(props)
163 self._opt_file['settings']['resolution'] = arg
164 self._opt_file.store()
165
166 def on_aa(self, arg):
167 self._pipeline.msaa_samples = 4 if arg else 1
168 info(f'msaa: {self._pipeline.msaa_samples}')
169 self._pipeline._setup_tonemapping()
170 self._opt_file['settings']['antialiasing'] = int(arg)
171 self._opt_file.store()
172
173 def on_back(self):
174 self._opt_file.store()
175 self.destroy()
176 self._set_main()
177
178 def destroy(self):
179 [wdg.destroy() for wdg in self._widgets]