[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[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 namespace Tizen.NUI.BaseComponents
22 {
23     /// <summary>
24     /// AnimatedImageView is a class for displaying Animated-GIF and Image-Array
25     /// </summary>
26     // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
27     [EditorBrowsable(EditorBrowsableState.Never)]
28     public partial class AnimatedImageView : ImageView
29     {
30         #region Internal
31         #endregion Internal
32
33         #region Private
34         private string url = "";
35         private List<string> resourceURLs = new List<string>();
36         private int batchSize = 1;
37         private int cacheSize = 1;
38         private int frameDelay = 0;
39         private int loopCount = -1;
40         private bool dirtyFlag = false;
41         private StopBehaviorType stopBehavior;
42         private PropertyMap propertyMap;
43         #endregion Private
44
45         #region Constructor, Destructor, Dispose
46         /// <summary>
47         /// Construct AnimatedImageView
48         /// </summary>
49         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
50         [EditorBrowsable(EditorBrowsableState.Never)]
51         public AnimatedImageView() : base()
52         {
53             ActionPlay = Interop.AnimatedImageView.AnimatedImageVisualActionPlayGet();
54             ActionPause = Interop.AnimatedImageView.AnimatedImageVisualActionPauseGet();
55             ActionStop = Interop.AnimatedImageView.AnimatedImageVisualActionStopGet();
56     
57             dirtyFlag = true;
58         }
59
60         /// <summary>
61         /// You can override it to clean-up your own resources
62         /// </summary>
63         /// <param name="type">DisposeTypes</param>
64         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
65         [EditorBrowsable(EditorBrowsableState.Never)]
66         protected override void Dispose(DisposeTypes type)
67         {
68             if (disposed)
69             {
70                 return;
71             }
72
73             //Release your own unmanaged resources here.
74             //You should not access any managed member here except static instance.
75             //because the execution order of Finalizes is non-deterministic.
76             base.Dispose(type);
77         }
78         #endregion Constructor, Destructor, Dispose
79
80         #region Property
81         /// <summary>
82         /// Image URL for Animated-GIF
83         /// </summary>
84         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
85         [EditorBrowsable(EditorBrowsableState.Never)]
86         public new string ResourceUrl
87         {
88             get
89             {
90                 return GetValue(ResourceUrlProperty) as string;
91             }
92             set
93             {
94                 SetValue(ResourceUrlProperty, value);
95                 NotifyPropertyChanged();
96             }
97         }
98
99         private string InternalResourceUrl
100         {
101             get
102             {
103                 return url;
104             }
105             set
106             {
107                 dirtyFlag = true;
108                 url = value;
109             }
110         }
111
112         /// <summary>
113         ///  Image URL list for Image-Array
114         /// </summary>
115         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
116         [EditorBrowsable(EditorBrowsableState.Never)]
117         public List<string> URLs
118         {
119             get
120             {
121                 return resourceURLs;
122             }
123         }
124
125         /// <summary>
126         /// Defines the batch size for pre-loading images in the Image-Array animation.
127         /// number of images to pre-load before starting to play. Default value: 1.
128         /// </summary>
129         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
130         [EditorBrowsable(EditorBrowsableState.Never)]
131         public int BatchSize
132         {
133             get
134             {
135                 return (int)GetValue(BatchSizeProperty);
136             }
137             set
138             {
139                 SetValue(BatchSizeProperty, value);
140                 NotifyPropertyChanged();
141             }
142         }
143
144         private int InternalBatchSize
145         {
146             get
147             {
148                 return batchSize;
149             }
150             set
151             {
152                 dirtyFlag = true;
153                 batchSize = value;
154             }
155         }
156
157         /// <summary>
158         /// Defines the cache size for loading images in the Image-Array animation.
159         /// number of images to keep cached ahead during playback. Default value: 1.
160         ///</summary>
161         ///<remarks>
162         /// cacheSize should be >= batchSize. If it isn't, then the cache will automatically be changed to batchSize.
163         /// because of the defaults, it is expected that the application developer tune the batch and cache sizes to their particular use case.
164         /// </remarks>
165         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
166         [EditorBrowsable(EditorBrowsableState.Never)]
167         public int CacheSize
168         {
169             get
170             {
171                 return (int)GetValue(CacheSizeProperty);
172             }
173             set
174             {
175                 SetValue(CacheSizeProperty, value);
176                 NotifyPropertyChanged();
177             }
178         }
179
180         private int InternalCacheSize
181         {
182             get
183             {
184                 return cacheSize;
185             }
186             set
187             {
188                 dirtyFlag = true;
189                 cacheSize = value;
190             }
191         }
192
193         /// <summary>
194         /// The number of milliseconds between each frame in the Image-Array animation.
195         /// The number of milliseconds between each frame.
196         /// </summary>
197         /// <remarks>
198         /// This is only used when URLs(multiple string) are provided.
199         /// </remarks>
200         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
201         [EditorBrowsable(EditorBrowsableState.Never)]
202         public int FrameDelay
203         {
204             get
205             {
206                 return (int)GetValue(FrameDelayProperty);
207             }
208             set
209             {
210                 SetValue(FrameDelayProperty, value);
211                 NotifyPropertyChanged();
212             }
213         }
214
215         private int InternalFrameDelay
216         {
217             get
218             {
219                 return frameDelay;
220             }
221             set
222             {
223                 dirtyFlag = true;
224                 frameDelay = value;
225             }
226         }
227
228         /// <summary>
229         /// The number of looping.
230         /// </summary>
231         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
232         [EditorBrowsable(EditorBrowsableState.Never)]
233         public int LoopCount
234         {
235             get
236             {
237                 return (int)GetValue(LoopCountProperty);
238             }
239             set
240             {
241                 SetValue(LoopCountProperty, value);
242                 NotifyPropertyChanged();
243             }
244         }
245
246         private int InternalLoopCount
247         {
248             get
249             {
250                 return loopCount;
251             }
252             set
253             {
254                 dirtyFlag = true;
255                 loopCount = value;
256             }
257         }
258
259         /// <summary>
260         /// Sets or gets the stop behavior.
261         /// </summary>
262         [EditorBrowsable(EditorBrowsableState.Never)]
263         public StopBehaviorType StopBehavior
264         {
265             get
266             {
267                 return (StopBehaviorType)GetValue(StopBehaviorProperty);
268             }
269             set
270             {
271                 SetValue(StopBehaviorProperty, value);
272                 NotifyPropertyChanged();
273             }
274         }
275
276         private StopBehaviorType InternalStopBehavior
277         {
278             set
279             {
280                 stopBehavior = (StopBehaviorType)value;
281                 dirtyFlag = true;
282             }
283             get
284             {
285                 return stopBehavior;
286             }
287         }
288
289         /// <summary>
290         /// Get the number of total frames
291         /// </summary>
292         [EditorBrowsable(EditorBrowsableState.Never)]
293         public int TotalFrame
294         {
295             get
296             {
297                 int ret = -1;
298                 PropertyMap map = base.Image;
299                 if (map != null)
300                 {
301                     PropertyValue val = map.Find(ImageVisualProperty.TotalFrameNumber);
302                     if (val != null)
303                     {
304                         if (val.Get(out ret))
305                         {
306                             return ret;
307                         }
308                     }
309                 }
310                 return ret;
311             }
312         }
313
314         /// <summary>
315         /// Set or get the current frame. When setting a specific frame, it is displayed as a still image.
316         /// </summary>
317         /// <remarks>
318         /// 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.
319         /// </remarks>
320         [EditorBrowsable(EditorBrowsableState.Never)]
321         public int CurrentFrame
322         {
323             get
324             {
325                 return (int)GetValue(CurrentFrameProperty);
326             }
327             set
328             {
329                 SetValue(CurrentFrameProperty, value);
330                 NotifyPropertyChanged();
331             }
332         }
333
334         private int InternalCurrentFrame
335         {
336             set
337             {
338                 DoAction(ImageView.Property.IMAGE, ActionJumpTo, new PropertyValue(value));
339             }
340             get
341             {
342                 int ret = -1;
343                 PropertyMap map = base.Image;
344                 if (map != null)
345                 {
346                     PropertyValue val = map.Find(ImageVisualProperty.CurrentFrameNumber);
347                     if (val != null)
348                     {
349                         if (val.Get(out ret))
350                         {
351                             return ret;
352                         }
353                     }
354                 }
355                 return ret;
356             }
357         }
358
359         /// <summary>
360         /// Actions property value to Jump to the specified frame.
361         /// This property can be redefined by child class if it use different value.
362         /// </summary>
363         [EditorBrowsable(EditorBrowsableState.Never)]
364         protected int ActionJumpTo { get; set; } = Interop.AnimatedImageView.AnimatedImageVisualActionJumpToGet();
365         #endregion Property
366
367         #region Method
368         /// <summary>
369         /// To make the properies be set. This should be called after the properties are set.
370         /// </summary>
371         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
372         [EditorBrowsable(EditorBrowsableState.Never)]
373         public void SetValues()
374         {
375             if (dirtyFlag == false)
376             {
377                 return;
378             }
379             dirtyFlag = false;
380
381             PropertyMap tMap = new PropertyMap();
382             PropertyValue animatiedImage = new PropertyValue((int)Visual.Type.AnimatedImage);
383             tMap.Insert(Visual.Property.Type, animatiedImage);
384             if (resourceURLs?.Count != 0)
385             {
386                 PropertyArray indexPropertyArray = new PropertyArray();
387                 PropertyArray returnedArr = new PropertyArray();
388                 PropertyValue index = new PropertyValue();
389                 foreach (var iter in resourceURLs)
390                 {
391                     index = new PropertyValue(iter);
392                     returnedArr = indexPropertyArray.Add(index);
393                 }
394                 index.Dispose();
395                 returnedArr.Dispose();
396                 PropertyValue arrayProperty = new PropertyValue(indexPropertyArray);
397                 tMap.Insert(ImageVisualProperty.URL, arrayProperty);
398                 PropertyValue frameDelayProperty = new PropertyValue(frameDelay);
399                 tMap.Insert(ImageVisualProperty.FrameDelay, frameDelayProperty);
400
401                 arrayProperty.Dispose();
402                 indexPropertyArray.Dispose();
403                 frameDelayProperty.Dispose();
404             }
405             else
406             {
407                 PropertyValue urlProperty = new PropertyValue(url);
408                 tMap.Insert(ImageVisualProperty.URL, urlProperty);
409                 urlProperty.Dispose();
410             }
411
412             PropertyValue batchSizeProperty = new PropertyValue(batchSize);
413             tMap.Insert(ImageVisualProperty.BatchSize, batchSizeProperty);
414             PropertyValue cacheSizeProperty = new PropertyValue(cacheSize);
415             tMap.Insert(ImageVisualProperty.CacheSize, cacheSizeProperty);
416             PropertyValue loopCountProperty = new PropertyValue(loopCount);
417             tMap.Insert(ImageVisualProperty.LoopCount, loopCountProperty);
418             PropertyValue stopBehaviorProperty = new PropertyValue((int)stopBehavior);
419             tMap.Insert(ImageVisualProperty.StopBehavior, stopBehaviorProperty);
420
421             loopCountProperty.Dispose();
422             cacheSizeProperty.Dispose();
423             batchSizeProperty.Dispose();
424             stopBehaviorProperty.Dispose();
425
426             propertyMap = tMap;
427             PropertyValue mapProperty = new PropertyValue(propertyMap);
428             SetProperty(ImageView.Property.IMAGE, mapProperty);
429             mapProperty.Dispose();
430
431             tMap.Dispose();
432             animatiedImage.Dispose();
433         }
434
435         /// <summary>
436         /// Play animation
437         /// </summary>
438         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
439         [EditorBrowsable(EditorBrowsableState.Never)]
440         public new void Play()
441         {
442             SetValues();
443             base.Play();
444         }
445
446         /// <summary>
447         /// Pause animation. Currently pause and stop are same
448         /// </summary>
449         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
450         [EditorBrowsable(EditorBrowsableState.Never)]
451         public new void Pause()
452         {
453             SetValues();
454             base.Pause();
455         }
456
457         /// <summary>
458         /// Stop animation. Currently pause and stop are same
459         /// </summary>
460         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
461         [EditorBrowsable(EditorBrowsableState.Never)]
462         public new void Stop()
463         {
464             SetValues();
465             base.Stop();
466         }
467         #endregion Method
468
469
470         #region Event, Enum, Struct, ETC
471
472         /// <summary>
473         /// Enumeration for what to do when the animation is stopped.
474         /// </summary>
475         [EditorBrowsable(EditorBrowsableState.Never)]
476         public enum StopBehaviorType
477         {
478             /// <summary>
479             /// When the animation is stopped, the current frame is shown.
480             /// </summary>
481             [EditorBrowsable(EditorBrowsableState.Never)]
482             CurrentFrame,
483             /// <summary>
484             /// When the animation is stopped, the min frame (first frame) is shown.
485             /// </summary>
486             [EditorBrowsable(EditorBrowsableState.Never)]
487             MinimumFrame,
488             /// <summary>
489             /// When the animation is stopped, the max frame (last frame) is shown.
490             /// </summary>
491             [EditorBrowsable(EditorBrowsableState.Never)]
492             MaximumFrame
493         }
494
495         #endregion Event, Enum, Struct, ETC
496     }
497 }