]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/elements/CanvasImage.hxx
OSG 3.2.0 compatibility and surface light effects.
[simgear.git] / simgear / canvas / elements / CanvasImage.hxx
1 // An image on the Canvas
2 //
3 // Copyright (C) 2012  Thomas Geymayer <tomgey@gmail.com>
4 //
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.
9 //
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.
14 //
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
18
19 #ifndef CANVAS_IMAGE_HXX_
20 #define CANVAS_IMAGE_HXX_
21
22 #include "CanvasElement.hxx"
23
24 #include <simgear/canvas/canvas_fwd.hxx>
25 #include <simgear/misc/CSSBorder.hxx>
26 #include <osg/Texture2D>
27
28 namespace simgear
29 {
30 namespace canvas
31 {
32
33   class Image:
34     public Element
35   {
36     public:
37       static const std::string TYPE_NAME;
38       static void staticInit();
39
40       /**
41        * @param node    Property node containing settings for this image:
42        *                  rect/[left/right/top/bottom]  Dimensions of source
43        *                                                rect
44        *                  size[0-1]                     Dimensions of rectangle
45        *                  [x,y]                         Position of rectangle
46        */
47       Image( const CanvasWeakPtr& canvas,
48              const SGPropertyNode_ptr& node,
49              const Style& parent_style = Style(),
50              Element* parent = 0 );
51       virtual ~Image();
52
53       virtual void update(double dt);
54       virtual void valueChanged(SGPropertyNode* child);
55
56       void setSrcCanvas(CanvasPtr canvas);
57       CanvasWeakPtr getSrcCanvas() const;
58
59       void setImage(osg::Image *img);
60       void setFill(const std::string& fill);
61
62       /**
63        * Set image slice (aka. 9-scale)
64        *
65        * @see http://www.w3.org/TR/css3-background/#border-image-slice
66        */
67       void setSlice(const std::string& slice);
68
69       /**
70        * Set image slice width.
71        *
72        * By default the size of the 9-scale grid is the same as specified
73        * with setSlice/"slice". Using this method allows setting values
74        * different to the size in the source image.
75        *
76        * @see http://www.w3.org/TR/css3-background/#border-image-width
77        */
78       void setSliceWidth(const std::string& width);
79
80       /**
81        * http://www.w3.org/TR/css3-background/#border-image-outset
82        */
83       void setOutset(const std::string& outset);
84
85       const SGRect<float>& getRegion() const;
86
87       bool handleEvent(EventPtr event);
88
89     protected:
90
91       enum ImageAttributes
92       {
93         SRC_RECT       = LAST_ATTRIBUTE << 1, // Source image rectangle
94         DEST_SIZE      = SRC_RECT << 1,       // Element size
95         SRC_CANVAS     = DEST_SIZE << 1
96       };
97
98       virtual void childChanged(SGPropertyNode * child);
99
100       void setupDefaultDimensions();
101       SGRect<int> getTextureDimensions() const;
102
103       void setQuad(size_t index, const SGVec2f& tl, const SGVec2f& br);
104       void setQuadUV(size_t index, const SGVec2f& tl, const SGVec2f& br);
105
106       osg::ref_ptr<osg::Texture2D> _texture;
107       // TODO optionally forward events to canvas
108       CanvasWeakPtr _src_canvas;
109
110       osg::ref_ptr<osg::Geometry>  _geom;
111       osg::ref_ptr<osg::DrawArrays>_prim;
112       osg::ref_ptr<osg::Vec3Array> _vertices;
113       osg::ref_ptr<osg::Vec2Array> _texCoords;
114       osg::ref_ptr<osg::Vec4Array> _colors;
115
116       SGPropertyNode *_node_src_rect;
117       SGRect<float>   _src_rect,
118                       _region;
119
120       CSSBorder       _slice,
121                       _slice_width,
122                       _outset;
123   };
124
125 } // namespace canvas
126 } // namespace canvas
127
128 #endif /* CANVAS_IMAGE_HXX_ */