Merge "Marked new API's since 1.1.4" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / renderers / npatch / npatch-renderer.h
1 #ifndef __DALI_TOOLKIT_INTERNAL_N_PATCH_RENDERER_H__
2 #define __DALI_TOOLKIT_INTERNAL_N_PATCH_RENDERER_H__
3
4 /*
5  * Copyright (c) 2015 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/controls/renderers/control-renderer-impl.h>
23
24 // EXTERNAL INCLUDES
25 #include <dali/public-api/images/image.h>
26 #include <dali/public-api/images/nine-patch-image.h>
27 #include <dali/public-api/images/image-operations.h>
28 #include <dali/devel-api/rendering/geometry.h>
29 #include <dali/devel-api/rendering/shader.h>
30 #include <dali/devel-api/rendering/sampler.h>
31
32 namespace Dali
33 {
34
35 namespace Toolkit
36 {
37
38 namespace Internal
39 {
40
41 /**
42  * The renderer 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  * | image-url                 | STRING           |
49  *
50  */
51 class NPatchRenderer: public ControlRenderer
52 {
53 public:
54
55   /**
56    * @brief Constructor.
57    */
58   NPatchRenderer();
59
60   /**
61    * @brief A reference counted object may only be deleted by calling Unreference().
62    */
63   ~NPatchRenderer();
64
65 public:  // from ControlRenderer
66
67   /**
68    * @copydoc ControlRenderer::Initialize
69    */
70   virtual void Initialize( RendererFactoryCache& factoryCache, const Property::Map& propertyMap );
71
72   /**
73    * @copydoc ControlRenderer::SetClipRect
74    */
75   virtual void SetClipRect( const Rect<int>& clipRect );
76
77   /**
78    * @copydoc ControlRenderer::SetOffset
79    */
80   virtual void SetOffset( const Vector2& offset );
81
82 protected:
83   /**
84    * @copydoc ControlRenderer::DoSetOnStage
85    */
86   virtual void DoSetOnStage( Actor& actor );
87
88   /**
89    * @copydoc ControlRenderer::DoSetOffStage
90    */
91   virtual void DoSetOffStage( Actor& actor );
92
93 public:
94
95   /**
96    * Request the geometry and shader from the cache, if not available, create and save to the cache for sharing.
97    *
98    * @param[in] factoryCache A pointer pointing to the RendererFactoryCache object
99    */
100   void Initialize( RendererFactoryCache& factoryCache );
101
102   /**
103    * @brief Sets the 9 patch image of this renderer to the resource at imageUrl
104    * The renderer will load the image synchronously when the associated actor is put on stage, and destroy the image when it is off stage
105    *
106    * @param[in] imageUrl The URL to 9 patch image resource to use
107    * @param[in] borderOnly A Flag to indicate if the image should omit the centre of the n-patch and only render the border
108    */
109   void SetImage( const std::string& imageUrl, bool borderOnly = false );
110
111   /**
112    * @brief Sets the 9 patch image of this renderer to the 9 patch image
113    *
114    * @param[in] image The NinePatchImage to use
115    * @param[in] borderOnly A Flag to indicate if the image should omit the centre of the n-patch and only render the border
116    */
117   void SetImage( NinePatchImage image, bool borderOnly = false );
118
119 private:
120
121   /**
122    * @brief Creates a geometry for the grid size to be used by this renderers' shaders
123    *
124    * @param gridSize The grid size of the solid geometry to create
125    * @return Returns the created geometry for the grid size
126    */
127   Geometry CreateGeometry( Uint16Pair gridSize );
128
129   /**
130    * @brief Creates a geometry with the border only for the grid size to be used by this renderers' shaders
131    * e.g. a 5x4 grid would create a geometry that would look like:
132    *
133    *   ---------------------
134    *   |  /|  /|  /|  /|  /|
135    *   |/  |/  |/  |/  |/  |
136    *   ---------------------
137    *   |  /|           |  /|
138    *   |/  |           |/  |
139    *   -----           -----
140    *   |  /|           |  /|
141    *   |/  |           |/  |
142    *   ---------------------
143    *   |  /|  /|  /|  /|  /|
144    *   |/  |/  |/  |/  |/  |
145    *   ---------------------
146    *
147    * @param gridSize The grid size of the solid geometry to create
148    * @return Returns the created geometry for the grid size
149    */
150   Geometry CreateGeometryBorder( Uint16Pair gridSize );
151
152   /**
153    * @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
154    *
155    * @param nPatchImage The NinePatchImage to base our cropped images and stretch borders from
156    */
157   void InitialiseFromImage( NinePatchImage nPatchImage );
158
159   /**
160    * @brief Creates a black Image to indicate that there was an error in either the image url or the parsing of the image
161    *
162    */
163   void CreateErrorImage();
164
165   /**
166    * @brief Applies this renderer's image to the sampler to the material used for this renderer
167    */
168   void ApplyImageToSampler();
169
170 private:
171
172   NinePatchImage mImage; ///< The image to render if the renderer was set from an NinePatchImage, empty otherwise
173   Image mCroppedImage;
174   Geometry mNinePatchGeometry;
175   Geometry mNinePatchBorderGeometry;
176   Shader mNinePatchShader;
177
178   std::string mImageUrl; ///< The url to the image resource to render if the renderer was set from an image resource url, empty otherwise
179   NinePatchImage::StretchRanges mStretchPixelsX;
180   NinePatchImage::StretchRanges mStretchPixelsY;
181   ImageDimensions mImageSize;
182   bool mBorderOnly;
183 };
184
185 } // namespace Internal
186
187 } // namespace Toolkit
188
189 } // namespace Dali
190
191 #endif /* __DALI_TOOLKIT_INTERNAL_N_PATCH_RENDERER_H__ */