(Visuals) Remove dead code & redundant Get prefix to check if on stage or from the...
[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 // INTERNAL INCLUDES
22 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
23
24 // EXTERNAL INCLUDES
25 #include <dali/public-api/images/image.h>
26 #include <dali/public-api/images/image-operations.h>
27 #include <dali/devel-api/images/nine-patch-image.h>
28 #include <dali/public-api/rendering/geometry.h>
29 #include <dali/public-api/rendering/sampler.h>
30 #include <dali/public-api/rendering/shader.h>
31
32 namespace Dali
33 {
34
35 namespace Toolkit
36 {
37
38 namespace Internal
39 {
40
41 /**
42  * The visual which renders an 9 patch image to the control's quad
43  *
44  * The following properties are optional
45  *
46  * | %Property Name           | Type             |
47  * |--------------------------|------------------|
48  * | url                      | STRING           |
49  * | borderOnly               | BOOLEAN
50  *
51  */
52 class NPatchVisual: public Visual::Base
53 {
54 public:
55
56   /**
57    * @brief Constructor.
58    *
59    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
60    */
61   NPatchVisual( VisualFactoryCache& factoryCache );
62
63   /**
64    * @brief Constructor which creates 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] borderOnly A Flag to indicate if the image should omit the centre of the n-patch and only render the border
71    */
72   NPatchVisual( VisualFactoryCache& factoryCache, const std::string& imageUrl, bool borderOnly = false );
73
74   /**
75    * @brief Constructor which creates an N-patch viusal with a NinePatchImage resource.
76    *
77    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
78    * @param[in] image The NinePatchImage to use
79    * @param[in] borderOnly A Flag to indicate if the image should omit the centre of the n-patch and only render the border
80    */
81   NPatchVisual( VisualFactoryCache& factoryCache, NinePatchImage image, bool borderOnly = false );
82
83   /**
84    * @brief A reference counted object may only be deleted by calling Unreference().
85    */
86   ~NPatchVisual();
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    * @copydoc Visual::Base::DoInitialize
114    */
115   virtual void DoInitialize( Actor& actor, const Property::Map& propertyMap );
116
117   /**
118    * @copydoc Visual::Base::DoSetOnStage
119    */
120   virtual void DoSetOnStage( Actor& actor );
121
122   /**
123    * @copydoc Visual::Base::DoSetOffStage
124    */
125   virtual void DoSetOffStage( Actor& actor );
126
127 private:
128
129   /**
130    * @brief Initialize the renderer with the geometry and shader from the cache, if not available, create and save to the cache for sharing.
131    */
132   void InitializeRenderer();
133
134   /**
135    * @brief Creates a geometry for this renderer's grid size
136    *
137    * @return Returns the created geometry for this renderer's grid size
138    */
139   Geometry CreateGeometry();
140
141   /**
142    * @brief Creates a shader for this renderer's grid size
143    *
144    * @return Returns the created shader for this renderer's grid size
145    */
146   Shader CreateShader();
147
148   /**
149    * @brief Creates a geometry for the grid size to be used by this visuals' shaders
150    *
151    * @param[in] gridSize The grid size of the solid geometry to create
152    * @return Returns the created geometry for the grid size
153    */
154   Geometry CreateGeometry( Uint16Pair gridSize );
155
156   /**
157    * @brief Creates a geometry with the border only for the grid size to be used by this visuals' shaders
158    * e.g. a 5x4 grid would create a geometry that would look like:
159    *
160    *   ---------------------
161    *   |  /|  /|  /|  /|  /|
162    *   |/  |/  |/  |/  |/  |
163    *   ---------------------
164    *   |  /|           |  /|
165    *   |/  |           |/  |
166    *   -----           -----
167    *   |  /|           |  /|
168    *   |/  |           |/  |
169    *   ---------------------
170    *   |  /|  /|  /|  /|  /|
171    *   |/  |/  |/  |/  |/  |
172    *   ---------------------
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 CreateGeometryBorder( Uint16Pair gridSize );
178
179   /**
180    * @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
181    *
182    * @param[in] nPatchImage The NinePatchImage to base our cropped images and stretch borders from
183    */
184   void InitializeFromImage( NinePatchImage nPatchImage );
185
186   /**
187    * @brief Creates an error Image to indicate that there was an error in either the image url or the parsing of the image
188    *
189    */
190   void InitializeFromBrokenImage();
191
192   /**
193    * @brief Applies this renderer's image to the sampler to the texture set used for this renderer
194    */
195   void ApplyImageToSampler();
196
197   /**
198    * @brief Changes the current renderer if the n-patch meta data has changed
199    *
200    * @param[in] oldBorderOnly The old flag indicating if the image should omit the centre of the n-patch and only render the border
201    * @param[in] oldGridX The old horizontal grid size of the solid geometry
202    * @param[in] oldGridY The old vertical grid size of the solid geometry
203    */
204   void ChangeRenderer( bool oldBorderOnly, size_t oldGridX, size_t oldGridY );
205
206 private:
207
208   NinePatchImage mImage; ///< The image to render if the visual was set from an NinePatchImage, empty otherwise
209   Image mCroppedImage;
210
211   std::string mImageUrl; ///< The url to the image resource to render if the visual was set from an image resource url, empty otherwise
212   NinePatchImage::StretchRanges mStretchPixelsX;
213   NinePatchImage::StretchRanges mStretchPixelsY;
214   ImageDimensions mImageSize;
215   bool mBorderOnly;
216 };
217
218 } // namespace Internal
219
220 } // namespace Toolkit
221
222 } // namespace Dali
223
224 #endif // DALI_TOOLKIT_INTERNAL_N_PATCH_VISUAL_H