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