1 // An image on the Canvas
3 // Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Library General Public
7 // License as published by the Free Software Foundation; either
8 // version 2 of the License, or (at your option) any later version.
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Library General Public License for more details.
15 // You should have received a copy of the GNU Library General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #ifndef CANVAS_IMAGE_HXX_
20 #define CANVAS_IMAGE_HXX_
22 #include "CanvasElement.hxx"
24 #include <simgear/canvas/canvas_fwd.hxx>
25 #include <simgear/misc/CSSBorder.hxx>
26 #include <osg/Texture2D>
30 namespace HTTP { class Request; }
38 static const std::string TYPE_NAME;
39 static void staticInit();
42 * @param node Property node containing settings for this image:
43 * rect/[left/right/top/bottom] Dimensions of source
45 * size[0-1] Dimensions of rectangle
46 * [x,y] Position of rectangle
48 Image( const CanvasWeakPtr& canvas,
49 const SGPropertyNode_ptr& node,
50 const Style& parent_style = Style(),
51 Element* parent = 0 );
54 virtual void update(double dt);
55 virtual void valueChanged(SGPropertyNode* child);
57 void setSrcCanvas(CanvasPtr canvas);
58 CanvasWeakPtr getSrcCanvas() const;
60 void setImage(osg::Image *img);
61 void setFill(const std::string& fill);
64 * Set image slice (aka. 9-scale)
66 * @see http://www.w3.org/TR/css3-background/#border-image-slice
68 void setSlice(const std::string& slice);
71 * Set image slice width.
73 * By default the size of the 9-scale grid is the same as specified
74 * with setSlice/"slice". Using this method allows setting values
75 * different to the size in the source image.
77 * @see http://www.w3.org/TR/css3-background/#border-image-width
79 void setSliceWidth(const std::string& width);
82 * http://www.w3.org/TR/css3-background/#border-image-outset
84 void setOutset(const std::string& outset);
86 const SGRect<float>& getRegion() const;
88 bool handleEvent(EventPtr event);
94 SRC_RECT = LAST_ATTRIBUTE << 1, // Source image rectangle
95 DEST_SIZE = SRC_RECT << 1, // Element size
96 SRC_CANVAS = DEST_SIZE << 1
99 virtual void childChanged(SGPropertyNode * child);
101 void setupDefaultDimensions();
102 SGRect<int> getTextureDimensions() const;
104 void setQuad(size_t index, const SGVec2f& tl, const SGVec2f& br);
105 void setQuadUV(size_t index, const SGVec2f& tl, const SGVec2f& br);
107 void handleImageLoadDone(HTTP::Request*);
108 bool loadImage( osgDB::ReaderWriter& reader,
109 const std::string& data,
110 HTTP::Request& request,
111 const std::string& type );
113 osg::ref_ptr<osg::Texture2D> _texture;
114 // TODO optionally forward events to canvas
115 CanvasWeakPtr _src_canvas;
117 osg::ref_ptr<osg::Geometry> _geom;
118 osg::ref_ptr<osg::DrawArrays>_prim;
119 osg::ref_ptr<osg::Vec3Array> _vertices;
120 osg::ref_ptr<osg::Vec2Array> _texCoords;
121 osg::ref_ptr<osg::Vec4Array> _colors;
123 SGPropertyNode *_node_src_rect;
124 SGRect<float> _src_rect,
132 } // namespace canvas
133 } // namespace canvas
135 #endif /* CANVAS_IMAGE_HXX_ */