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