ya2 · news · projects · code · about

mouse cursor
[pmachines.git] / lib / engine / gui / cursor.py
1 from panda3d.core import GraphicsWindow, WindowProperties
2 from lib.lib.gui import Img
3 from lib.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 def hide(self): return self.cursor_img.hide()
12
13
14 class MouseCursor(GameObject, MouseCursorFacade):
15
16 def __init__(self, filepath, scale, color, hotspot):
17 GameObject.__init__(self)
18 MouseCursorFacade.__init__(self)
19 if not filepath: return
20 self.__set_std_cursor(False)
21 self.cursor_img = Img(filepath, scale=scale, foreground=True)
22 self.cursor_img.img.set_color(color)
23 #if self.eng.cfg.dev_cfg.functional_test:
24 # self.cursor_img.hide()
25 self.hotspot_dx = scale[0] * (1 - 2 * hotspot[0])
26 self.hotspot_dy = scale[2] * (1 - 2 * hotspot[1])
27 #self.eng.attach_obs(self.on_frame)
28 #self.eng.attach_obs(self.on_frame_unpausable)
29 taskMgr.add(self.__on_frame, 'on frame cursor')
30
31 @staticmethod
32 def __set_std_cursor(show):
33 props = WindowProperties()
34 props.set_cursor_hidden(not show)
35 if isinstance(base.win, GraphicsWindow):
36 base.win.requestProperties(props)
37
38 #def show_standard(self): self.eng.lib.show_std_cursor()
39
40 #def hide_standard(self): self.eng.lib.hide_std_cursor()
41
42 #def cursor_top(self):
43 # self.cursor_img.reparent_to(self.cursor_img.parent)
44
45 def __on_frame(self, task):
46 mwn = base.mouseWatcherNode
47 if not mwn: mouse = 0, 0
48 elif not mwn.hasMouse(): mouse = 0, 0
49 else: mouse = mwn.get_mouse_x(), mwn.get_mouse_y()
50 if not mouse: return task.again
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)