Added Auxiliary image to NPatch visual.
[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) 2017 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 #include <dali-toolkit/internal/visuals/visual-url.h>
33
34 namespace Dali
35 {
36
37 namespace Toolkit
38 {
39
40 namespace Internal
41 {
42
43 class NPatchVisual;
44 typedef IntrusivePtr< NPatchVisual > NPatchVisualPtr;
45
46 /**
47  * The visual which renders an 9 patch image to the control's quad
48  *
49  * The following properties are optional
50  *
51  * | %Property Name           | Type             |
52  * |--------------------------|------------------|
53  * | url                      | STRING           |
54  * | borderOnly               | BOOLEAN          |
55  * | border                   | RECTANGLE        |
56  * | auxiliaryImage           | STRING           |
57  * | auxiliaryImageAlpha      | FLOAT            |
58  */
59 class NPatchVisual: public Visual::Base
60 {
61 public:
62
63   /**
64    * @brief Create an N-patch visual using an image URL.
65    *
66    * The visual will load the image synchronously when the associated actor is put on stage, and destroy the image when it is off stage
67    *
68    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
69    * @param[in] imageUrl The URL to 9 patch image resource to use
70    * @param[in] properties A Property::Map containing settings for this visual
71    * @return A smart-pointer to the newly allocated visual.
72    */
73   static NPatchVisualPtr New( VisualFactoryCache& factoryCache, const VisualUrl& imageUrl, const Property::Map& properties );
74
75   /**
76    * @brief Create an N-patch visual using an image URL.
77    *
78    * The visual will load the image synchronously when the associated actor is put on stage, and destroy the image when it is off stage
79    *
80    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
81    * @param[in] imageUrl The URL to 9 patch image resource to use
82    * @return A smart-pointer to the newly allocated visual.
83    */
84   static NPatchVisualPtr New( VisualFactoryCache& factoryCache, const VisualUrl& imageUrl );
85
86   /**
87    * @brief Create an N-patch visual with a NinePatchImage resource.
88    *
89    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
90    * @param[in] image The NinePatchImage to use
91    */
92   static NPatchVisualPtr New( VisualFactoryCache& factoryCache, NinePatchImage image );
93
94 public:  // from Visual
95
96   /**
97    * @copydoc Visual::Base::GetNaturalSize
98    */
99   virtual void GetNaturalSize( Vector2& naturalSize );
100
101   /**
102    * @copydoc Visual::Base::CreatePropertyMap
103    */
104   virtual void DoCreatePropertyMap( Property::Map& map ) const;
105
106   /**
107    * @copydoc Visual::Base::CreateInstancePropertyMap
108    */
109   virtual void DoCreateInstancePropertyMap( Property::Map& map ) const;
110
111 protected:
112
113   /**
114    * @brief Constructor.
115    *
116    * @param[in] factoryCache Reference to the VisualFactoryCache object
117    */
118   NPatchVisual( VisualFactoryCache& factoryCache );
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    * Loads the NPatch image and the Auxiliary image if needed
149    */
150   void LoadImages();
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 Applies texture and related uniforms
168    */
169   void ApplyTextureAndUniforms();
170
171   /**
172    * Helper method to get the default Nine patch geometry from cache or create and store it there
173    * @param subType to use
174    * @return the geometry
175    */
176   Geometry GetNinePatchGeometry( VisualFactoryCache::GeometryType subType );
177
178   /**
179    * @brief Creates a geometry for the grid size to be used by this visuals' shaders
180    *
181    * @param[in] gridSize The grid size of the solid geometry to create
182    * @return Returns the created geometry for the grid size
183    */
184   Geometry CreateGridGeometry( Uint16Pair gridSize );
185
186   /**
187    * @brief Creates a geometry with the border only for the grid size to be used by this visuals' shaders
188    * e.g. a 5x4 grid would create a geometry that would look like:
189    *
190    *   ---------------------
191    *   |  /|  /|  /|  /|  /|
192    *   |/  |/  |/  |/  |/  |
193    *   ---------------------
194    *   |  /|           |  /|
195    *   |/  |           |/  |
196    *   -----           -----
197    *   |  /|           |  /|
198    *   |/  |           |/  |
199    *   ---------------------
200    *   |  /|  /|  /|  /|  /|
201    *   |/  |/  |/  |/  |/  |
202    *   ---------------------
203    *
204    * @param[in] gridSize The grid size of the solid geometry to create
205    * @return Returns the created geometry for the grid size
206    */
207   Geometry CreateBorderGeometry( Uint16Pair gridSize );
208
209 private:
210
211   NPatchLoader&      mLoader;               ///< reference to N patch loader for fast access
212   VisualUrl          mImageUrl;             ///< The url to the N patch to load
213   VisualUrl          mAuxiliaryUrl;         ///< An auxiliary image that can be displayed on top of the N-Patch
214   std::size_t        mId;                   ///< id of the N patch (from loader/cache)
215   Devel::PixelBuffer mAuxiliaryPixelBuffer; ///< pixel buffer of the auxiliary mask image
216   bool               mBorderOnly;           ///< if only border is desired
217   Rect<int>          mBorder;               ///< The size of the border
218   float              mAuxiliaryImageAlpha;  ///< The alpha value for the auxiliary image only
219 };
220
221 } // namespace Internal
222
223 } // namespace Toolkit
224
225 } // namespace Dali
226
227 #endif // DALI_TOOLKIT_INTERNAL_N_PATCH_VISUAL_H