ya2 · news · projects · code · about

mouse cursor
authorFlavio Calva <f.calva@gmail.com>
Sat, 8 Jan 2022 11:30:08 +0000 (12:30 +0100)
committerFlavio Calva <f.calva@gmail.com>
Sat, 8 Jan 2022 11:30:08 +0000 (12:30 +0100)
assets/buttons/arrowUpLeft.png [new file with mode: 0644]
assets/buttons/mouse.png [new file with mode: 0644]
assets/buttons/move.png [new file with mode: 0644]
assets/buttons/rotate.png [new file with mode: 0644]
assets/buttons/trophy.png [new file with mode: 0644]
lib/engine/gui/cursor.py
pmachines/scene.py
prj.org

diff --git a/assets/buttons/arrowUpLeft.png b/assets/buttons/arrowUpLeft.png
new file mode 100644 (file)
index 0000000..5726d5a
Binary files /dev/null and b/assets/buttons/arrowUpLeft.png differ
diff --git a/assets/buttons/mouse.png b/assets/buttons/mouse.png
new file mode 100644 (file)
index 0000000..46042c2
Binary files /dev/null and b/assets/buttons/mouse.png differ
diff --git a/assets/buttons/move.png b/assets/buttons/move.png
new file mode 100644 (file)
index 0000000..4795f1b
Binary files /dev/null and b/assets/buttons/move.png differ
diff --git a/assets/buttons/rotate.png b/assets/buttons/rotate.png
new file mode 100644 (file)
index 0000000..11b093f
Binary files /dev/null and b/assets/buttons/rotate.png differ
diff --git a/assets/buttons/trophy.png b/assets/buttons/trophy.png
new file mode 100644 (file)
index 0000000..a013f42
Binary files /dev/null and b/assets/buttons/trophy.png differ
index 580dd2e1a1162958c1f654b6bade0f7715fb1485..90448679816a7ed0406cc2e1d5427858280962df 100644 (file)
@@ -1,3 +1,4 @@
+from panda3d.core import GraphicsWindow, WindowProperties
 from lib.lib.gui import Img
 from lib.gameobject import GameObject
 
@@ -16,31 +17,46 @@ class MouseCursor(GameObject, MouseCursorFacade):
         GameObject.__init__(self)
         MouseCursorFacade.__init__(self)
         if not filepath: return
-        self.eng.lib.hide_std_cursor()
+        self.__set_std_cursor(False)
         self.cursor_img = Img(filepath, scale=scale, foreground=True)
         self.cursor_img.img.set_color(color)
-        if self.eng.cfg.dev_cfg.functional_test:
-            self.cursor_img.hide()
+        #if self.eng.cfg.dev_cfg.functional_test:
+        #    self.cursor_img.hide()
         self.hotspot_dx = scale[0] * (1 - 2 * hotspot[0])
         self.hotspot_dy = scale[2] * (1 - 2 * hotspot[1])
-        self.eng.attach_obs(self.on_frame)
-        self.eng.attach_obs(self.on_frame_unpausable)
-
-    def show_standard(self): self.eng.lib.show_std_cursor()
-
-    def hide_standard(self): self.eng.lib.hide_std_cursor()
-
-    def cursor_top(self):
-        self.cursor_img.reparent_to(self.cursor_img.parent)
-
-    def __on_frame(self):
-        mouse = self.eng.lib.mousepos
-        if not mouse: return
-        h_x = mouse[0] * self.eng.lib.aspect_ratio + self.hotspot_dx
+        #self.eng.attach_obs(self.on_frame)
+        #self.eng.attach_obs(self.on_frame_unpausable)
+        taskMgr.add(self.__on_frame, 'on frame cursor')
+
+    @staticmethod
+    def __set_std_cursor(show):
+        props = WindowProperties()
+        props.set_cursor_hidden(not show)
+        if isinstance(base.win, GraphicsWindow):
+            base.win.requestProperties(props)
+
+    #def show_standard(self): self.eng.lib.show_std_cursor()
+
+    #def hide_standard(self): self.eng.lib.hide_std_cursor()
+
+    #def cursor_top(self):
+    #    self.cursor_img.reparent_to(self.cursor_img.parent)
+
+    def __on_frame(self, task):
+        mwn = base.mouseWatcherNode
+        if not mwn: mouse = 0, 0
+        elif not mwn.hasMouse(): mouse = 0, 0
+        else: mouse = mwn.get_mouse_x(), mwn.get_mouse_y()
+        if not mouse: return task.again
+        h_x = mouse[0] * base.getAspectRatio() + self.hotspot_dx
         self.cursor_img.set_pos((h_x, mouse[1] - self.hotspot_dy))
+        return task.again
+
+    #def on_frame(self):
+    #    if not self.eng.pause.paused: self.__on_frame()
 
-    def on_frame(self):
-        if not self.eng.pause.paused: self.__on_frame()
+    #def on_frame_unpausable(self):
+    #    if self.eng.pause.paused: self.__on_frame()
 
-    def on_frame_unpausable(self):
-        if self.eng.pause.paused: self.__on_frame()
+    def set_image(self, img):
+        self.cursor_img.img.set_texture(loader.load_texture(img), 1)
index 3009fb7e6aff06f324b045aa59480e01b57ccd17..75886b4c5d3213e8148b739c2ac16e0fc2dcccd1 100644 (file)
@@ -6,6 +6,7 @@ from direct.gui.DirectGuiGlobals import FLAT, DISABLED, NORMAL
 from direct.showbase.DirectObject import DirectObject
 from pmachines.items.background import Background
 from pmachines.items.box import Box
+from lib.engine.gui.cursor import MouseCursor
 
 
 class Scene(DirectObject):
@@ -14,6 +15,9 @@ class Scene(DirectObject):
         super().__init__()
         self._world = world
         self._set_camera()
+        self._cursor = MouseCursor(
+            'assets/buttons/arrowUpLeft.png', (.04, 1, .04), (.5, .5, .5, 1),
+            (.01, .01))
         self._set_gui()
         self._set_lights()
         self._set_input()
@@ -119,6 +123,8 @@ class Scene(DirectObject):
         for hit in self._get_hits():
             for item in [i for i in self.items if hit.get_node() == i.node]:
                 getattr(item, method)(pos)
+                img = 'move' if method == 'on_click_l' else 'rotate'
+                self._cursor.set_image('assets/buttons/%s.png' % img)
 
     def on_click_l(self):
         self._on_click('on_click_l')
@@ -128,6 +134,7 @@ class Scene(DirectObject):
 
     def on_release(self):
         [item.on_release() for item in self.items]
+        self._cursor.set_image('assets/buttons/arrowUpLeft.png')
 
     def on_frame(self, task):
         hits = self._get_hits()
diff --git a/prj.org b/prj.org
index b4213b6adb363eeb4e18f42e1f7b60748655cf5a..de7dc9fb5f90b2249aa161286e8a347b1390e64f 100644 (file)
--- a/prj.org
+++ b/prj.org
@@ -1,11 +1,10 @@
 * issues
 * todo
-** mouse cursor
 ** music and sfx
 ** instructions
-** main menu
 ** configuration (physics debug)
 ** rotation with vectors/quaternions (fixes some glitches)
+** main menu
 ** implement the operations of the buttons
 ** build pipeline
 ** magnet