1 #ifndef DALI_TOOLKIT_INTERNAL_N_PATCH_VISUAL_H
2 #define DALI_TOOLKIT_INTERNAL_N_PATCH_VISUAL_H
5 * Copyright (c) 2020 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>
29 #include <dali/public-api/object/weak-handle.h>
32 #include <dali-toolkit/internal/visuals/texture-upload-observer.h>
33 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
34 #include <dali-toolkit/internal/visuals/visual-url.h>
46 typedef IntrusivePtr< NPatchVisual > NPatchVisualPtr;
49 * The visual which renders an 9 patch image to the control's quad
51 * The following properties are optional
53 * | %Property Name | Type |
54 * |--------------------------|------------------|
56 * | borderOnly | BOOLEAN |
57 * | border | RECTANGLE |
58 * | auxiliaryImage | STRING |
59 * | auxiliaryImageAlpha | FLOAT |
61 class NPatchVisual: public Visual::Base, public TextureUploadObserver
66 * @brief Create an N-patch visual using an image URL.
68 * The visual will load the image synchronously when the associated actor is put on stage, and destroy the image when it is off stage
70 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
71 * @param[in] imageUrl The URL to 9 patch image resource to use
72 * @param[in] properties A Property::Map containing settings for this visual
73 * @return A smart-pointer to the newly allocated visual.
75 static NPatchVisualPtr New( VisualFactoryCache& factoryCache, const VisualUrl& imageUrl, const Property::Map& properties );
78 * @brief Create an N-patch visual using an image URL.
80 * The visual will load the image synchronously when the associated actor is put on stage, and destroy the image when it is off stage
82 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
83 * @param[in] imageUrl The URL to 9 patch image resource to use
84 * @return A smart-pointer to the newly allocated visual.
86 static NPatchVisualPtr New( VisualFactoryCache& factoryCache, const VisualUrl& imageUrl );
89 * @brief Create an N-patch visual with a NinePatchImage resource.
91 * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
92 * @param[in] image The NinePatchImage to use
94 static NPatchVisualPtr New( VisualFactoryCache& factoryCache, NinePatchImage image );
96 public: // from Visual
99 * @copydoc Visual::Base::GetNaturalSize
101 void GetNaturalSize( Vector2& naturalSize ) override;
104 * @copydoc Visual::Base::CreatePropertyMap
106 void DoCreatePropertyMap( Property::Map& map ) const override;
109 * @copydoc Visual::Base::CreateInstancePropertyMap
111 void DoCreateInstancePropertyMap( Property::Map& map ) const override;
116 * @brief Constructor.
118 * @param[in] factoryCache Reference to the VisualFactoryCache object
120 NPatchVisual( VisualFactoryCache& factoryCache );
123 * @brief A reference counted object may only be deleted by calling Unreference().
125 virtual ~NPatchVisual();
128 * @copydoc Visual::Base::DoSetProperties
130 void DoSetProperties( const Property::Map& propertyMap ) override;
133 * @copydoc Visual::Base::DoSetOnStage
135 void DoSetOnStage( Actor& actor ) override;
138 * @copydoc Visual::Base::DoSetOffStage
140 void DoSetOffStage( Actor& actor ) override;
143 * @copydoc Visual::Base::OnSetTransform
145 void OnSetTransform() override;
150 * Loads the NPatch image and the Auxiliary image if needed
155 * @brief Creates a geometry for this renderer's grid size
157 * @return Returns the created geometry for this renderer's grid size
159 Geometry CreateGeometry();
162 * @brief Creates a shader for this renderer's grid size
164 * @return Returns the created shader for this renderer's grid size
166 Shader CreateShader();
169 * @brief Applies texture and related uniforms
171 void ApplyTextureAndUniforms();
174 * Helper method to get the default Nine patch geometry from cache or create and store it there
175 * @param subType to use
176 * @return the geometry
178 Geometry GetNinePatchGeometry( VisualFactoryCache::GeometryType subType );
181 * @brief Creates a geometry for the grid size to be used by this visuals' shaders
183 * @param[in] gridSize The grid size of the solid geometry to create
184 * @return Returns the created geometry for the grid size
186 Geometry CreateGridGeometry( Uint16Pair gridSize );
189 * @brief Creates a geometry with the border only for the grid size to be used by this visuals' shaders
190 * e.g. a 5x4 grid would create a geometry that would look like:
192 * ---------------------
195 * ---------------------
201 * ---------------------
204 * ---------------------
206 * @param[in] gridSize The grid size of the solid geometry to create
207 * @return Returns the created geometry for the grid size
209 Geometry CreateBorderGeometry( Uint16Pair gridSize );
212 * @brief Creates a renderer by using loaded resource.
219 * @copydoc TextureUploadObserver::UploadCompleted
221 * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
222 * This callback is the place to add the renderer as it would be called once the loading is finished.
224 void UploadComplete( bool loadSuccess, int32_t textureId, TextureSet textureSet, bool useAtlasing, const Vector4& atlasRect, bool preMultiplied ) override {}
227 * @copydoc TextureUploadObserver::LoadComplete
229 * To avoid rendering garbage pixels, renderer should be added to actor after the resources are ready.
230 * This callback is the place to add the renderer as it would be called once the loading is finished.
232 void LoadComplete( bool loadSuccess, Devel::PixelBuffer pixelBuffer, const VisualUrl& url, bool preMultiplied ) override;
236 WeakHandle<Actor> mPlacementActor; ///< Weakhandle to contain Actor during texture loading
237 NPatchLoader& mLoader; ///< reference to N patch loader for fast access
238 VisualUrl mImageUrl; ///< The url to the N patch to load
239 VisualUrl mAuxiliaryUrl; ///< An auxiliary image that can be displayed on top of the N-Patch
240 std::size_t mId; ///< id of the N patch (from loader/cache)
241 Devel::PixelBuffer mAuxiliaryPixelBuffer; ///< pixel buffer of the auxiliary mask image
242 bool mBorderOnly; ///< if only border is desired
243 Rect<int> mBorder; ///< The size of the border
244 float mAuxiliaryImageAlpha; ///< The alpha value for the auxiliary image only
247 } // namespace Internal
249 } // namespace Toolkit
253 #endif // DALI_TOOLKIT_INTERNAL_N_PATCH_VISUAL_H