ya2 · news · projects · code · about

drag and drop
[pmachines.git] / pmachines / items / box.py
CommitLineData
37ba71d8 1from panda3d.core import CullFaceAttrib
1be87278
FC
2from panda3d.bullet import BulletBoxShape, BulletRigidBodyNode
3
4class Box:
5
6 def __init__(self, world):
c8d8653f 7 self._world = world
1be87278
FC
8 shape = BulletBoxShape((.5, .5, .5))
9 self.node = BulletRigidBodyNode('box')
1be87278 10 self.node.add_shape(shape)
c8d8653f
FC
11 self._np = render.attach_new_node(self.node)
12 self._np.set_pos(0, 0, 1)
1be87278
FC
13 world.attach_rigid_body(self.node)
14 model = loader.load_model('assets/gltf/box/box.gltf')
15 model.flatten_light()
c8d8653f
FC
16 model.reparent_to(self._np)
17 self._set_outline_model()
18 self._start_drag_pos = None
37ba71d8 19
c8d8653f 20 def _set_outline_model(self):
37ba71d8
FC
21 self._outline_model = loader.load_model('assets/gltf/box/box.gltf')
22 clockw = CullFaceAttrib.MCullClockwise
23 self._outline_model.set_attrib(CullFaceAttrib.make(clockw))
c8d8653f 24 self._outline_model.reparent_to(self._np)
37ba71d8
FC
25 self._outline_model.set_scale(-1.08, -1.08, -1.08)
26 self._outline_model.set_light_off()
27 self._outline_model.set_color(.4, .4, .4, 1)
28 self._outline_model.set_color_scale(.4, .4, .4, 1)
29 self._outline_model.hide()
1be87278 30
c8d8653f
FC
31 def play(self):
32 self._world.remove_rigid_body(self.node)
33 self.node.set_mass(1)
34 self._world.attach_rigid_body(self.node)
35
36 def on_click(self, pos):
37 self._start_drag_pos = pos, self._np.get_pos()
38
39 def on_release(self):
40 self._start_drag_pos = None
37ba71d8
FC
41
42 def on_mouse_on(self):
43 self._outline_model.show()
44
45 def on_mouse_off(self):
46 self._outline_model.hide()
c8d8653f
FC
47
48 def on_mouse_move(self, pos):
49 if not self._start_drag_pos: return
50 self._np.set_pos(self._start_drag_pos[1] + (pos - self._start_drag_pos[0]))