self._set_input()
Background()
self.items = [Box(world)]
+ taskMgr.add(self.on_frame, 'on_frame')
def _set_camera(self):
base.camera.set_pos(0, -20, 0)
def _set_input(self):
self.accept('mouse1-up', self.on_click)
- def on_click(self):
- if not base.mouseWatcherNode.has_mouse(): return
+ def _get_hits(self):
+ if not base.mouseWatcherNode.has_mouse(): return []
p_from = Point3() # in camera coordinates
p_to = Point3() # in camera coordinates
base.camLens.extrude(base.mouseWatcherNode.get_mouse(), 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
- for hit in self._world.ray_test_all(p_from, p_to).get_hits():
+ return self._world.ray_test_all(p_from, p_to).get_hits()
+
+ def on_click(self):
+ for hit in self._get_hits():
for item in [i for i in self.items if hit.get_node() == i.node]:
item.on_click(hit)
+
+ def on_frame(self, task):
+ hit_nodes = [hit.get_node() for hit in self._get_hits()]
+ items_hit = [itm for itm in self.items if itm.node in hit_nodes]
+ items_no_hit = [itm for itm in self.items if itm not in items_hit]
+ [itm.on_mouse_on() for itm in items_hit]
+ [itm.on_mouse_off() for itm in items_no_hit]
+ return task.cont