ya2 · news · projects · code · about

699cce610a1dc7f69c26bd3f4d63579f68160bd6
[pmachines.git] / ya2 / engine / gui / gui.py
1 from logging import info
2 from ya2.gameobject import GuiColleague
3 from ya2.engine.gui.cursor import MouseCursor
4 from ya2.engine.gui.browser import Browser
5
6
7 up = (0, 1)
8 down = (0, -1)
9 left = (-1, 0)
10 right = (1, 0)
11
12
13 class EngineGuiBase(GuiColleague): # no win: EngineGui strictly manages win
14
15 @staticmethod
16 def init_cls():
17 return EngineGui if EngineGuiBase.eng.lib.has_window else EngineGuiBase
18
19 @staticmethod
20 def open_browser(url): Browser.open(url)
21
22 @property
23 def resolutions(self):
24 return sorted(list(set(self.eng.lib.resolutions)))
25
26 @property
27 def closest_resolution(self):
28 def distance(res):
29 curr_res = self.eng.lib.resolution
30 return abs(res[0] - curr_res[0]) + abs(res[1] - curr_res[1])
31
32 try:
33 return min(self.resolutions, key=distance)
34 except ValueError: # sometimes we have empty resolutions
35 return self.eng.lib.resolution
36
37 def set_resolution_check(self, res):
38 res_msg = 'resolutions: {curr} (current), {res} (wanted)'
39 info(res_msg.format(curr=self.eng.lib.resolution, res=res))
40 if self.eng.lib.resolution == res: return
41 retry = 'second attempt: {curr} (current) {res} (wanted)'
42 info(retry.format(curr=self.eng.lib.resolution, res=res))
43 self.set_resolution(res, False)
44
45 def toggle_fullscreen(self):
46 self.set_resolution(self.closest_resolution)
47 self.eng.lib.toggle_fullscreen()
48
49
50 class EngineGui(EngineGuiBase):
51
52 def __init__(self, mediator):
53 EngineGuiBase.__init__(self, mediator)
54 cfg = self.eng.cfg
55 res_strings = cfg.gui_cfg.win_size.split()
56 res_ints = tuple(int(size) for size in res_strings)
57 self.set_resolution(res_ints, fullscreen=cfg.gui_cfg.fullscreen)
58 cur_cfg = cfg.cursor_cfg
59 self.cursor = MouseCursor(
60 cur_cfg.cursor_path, cur_cfg.cursor_scale, cur_cfg.cursor_color,
61 cur_cfg.cursor_hotspot)
62
63 def set_resolution(self, res, check=True, fullscreen=None):
64 info('setting resolution ' + str(res))
65 self.eng.lib.set_resolution(res, fullscreen)
66 if check: self.eng.do_later(3.0, self.set_resolution_check, [res])