[NUI] Rebase DevelNUI (#2507)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / BaseComponents / AnimatedImageView.cs
1 /*
2  * Copyright(c) 2019 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 using System.Collections.Generic;
19 using System.ComponentModel;
20
21 #if (NUI_DEBUG_ON)
22 using tlog = Tizen.Log;
23 #endif
24
25 namespace Tizen.NUI.BaseComponents
26 {
27     /// <summary>
28     /// AnimatedImageView is a class for displaying Animated-GIF and Image-Array
29     /// </summary>
30     // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
31     [EditorBrowsable(EditorBrowsableState.Never)]
32     public class AnimatedImageView : ImageView
33     {
34         #region Constructor, Distructor, Dispose
35         /// <summary>
36         /// Construct AnimatedImageView
37         /// </summary>
38         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
39         [EditorBrowsable(EditorBrowsableState.Never)]
40         public AnimatedImageView() : base()
41         {
42             dirtyFlag = true;
43         }
44
45         /// <summary>
46         /// You can override it to clean-up your own resources
47         /// </summary>
48         /// <param name="type">DisposeTypes</param>
49         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
50         [EditorBrowsable(EditorBrowsableState.Never)]
51         protected override void Dispose(DisposeTypes type)
52         {
53             if (disposed)
54             {
55                 return;
56             }
57
58             //Release your own unmanaged resources here.
59             //You should not access any managed member here except static instance.
60             //because the execution order of Finalizes is non-deterministic.
61             base.Dispose(type);
62         }
63         #endregion Constructor, Distructor, Dispose
64
65         #region Property
66         /// <summary>
67         /// Image URL for Animated-GIF
68         /// </summary>
69         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
70         [EditorBrowsable(EditorBrowsableState.Never)]
71         public new string ResourceUrl
72         {
73             get
74             {
75                 return url;
76             }
77             set
78             {
79                 dirtyFlag = true;
80                 url = value;
81             }
82         }
83
84         /// <summary>
85         ///  Image URL list for Image-Array
86         /// </summary>
87         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
88         [EditorBrowsable(EditorBrowsableState.Never)]
89         public List<string> URLs
90         {
91             get
92             {
93                 return resourceURLs;
94             }
95         }
96
97         /// <summary>
98         /// Defines the batch size for pre-loading images in the Image-Array animation.
99         /// number of images to pre-load before starting to play. Default value: 1.
100         /// </summary>
101         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
102         [EditorBrowsable(EditorBrowsableState.Never)]
103         public int BatchSize
104         {
105             get
106             {
107                 return batchSize;
108             }
109             set
110             {
111                 dirtyFlag = true;
112                 batchSize = value;
113             }
114         }
115
116         /// <summary>
117         /// Defines the cache size for loading images in the Image-Array animation.
118         /// number of images to keep cached ahead during playback. Default value: 1.
119         ///</summary>
120         ///<remarks>
121         /// cacheSize should be >= batchSize. If it isn't, then the cache will automatically be changed to batchSize.
122         /// because of the defaults, it is expected that the application developer tune the batch and cache sizes to their particular use case.
123         /// </remarks>
124         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
125         [EditorBrowsable(EditorBrowsableState.Never)]
126         public int CacheSize
127         {
128             get
129             {
130                 return cacheSize;
131             }
132             set
133             {
134                 dirtyFlag = true;
135                 cacheSize = value;
136             }
137         }
138
139         /// <summary>
140         /// The number of milliseconds between each frame in the Image-Array animation.
141         /// The number of milliseconds between each frame.
142         /// </summary>
143         /// <remarks>
144         /// This is only used when URLs(multiple string) are provided.
145         /// </remarks>
146         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
147         [EditorBrowsable(EditorBrowsableState.Never)]
148         public int FrameDelay
149         {
150             get
151             {
152                 return frameDelay;
153             }
154             set
155             {
156                 dirtyFlag = true;
157                 frameDelay = value;
158             }
159         }
160
161         /// <summary>
162         /// The number of looping.
163         /// </summary>
164         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
165         [EditorBrowsable(EditorBrowsableState.Never)]
166         public int LoopCount
167         {
168             get
169             {
170                 return loopCount;
171             }
172             set
173             {
174                 dirtyFlag = true;
175                 loopCount = value;
176             }
177         }
178
179         /// <summary>
180         /// Sets or gets the stop behavior.
181         /// </summary>
182         [EditorBrowsable(EditorBrowsableState.Never)]
183         public StopBehaviorType StopBehavior
184         {
185             set
186             {
187                 stopBehavior = (StopBehaviorType)value;
188                 dirtyFlag = true;
189             }
190             get
191             {
192                 return stopBehavior;
193             }
194         }
195
196         /// <summary>
197         /// Get the number of total frames
198         /// </summary>
199         [EditorBrowsable(EditorBrowsableState.Never)]
200         public int TotalFrame
201         {
202             get
203             {
204                 int ret = -1;
205                 PropertyMap map = Image;
206                 if (map != null)
207                 {
208                     PropertyValue val = map.Find(ImageVisualProperty.TotalFrameNumber);
209                     if (val != null)
210                     {
211                         if (val.Get(out ret))
212                         {
213                             return ret;
214                         }
215                     }
216                 }
217                 return ret;
218             }
219         }
220
221         /// <summary>
222         /// Set or get the current frame. When setting a specific frame, it is displayed as a still image.
223         /// </summary>
224         /// <remarks>
225         /// Gets the value set by a user. If the setting value is out-ranged, it is reset as a minimum frame or a maximum frame.
226         /// </remarks>
227         [EditorBrowsable(EditorBrowsableState.Never)]
228         public int CurrentFrame
229         {
230             set
231             {
232                 DoAction(ImageView.Property.IMAGE, (int)ActionType.jumpTo, new PropertyValue(value));
233             }
234             get
235             {
236                 int ret = -1;
237                 PropertyMap map = Image;
238                 if (map != null)
239                 {
240                     PropertyValue val = map.Find(ImageVisualProperty.CurrentFrameNumber);
241                     if (val != null)
242                     {
243                         if (val.Get(out ret))
244                         {
245                             return ret;
246                         }
247                     }
248                 }
249                 return ret;
250             }
251         }
252         #endregion Property
253
254         #region Method
255         /// <summary>
256         /// To make the properies be set. This should be called after the properties are set.
257         /// </summary>
258         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
259         [EditorBrowsable(EditorBrowsableState.Never)]
260         public void SetValues()
261         {
262             if (dirtyFlag == false)
263             {
264                 return;
265             }
266             dirtyFlag = false;
267
268             PropertyMap tMap = new PropertyMap();
269             PropertyValue animatiedImage = new PropertyValue((int)Visual.Type.AnimatedImage);
270             tMap.Insert(Visual.Property.Type, animatiedImage);
271             if (resourceURLs?.Count != 0)
272             {
273                 PropertyArray indexPropertyArray = new PropertyArray();
274                 PropertyArray returnedArr = new PropertyArray();
275                 PropertyValue index = new PropertyValue();
276                 foreach (var iter in resourceURLs)
277                 {
278                     index = new PropertyValue(iter);
279                     returnedArr = indexPropertyArray.Add(index);
280                 }
281                 index.Dispose();
282                 returnedArr.Dispose();
283                 PropertyValue arrayProperty = new PropertyValue(indexPropertyArray);
284                 tMap.Insert(ImageVisualProperty.URL, arrayProperty);
285                 PropertyValue frameDelayProperty = new PropertyValue(frameDelay);
286                 tMap.Insert(ImageVisualProperty.FrameDelay, frameDelayProperty);
287
288                 arrayProperty.Dispose();
289                 indexPropertyArray.Dispose();
290                 frameDelayProperty.Dispose();
291             }
292             else
293             {
294                 PropertyValue urlProperty = new PropertyValue(url);
295                 tMap.Insert(ImageVisualProperty.URL, urlProperty);
296                 urlProperty.Dispose();
297             }
298
299             PropertyValue batchSizeProperty = new PropertyValue(batchSize);
300             tMap.Insert(ImageVisualProperty.BatchSize, batchSizeProperty);
301             PropertyValue cacheSizeProperty = new PropertyValue(cacheSize);
302             tMap.Insert(ImageVisualProperty.CacheSize, cacheSizeProperty);
303             PropertyValue loopCountProperty = new PropertyValue(loopCount);
304             tMap.Insert(ImageVisualProperty.LoopCount, loopCountProperty);
305             PropertyValue stopBehaviorProperty = new PropertyValue((int)stopBehavior);
306             tMap.Insert(ImageVisualProperty.StopBehavior, stopBehaviorProperty);
307
308             loopCountProperty.Dispose();
309             cacheSizeProperty.Dispose();
310             batchSizeProperty.Dispose();
311             stopBehaviorProperty.Dispose();
312
313             propertyMap = tMap;
314             PropertyValue mapProperty = new PropertyValue(propertyMap);
315             SetProperty(ImageView.Property.IMAGE, mapProperty);
316             mapProperty.Dispose();
317
318             tMap.Dispose();
319             animatiedImage.Dispose();
320         }
321
322         /// <summary>
323         /// Play animation
324         /// </summary>
325         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
326         [EditorBrowsable(EditorBrowsableState.Never)]
327         public new void Play()
328         {
329             SetValues();
330             base.Play();
331         }
332
333         /// <summary>
334         /// Pause animation. Currently pause and stop are same
335         /// </summary>
336         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
337         [EditorBrowsable(EditorBrowsableState.Never)]
338         public new void Pause()
339         {
340             SetValues();
341             base.Pause();
342         }
343
344         /// <summary>
345         /// Stop animation. Currently pause and stop are same
346         /// </summary>
347         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
348         [EditorBrowsable(EditorBrowsableState.Never)]
349         public new void Stop()
350         {
351             SetValues();
352             base.Stop();
353         }
354         #endregion Method
355
356
357         #region Event, Enum, Struct, ETC
358
359         /// <summary>
360         /// Enumeration for what to do when the animation is stopped.
361         /// </summary>
362         [EditorBrowsable(EditorBrowsableState.Never)]
363         public enum StopBehaviorType
364         {
365             /// <summary>
366             /// When the animation is stopped, the current frame is shown.
367             /// </summary>
368             [EditorBrowsable(EditorBrowsableState.Never)]
369             CurrentFrame,
370             /// <summary>
371             /// When the animation is stopped, the min frame (first frame) is shown.
372             /// </summary>
373             [EditorBrowsable(EditorBrowsableState.Never)]
374             MinimumFrame,
375             /// <summary>
376             /// When the animation is stopped, the max frame (last frame) is shown.
377             /// </summary>
378             [EditorBrowsable(EditorBrowsableState.Never)]
379             MaximumFrame
380         }
381
382         private enum ActionType
383         {
384             play,
385             pause,
386             stop,
387             jumpTo,
388         };
389         #endregion Event, Enum, Struct, ETC
390
391
392         #region Internal
393         #endregion Internal
394
395
396         #region Private
397         private string url = "";
398         private List<string> resourceURLs = new List<string>();
399         private int batchSize = 1;
400         private int cacheSize = 1;
401         private int frameDelay = 0;
402         private int loopCount = -1;
403         private bool dirtyFlag = false;
404         private StopBehaviorType stopBehavior;
405         private PropertyMap propertyMap;
406         #endregion Private
407     }
408 }