1 #ifndef DALI_TOOLKIT_INTERNAL_N_PATCH_VISUAL_H
2 #define DALI_TOOLKIT_INTERNAL_N_PATCH_VISUAL_H
5 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
31 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
43 typedef IntrusivePtr< NPatchVisual > NPatchVisualPtr;
46 * The visual which renders an 9 patch image to the control's quad
48 * The following properties are optional
50 * | %Property Name | Type |
51 * |--------------------------|------------------|
53 * | borderOnly | BOOLEAN
56 class NPatchVisual: public Visual::Base
61 * @brief Create a new n-patch visual.
63 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
64 * @return A smart-pointer to the newly allocated visual.
66 static NPatchVisualPtr New( VisualFactoryCache& factoryCache );
69 * @brief Create an N-patch visual using an image URL.
71 * The visual will load the image synchronously when the associated actor is put on stage, and destroy the image when it is off stage
73 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
74 * @param[in] imageUrl The URL to 9 patch image resource to use
75 * @param[in] borderOnly A Flag to indicate if the image should omit the centre of the n-patch and only render the border
77 static NPatchVisualPtr New( VisualFactoryCache& factoryCache, const std::string& imageUrl, bool borderOnly = false );
80 * @brief Create an N-patch visual with a NinePatchImage resource.
82 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
83 * @param[in] image The NinePatchImage to use
84 * @param[in] borderOnly A Flag to indicate if the image should omit the centre of the n-patch and only render the border
86 static NPatchVisualPtr New( VisualFactoryCache& factoryCache, NinePatchImage image, bool borderOnly = false );
88 public: // from Visual
91 * @copydoc Visual::Base::GetNaturalSize
93 virtual void GetNaturalSize( Vector2& naturalSize ) const;
96 * @copydoc Visual::Base::CreatePropertyMap
98 virtual void DoCreatePropertyMap( Property::Map& map ) const;
101 * @copydoc Visual::Base::DoSetProperty
103 virtual void DoSetProperty( Dali::Property::Index index, const Dali::Property::Value& propertyValue );
106 * @copydoc Visual::Base::DoGetProperty
108 virtual Dali::Property::Value DoGetProperty( Dali::Property::Index index );
113 * @brief Constructor.
115 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
116 * @param[in] borderOnly A Flag to indicate if the image should omit the centre of the n-patch and only render the border
118 NPatchVisual( VisualFactoryCache& factoryCache, bool borderOnly = false );
121 * @brief A reference counted object may only be deleted by calling Unreference().
123 virtual ~NPatchVisual();
126 * @copydoc Visual::Base::DoSetProperties
128 virtual void DoSetProperties( const Property::Map& propertyMap );
131 * @copydoc Visual::Base::DoSetOnStage
133 virtual void DoSetOnStage( Actor& actor );
136 * @copydoc Visual::Base::DoSetOffStage
138 virtual void DoSetOffStage( Actor& actor );
143 * @brief Initialize the renderer with the geometry and shader from the cache, if not available, create and save to the cache for sharing.
145 void InitializeRenderer();
148 * @brief Creates a geometry for this renderer's grid size
150 * @return Returns the created geometry for this renderer's grid size
152 Geometry CreateGeometry();
155 * @brief Creates a shader for this renderer's grid size
157 * @return Returns the created shader for this renderer's grid size
159 Shader CreateShader();
162 * @brief Creates a geometry for the grid size to be used by this visuals' shaders
164 * @param[in] gridSize The grid size of the solid geometry to create
165 * @return Returns the created geometry for the grid size
167 Geometry CreateGeometry( Uint16Pair gridSize );
170 * @brief Creates a geometry with the border only for the grid size to be used by this visuals' shaders
171 * e.g. a 5x4 grid would create a geometry that would look like:
173 * ---------------------
176 * ---------------------
182 * ---------------------
185 * ---------------------
187 * @param[in] gridSize The grid size of the solid geometry to create
188 * @return Returns the created geometry for the grid size
190 Geometry CreateGeometryBorder( Uint16Pair gridSize );
193 * @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
195 * @param[in] nPatchImage The NinePatchImage to base our cropped images and stretch borders from
197 void InitializeFromImage( NinePatchImage nPatchImage );
200 * @brief Creates an error Image to indicate that there was an error in either the image url or the parsing of the image
203 void InitializeFromBrokenImage();
206 * @brief Applies this renderer's image to the sampler to the texture set used for this renderer
208 void ApplyImageToSampler();
211 * @brief Changes the current renderer if the n-patch meta data has changed
213 * @param[in] oldBorderOnly The old flag indicating if the image should omit the centre of the n-patch and only render the border
214 * @param[in] oldGridX The old horizontal grid size of the solid geometry
215 * @param[in] oldGridY The old vertical grid size of the solid geometry
217 void ChangeRenderer( bool oldBorderOnly, size_t oldGridX, size_t oldGridY );
221 NinePatchImage mImage; ///< The image to render if the visual was set from an NinePatchImage, empty otherwise
224 std::string mImageUrl; ///< The url to the image resource to render if the visual was set from an image resource url, empty otherwise
225 NinePatchImage::StretchRanges mStretchPixelsX;
226 NinePatchImage::StretchRanges mStretchPixelsY;
227 ImageDimensions mImageSize;
231 } // namespace Internal
233 } // namespace Toolkit
237 #endif // DALI_TOOLKIT_INTERNAL_N_PATCH_VISUAL_H