From 651713a9836843f183754bb5a7d2025e78c2f17b Mon Sep 17 00:00:00 2001 From: Flavio Calva Date: Mon, 10 Jan 2022 19:21:45 +0100 Subject: [PATCH] on aspect ratio changed --- pmachines/items/box.py | 32 ++++++++++++++++++-------------- pmachines/pmachines.py | 7 ++++++- pmachines/scene.py | 5 ++++- prj.org | 2 +- 4 files changed, 29 insertions(+), 17 deletions(-) diff --git a/pmachines/items/box.py b/pmachines/items/box.py index 493ae2a..d37d8ab 100644 --- a/pmachines/items/box.py +++ b/pmachines/items/box.py @@ -23,12 +23,9 @@ class Box: 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 @@ -37,7 +34,9 @@ class Box: 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])) @@ -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) - 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): @@ -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() + + def on_aspect_ratio_changed(self): + if not self._instantiated: + self._repos() diff --git a/pmachines/pmachines.py b/pmachines/pmachines.py index 0936b42..7e309e5 100755 --- a/pmachines/pmachines.py +++ b/pmachines/pmachines.py @@ -29,7 +29,7 @@ class Pmachines: 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') @@ -83,6 +83,8 @@ class Pmachines: 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']: @@ -102,3 +104,6 @@ class Pmachines: self.world.do_physics(dt) return task.cont taskMgr.add(update, 'update') + + def _on_aspect_ratio_changed(self): + self._scene.on_aspect_ratio_changed() diff --git a/pmachines/scene.py b/pmachines/scene.py index 5d046e9..339a452 100644 --- a/pmachines/scene.py +++ b/pmachines/scene.py @@ -101,7 +101,7 @@ class Scene(DirectObject): 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) @@ -137,6 +137,9 @@ class Scene(DirectObject): [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 diff --git a/prj.org b/prj.org index f4c05e2..d09a51f 100644 --- a/prj.org +++ b/prj.org @@ -1,6 +1,6 @@ * 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 -- 2.30.2