ya2 · news · projects · code · about

functional tests: editor
[pmachines.git] / ya2 / utils / cursor.py
1 from panda3d.core import WindowProperties, Texture
2 from direct.gui.OnscreenImage import OnscreenImage
3 from ya2.utils.logics import LogicsTools
4
5
6 class MouseCursor:
7
8 def __init__(self, image_path, scale, color, hotspot_position, functional_test):
9 self.__functional_test = functional_test
10 if not image_path: return
11 self.__hide_system_cursor()
12 self.__set_image(image_path, scale, color)
13 self.__set_hotspot(hotspot_position, scale)
14 self._tsk = taskMgr.add(self.__on_frame, 'on_frame_cursor')
15
16 def __hide_system_cursor(self):
17 p = WindowProperties()
18 p.set_cursor_hidden(True)
19 if LogicsTools.windowed: base.win.request_properties(p)
20
21 def __set_image(self, image_path, scale, color):
22 self.__image = OnscreenImage(image_path, scale=scale)
23 self.__image.set_color(color)
24 self.__image.set_bin('gui-popup', 50)
25 alpha_formats = [Texture.FRgba]
26 if self.__image.get_texture().get_format() in alpha_formats:
27 self.__image.set_transparency(True)
28
29 def __set_hotspot(self, hotspot_position, scale):
30 self.__hotspot_dx = scale[0] * (1 - 2 * hotspot_position[0])
31 self.__hotspot_dy = scale[2] * (1 - 2 * hotspot_position[1])
32
33 def __on_frame(self, task):
34 m = base.mouseWatcherNode
35 if not m or not m.hasMouse(): return task.again
36 mouse = m.get_mouse_x(), m.get_mouse_y()
37 h_x = mouse[0] * base.get_aspect_ratio() + self.__hotspot_dx
38 self.__image.set_pos((h_x, 1, mouse[1] - self.__hotspot_dy))
39 return task.again
40
41 def set_image(self, image):
42 self.__image.set_texture(loader.load_texture(image), 1)
43
44 def set_color(self, color):
45 self.__image.set_color(color)
46
47 def show(self):
48 if not self.__functional_test: return self.__image.show()
49
50 def hide(self):
51 return self.__image.hide()
52
53 def destroy(self):
54 taskMgr.remove(self._tsk)
55 self.__image.destroy()