]> git.mxchange.org Git - simgear.git/blobdiff - simgear/canvas/elements/CanvasPath.cxx
Canvas: Respect clipping while event handling.
[simgear.git] / simgear / canvas / elements / CanvasPath.cxx
index 045aabf5e0ff6799cc11553b65f6c2be736067be..27c847a7ef9b328cfaebe07f548099ebcee90a11 100644 (file)
@@ -20,7 +20,6 @@
 #include <simgear/scene/util/parse_color.hxx>
 
 #include <osg/Drawable>
-#include <osg/BlendFunc>
 
 #include <vg/openvg.h>
 #include <cassert>
@@ -111,7 +110,7 @@ namespace canvas
        */
       void setFill(const std::string& fill)
       {
-        if( fill == "none" )
+        if( fill.empty() || fill == "none" )
         {
           _mode &= ~VG_FILL_PATH;
         }
@@ -151,7 +150,7 @@ namespace canvas
        */
       void setStroke(const std::string& stroke)
       {
-        if( stroke == "none" )
+        if( stroke.empty() || stroke == "none" )
         {
           _mode &= ~VG_STROKE_PATH;
         }
@@ -278,7 +277,8 @@ namespace canvas
       {
         osg::BoundingBox bb;
 
-        osg::Vec2f cur; // VG "Current point" (in local coordinates)
+        osg::Vec2f cur(0, 0), // VG "Current point" (in local coordinates)
+                   sub(0, 0); // beginning of current sub path
         VGubyte cmd_index = 0;
         for( size_t i = 0,              ci = 0;
                     i < _cmds.size() && ci < _coords.size();
@@ -298,6 +298,7 @@ namespace canvas
           switch( cmd )
           {
             case VG_CLOSE_PATH:
+              cur = sub;
               break;
             case VG_MOVE_TO:
             case VG_LINE_TO:
@@ -353,7 +354,12 @@ namespace canvas
           }
 
           if( num_coords > 0 )
+          {
             cur = points[ num_coords - 1 ];
+
+            if( cmd == VG_MOVE_TO )
+              sub = cur;
+          }
         }
 
         return bb;
@@ -429,6 +435,9 @@ namespace canvas
        */
       void update()
       {
+        if( !vgHasContextSH() )
+          return;
+
         if( _attributes_dirty & PATH )
         {
           const VGbitfield caps = VG_PATH_CAPABILITY_APPEND_TO
@@ -467,6 +476,25 @@ namespace canvas
       };
   };
 
+  //----------------------------------------------------------------------------
+  const std::string Path::TYPE_NAME = "path";
+
+  //----------------------------------------------------------------------------
+  void Path::staticInit()
+  {
+    if( isInit<Path>() )
+      return;
+
+    PathDrawableRef Path::*path = &Path::_path;
+
+    addStyle("fill", "color", &PathDrawable::setFill, path);
+    addStyle("fill-rule", "", &PathDrawable::setFillRule, path);
+    addStyle("stroke", "color", &PathDrawable::setStroke, path);
+    addStyle("stroke-width", "numeric", &PathDrawable::setStrokeWidth, path);
+    addStyle("stroke-dasharray", "", &PathDrawable::setStrokeDashArray, path);
+    addStyle("stroke-linecap", "", &PathDrawable::setStrokeLinecap, path);
+  }
+
   //----------------------------------------------------------------------------
   Path::Path( const CanvasWeakPtr& canvas,
               const SGPropertyNode_ptr& node,
@@ -475,20 +503,9 @@ namespace canvas
     Element(canvas, node, parent_style, parent),
     _path( new PathDrawable(this) )
   {
-    setDrawable(_path);
-
-    if( !isInit<Path>() )
-    {
-      PathDrawableRef Path::*path = &Path::_path;
-
-      addStyle("fill", "color", &PathDrawable::setFill, path);
-      addStyle("fill-rule", "", &PathDrawable::setFillRule, path);
-      addStyle("stroke", "color", &PathDrawable::setStroke, path);
-      addStyle("stroke-width", "numeric", &PathDrawable::setStrokeWidth, path);
-      addStyle("stroke-dasharray", "", &PathDrawable::setStrokeDashArray, path);
-      addStyle("stroke-linecap", "", &PathDrawable::setStrokeLinecap, path);
-    }
+    staticInit();
 
+    setDrawable(_path);
     setupStyle();
   }
 
@@ -521,6 +538,60 @@ namespace canvas
     return _path->getTransformedBounds(m);
   }
 
+  //----------------------------------------------------------------------------
+  Path& Path::moveTo(float x_abs, float y_abs)
+  {
+    return addSegment(VG_MOVE_TO_ABS, x_abs, y_abs);
+  }
+
+  //----------------------------------------------------------------------------
+  Path& Path::move(float x_rel, float y_rel)
+  {
+    return addSegment(VG_MOVE_TO_REL, x_rel, y_rel);
+  }
+
+  //----------------------------------------------------------------------------
+  Path& Path::lineTo(float x_abs, float y_abs)
+  {
+    return addSegment(VG_LINE_TO_ABS, x_abs, y_abs);
+  }
+
+  //----------------------------------------------------------------------------
+  Path& Path::line(float x_rel, float y_rel)
+  {
+    return addSegment(VG_LINE_TO_REL, x_rel, y_rel);
+  }
+
+  //----------------------------------------------------------------------------
+  Path& Path::horizTo(float x_abs)
+  {
+    return addSegment(VG_HLINE_TO_ABS, x_abs);
+  }
+
+  //----------------------------------------------------------------------------
+  Path& Path::horiz(float x_rel)
+  {
+    return addSegment(VG_HLINE_TO_REL, x_rel);
+  }
+
+  //----------------------------------------------------------------------------
+  Path& Path::vertTo(float y_abs)
+  {
+    return addSegment(VG_VLINE_TO_ABS, y_abs);
+  }
+
+  //----------------------------------------------------------------------------
+  Path& Path::vert(float y_rel)
+  {
+    return addSegment(VG_VLINE_TO_REL, y_rel);
+  }
+
+  //----------------------------------------------------------------------------
+  Path& Path::close()
+  {
+    return addSegment(VG_CLOSE_PATH);
+  }
+
   //----------------------------------------------------------------------------
   void Path::childRemoved(SGPropertyNode* child)
   {