1 ///@file 2 /// An image on the Canvas 3 // 4 // Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com> 5 // 6 // This library is free software; you can redistribute it and/or 7 // modify it under the terms of the GNU Library General Public 8 // License as published by the Free Software Foundation; either 9 // version 2 of the License, or (at your option) any later version. 10 // 11 // This library is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 // Library General Public License for more details. 15 // 16 // You should have received a copy of the GNU Library General Public 17 // License along with this library; if not, write to the Free Software 18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA 19 20 #ifndef CANVAS_IMAGE_HXX_ 21 #define CANVAS_IMAGE_HXX_ 22 23 #include "CanvasElement.hxx" 24 25 #include <simgear/canvas/canvas_fwd.hxx> 26 #include <simgear/io/HTTPClient.hxx> 27 #include <simgear/misc/CSSBorder.hxx> 28 #include <simgear/misc/SVGpreserveAspectRatio.hxx> 29 #include <osg/Texture2D> 30 31 namespace simgear 32 { 33 namespace HTTP { class Request; } 34 namespace canvas 35 { 36 37 class Image: 38 public Element 39 { 40 public: 41 static const std::string TYPE_NAME; 42 static void staticInit(); 43 44 /** 45 * @param node Property node containing settings for this image: 46 * rect/[left/right/top/bottom] Dimensions of source 47 * rect 48 * size[0-1] Dimensions of rectangle 49 * [x,y] Position of rectangle 50 */ 51 Image( const CanvasWeakPtr& canvas, 52 const SGPropertyNode_ptr& node, 53 const Style& parent_style = Style(), 54 ElementWeakPtr parent = 0 ); 55 virtual ~Image(); 56 57 void valueChanged(SGPropertyNode* child) override; 58 59 void setSrcCanvas(CanvasPtr canvas); 60 CanvasWeakPtr getSrcCanvas() const; 61 62 void setImage(osg::ref_ptr<osg::Image> img); 63 void setFill(const std::string& fill); 64 void setFill(const osg::Vec4& color); 65 66 /** 67 * @see http://www.w3.org/TR/css3-background/#border-image-outset 68 */ 69 void setOutset(const std::string& outset); 70 71 /** 72 * @see 73 * http://www.w3.org/TR/SVG11/coords.html#PreserveAspectRatioAttribute 74 */ 75 void setPreserveAspectRatio(const std::string& scale); 76 77 /** 78 * Set image slice (aka. 9-scale) 79 * 80 * @see http://www.w3.org/TR/css3-background/#border-image-slice 81 */ 82 void setSlice(const std::string& slice); 83 84 /** 85 * Set image slice width. 86 * 87 * By default the size of the 9-scale grid is the same as specified 88 * with setSlice/"slice". Using this method allows setting values 89 * different to the size in the source image. 90 * 91 * @see http://www.w3.org/TR/css3-background/#border-image-width 92 */ 93 void setSliceWidth(const std::string& width); 94 95 const SGRect<float>& getRegion() const; 96 97 bool handleEvent(const EventPtr& event) override; 98 99 /** 100 * 101 */ 102 void setSourceRect(const SGRect<float>& sourceRect); 103 104 /** 105 * fill the specified rectangle of the image, with an RGB value 106 */ 107 void fillRect(const SGRect<int>& rect, const std::string& color); 108 109 /** 110 * fill the specified rectangle of the image, with an RGB value 111 */ 112 void fillRect(const SGRect<int>& rect, const osg::Vec4& color); 113 114 void setPixel(int x, int y, const std::string& color); 115 116 void setPixel(int x, int y, const osg::Vec4& color); 117 118 /** 119 * mark the image pixels as modified, so the canvas is re-painted 120 */ 121 void dirtyPixels(); 122 123 osg::ref_ptr<osg::Image> getImage() const; 124 125 // void setRow(int row, int offset, ) 126 protected: 127 enum ImageAttributes 128 { 129 SRC_RECT = LAST_ATTRIBUTE << 1, // Source image rectangle 130 DEST_SIZE = SRC_RECT << 1, // Element size 131 SRC_CANVAS = DEST_SIZE << 1 132 }; 133 134 void updateImpl(double dt) override; 135 136 void childChanged(SGPropertyNode * child) override; 137 138 void setupDefaultDimensions(); 139 SGRect<int> getTextureDimensions() const; 140 141 void setQuad(size_t index, const SGVec2f& tl, const SGVec2f& br); 142 void setQuadUV(size_t index, const SGVec2f& tl, const SGVec2f& br); 143 144 void handleImageLoadDone(HTTP::Request*); 145 bool loadImage( osgDB::ReaderWriter& reader, 146 const std::string& data, 147 HTTP::Request& request, 148 const std::string& type ); 149 150 void allocateImage(); 151 152 osg::ref_ptr<osg::Texture2D> _texture; 153 // TODO optionally forward events to canvas 154 CanvasWeakPtr _src_canvas; 155 HTTP::Request_ptr _http_request; 156 157 osg::ref_ptr<osg::Geometry> _geom; 158 osg::ref_ptr<osg::DrawArrays>_prim; 159 osg::ref_ptr<osg::Vec3Array> _vertices; 160 osg::ref_ptr<osg::Vec2Array> _texCoords; 161 osg::ref_ptr<osg::Vec4Array> _colors; 162 163 SGPropertyNode *_node_src_rect = nullptr; 164 SGRect<float> _src_rect {0, 0}, 165 _region {0, 0}; 166 167 SVGpreserveAspectRatio _preserve_aspect_ratio; 168 169 CSSBorder _outset, 170 _slice, 171 _slice_width; 172 }; 173 174 } // namespace canvas 175 } // namespace canvas 176 177 #endif /* CANVAS_IMAGE_HXX_ */ 178