ya2 · news · projects · code · about

drag and drop
[pmachines.git] / pmachines / items / box.py
index ffb88f058fb9c614619c1c6f7f02b9b354e3b383..52c577b9984cbbdc0c5e18747f286c09fb26aa48 100644 (file)
@@ -4,34 +4,47 @@ from panda3d.bullet import BulletBoxShape, BulletRigidBodyNode
 class Box:
 
     def __init__(self, world):
+        self._world = world
         shape = BulletBoxShape((.5, .5, .5))
         self.node = BulletRigidBodyNode('box')
-        # self.node.set_mass(1)  # static/dynamic
         self.node.add_shape(shape)
-        np = render.attach_new_node(self.node)
-        np.set_pos(0, 0, 1)
+        self._np = render.attach_new_node(self.node)
+        self._np.set_pos(0, 0, 1)
         world.attach_rigid_body(self.node)
         model = loader.load_model('assets/gltf/box/box.gltf')
         model.flatten_light()
-        model.reparent_to(np)
-        self._set_outline_model(np)
+        model.reparent_to(self._np)
+        self._set_outline_model()
+        self._start_drag_pos = None
 
-    def _set_outline_model(self, np):
+    def _set_outline_model(self):
         self._outline_model = loader.load_model('assets/gltf/box/box.gltf')
         clockw = CullFaceAttrib.MCullClockwise
         self._outline_model.set_attrib(CullFaceAttrib.make(clockw))
-        self._outline_model.reparent_to(np)
+        self._outline_model.reparent_to(self._np)
         self._outline_model.set_scale(-1.08, -1.08, -1.08)
         self._outline_model.set_light_off()
         self._outline_model.set_color(.4, .4, .4, 1)
         self._outline_model.set_color_scale(.4, .4, .4, 1)
         self._outline_model.hide()
 
-    def on_click(self, hit):
-        print(hit.get_hit_pos())
+    def play(self):
+        self._world.remove_rigid_body(self.node)
+        self.node.set_mass(1)
+        self._world.attach_rigid_body(self.node)
+
+    def on_click(self, pos):
+        self._start_drag_pos = pos, self._np.get_pos()
+
+    def on_release(self):
+        self._start_drag_pos = None
 
     def on_mouse_on(self):
         self._outline_model.show()
 
     def on_mouse_off(self):
         self._outline_model.hide()
+
+    def on_mouse_move(self, pos):
+        if not self._start_drag_pos: return
+        self._np.set_pos(self._start_drag_pos[1] + (pos - self._start_drag_pos[0]))