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/math/SGRect.hxx>
26 #include <osg/Texture2D>
38 * @param node Property node containing settings for this image:
39 * rect/[left/right/top/bottom] Dimensions of source
41 * size[0-1] Dimensions of rectangle
42 * [x,y] Position of rectangle
44 Image( const CanvasWeakPtr& canvas,
45 const SGPropertyNode_ptr& node,
46 const Style& parent_style,
47 Element* parent = 0 );
50 virtual void update(double dt);
51 virtual void valueChanged(SGPropertyNode* child);
53 void setSrcCanvas(CanvasPtr canvas);
54 CanvasWeakPtr getSrcCanvas() const;
56 void setImage(osg::Image *img);
57 void setFill(const std::string& fill);
60 * Set image slice (aka. 9-scale)
62 * @see http://www.w3.org/TR/css3-background/#border-image-slice
64 void setSlice(const std::string& slice);
67 * Set image slice width.
69 * By default the size of the 9-scale grid is the same as specified
70 * with setSlice/"slice". Using this method allows setting values
71 * different to the size in the source image.
73 * @see http://www.w3.org/TR/css3-background/#border-image-width
75 void setSliceWidth(const std::string& width);
78 * http://www.w3.org/TR/css3-background/#border-image-outset
80 void setOutset(const std::string& outset);
82 const SGRect<float>& getRegion() const;
84 bool handleEvent(EventPtr event);
90 SRC_RECT = LAST_ATTRIBUTE << 1, // Source image rectangle
91 DEST_SIZE = SRC_RECT << 1 // Element size
97 struct { float t, r, b, l; };
100 union CSSOffsetsTypes
103 struct { bool t_rel, r_rel, b_rel, l_rel; };
112 CSSOffsets getRelOffsets(const SGRect<int>& dim) const;
113 CSSOffsets getAbsOffsets(const SGRect<int>& dim) const;
116 CSSOffsetsTypes types;
121 virtual void childChanged(SGPropertyNode * child);
123 void setupDefaultDimensions();
124 SGRect<int> getTextureDimensions() const;
126 void setQuad(size_t index, const SGVec2f& tl, const SGVec2f& br);
127 void setQuadUV(size_t index, const SGVec2f& tl, const SGVec2f& br);
129 CSSBorder parseSideOffsets(const std::string& str) const;
131 osg::ref_ptr<osg::Texture2D> _texture;
132 // TODO optionally forward events to canvas
133 CanvasWeakPtr _src_canvas;
135 osg::ref_ptr<osg::Geometry> _geom;
136 osg::ref_ptr<osg::DrawArrays>_prim;
137 osg::ref_ptr<osg::Vec3Array> _vertices;
138 osg::ref_ptr<osg::Vec2Array> _texCoords;
139 osg::ref_ptr<osg::Vec4Array> _colors;
141 SGPropertyNode *_node_src_rect;
142 SGRect<float> _src_rect,
150 } // namespace canvas
151 } // namespace canvas
153 #endif /* CANVAS_IMAGE_HXX_ */