ya2 · news · projects · code · about

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