ya2 · news · projects · code · about

volume option
[pmachines.git] / pmachines / menu.py
1 from panda3d.core import Texture, TextNode
2 from direct.gui.DirectGui import DirectButton, DirectCheckButton, \
3 DirectOptionMenu, DirectSlider
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 self._common_slider = self._common | {
31 'range': (0, 1),
32 'thumb_frameColor': (.4, .4, .4, .4),
33 'thumb_scale': 1.6,
34 'scale': .4}
35 del self._common_slider['rolloverSound']
36 del self._common_slider['clickSound']
37 self._set_main()
38
39 def _set_main(self):
40 self._widgets = []
41 self._widgets += [DirectButton(
42 text=_('Play'), pos=(0, 1, .6), command=self.on_play,
43 **self._common_btn)]
44 self._widgets += [DirectButton(
45 text=_('Options'), pos=(0, 1, .3), command=self.on_options,
46 **self._common_btn)]
47 self._widgets += [DirectButton(
48 text=_('Support us'), pos=(0, 1, 0), command=self.on_play,
49 **self._common_btn)]
50 self._widgets += [DirectButton(
51 text=_('Credits'), pos=(0, 1, -.3), command=self.on_play,
52 **self._common_btn)]
53 self._widgets += [DirectButton(
54 text=_('Exit'), pos=(0, 1, -.6), command=self.on_play,
55 **self._common_btn)]
56
57 def _set_options(self):
58 self._widgets = []
59 self._lang_funcs = [lambda: _('English'), lambda: _('Italian')]
60 items = [fnc() for fnc in self._lang_funcs]
61 inititem = {
62 'en': _('English'),
63 'it': _('Italian')
64 }[self._opt_file['settings']['language']]
65 hlc = self._common_btn['frameColor']
66 hlc = (hlc[0] + .2, hlc[1] + .2, hlc[2] + .2, hlc[3] + .2)
67 btn = DirectOptionMenu(
68 text=_('Language'), items=items, initialitem=inititem,
69 pos=(0, 1, .6), command=self.on_language,
70 item_frameColor=self._common_btn['frameColor'],
71 popupMarker_frameColor=self._common_btn['frameColor'],
72 item_relief=self._common_btn['relief'],
73 item_text_font=self._common_btn['text_font'],
74 item_text_fg=self._common_btn['text_fg'],
75 textMayChange=1, highlightColor=hlc,
76 **self._common_btn)
77 btn.popupMenu['frameColor'] = self._common_btn['frameColor']
78 btn.popupMenu['relief'] = self._common_btn['relief']
79 self._widgets += [btn]
80 self._widgets += [OnscreenText(
81 _('Volume'), pos=(-.1, .3), font=self._common['text_font'],
82 scale=self._common['scale'], fg=self._common['text_fg'],
83 align=TextNode.A_right)]
84 self._widgets += [DirectSlider(
85 pos=(.5, 1, .32),
86 value=self._opt_file['settings']['volume'],
87 command=self.on_volume,
88 **self._common_slider)]
89 self._slider = self._widgets[-1]
90 self._widgets += [DirectButton(
91 text=_('Back'), pos=(0, 1, -.6), command=self.on_back,
92 **self._common_btn)]
93
94 def on_play(self):
95 self._fsm.demand('Scene')
96
97 def on_options(self):
98 self.destroy()
99 self._set_options()
100
101 def on_language(self, arg):
102 lang_code = {
103 _('English'): 'en_EN',
104 _('Italian'): 'it_IT'}[arg]
105 self._lang_mgr.set_lang(lang_code)
106 self._opt_file['settings']['language'] = lang_code[:2]
107 self._opt_file.store()
108 self.on_options()
109
110 def on_volume(self):
111 self._opt_file['settings']['volume'] = self._slider['value']
112 self._music.set_volume(self._slider['value'])
113
114 def on_back(self):
115 self._opt_file.store()
116 self.destroy()
117 self._set_main()
118
119 def destroy(self):
120 [wdg.destroy() for wdg in self._widgets]