Merge "Changed NPatchRenderer and ImageRenderer to use an "broken image" if they...
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / renderers / npatch / npatch-renderer.h
1 #ifndef __DALI_TOOLKIT_INTERNAL_N_PATCH_RENDERER_H__
2 #define __DALI_TOOLKIT_INTERNAL_N_PATCH_RENDERER_H__
3
4 /*
5  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // INTERNAL INCLUDES
22 #include <dali-toolkit/internal/controls/renderers/control-renderer-impl.h>
23
24 // EXTERNAL INCLUDES
25 #include <dali/public-api/images/image.h>
26 #include <dali/public-api/images/nine-patch-image.h>
27 #include <dali/public-api/images/image-operations.h>
28 #include <dali/devel-api/rendering/geometry.h>
29 #include <dali/devel-api/rendering/shader.h>
30 #include <dali/devel-api/rendering/sampler.h>
31
32 namespace Dali
33 {
34
35 namespace Toolkit
36 {
37
38 namespace Internal
39 {
40
41 /**
42  * The renderer which renders an 9 patch image to the control's quad
43  *
44  * The following properties are optional
45  *
46  * | %Property Name            | Type             |
47  * |---------------------------|------------------|
48  * | image-url                 | STRING           |
49  * | border-only               | BOOLEAN
50  *
51  */
52 class NPatchRenderer: public ControlRenderer
53 {
54 public:
55
56   /**
57    * @brief Constructor.
58    *
59    * @param[in] factoryCache A pointer pointing to the RendererFactoryCache object
60    */
61   NPatchRenderer( RendererFactoryCache& factoryCache );
62
63   /**
64    * @brief A reference counted object may only be deleted by calling Unreference().
65    */
66   ~NPatchRenderer();
67
68 public:  // from ControlRenderer
69
70   /**
71    * @copydoc ControlRenderer::GetNaturalSize
72    */
73   virtual void GetNaturalSize( Vector2& naturalSize ) const;
74
75   /**
76    * @copydoc ControlRenderer::SetClipRect
77    */
78   virtual void SetClipRect( const Rect<int>& clipRect );
79
80   /**
81    * @copydoc ControlRenderer::SetOffset
82    */
83   virtual void SetOffset( const Vector2& offset );
84
85   /**
86    * @copydoc ControlRenderer::CreatePropertyMap
87    */
88   virtual void DoCreatePropertyMap( Property::Map& map ) const;
89
90 protected:
91
92   /**
93    * @copydoc ControlRenderer::DoInitialize
94    */
95   virtual void DoInitialize( const Property::Map& propertyMap );
96
97   /**
98    * @copydoc ControlRenderer::InitializeRenderer
99    */
100   virtual void InitializeRenderer( Renderer& renderer );
101
102   /**
103    * @copydoc ControlRenderer::DoSetOnStage
104    */
105   virtual void DoSetOnStage( Actor& actor );
106
107   /**
108    * @copydoc ControlRenderer::DoSetOffStage
109    */
110   virtual void DoSetOffStage( Actor& actor );
111
112 public:
113
114   /**
115    * @brief Sets the 9 patch image of this renderer to the resource at imageUrl
116    * The renderer will load the image synchronously when the associated actor is put on stage, and destroy the image when it is off stage
117    *
118    * @param[in] imageUrl The URL to 9 patch image resource to use
119    * @param[in] borderOnly A Flag to indicate if the image should omit the centre of the n-patch and only render the border
120    */
121   void SetImage( const std::string& imageUrl, bool borderOnly = false );
122
123   /**
124    * @brief Sets the 9 patch image of this renderer to the 9 patch image
125    *
126    * @param[in] image The NinePatchImage to use
127    * @param[in] borderOnly A Flag to indicate if the image should omit the centre of the n-patch and only render the border
128    */
129   void SetImage( NinePatchImage image, bool borderOnly = false );
130
131 private:
132
133   /**
134    * @brief Creates a geometry for the grid size to be used by this renderers' shaders
135    *
136    * @param gridSize The grid size of the solid geometry to create
137    * @return Returns the created geometry for the grid size
138    */
139   Geometry CreateGeometry( Uint16Pair gridSize );
140
141   /**
142    * @brief Creates a geometry with the border only for the grid size to be used by this renderers' shaders
143    * e.g. a 5x4 grid would create a geometry that would look like:
144    *
145    *   ---------------------
146    *   |  /|  /|  /|  /|  /|
147    *   |/  |/  |/  |/  |/  |
148    *   ---------------------
149    *   |  /|           |  /|
150    *   |/  |           |/  |
151    *   -----           -----
152    *   |  /|           |  /|
153    *   |/  |           |/  |
154    *   ---------------------
155    *   |  /|  /|  /|  /|  /|
156    *   |/  |/  |/  |/  |/  |
157    *   ---------------------
158    *
159    * @param gridSize The grid size of the solid geometry to create
160    * @return Returns the created geometry for the grid size
161    */
162   Geometry CreateGeometryBorder( Uint16Pair gridSize );
163
164   /**
165    * @brief Creates Image from the image url and parses the image for the stretch borders. Will create a error image if the n patch image is invalid
166    *
167    * @param nPatchImage The NinePatchImage to base our cropped images and stretch borders from
168    */
169   void InitializeFromImage( NinePatchImage nPatchImage );
170
171   /**
172    * @brief Creates an error Image to indicate that there was an error in either the image url or the parsing of the image
173    *
174    */
175   void InitializeFromBrokenImage();
176
177   /**
178    * @brief Applies this renderer's image to the sampler to the material used for this renderer
179    */
180   void ApplyImageToSampler();
181
182 private:
183
184   NinePatchImage mImage; ///< The image to render if the renderer was set from an NinePatchImage, empty otherwise
185   Image mCroppedImage;
186
187   std::string mImageUrl; ///< The url to the image resource to render if the renderer was set from an image resource url, empty otherwise
188   NinePatchImage::StretchRanges mStretchPixelsX;
189   NinePatchImage::StretchRanges mStretchPixelsY;
190   ImageDimensions mImageSize;
191   bool mBorderOnly;
192 };
193
194 } // namespace Internal
195
196 } // namespace Toolkit
197
198 } // namespace Dali
199
200 #endif /* __DALI_TOOLKIT_INTERNAL_N_PATCH_RENDERER_H__ */