]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/elements/CanvasImage.hxx
CanvasImage: improve fetching from http and add mime detection.
[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 HTTP { class Request; }
31 namespace canvas
32 {
33
34   class Image:
35     public Element
36   {
37     public:
38       static const std::string TYPE_NAME;
39       static void staticInit();
40
41       /**
42        * @param node    Property node containing settings for this image:
43        *                  rect/[left/right/top/bottom]  Dimensions of source
44        *                                                rect
45        *                  size[0-1]                     Dimensions of rectangle
46        *                  [x,y]                         Position of rectangle
47        */
48       Image( const CanvasWeakPtr& canvas,
49              const SGPropertyNode_ptr& node,
50              const Style& parent_style = Style(),
51              Element* parent = 0 );
52       virtual ~Image();
53
54       virtual void update(double dt);
55       virtual void valueChanged(SGPropertyNode* child);
56
57       void setSrcCanvas(CanvasPtr canvas);
58       CanvasWeakPtr getSrcCanvas() const;
59
60       void setImage(osg::Image *img);
61       void setFill(const std::string& fill);
62
63       /**
64        * Set image slice (aka. 9-scale)
65        *
66        * @see http://www.w3.org/TR/css3-background/#border-image-slice
67        */
68       void setSlice(const std::string& slice);
69
70       /**
71        * Set image slice width.
72        *
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.
76        *
77        * @see http://www.w3.org/TR/css3-background/#border-image-width
78        */
79       void setSliceWidth(const std::string& width);
80
81       /**
82        * http://www.w3.org/TR/css3-background/#border-image-outset
83        */
84       void setOutset(const std::string& outset);
85
86       const SGRect<float>& getRegion() const;
87
88       bool handleEvent(EventPtr event);
89
90     protected:
91
92       enum ImageAttributes
93       {
94         SRC_RECT       = LAST_ATTRIBUTE << 1, // Source image rectangle
95         DEST_SIZE      = SRC_RECT << 1,       // Element size
96         SRC_CANVAS     = DEST_SIZE << 1
97       };
98
99       virtual void childChanged(SGPropertyNode * child);
100
101       void setupDefaultDimensions();
102       SGRect<int> getTextureDimensions() const;
103
104       void setQuad(size_t index, const SGVec2f& tl, const SGVec2f& br);
105       void setQuadUV(size_t index, const SGVec2f& tl, const SGVec2f& br);
106
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 );
112
113       osg::ref_ptr<osg::Texture2D> _texture;
114       // TODO optionally forward events to canvas
115       CanvasWeakPtr _src_canvas;
116
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;
122
123       SGPropertyNode *_node_src_rect;
124       SGRect<float>   _src_rect,
125                       _region;
126
127       CSSBorder       _slice,
128                       _slice_width,
129                       _outset;
130   };
131
132 } // namespace canvas
133 } // namespace canvas
134
135 #endif /* CANVAS_IMAGE_HXX_ */