]> git.mxchange.org Git - simgear.git/blobdiff - simgear/canvas/CanvasEventVisitor.cxx
Fix #1783: repeated error message on console
[simgear.git] / simgear / canvas / CanvasEventVisitor.cxx
index 40d4e26b8a76f9a49290880fe6cc6e46b9e81827..efb12dbb49ad0073c0558d869765f75021dd17b6 100644 (file)
@@ -20,7 +20,6 @@
 #include "CanvasEvent.hxx"
 #include "CanvasEventVisitor.hxx"
 #include <simgear/canvas/elements/CanvasElement.hxx>
-#include <iostream>
 
 namespace simgear
 {
@@ -30,14 +29,12 @@ namespace canvas
   //----------------------------------------------------------------------------
   EventVisitor::EventVisitor( TraverseMode mode,
                               const osg::Vec2f& pos,
-                              const osg::Vec2f& delta ):
-    _traverse_mode( mode )
+                              const ElementPtr& root ):
+    _traverse_mode( mode ),
+    _root(root)
   {
     if( mode == TRAVERSE_DOWN )
-    {
-      EventTarget target = {ElementWeakPtr(), pos, delta};
-      _target_path.push_back(target);
-    }
+      _target_path.push_back( EventTarget(NULL, pos) );
   }
 
   //----------------------------------------------------------------------------
@@ -61,32 +58,20 @@ namespace canvas
     // We only need to check for hits while traversing down
     if( _traverse_mode == TRAVERSE_DOWN )
     {
-      // Transform event to local coordinates
-      const osg::Matrix& m = el.getMatrixTransform()->getInverseMatrix();
       const osg::Vec2f& pos = _target_path.back().local_pos;
-      const osg::Vec2f local_pos
-      (
-        m(0, 0) * pos[0] + m(1, 0) * pos[1] + m(3, 0),
-        m(0, 1) * pos[0] + m(1, 1) * pos[1] + m(3, 1)
-      );
+      const osg::Vec2f local_pos = el.posToLocal(pos);
 
-      // Don't check collision with root element (2nd element in _target_path)
-      // do event listeners attached to the canvas itself (its root group)
-      // always get called even if no element has been hit.
-      if( _target_path.size() > 1 && !el.hitBound(pos, local_pos) )
+      // Don't check specified root element for collision, as its purpose is to
+      // catch all events which have no target. This allows for example calling
+      // event listeners attached to the canvas itself (its root group) even if
+      // no element has been hit.
+      if(    _root.get() != &el
+          && !el.hitBound(_target_path.front().local_pos, pos, local_pos) )
         return false;
 
-      const osg::Vec2f& delta = _target_path.back().local_delta;
-      const osg::Vec2f local_delta
-      (
-        m(0, 0) * delta[0] + m(1, 0) * delta[1],
-        m(0, 1) * delta[0] + m(1, 1) * delta[1]
-      );
-
-      EventTarget target = {el.getWeakPtr(), local_pos, local_delta};
-      _target_path.push_back(target);
+      _target_path.push_back( EventTarget(&el, local_pos) );
 
-      if( el.traverse(*this) || _target_path.size() <= 2 )
+      if( el.traverse(*this) || &el == _root.get() )
         return true;
 
       _target_path.pop_back();