ya2 · news · projects · code · about

on aspect ratio changed
authorFlavio Calva <f.calva@gmail.com>
Mon, 10 Jan 2022 18:21:45 +0000 (19:21 +0100)
committerFlavio Calva <f.calva@gmail.com>
Mon, 10 Jan 2022 18:21:45 +0000 (19:21 +0100)
pmachines/items/box.py
pmachines/pmachines.py
pmachines/scene.py
prj.org

index 493ae2a26766d046a5df3d773c1e7b5eb7ff7120..d37d8ab277c17bb4111272b44347735dbfc99119 100644 (file)
@@ -23,12 +23,9 @@ class Box:
         self._prev_rot_info = None
         self._instantiated = False
         taskMgr.add(self.on_frame, 'on_frame')
         self._prev_rot_info = None
         self._instantiated = False
         taskMgr.add(self.on_frame, 'on_frame')
-        #self._set_side()
-        taskMgr.doMethodLater(.01, lambda task: self._set_side(), 'a')
-        # i get weird values in the first frame; i could restore this approach
-        # when the hook with the events of the window is up
+        self._repos()
 
 
-    def _set_side(self):
+    def _repos(self):
         p_from, p_to = Point3(), Point3()    # in camera coordinates
         base.camLens.extrude((-1, 1), p_from, p_to)
         p_from = render.get_relative_point(base.cam, p_from)  # global coords
         p_from, p_to = Point3(), Point3()    # in camera coordinates
         base.camLens.extrude((-1, 1), p_from, p_to)
         p_from = render.get_relative_point(base.cam, p_from)  # global coords
@@ -37,7 +34,9 @@ class Box:
             if hit.get_node() == self._plane_node:
                 pos = hit.get_hit_pos()
         bounds = self._np.get_tight_bounds()
             if hit.get_node() == self._plane_node:
                 pos = hit.get_hit_pos()
         bounds = self._np.get_tight_bounds()
-        dpos = bounds[1][0], 0, -bounds[1][2]
+        bounds = bounds[0] - self._np.get_pos(), bounds[1] - self._np.get_pos()
+        margin = .3, .2
+        dpos = bounds[1][0] + margin[0], 0, -bounds[1][2] - margin[1]
         self._np.set_pos(pos + dpos)
         new_node = NodePath('temp')
         new_node.set_pos(pos + dpos + (bounds[1][0], bounds[1][1], -bounds[1][2]))
         self._np.set_pos(pos + dpos)
         new_node = NodePath('temp')
         new_node.set_pos(pos + dpos + (bounds[1][0], bounds[1][1], -bounds[1][2]))
@@ -46,14 +45,15 @@ class Box:
         base.camLens.project(coord3d, coord2d)
         coord_r2d = Point3(coord2d[0], 0, coord2d[1])
         coord_a2d = base.aspect2d.get_relative_point(render2d, coord_r2d)
         base.camLens.project(coord3d, coord2d)
         coord_r2d = Point3(coord2d[0], 0, coord2d[1])
         coord_a2d = base.aspect2d.get_relative_point(render2d, coord_r2d)
-        font = base.loader.load_font('assets/fonts/Hanken-Book.ttf')
-        font.clear()
-        font.set_pixels_per_unit(60)
-        font.set_minfilter(Texture.FTLinearMipmapLinear)
-        font.set_outline((0, 0, 0, 1), .8, .2)
-        self._txt = OnscreenText(
-            str(self._count), pos=(coord_a2d[0], coord_a2d[2]),
-            font=font, scale=0.06, fg=(.9, .9, .9, 1))
+        if not hasattr(self, '_txt'):
+            font = base.loader.load_font('assets/fonts/Hanken-Book.ttf')
+            font.clear()
+            font.set_pixels_per_unit(60)
+            font.set_minfilter(Texture.FTLinearMipmapLinear)
+            font.set_outline((0, 0, 0, 1), .8, .2)
+            self._txt = OnscreenText(
+                str(self._count), font=font, scale=0.06, fg=(.9, .9, .9, 1))
+        self._txt['pos'] = coord_a2d[0], coord_a2d[2]
         new_node.remove_node()
 
     def _set_outline_model(self):
         new_node.remove_node()
 
     def _set_outline_model(self):
