X-Git-Url: http://git.ya2.it/?p=pmachines.git;a=blobdiff_plain;f=pmachines%2Fitems%2Fbox.py;h=90d7d3fea14def966b51d7eb24a9f41fe541e786;hp=493ae2a26766d046a5df3d773c1e7b5eb7ff7120;hb=13263131bcd31eeab174a2604ff01a9a12e7910e;hpb=7850ccc485a9bf387a6c9f12ce86b1e5c2b2d059 diff --git a/pmachines/items/box.py b/pmachines/items/box.py index 493ae2a..90d7d3f 100644 --- a/pmachines/items/box.py +++ b/pmachines/items/box.py @@ -1,7 +1,7 @@ from panda3d.core import CullFaceAttrib, Point3, NodePath, Point2, Texture from panda3d.bullet import BulletBoxShape, BulletRigidBodyNode from direct.gui.OnscreenText import OnscreenText - +from lib.lib.p3d.gfx import P3dGfxMgr class Box: @@ -23,38 +23,49 @@ 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): - 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_to = render.get_relative_point(base.cam, p_to) # global coords + def _repos(self): + p_from, p_to = P3dGfxMgr.world_from_to((-1, 1)) for hit in self._world.ray_test_all(p_from, p_to).get_hits(): if hit.get_node() == self._plane_node: pos = hit.get_hit_pos() + corner = P3dGfxMgr.screen_coord(pos) + bounds = self._np.get_tight_bounds() + bounds = bounds[0] - self._np.get_pos(), bounds[1] - self._np.get_pos() + self._np.set_pos(pos) + dist = -1, -1 + def __update(set, get, delta): + set(get() + delta) + top_left = self._np.get_pos() + (bounds[0][0], bounds[0][1], bounds[1][2]) + tl2d = P3dGfxMgr.screen_coord(top_left) + tmpnode = NodePath('tmp') + tmpnode.set_pos(tl2d[0], 0, tl2d[1]) + cornernode = NodePath('corner') + cornernode.set_pos(corner[0], 0, corner[1]) + dist = tmpnode.get_pos(cornernode) + tmpnode.remove_node() + cornernode.remove_node() + return dist + while dist[0] < .01: + dist = __update(self._np.set_x, self._np.get_x, .01) + while dist[2] > -.01: + dist = __update(self._np.set_z, self._np.get_z, -.01) + 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)) + pos = self._np.get_pos() + (bounds[1][0], bounds[0][1], bounds[0][2]) + p2d = P3dGfxMgr.screen_coord(pos) + self._txt['pos'] = p2d + + def get_corner(self): bounds = self._np.get_tight_bounds() - dpos = bounds[1][0], 0, -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])) - coord3d = new_node.get_pos(base.cam) - coord2d = Point2() - 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)) - new_node.remove_node() + return bounds[1][0], bounds[1][1], bounds[0][2] def _set_outline_model(self): self._outline_model = loader.load_model('assets/gltf/box/box.gltf') @@ -113,3 +124,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()