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