Merge "Fixed bug in 3x3 NPatch shader and Color visual shader" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / npatch / npatch-visual.h
1 #ifndef DALI_TOOLKIT_INTERNAL_N_PATCH_VISUAL_H
2 #define DALI_TOOLKIT_INTERNAL_N_PATCH_VISUAL_H
3
4 /*
5  * Copyright (c) 2016 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 // EXTERNAL INCLUDES
22 #include <dali/public-api/common/intrusive-ptr.h>
23 #include <dali/public-api/images/image.h>
24 #include <dali/public-api/images/image-operations.h>
25 #include <dali/devel-api/images/nine-patch-image.h>
26 #include <dali/public-api/rendering/geometry.h>
27 #include <dali/public-api/rendering/sampler.h>
28 #include <dali/public-api/rendering/shader.h>
29
30 // INTERNAL INCLUDES
31 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
32
33 namespace Dali
34 {
35
36 namespace Toolkit
37 {
38
39 namespace Internal
40 {
41
42 class NPatchVisual;
43 typedef IntrusivePtr< NPatchVisual > NPatchVisualPtr;
44
45 /**
46  * The visual which renders an 9 patch image to the control's quad
47  *
48  * The following properties are optional
49  *
50  * | %Property Name           | Type             |
51  * |--------------------------|------------------|
52  * | url                      | STRING           |
53  * | borderOnly               | BOOLEAN
54  *
55  */
56 class NPatchVisual: public Visual::Base
57 {
58 public:
59
60   /**
61    * @brief Create a new n-patch visual.
62    *
63    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
64    * @return A smart-pointer to the newly allocated visual.
65    */
66   static NPatchVisualPtr New( VisualFactoryCache& factoryCache );
67
68   /**
69    * @brief Create an N-patch visual using an image URL.
70    *
71    * The visual will load the image synchronously when the associated actor is put on stage, and destroy the image when it is off stage
72    *
73    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
74    * @param[in] imageUrl The URL to 9 patch image resource to use
75    * @param[in] borderOnly A Flag to indicate if the image should omit the centre of the n-patch and only render the border
76    */
77   static NPatchVisualPtr New( VisualFactoryCache& factoryCache, const std::string& imageUrl, bool borderOnly = false );
78
79   /**
80    * @brief Create an N-patch visual with a NinePatchImage resource.
81    *
82    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
83    * @param[in] image The NinePatchImage to use
84    * @param[in] borderOnly A Flag to indicate if the image should omit the centre of the n-patch and only render the border
85    */
86   static NPatchVisualPtr New( VisualFactoryCache& factoryCache, NinePatchImage image, bool borderOnly = false );
87
88 public:  // from Visual
89
90   /**
91    * @copydoc Visual::Base::GetNaturalSize
92    */
93   virtual void GetNaturalSize( Vector2& naturalSize ) const;
94
95   /**
96    * @copydoc Visual::Base::CreatePropertyMap
97    */
98   virtual void DoCreatePropertyMap( Property::Map& map ) const;
99
100   /**
101    * @copydoc Visual::Base::DoSetProperty
102    */
103   virtual void DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue );
104
105   /**
106    * @copydoc Visual::Base::DoGetProperty
107    */
108   virtual Dali::Property::Value DoGetProperty( Dali::Property::Index index );
109
110 protected:
111
112   /**
113    * @brief Constructor.
114    *
115    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
116    * @param[in] borderOnly A Flag to indicate if the image should omit the centre of the n-patch and only render the border
117    */
118   NPatchVisual( VisualFactoryCache& factoryCache, bool borderOnly = false );
119
120   /**
121    * @brief A reference counted object may only be deleted by calling Unreference().
122    */
123   virtual ~NPatchVisual();
124
125   /**
126    * @copydoc Visual::Base::DoSetProperties
127    */
128   virtual void DoSetProperties( const Property::Map& propertyMap );
129
130   /**
131    * @copydoc Visual::Base::DoSetOnStage
132    */
133   virtual void DoSetOnStage( Actor& actor );
134
135   /**
136    * @copydoc Visual::Base::DoSetOffStage
137    */
138   virtual void DoSetOffStage( Actor& actor );
139
140   /**
141    * @copydoc Visual::Base::OnSetTransform
142    */
143   virtual void OnSetTransform();
144
145 private:
146
147   /**
148    * @brief Initialize the renderer with the geometry and shader from the cache, if not available, create and save to the cache for sharing.
149    */
150   void InitializeRenderer();
151
152   /**
153    * @brief Creates a geometry for this renderer's grid size
154    *
155    * @return Returns the created geometry for this renderer's grid size
156    */
157   Geometry CreateGeometry();
158
159   /**
160    * @brief Creates a shader for this renderer's grid size
161    *
162    * @return Returns the created shader for this renderer's grid size
163    */
164   Shader CreateShader();
165
166   /**
167    * @brief Creates a geometry for the grid size to be used by this visuals' shaders
168    *
169    * @param[in] gridSize The grid size of the solid geometry to create
170    * @return Returns the created geometry for the grid size
171    */
172   Geometry CreateGeometry( Uint16Pair gridSize );
173
174   /**
175    * @brief Creates a geometry with the border only for the grid size to be used by this visuals' shaders
176    * e.g. a 5x4 grid would create a geometry that would look like:
177    *
178    *   ---------------------
179    *   |  /|  /|  /|  /|  /|
180    *   |/  |/  |/  |/  |/  |
181    *   ---------------------
182    *   |  /|           |  /|
183    *   |/  |           |/  |
184    *   -----           -----
185    *   |  /|           |  /|
186    *   |/  |           |/  |
187    *   ---------------------
188    *   |  /|  /|  /|  /|  /|
189    *   |/  |/  |/  |/  |/  |
190    *   ---------------------
191    *
192    * @param[in] gridSize The grid size of the solid geometry to create
193    * @return Returns the created geometry for the grid size
194    */
195   Geometry CreateGeometryBorder( Uint16Pair gridSize );
196
197   /**
198    * @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
199    *
200    * @param[in] nPatchImage The NinePatchImage to base our cropped images and stretch borders from
201    */
202   void InitializeFromImage( NinePatchImage nPatchImage );
203
204   /**
205    * @brief Creates an error Image to indicate that there was an error in either the image url or the parsing of the image
206    *
207    */
208   void InitializeFromBrokenImage();
209
210   /**
211    * @brief Applies this renderer's image to the sampler to the texture set used for this renderer
212    */
213   void ApplyImageToSampler();
214
215   /**
216    * @brief Changes the current renderer if the n-patch meta data has changed
217    *
218    * @param[in] oldBorderOnly The old flag indicating if the image should omit the centre of the n-patch and only render the border
219    * @param[in] oldGridX The old horizontal grid size of the solid geometry
220    * @param[in] oldGridY The old vertical grid size of the solid geometry
221    */
222   void ChangeRenderer( bool oldBorderOnly, size_t oldGridX, size_t oldGridY );
223
224 private:
225
226   NinePatchImage mImage; ///< The image to render if the visual was set from an NinePatchImage, empty otherwise
227   Image mCroppedImage;
228
229   std::string mImageUrl; ///< The url to the image resource to render if the visual was set from an image resource url, empty otherwise
230   NinePatchImage::StretchRanges mStretchPixelsX;
231   NinePatchImage::StretchRanges mStretchPixelsY;
232   ImageDimensions mImageSize;
233   bool mBorderOnly;
234 };
235
236 } // namespace Internal
237
238 } // namespace Toolkit
239
240 } // namespace Dali
241
242 #endif // DALI_TOOLKIT_INTERNAL_N_PATCH_VISUAL_H