]> git.mxchange.org Git - flightgear.git/commitdiff
Canvas: default image element dimensions to texture size
authorThomas Geymayer <thomas.geymayer@student.tugraz.at>
Thu, 9 Aug 2012 19:58:55 +0000 (21:58 +0200)
committerThomas Geymayer <thomas.geymayer@student.tugraz.at>
Thu, 9 Aug 2012 19:58:55 +0000 (21:58 +0200)
src/Canvas/elements/CanvasImage.cxx
src/Canvas/elements/CanvasImage.hxx
src/Canvas/rect.hxx

index 3addb4901adaa6e40f9ccd0b0d5e1b6421f36780..62a2e4dc23316902269e34c50497c2cb97ca427b 100644 (file)
@@ -74,9 +74,7 @@ namespace canvas
   Image::Image(SGPropertyNode_ptr node):
     Element(node, COLOR_FILL | BOUNDING_BOX),
     _texture(new osg::Texture2D),
-    _node_src_rect( node->getNode("source", 0, true) ),
-    _src_rect(0,0,0,0),
-    _region(0,0,0,0)
+    _node_src_rect( node->getNode("source", 0, true) )
   {
     _geom = new osg::Geometry;
     _geom->setUseDisplayList(false);
@@ -139,17 +137,12 @@ namespace canvas
 
       if( !_node_src_rect->getBoolValue("normalized", true) )
       {
-        osg::Texture2D *texture = !_canvas.expired()
-                                ? _canvas.lock()->getTexture()
-                                : _texture.get();
+        const Rect<int>& tex_dim = getTextureDimensions();
 
-        int texWidth = texture->getTextureWidth();
-        int texHeight = texture->getTextureHeight();
-
-        u0 /= texWidth;
-        u1 /= texWidth;
-        v0 /= texHeight;
-        v1 /= texHeight;
+        u0 /= tex_dim.width();
+        u1 /= tex_dim.width();
+        v0 /= tex_dim.height();
+        v1 /= tex_dim.height();
       }
 
       (*_texCoords)[0].set(u0, v0);
@@ -171,6 +164,9 @@ namespace canvas
     _geom->setCullCallback(
       canvas ? new CullCallback(canvas->getCameraCullCallback()) : 0
     );
+
+    if( !_canvas.expired() )
+      setupDefaultDimensions();
   }
 
   //----------------------------------------------------------------------------
@@ -188,6 +184,9 @@ namespace canvas
     _texture->setImage(img);
     _geom->getOrCreateStateSet()
          ->setTextureAttributeAndModes(0, _texture);
+
+    if( img )
+      setupDefaultDimensions();
   }
 
   //----------------------------------------------------------------------------
@@ -298,4 +297,38 @@ namespace canvas
     _colors->dirty();
   }
 
+  //----------------------------------------------------------------------------
+  void Image::setupDefaultDimensions()
+  {
+    if( !_src_rect.width() || !_src_rect.height() )
+    {
+      const Rect<int>& tex_dim = getTextureDimensions();
+
+      _node_src_rect->setBoolValue("normalized", false);
+      _node_src_rect->setFloatValue("right", tex_dim.width());
+      _node_src_rect->setFloatValue("bottom", tex_dim.height());
+    }
+
+    if( !_region.width() || !_region.height() )
+    {
+      _node->setFloatValue("size[0]", _src_rect.width());
+      _node->setFloatValue("size[1]", _src_rect.height());
+    }
+  }
+
+  //----------------------------------------------------------------------------
+  Rect<int> Image::getTextureDimensions() const
+  {
+    osg::Texture2D *texture = !_canvas.expired()
+                              ? _canvas.lock()->getTexture()
+                              : _texture.get();
+
+    return Rect<int>
+    (
+      0,0,
+      texture->getTextureWidth(),
+      texture->getTextureHeight()
+    );
+  }
+
 } // namespace canvas
index 934df7a0a210c798ce014bcc9e19696f0d4fe0a3..d95268a6fa60727a628e638354acd505bcc254d4 100644 (file)
@@ -72,6 +72,9 @@ namespace canvas
 
       void handleHit(float x, float y);
 
+      void setupDefaultDimensions();
+      Rect<int> getTextureDimensions() const;
+
       osg::ref_ptr<osg::Texture2D> _texture;
       // TODO optionally forward events to canvas
       CanvasWeakPtr _canvas;
index f140cebf231d3567fdbe4613177b881fee8b6663..76c5ecf01418702c16373b104a0999d2f4ea24d1 100644 (file)
@@ -27,7 +27,12 @@ namespace canvas
   class Rect
   {
     public:
-      Rect() {}
+      Rect():
+        _x1(0),
+        _x2(0),
+        _y1(0),
+        _y2(0)
+      {}
 
       Rect(T x, T y, T w, T h):
         _x1(x),