ya2 · news · projects · code · about

fullscreen and resolution option
[pmachines.git] / pmachines / menu.py
CommitLineData
6fff1464 1from panda3d.core import Texture, TextNode, WindowProperties, LVector2i
4071c6d8 2from direct.gui.DirectGui import DirectButton, DirectCheckButton, \
6fff1464 3 DirectOptionMenu, DirectSlider, DirectCheckButton
4071c6d8 4from direct.gui.DirectGuiGlobals import FLAT
e1e44d5c 5from direct.gui.OnscreenText import OnscreenText
4071c6d8
FC
6
7
8class Menu:
9
e1e44d5c 10 def __init__(self, fsm, lang_mgr, opt_file, music):
4071c6d8
FC
11 self._fsm = fsm
12 self._lang_mgr = lang_mgr
13 self._opt_file = opt_file
e1e44d5c 14 self._music = music
4071c6d8
FC
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)
e1e44d5c 20 self._widgets = []
4071c6d8
FC
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
6fff1464
FC
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]
e1e44d5c
FC
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']
4071c6d8
FC
51 self._set_main()
52
53 def _set_main(self):
e1e44d5c
FC
54 self._widgets = []
55 self._widgets += [DirectButton(
4071c6d8
FC
56 text=_('Play'), pos=(0, 1, .6), command=self.on_play,
57 **self._common_btn)]
e1e44d5c 58 self._widgets += [DirectButton(
4071c6d8
FC
59 text=_('Options'), pos=(0, 1, .3), command=self.on_options,
60 **self._common_btn)]
e1e44d5c 61 self._widgets += [DirectButton(
4071c6d8
FC
62 text=_('Support us'), pos=(0, 1, 0), command=self.on_play,
63 **self._common_btn)]
e1e44d5c 64 self._widgets += [DirectButton(
4071c6d8
FC
65 text=_('Credits'), pos=(0, 1, -.3), command=self.on_play,
66 **self._common_btn)]
e1e44d5c 67 self._widgets += [DirectButton(
4071c6d8
FC
68 text=_('Exit'), pos=(0, 1, -.6), command=self.on_play,
69 **self._common_btn)]
70
71 def _set_options(self):
e1e44d5c 72 self._widgets = []
4071c6d8
FC
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']]
4071c6d8
FC
79 btn = DirectOptionMenu(
80 text=_('Language'), items=items, initialitem=inititem,
6fff1464 81 pos=(0, 1, .6), command=self.on_language, **self._common_opt)
4071c6d8
FC
82 btn.popupMenu['frameColor'] = self._common_btn['frameColor']
83 btn.popupMenu['relief'] = self._common_btn['relief']
e1e44d5c
FC
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]
6fff1464
FC
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]
e1e44d5c 120 self._widgets += [DirectButton(
4071c6d8
FC
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
e1e44d5c
FC
140 def on_volume(self):
141 self._opt_file['settings']['volume'] = self._slider['value']
142 self._music.set_volume(self._slider['value'])
143
6fff1464
FC
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
4071c6d8 158 def on_back(self):
e1e44d5c 159 self._opt_file.store()
4071c6d8
FC
160 self.destroy()
161 self._set_main()
162
163 def destroy(self):
e1e44d5c 164 [wdg.destroy() for wdg in self._widgets]