Wrap mode support for AnimatedImageVisual
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / visuals / animated-image / animated-image-visual.h
1 #ifndef DALI_TOOLKIT_INTERNAL_ANIMATED_IMAGE_VISUAL_H
2 #define DALI_TOOLKIT_INTERNAL_ANIMATED_IMAGE_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/common/dali-vector.h>
24 #include <dali/public-api/math/vector4.h>
25 #include <dali/public-api/adaptor-framework/timer.h>
26
27 // INTERNAL INCLUDES
28 #include <dali-toolkit/internal/visuals/visual-base-impl.h>
29
30 namespace Dali
31 {
32
33 namespace Toolkit
34 {
35
36 namespace Internal
37 {
38
39 class AnimatedImageVisual;
40 typedef IntrusivePtr< AnimatedImageVisual > AnimatedImageVisualPtr;
41
42 /**
43  * The visual which renders an animated image
44  *
45  * The following property is essential
46  *
47  * | %Property Name     | Type              |
48  * |------------------- |-------------------|
49  * | url                | STRING            |
50  * | pixelArea          | VECTOR4           |
51  * | wrapModeU          | INTEGER OR STRING |
52  * | wrapModeV          | INTEGER OR STRING |
53  *
54  * where pixelArea is a rectangular area.
55  * In its Vector4 value, the first two elements indicate the top-left position of the area,
56  * and the last two elements are the area width and height respectively.
57  * If not specified, the default value is [0.0, 0.0, 1.0, 1.0], i.e. the entire area of the image.
58  *
59  * where wrapModeU and wrapModeV separately decide how the texture should be sampled when the u and v coordinate exceeds the range of 0.0 to 1.0.
60  * Its value should be one of the following wrap mode:
61  *   "DEFAULT"
62  *   "CLAMP_TO_EDGE"
63  *   "REPEAT"
64  *   "MIRRORED_REPEAT"
65  */
66
67 class AnimatedImageVisual : public Visual::Base, public ConnectionTracker
68 {
69
70 public:
71
72   /**
73    * @brief Create the animated image Visual using the image URL.
74    *
75    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
76    * @param[in] imageUrl The URL to svg resource to use
77    * @param[in] properties A Property::Map containing settings for this visual
78    * @return A smart-pointer to the newly allocated visual.
79    */
80   static AnimatedImageVisualPtr New( VisualFactoryCache& factoryCache, const std::string& imageUrl, const Property::Map& properties );
81
82   /**
83    * @brief Create the animated image visual using the image URL.
84    *
85    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
86    * @param[in] imageUrl The URL to animated image resource to use
87    */
88   static AnimatedImageVisualPtr New( VisualFactoryCache& factoryCache, const std::string& imageUrl );
89
90 public:  // from Visual
91
92   /**
93    * @copydoc Visual::Base::GetNaturalSize
94    */
95   virtual void GetNaturalSize( Vector2& naturalSize );
96
97   /**
98    * @copydoc Visual::Base::CreatePropertyMap
99    */
100   virtual void DoCreatePropertyMap( Property::Map& map ) const;
101
102 protected:
103
104   /**
105    * @brief Constructor.
106    *
107    * @param[in] factoryCache A pointer pointing to the VisualFactoryCache object
108    */
109   AnimatedImageVisual( VisualFactoryCache& factoryCache );
110
111   /**
112    * @brief A reference counted object may only be deleted by calling Unreference().
113    */
114   virtual ~AnimatedImageVisual();
115
116   /**
117    * @copydoc Visual::Base::DoSetProperties
118    */
119   virtual void DoSetProperties( const Property::Map& propertyMap );
120
121   /**
122    * @copydoc Visual::Base::DoSetOnStage
123    */
124   virtual void DoSetOnStage( Actor& actor );
125
126   /**
127    * @copydoc Visual::Base::DoSetOffStage
128    */
129   virtual void DoSetOffStage( Actor& actor );
130
131   /**
132    * @copydoc Visual::Base::OnSetTransform
133    */
134   virtual void OnSetTransform();
135
136 private:
137
138   /**
139    * Load the animated image and pack the frames into atlas.
140    * @return That atlas texture.
141    */
142   Texture PrepareAnimatedImage();
143
144   /**
145    * Display the next frame. It is called when the mFrameDelayTimes ticks.
146    */
147   bool DisplayNextFrame();
148
149   // Undefined
150   AnimatedImageVisual( const AnimatedImageVisual& animatedImageVisual );
151
152   // Undefined
153   AnimatedImageVisual& operator=( const AnimatedImageVisual& animatedImageVisual );
154
155 private:
156
157   Timer mFrameDelayTimer;
158   Dali::Vector<Vector4> mTextureRectContainer;
159   Dali::Vector<uint32_t> mFrameDelayContainer;
160   Vector4 mPixelArea;
161   std::string  mImageUrl;
162
163   ImageDimensions mImageSize;
164   uint32_t mCurrentFrameIndex;
165   Dali::WrapMode::Type mWrapModeU:3;
166   Dali::WrapMode::Type mWrapModeV:3;
167 };
168
169 } // namespace Internal
170
171 } // namespace Toolkit
172
173 } // namespace Dali
174 #endif /* DALI_TOOLKIT_INTERNAL_ANIMATED_IMAGE_VISUAL_H */