ya2 · news · projects · code · about

drag and drop
[pmachines.git] / pmachines / items / box.py
1 from panda3d.core import CullFaceAttrib
2 from panda3d.bullet import BulletBoxShape, BulletRigidBodyNode
3
4 class Box:
5
6 def __init__(self, world):
7 self._world = world
8 shape = BulletBoxShape((.5, .5, .5))
9 self.node = BulletRigidBodyNode('box')
10 self.node.add_shape(shape)
11 self._np = render.attach_new_node(self.node)
12 self._np.set_pos(0, 0, 1)
13 world.attach_rigid_body(self.node)
14 model = loader.load_model('assets/gltf/box/box.gltf')
15 model.flatten_light()
16 model.reparent_to(self._np)
17 self._set_outline_model()
18 self._start_drag_pos = None
19
20 def _set_outline_model(self):
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))
24 self._outline_model.reparent_to(self._np)
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()
30
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
41
42 def on_mouse_on(self):
43 self._outline_model.show()
44
45 def on_mouse_off(self):
46 self._outline_model.hide()
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]))