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