ya2 · news · projects · code · about

0968759c0833f28ceb91b845306248bcfc48b9ca
[pmachines.git] / ya2 / utils / cursor.py
1 from panda3d.core import GraphicsWindow, WindowProperties
2 from ya2.p3d.gui import P3dImg as Img
3 from ya2.patterns.gameobject import GameObject
4
5
6 class MouseCursorFacade:
7
8 def show(self):
9 if not self.eng.cfg.dev_cfg.functional_test:
10 return self.cursor_img.show()
11
12 def hide(self):
13 return self.cursor_img.hide()
14
15
16 class MouseCursor(GameObject, MouseCursorFacade):
17
18 def __init__(self, filepath, scale, color, hotspot):
19 GameObject.__init__(self)
20 MouseCursorFacade.__init__(self)
21 if not filepath:
22 return
23 self.__set_std_cursor(False)
24 self.cursor_img = Img(filepath, scale=scale, foreground=True)
25 self.cursor_img.img.set_color(color)
26 # if self.eng.cfg.dev_cfg.functional_test:
27 # self.cursor_img.hide()
28 self.hotspot_dx = scale[0] * (1 - 2 * hotspot[0])
29 self.hotspot_dy = scale[2] * (1 - 2 * hotspot[1])
30 # self.eng.attach_obs(self.on_frame)
31 # self.eng.attach_obs(self.on_frame_unpausable)
32 self._tsk = taskMgr.add(self.__on_frame, 'on frame cursor')
33
34 @staticmethod
35 def __set_std_cursor(show):
36 props = WindowProperties()
37 props.set_cursor_hidden(not show)
38 if isinstance(base.win, GraphicsWindow):
39 base.win.requestProperties(props)
40
41 # def show_standard(self): self.eng.lib.show_std_cursor()
42
43 # def hide_standard(self): self.eng.lib.hide_std_cursor()
44
45 # def cursor_top(self):
46 # self.cursor_img.reparent_to(self.cursor_img.parent)
47
48 def __on_frame(self, task):
49 mwn = base.mouseWatcherNode
50 if not mwn or not mwn.hasMouse():
51 return task.again
52 mouse = mwn.get_mouse_x(), mwn.get_mouse_y()
53 h_x = mouse[0] * base.getAspectRatio() + self.hotspot_dx
54 self.cursor_img.set_pos((h_x, mouse[1] - self.hotspot_dy))
55 return task.again
56
57 # def on_frame(self):
58 # if not self.eng.pause.paused: self.__on_frame()
59
60 # def on_frame_unpausable(self):
61 # if self.eng.pause.paused: self.__on_frame()
62
63 def set_image(self, img):
64 self.cursor_img.img.set_texture(loader.load_texture(img), 1)
65
66 def destroy(self):
67 taskMgr.remove(self._tsk)
68 self.cursor_img.destroy()