[dali_1.2.19] Merge branch '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 an N-patch visual using an image URL.
62    *
63    * The visual will load the image synchronously when the associated actor is put on stage, and destroy the image when it is off stage
64    *
65    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
66    * @param[in] imageUrl The URL to 9 patch image resource to use
67    * @param[in] properties A Property::Map containing settings for this visual
68    * @return A smart-pointer to the newly allocated visual.
69    */
70   static NPatchVisualPtr New( VisualFactoryCache& factoryCache, const std::string& imageUrl, const Property::Map& properties );
71
72   /**
73    * @brief Create an N-patch visual using an image URL.
74    *
75    * The visual will load the image synchronously when the associated actor is put on stage, and destroy the image when it is off stage
76    *
77    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
78    * @param[in] imageUrl The URL to 9 patch image resource to use
79    * @return A smart-pointer to the newly allocated visual.
80    */
81   static NPatchVisualPtr New( VisualFactoryCache& factoryCache, const std::string& imageUrl );
82
83   /**
84    * @brief Create an N-patch visual with a NinePatchImage resource.
85    *
86    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
87    * @param[in] image The NinePatchImage to use
88    */
89   static NPatchVisualPtr New( VisualFactoryCache& factoryCache, NinePatchImage image );
90
91 public:  // from Visual
92
93   /**
94    * @copydoc Visual::Base::GetNaturalSize
95    */
96   virtual void GetNaturalSize( Vector2& naturalSize );
97
98   /**
99    * @copydoc Visual::Base::CreatePropertyMap
100    */
101   virtual void DoCreatePropertyMap( Property::Map& map ) const;
102
103 protected:
104
105   /**
106    * @brief Constructor.
107    *
108    * @param[in] factoryCache Reference to the VisualFactoryCache object
109    */
110   NPatchVisual( VisualFactoryCache& factoryCache );
111
112   /**
113    * @brief A reference counted object may only be deleted by calling Unreference().
114    */
115   virtual ~NPatchVisual();
116
117   /**
118    * @copydoc Visual::Base::DoSetProperties
119    */
120   virtual void DoSetProperties( const Property::Map& propertyMap );
121
122   /**
123    * @copydoc Visual::Base::DoSetOnStage
124    */
125   virtual void DoSetOnStage( Actor& actor );
126
127   /**
128    * @copydoc Visual::Base::DoSetOffStage
129    */
130   virtual void DoSetOffStage( Actor& actor );
131
132   /**
133    * @copydoc Visual::Base::OnSetTransform
134    */
135   virtual void OnSetTransform();
136
137 private:
138
139   /**
140    * @brief Creates a geometry for this renderer's grid size
141    *
142    * @return Returns the created geometry for this renderer's grid size
143    */
144   Geometry CreateGeometry();
145
146   /**
147    * @brief Creates a shader for this renderer's grid size
148    *
149    * @return Returns the created shader for this renderer's grid size
150    */
151   Shader CreateShader();
152
153   /**
154    * @brief Applies texture and related uniforms
155    */
156   void ApplyTextureAndUniforms();
157
158   /**
159    * Helper method to get the default Nine patch geometry from cache or create and store it there
160    * @param subType to use
161    * @return the geometry
162    */
163   Geometry GetNinePatchGeometry( VisualFactoryCache::GeometryType subType );
164
165   /**
166    * @brief Creates a geometry for the grid size to be used by this visuals' shaders
167    *
168    * @param[in] gridSize The grid size of the solid geometry to create
169    * @return Returns the created geometry for the grid size
170    */
171   Geometry CreateGridGeometry( Uint16Pair gridSize );
172
173   /**
174    * @brief Creates a geometry with the border only for the grid size to be used by this visuals' shaders
175    * e.g. a 5x4 grid would create a geometry that would look like:
176    *
177    *   ---------------------
178    *   |  /|  /|  /|  /|  /|
179    *   |/  |/  |/  |/  |/  |
180    *   ---------------------
181    *   |  /|           |  /|
182    *   |/  |           |/  |
183    *   -----           -----
184    *   |  /|           |  /|
185    *   |/  |           |/  |
186    *   ---------------------
187    *   |  /|  /|  /|  /|  /|
188    *   |/  |/  |/  |/  |/  |
189    *   ---------------------
190    *
191    * @param[in] gridSize The grid size of the solid geometry to create
192    * @return Returns the created geometry for the grid size
193    */
194   Geometry CreateBorderGeometry( Uint16Pair gridSize );
195
196 private:
197
198   NPatchLoader& mLoader;      ///< reference to N patch loader for fast access
199   std::string mImageUrl;      ///< The url to the N patch to load
200   std::size_t mId;            ///< id of the N patch (from loader/cache)
201   bool mBorderOnly;           ///< if only border is desired
202
203 };
204
205 } // namespace Internal
206
207 } // namespace Toolkit
208
209 } // namespace Dali
210
211 #endif // DALI_TOOLKIT_INTERNAL_N_PATCH_VISUAL_H