1 from logging
import info
, debug
2 from panda3d
.core
import WindowProperties
, LVector2i
3 from direct
.showbase
.DirectObject
import DirectObject
4 from direct
.gui
.DirectGui
import DirectFrame
5 from ya2
.utils
.gfx
import DirectGuiMixin
, pos_pixel
6 from ya2
.utils
.audio
import AudioTools
7 from ya2
.utils
.gui
.base_page
import BasePage
, OptionMenuArgs
, TextArgs
, SliderArgs
, CheckButtonArgs
, ButtonArgs
10 class OptionsPage(BasePage
, DirectObject
):
12 def __init__(self
, page_info
, option_file
, set_language
, gfx_pipeline
):
13 DirectObject
.__init
__(self
)
14 self
.__option
_file
= option_file
15 self
.__set
_language
= set_language
16 self
.__gfx
_pipeline
= gfx_pipeline
17 self
.__enforced
_resolution
= ''
18 super().__init
__(page_info
)
20 def _build_widgets(self
):
21 self
.__build
_language
()
23 self
.__build
_fullscreen
()
24 self
.__build
_resolution
()
26 self
.__build
_shadows
()
29 def __build_language(self
):
30 self
._lang
_funcs
= [lambda: _('English'), lambda: _('Italian')]
31 items
= [fnc() for fnc
in self
._lang
_funcs
]
35 }[self
.__option
_file
['settings']['language']]
38 for element
in ['english', 'italian']:
39 if element
in self
._page
_info
.test_positions
:
40 del self
._page
_info
.test_positions
[element
]
41 for lng
, btn
in zip(['english', 'italian'], comps
):
42 btn
.__class
__ = type('DirectFrameMixed', (DirectFrame
, DirectGuiMixin
), {})
44 self
._page
_info
.test_positions
[lng
] = (pos
[0] + 5, pos
[1])
45 language_menu
= OptionMenuArgs(
49 initial_item
=inititem
,
51 command
=self
.on_language
)
52 self
._add
_option
_menu
(language_menu
, lang_cb
)
54 def __build_volume(self
):
62 value
=self
.__option
_file
['settings']['volume'],
64 command
=self
.on_volume
)
65 self
._slider
= self
._add
_slider
(s
)
67 def __build_fullscreen(self
):
72 command
=self
.on_fullscreen
,
73 value
=self
.__option
_file
['settings']['fullscreen'])
74 self
._add
_check
_button
(c
)
76 def __build_resolution(self
):
77 res
= self
.__option
_file
['settings']['resolution']
78 d_i
= base
.pipe
.get_display_information()
80 return d_i
.get_display_mode_width(idx
), \
81 d_i
.get_display_mode_height(idx
)
83 _res(idx
) for idx
in range(d_i
.get_total_display_modes())]
84 resolutions
= list(set(resolutions
))
85 resolutions
= sorted(resolutions
)
86 resolutions
= [(str(_res
[0]), str(_res
[1])) for _res
in resolutions
]
87 resolutions
= ['x'.join(_res
) for _res
in resolutions
]
91 for element
in ['res_1440x900', 'res_1360x768']:
92 if element
in self
._page
_info
.test_positions
:
93 del self
._page
_info
.test_positions
[element
]
94 for tgt_res
in ['1440x900', '1360x768']:
96 if btn
['text'] == tgt_res
:
98 self
._page
_info
.test_positions
['res_' + tgt_res
] = (pos
[0] + 5, pos
[1])
101 text
=_('Resolution'),
105 command
=self
.on_resolution
)
106 o
= self
._add
_option
_menu
(r
, res_cb
)
107 pos_res
= o
.pos_pixel()
108 self
._page
_info
.test_positions
['res_1440x900'] = [pos_res
[0] + 320, pos_res
[1] + 75]
109 self
._page
_info
.test_positions
['res_1360x768'] = [pos_res
[0] + 430, pos_res
[1] -285]
111 def __build_aa(self
):
114 text
=_('Antialiasing'),
117 value
=self
.__option
_file
['settings']['antialiasing'])
118 self
._add
_check
_button
(c
)
120 def __build_shadows(self
):
125 command
=self
.on_shadows
,
126 value
=self
.__option
_file
['settings']['shadows'])
127 self
._add
_check
_button
(c
)
129 def __build_back(self
):
130 back_button
= ButtonArgs(
134 command
=self
._set
_page
,
135 command_args
=['main'])
136 self
._add
_button
(back_button
)
137 self
.accept('enforce_resolution', self
.enforce_resolution
)
139 def enforce_resolution(self
, resolution
):
140 self
.__enforced
_resolution
= resolution
141 info('enforced resolution: ' + resolution
)
143 def on_language(self
, arg
):
145 _('English'): 'en_EN',
146 _('Italian'): 'it_IT'}[arg
]
147 self
.__set
_language
(lang_code
)
148 self
.__option
_file
['settings']['language'] = lang_code
[:2]
149 self
.__option
_file
.store()
150 self
._set
_page
('options')
153 self
.__option
_file
['settings']['volume'] = self
._slider
['value']
154 AudioTools
.set_volume(self
._slider
['value'])
156 def on_fullscreen(self
, arg
):
157 props
= WindowProperties()
158 props
.set_fullscreen(arg
)
159 if not self
._page
_info
.running_functional_tests
:
160 base
.win
.request_properties(props
)
161 # if we actually switch to fullscreen during the tests then
162 # exwm inside qemu can't restore the correct resolution
163 # i may re-enable this if/when i run the tests onto a
165 self
.__option
_file
['settings']['fullscreen'] = int(arg
)
166 self
.__option
_file
.store()
168 def on_resolution(self
, arg
):
169 info('on resolution: %s (%s)' % (arg
, self
.__enforced
_resolution
))
170 arg
= self
.__enforced
_resolution
or arg
171 info('set resolution: %s' % arg
)
172 props
= WindowProperties()
173 props
.set_size(LVector2i(*[int(_res
) for _res
in arg
.split('x')]))
174 base
.win
.request_properties(props
)
175 self
.__option
_file
['settings']['resolution'] = arg
176 self
.__option
_file
.store()
178 def on_aa(self
, arg
):
179 self
.__gfx
_pipeline
.msaa_samples
= 4 if arg
else 1
180 debug(f
'msaa: {self.__gfx_pipeline.msaa_samples}')
181 self
.__option
_file
['settings']['antialiasing'] = int(arg
)
182 self
.__option
_file
.store()
184 def on_shadows(self
, arg
):
185 self
.__gfx
_pipeline
.enable_shadows
= int(arg
)
186 debug(f
'shadows: {self.__gfx_pipeline.enable_shadows}')
187 self
.__option
_file
['settings']['shadows'] = int(arg
)
188 self
.__option
_file
.store()
191 self
.__option
_file
.store()
196 self
.ignore('enforce_resolution')