]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/elements/CanvasImage.hxx
CanvasImage: Fix border abs/rel calculations; add slice-width property
[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/math/SGRect.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       /**
38        * @param node    Property node containing settings for this image:
39        *                  rect/[left/right/top/bottom]  Dimensions of source
40        *                                                rect
41        *                  size[0-1]                     Dimensions of rectangle
42        *                  [x,y]                         Position of rectangle
43        */
44       Image( const CanvasWeakPtr& canvas,
45              const SGPropertyNode_ptr& node,
46              const Style& parent_style,
47              Element* parent = 0 );
48       virtual ~Image();
49
50       virtual void update(double dt);
51
52       void setSrcCanvas(CanvasPtr canvas);
53       CanvasWeakPtr getSrcCanvas() const;
54
55       void setImage(osg::Image *img);
56       void setFill(const std::string& fill);
57
58       /**
59        * Set image slice (aka. 9-scale)
60        *
61        * @see http://www.w3.org/TR/css3-background/#border-image-slice
62        */
63       void setSlice(const std::string& slice);
64
65       /**
66        * Set image slice width.
67        *
68        * By default the size of the 9-scale grid is the same as specified
69        * with setSlice/"slice". Using this method allows setting values
70        * different to the size in the source image.
71        *
72        * @see http://www.w3.org/TR/css3-background/#border-image-width
73        */
74       void setSliceWidth(const std::string& width);
75
76       /**
77        * http://www.w3.org/TR/css3-background/#border-image-outset
78        */
79       void setOutset(const std::string& outset);
80
81       const SGRect<float>& getRegion() const;
82
83       bool handleMouseEvent(MouseEventPtr event);
84
85     protected:
86
87       enum ImageAttributes
88       {
89         SRC_RECT       = LAST_ATTRIBUTE << 1, // Source image rectangle
90         DEST_SIZE      = SRC_RECT << 1        // Element size
91       };
92
93       union CSSOffsets
94       {
95         float          val[4];
96         struct { float t, r, b, l; };
97       };
98
99       union CSSOffsetsTypes
100       {
101         bool          rel[4];
102         struct { bool t_rel, r_rel, b_rel, l_rel; };
103       };
104
105       struct CSSBorder
106       {
107         CSSBorder():
108           valid(false)
109         {}
110
111         CSSOffsets getRelOffsets(const SGRect<int>& dim) const;
112         CSSOffsets getAbsOffsets(const SGRect<int>& dim) const;
113
114         CSSOffsets      offsets;
115         CSSOffsetsTypes types;
116         std::string     keyword;
117         bool            valid;
118       };
119
120       virtual void childChanged(SGPropertyNode * child);
121
122       void setupDefaultDimensions();
123       SGRect<int> getTextureDimensions() const;
124
125       void setQuad(size_t index, const SGVec2f& tl, const SGVec2f& br);
126       void setQuadUV(size_t index, const SGVec2f& tl, const SGVec2f& br);
127
128       CSSBorder parseSideOffsets(const std::string& str) const;
129
130       osg::ref_ptr<osg::Texture2D> _texture;
131       // TODO optionally forward events to canvas
132       CanvasWeakPtr _src_canvas;
133
134       osg::ref_ptr<osg::Geometry>  _geom;
135       osg::ref_ptr<osg::DrawArrays>_prim;
136       osg::ref_ptr<osg::Vec3Array> _vertices;
137       osg::ref_ptr<osg::Vec2Array> _texCoords;
138       osg::ref_ptr<osg::Vec4Array> _colors;
139
140       SGPropertyNode *_node_src_rect;
141       SGRect<float>   _src_rect,
142                       _region;
143
144       CSSBorder       _slice,
145                       _slice_width,
146                       _outset;
147   };
148
149 } // namespace canvas
150 } // namespace canvas
151
152 #endif /* CANVAS_IMAGE_HXX_ */