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>
37 static const std::string TYPE_NAME;
40 * @param node Property node containing settings for this image:
41 * rect/[left/right/top/bottom] Dimensions of source
43 * size[0-1] Dimensions of rectangle
44 * [x,y] Position of rectangle
46 Image( const CanvasWeakPtr& canvas,
47 const SGPropertyNode_ptr& node,
48 const Style& parent_style = Style(),
49 Element* parent = 0 );
52 virtual void update(double dt);
53 virtual void valueChanged(SGPropertyNode* child);
55 void setSrcCanvas(CanvasPtr canvas);
56 CanvasWeakPtr getSrcCanvas() const;
58 void setImage(osg::Image *img);
59 void setFill(const std::string& fill);
62 * Set image slice (aka. 9-scale)
64 * @see http://www.w3.org/TR/css3-background/#border-image-slice
66 void setSlice(const std::string& slice);
69 * Set image slice width.
71 * By default the size of the 9-scale grid is the same as specified
72 * with setSlice/"slice". Using this method allows setting values
73 * different to the size in the source image.
75 * @see http://www.w3.org/TR/css3-background/#border-image-width
77 void setSliceWidth(const std::string& width);
80 * http://www.w3.org/TR/css3-background/#border-image-outset
82 void setOutset(const std::string& outset);
84 const SGRect<float>& getRegion() const;
86 bool handleEvent(EventPtr event);
92 SRC_RECT = LAST_ATTRIBUTE << 1, // Source image rectangle
93 DEST_SIZE = SRC_RECT << 1, // Element size
94 SRC_CANVAS = DEST_SIZE << 1
97 virtual void childChanged(SGPropertyNode * child);
99 void setupDefaultDimensions();
100 SGRect<int> getTextureDimensions() const;
102 void setQuad(size_t index, const SGVec2f& tl, const SGVec2f& br);
103 void setQuadUV(size_t index, const SGVec2f& tl, const SGVec2f& br);
105 osg::ref_ptr<osg::Texture2D> _texture;
106 // TODO optionally forward events to canvas
107 CanvasWeakPtr _src_canvas;
109 osg::ref_ptr<osg::Geometry> _geom;
110 osg::ref_ptr<osg::DrawArrays>_prim;
111 osg::ref_ptr<osg::Vec3Array> _vertices;
112 osg::ref_ptr<osg::Vec2Array> _texCoords;
113 osg::ref_ptr<osg::Vec4Array> _colors;
115 SGPropertyNode *_node_src_rect;
116 SGRect<float> _src_rect,
124 } // namespace canvas
125 } // namespace canvas
127 #endif /* CANVAS_IMAGE_HXX_ */