ya2 · news · projects · code · about

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