Commit | Line | Data |
---|---|---|
79f81d48 | 1 | from panda3d.core import GraphicsWindow, WindowProperties |
8ee66edd FC |
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 | |
79f81d48 | 20 | self.__set_std_cursor(False) |
8ee66edd FC |
21 | self.cursor_img = Img(filepath, scale=scale, foreground=True) |
22 | self.cursor_img.img.set_color(color) | |
79f81d48 FC |
23 | #if self.eng.cfg.dev_cfg.functional_test: |
24 | # self.cursor_img.hide() | |
8ee66edd FC |
25 | self.hotspot_dx = scale[0] * (1 - 2 * hotspot[0]) |
26 | self.hotspot_dy = scale[2] * (1 - 2 * hotspot[1]) | |
79f81d48 FC |
27 | #self.eng.attach_obs(self.on_frame) |
28 | #self.eng.attach_obs(self.on_frame_unpausable) | |
638deddf | 29 | self._tsk = taskMgr.add(self.__on_frame, 'on frame cursor') |
79f81d48 FC |
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 | |
13263131 FC |
47 | if not mwn or not mwn.hasMouse(): |
48 | return task.again | |
49 | mouse = mwn.get_mouse_x(), mwn.get_mouse_y() | |
79f81d48 | 50 | h_x = mouse[0] * base.getAspectRatio() + self.hotspot_dx |
8ee66edd | 51 | self.cursor_img.set_pos((h_x, mouse[1] - self.hotspot_dy)) |
79f81d48 FC |
52 | return task.again |
53 | ||
54 | #def on_frame(self): | |
55 | # if not self.eng.pause.paused: self.__on_frame() | |
8ee66edd | 56 | |
79f81d48 FC |
57 | #def on_frame_unpausable(self): |
58 | # if self.eng.pause.paused: self.__on_frame() | |
8ee66edd | 59 | |
79f81d48 FC |
60 | def set_image(self, img): |
61 | self.cursor_img.img.set_texture(loader.load_texture(img), 1) | |
638deddf FC |
62 | |
63 | def destroy(self): | |
64 | taskMgr.remove(self._tsk) | |
65 | self.cursor_img.destroy() |