Commit | Line | Data |
---|---|---|
79f81d48 | 1 | from panda3d.core import GraphicsWindow, WindowProperties |
b35b1f62 | 2 | from ya2.p3d.gui import P3dImg as Img |
8ee66edd FC |
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() | |
e65a09cf FC |
10 | |
11 | def hide(self): | |
12 | return self.cursor_img.hide() | |
8ee66edd FC |
13 | |
14 | ||
aed9737a | 15 | class 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) |
8ee66edd FC |
22 | self.cursor_img = Img(filepath, scale=scale, foreground=True) |
23 | self.cursor_img.img.set_color(color) | |
e65a09cf FC |
24 | # if self.eng.cfg.dev_cfg.functional_test: |
25 | # self.cursor_img.hide() | |
8ee66edd FC |
26 | self.hotspot_dx = scale[0] * (1 - 2 * hotspot[0]) |
27 | self.hotspot_dy = scale[2] * (1 - 2 * hotspot[1]) | |
e65a09cf FC |
28 | # self.eng.attach_obs(self.on_frame) |
29 | # self.eng.attach_obs(self.on_frame_unpausable) | |
638deddf | 30 | self._tsk = taskMgr.add(self.__on_frame, 'on frame cursor') |
79f81d48 FC |
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 | ||
e65a09cf | 39 | # def show_standard(self): self.eng.lib.show_std_cursor() |
79f81d48 | 40 | |
e65a09cf | 41 | # def hide_standard(self): self.eng.lib.hide_std_cursor() |
79f81d48 | 42 | |
e65a09cf FC |
43 | # def cursor_top(self): |
44 | # self.cursor_img.reparent_to(self.cursor_img.parent) | |
79f81d48 FC |
45 | |
46 | def __on_frame(self, task): | |
47 | mwn = base.mouseWatcherNode | |
13263131 FC |
48 | if not mwn or not mwn.hasMouse(): |
49 | return task.again | |
50 | mouse = mwn.get_mouse_x(), mwn.get_mouse_y() | |
79f81d48 | 51 | h_x = mouse[0] * base.getAspectRatio() + self.hotspot_dx |
8ee66edd | 52 | self.cursor_img.set_pos((h_x, mouse[1] - self.hotspot_dy)) |
79f81d48 FC |
53 | return task.again |
54 | ||
e65a09cf FC |
55 | # def on_frame(self): |
56 | # if not self.eng.pause.paused: self.__on_frame() | |
8ee66edd | 57 | |
e65a09cf FC |
58 | # def on_frame_unpausable(self): |
59 | # if self.eng.pause.paused: self.__on_frame() | |
8ee66edd | 60 | |
79f81d48 FC |
61 | def set_image(self, img): |
62 | self.cursor_img.img.set_texture(loader.load_texture(img), 1) | |
638deddf FC |
63 | |
64 | def destroy(self): | |
65 | taskMgr.remove(self._tsk) | |
66 | self.cursor_img.destroy() |