ya2 · news · projects · code · about

better arrangement of the side panel
[pmachines.git] / pmachines / items / box.py
index d37d8ab277c17bb4111272b44347735dbfc99119..90d7d3fea14def966b51d7eb24a9f41fe541e786 100644 (file)
@@ -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:
 
@@ -26,25 +26,31 @@ class Box:
         self._repos()
 
     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_to = render.get_relative_point(base.cam, p_to)  # global coords
+        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()
-        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]))
-        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)
+        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()
@@ -53,8 +59,13 @@ class Box:
             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()
+        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()
+        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')