]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/elements/CanvasImage.hxx
canvas::Layout: support for contents margins.
[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/io/HTTPClient.hxx>
26 #include <simgear/misc/CSSBorder.hxx>
27 #include <simgear/misc/SVGpreserveAspectRatio.hxx>
28 #include <osg/Texture2D>
29
30 namespace simgear
31 {
32 namespace HTTP { class Request; }
33 namespace canvas
34 {
35
36   class Image:
37     public Element
38   {
39     public:
40       static const std::string TYPE_NAME;
41       static void staticInit();
42
43       /**
44        * @param node    Property node containing settings for this image:
45        *                  rect/[left/right/top/bottom]  Dimensions of source
46        *                                                rect
47        *                  size[0-1]                     Dimensions of rectangle
48        *                  [x,y]                         Position of rectangle
49        */
50       Image( const CanvasWeakPtr& canvas,
51              const SGPropertyNode_ptr& node,
52              const Style& parent_style = Style(),
53              Element* parent = 0 );
54       virtual ~Image();
55
56       virtual void update(double dt);
57       virtual void valueChanged(SGPropertyNode* child);
58
59       void setSrcCanvas(CanvasPtr canvas);
60       CanvasWeakPtr getSrcCanvas() const;
61
62       void setImage(osg::Image *img);
63       void setFill(const std::string& fill);
64
65       /**
66        * @see http://www.w3.org/TR/css3-background/#border-image-outset
67        */
68       void setOutset(const std::string& outset);
69
70       /**
71        * @see
72        *   http://www.w3.org/TR/SVG11/coords.html#PreserveAspectRatioAttribute
73        */
74       void setPreserveAspectRatio(const std::string& scale);
75
76       /**
77        * Set image slice (aka. 9-scale)
78        *
79        * @see http://www.w3.org/TR/css3-background/#border-image-slice
80        */
81       void setSlice(const std::string& slice);
82
83       /**
84        * Set image slice width.
85        *
86        * By default the size of the 9-scale grid is the same as specified
87        * with setSlice/"slice". Using this method allows setting values
88        * different to the size in the source image.
89        *
90        * @see http://www.w3.org/TR/css3-background/#border-image-width
91        */
92       void setSliceWidth(const std::string& width);
93
94       const SGRect<float>& getRegion() const;
95
96       bool handleEvent(const EventPtr& event);
97
98     protected:
99
100       enum ImageAttributes
101       {
102         SRC_RECT       = LAST_ATTRIBUTE << 1, // Source image rectangle
103         DEST_SIZE      = SRC_RECT << 1,       // Element size
104         SRC_CANVAS     = DEST_SIZE << 1
105       };
106
107       virtual void childChanged(SGPropertyNode * child);
108
109       void setupDefaultDimensions();
110       SGRect<int> getTextureDimensions() const;
111
112       void setQuad(size_t index, const SGVec2f& tl, const SGVec2f& br);
113       void setQuadUV(size_t index, const SGVec2f& tl, const SGVec2f& br);
114
115       void handleImageLoadDone(HTTP::Request*);
116       bool loadImage( osgDB::ReaderWriter& reader,
117                       const std::string& data,
118                       HTTP::Request& request,
119                       const std::string& type );
120
121       osg::ref_ptr<osg::Texture2D> _texture;
122       // TODO optionally forward events to canvas
123       CanvasWeakPtr _src_canvas;
124       HTTP::Request_ptr _http_request;
125
126       osg::ref_ptr<osg::Geometry>  _geom;
127       osg::ref_ptr<osg::DrawArrays>_prim;
128       osg::ref_ptr<osg::Vec3Array> _vertices;
129       osg::ref_ptr<osg::Vec2Array> _texCoords;
130       osg::ref_ptr<osg::Vec4Array> _colors;
131
132       SGPropertyNode *_node_src_rect;
133       SGRect<float>   _src_rect,
134                       _region;
135
136       SVGpreserveAspectRatio _preserve_aspect_ratio;
137
138       CSSBorder       _outset,
139                       _slice,
140                       _slice_width;
141   };
142
143 } // namespace canvas
144 } // namespace canvas
145
146 #endif /* CANVAS_IMAGE_HXX_ */