@@ -113,3 +113,7 @@ class Box:
             d_angle = curr_vec.signed_angle_deg(start_vec, (0, -1, 0))
             self._np.set_r(self._prev_rot_info[2] + d_angle)
             self._prev_rot_info = pos, self._np.get_pos(), self._np.get_r()
             d_angle = curr_vec.signed_angle_deg(start_vec, (0, -1, 0))
             self._np.set_r(self._prev_rot_info[2] + d_angle)
             self._prev_rot_info = pos, self._np.get_pos(), self._np.get_r()
+
+    def on_aspect_ratio_changed(self):
+        if not self._instantiated:
+            self._repos()
index 0936b42bb0203aad58c4a1b3d88bbf28f40ff95e..7e309e5adef96eb1fbd1bfb249403ad6e6c4706e 100755 (executable)
@@ -29,7 +29,7 @@ class Pmachines:
             return
         MusicMgr(self._options['settings']['volume'])
         self._set_physics()
             return
         MusicMgr(self._options['settings']['volume'])
         self._set_physics()
-        Scene(self.world)
+        self._scene = Scene(self.world)
 
     def _configure(self):
         load_prc_file_data('', 'window-title pmachines')
 
     def _configure(self):
         load_prc_file_data('', 'window-title pmachines')
@@ -83,6 +83,8 @@ class Pmachines:
         render.setAntialias(AntialiasAttrib.MAuto)
         self.base.set_background_color(0, 0, 0, 1)
         self.base.disable_mouse()
         render.setAntialias(AntialiasAttrib.MAuto)
         self.base.set_background_color(0, 0, 0, 1)
         self.base.disable_mouse()
+        #self.base.accept('window-event', self._on_win_evt)
+        self.base.accept('aspectRatioChanged', self._on_aspect_ratio_changed)
 
     def _set_physics(self):
         if self._options['development']['physics_debug']:
 
     def _set_physics(self):
         if self._options['development']['physics_debug']:
@@ -102,3 +104,6 @@ class Pmachines:
             self.world.do_physics(dt)
             return task.cont
         taskMgr.add(update, 'update')
             self.world.do_physics(dt)
             return task.cont
         taskMgr.add(update, 'update')
+
+    def _on_aspect_ratio_changed(self):
+        self._scene.on_aspect_ratio_changed()
index 5d046e9b925b15b50b30d140e1562d75f43590cb..339a452b34e6098b02dc6c53e28cc194c9f07787 100644 (file)
@@ -101,7 +101,7 @@ class Scene(DirectObject):
         self.accept('mouse3-up', self.on_release)
 
     def _set_mouse_plane(self):
         self.accept('mouse3-up', self.on_release)
 
     def _set_mouse_plane(self):
-        shape = BulletPlaneShape((0, -1, 0), 1)
+        shape = BulletPlaneShape((0, -1, 0), 0)
         #self._mouse_plane_node = BulletRigidBodyNode('mouse plane')
         self._mouse_plane_node = BulletGhostNode('mouse plane')
         self._mouse_plane_node.addShape(shape)
         #self._mouse_plane_node = BulletRigidBodyNode('mouse plane')
         self._mouse_plane_node = BulletGhostNode('mouse plane')
         self._mouse_plane_node.addShape(shape)
@@ -137,6 +137,9 @@ class Scene(DirectObject):
         [item.on_release() for item in self.items]
         self._cursor.set_image('assets/buttons/arrowUpLeft.png')
 
         [item.on_release() for item in self.items]
         self._cursor.set_image('assets/buttons/arrowUpLeft.png')
 
+    def on_aspect_ratio_changed(self):
+        [item.on_aspect_ratio_changed() for item in self.items]
+
     def on_frame(self, task):
         hits = self._get_hits()
         pos = None
     def on_frame(self, task):
         hits = self._get_hits()
         pos = None
diff --git a/prj.org b/prj.org
index f4c05e216f90cbac72fc183fbecbebc02a0ac6e3..d09a51fe48a25e7003d1f966e235f513717f8c33 100644 (file)
--- a/prj.org
+++ b/prj.org
@@ -1,6 +1,6 @@
 * issues
 * todo
 * issues
 * todo
-** update the to-be-instantiated's pos if the windows is updated
+** make a semi-transparent plane under the instantiable items
 ** manage the physics of the to-be-instantiated with a ghost
 ** instructions
 ** main menu
 ** manage the physics of the to-be-instantiated with a ghost
 ** instructions
 ** main menu