]> git.mxchange.org Git - simgear.git/blobdiff - simgear/canvas/elements/CanvasElement.cxx
Canvas: Respect clipping while event handling.
[simgear.git] / simgear / canvas / elements / CanvasElement.cxx
index 34088814abade7cebbb11d5890295b34daaa30d6..373632fc4fbd4cbb79bcbbcd9bd3b482041046ea 100644 (file)
@@ -56,6 +56,26 @@ namespace canvas
         _coord_reference(GLOBAL)
       {}
 
+      bool contains(const osg::Vec2f& pos) const
+      {
+        return _x <= pos.x() && pos.x() <= _x + _width
+            && _y <= pos.y() && pos.y() <= _y + _height;
+      }
+
+      bool contains( const osg::Vec2f& global_pos,
+                     const osg::Vec2f& parent_pos,
+                     const osg::Vec2f& local_pos ) const
+      {
+        switch( _coord_reference )
+        {
+          case GLOBAL: return contains(global_pos);
+          case PARENT: return contains(parent_pos);
+          case LOCAL:  return contains(local_pos);
+        }
+
+        return false;
+      }
+
       virtual void apply(osg::State& state) const
       {
         const osg::Viewport* vp = state.getCurrentViewport();
@@ -163,8 +183,7 @@ namespace canvas
   //----------------------------------------------------------------------------
   void Element::update(double dt)
   {
-    if( !_transform->getNodeMask() )
-      // Don't do anything if element is hidden
+    if( !isVisible() )
       return;
 
     // Trigger matrix update
@@ -265,17 +284,30 @@ namespace canvas
   }
 
   //----------------------------------------------------------------------------
-  bool Element::hitBound( const osg::Vec2f& pos,
+  bool Element::hitBound( const osg::Vec2f& global_pos,
+                          const osg::Vec2f& parent_pos,
                           const osg::Vec2f& local_pos ) const
   {
-    const osg::Vec3f pos3(pos, 0);
+    if( _scissor && !_scissor->contains(global_pos, parent_pos, local_pos) )
+      return false;
+
+    const osg::Vec3f pos3(parent_pos, 0);
 
     // Drawables have a bounding box...
     if( _drawable )
       return _drawable->getBound().contains(osg::Vec3f(local_pos, 0));
-    // ... for other elements, i.e. groups only a bounding sphere is available
     else
-      return _transform->getBound().contains(osg::Vec3f(pos, 0));
+      // ... for other elements, i.e. groups only a bounding sphere is available
+      return _transform->getBound().contains(osg::Vec3f(parent_pos, 0));
+  }
+
+
+  //----------------------------------------------------------------------------
+  void Element::setVisible(bool visible)
+  {
+    if( _transform.valid() )
+      // TODO check if we need another nodemask
+      _transform->setNodeMask(visible ? 0xffffffff : 0);
   }
 
   //----------------------------------------------------------------------------
@@ -296,6 +328,18 @@ namespace canvas
     return _transform.get();
   }
 
+  //----------------------------------------------------------------------------
+  osg::Vec2f Element::posToLocal(const osg::Vec2f& pos) const
+  {
+    getMatrix();
+    const osg::Matrix& m = _transform->getInverseMatrix();
+    return osg::Vec2f
+    (
+      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)
+    );
+  }
+
   //----------------------------------------------------------------------------
   void Element::childAdded(SGPropertyNode* parent, SGPropertyNode* child)
   {
@@ -393,9 +437,6 @@ namespace canvas
       }
       else if( name == "update" )
         return update(0);
-      else if( name == "visible" )
-        // TODO check if we need another nodemask
-        return _transform->setNodeMask( child->getBoolValue() ? 0xffffffff : 0 );
       else if( boost::starts_with(name, "blend-") )
         return (void)(_attributes_dirty |= BLEND_FUNC);
     }
@@ -636,6 +677,7 @@ namespace canvas
 
     addStyle("clip", "", &Element::setClip, false);
     addStyle("clip-frame", "", &Element::setClipFrame, false);
+    addStyle("visible", "", &Element::setVisible, false);
   }
 
   //----------------------------------------------------------------------------