[NUI] Reduce code duplication - refactor dispose codes (#1010)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / BaseComponents / VectorAnimationView.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 global::System;
19 using System.ComponentModel;
20 #if (NUI_DEBUG_ON)
21 using tlog = Tizen.Log;
22 #endif
23
24 namespace Tizen.NUI.BaseComponents
25 {
26     /// <summary>
27     /// VectorAnimationView is a class for displaying a vector resource.
28     /// </summary>
29     // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
30     [EditorBrowsable(EditorBrowsableState.Never)]
31     public class VectorAnimationViewTBD : AnimatedVectorImageView
32     {
33         #region Constructor, Distructor, Dispose
34         /// <summary>
35         /// Construct VectorAnimationView.
36         /// </summary>
37         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
38         [EditorBrowsable(EditorBrowsableState.Never)]
39         public VectorAnimationViewTBD() : base()
40         {
41             tlog.Fatal(tag, $"<<< VectorAnimationView() constuctor objId={GetId()} >>>");
42         }
43
44         /// <summary>
45         /// Construct VectorAnimationView.
46         /// </summary>
47         /// <param name="scale">Set scaling factor for Vector Animation, while creating.</param>
48         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
49         [EditorBrowsable(EditorBrowsableState.Never)]
50         public VectorAnimationViewTBD(float scale) : base(scale)
51         {
52             tlog.Fatal(tag, $"<<< VectorAnimationView(scale={scale}) constuctor objId={GetId()}>>>");
53         }
54
55         /// <summary>
56         /// You can override it to clean-up your own resources
57         /// </summary>
58         /// <param name="type">DisposeTypes</param>
59         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
60         [EditorBrowsable(EditorBrowsableState.Never)]
61         protected override void Dispose(DisposeTypes type)
62         {
63             if (disposed)
64             {
65                 return;
66             }
67             tlog.Fatal(tag, $"<<< [{GetId()}] VectorAnimationView.Dispose(type={type})");
68
69             //Release your own unmanaged resources here.
70             //You should not access any managed member here except static instance.
71             //because the execution order of Finalizes is non-deterministic.
72
73             base.Dispose(type);
74
75             tlog.Fatal(tag, $"[{GetId()}] VectorAnimationView.Dispose() >>>");
76         }
77         #endregion Constructor, Distructor, Dispose
78
79
80         #region Property
81         /// <summary>
82         /// Set Resource URL
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 string ResourceURL
87         {
88             set
89             {
90                 tlog.Fatal(tag, $"<<< [{GetId()}] VectorAnimationView.ResourceURL SET");
91
92                 if (value == mResourceURL)
93                 {
94                     tlog.Fatal(tag, $"set same URL! ");
95                     return;
96                 }
97                 mResourceURL = (value == null) ? "" : value;
98                 URL = mResourceURL;
99                 mIsMinMaxSet = false;
100
101                 tlog.Fatal(tag, $" [{GetId()}] VectorAnimationView.ResourceURL SET mResourceURL={mResourceURL}) >>>");
102             }
103             get => mResourceURL;
104         }
105
106         /// <summary>
107         /// RepeatCount of animation.
108         /// The repeat count is 0 by default.
109         /// If the RepeatCount is 0, the animation is never repeated.
110         /// If the RepeatCount is greater than 0, the repeat mode will be taken into account.
111         /// If RepeatCount is -1, animation is infinite loops.
112         /// </summary>
113         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
114         [EditorBrowsable(EditorBrowsableState.Never)]
115         public int RepeatCount
116         {
117             set
118             {
119                 tlog.Fatal(tag, $"<<< [{GetId()}] VectorAnimationView.RepeatCount SET");
120
121                 mRepeatCount = (value < -1) ? -1 : value;
122                 //LoopCount = mRepeatCount;
123                 LoopCount = (mRepeatCount < 0) ? mRepeatCount : mRepeatCount + 1;
124
125                 tlog.Fatal(tag, $"[{GetId()}] VectorAnimationView.RepeatCount SET mRepeatCount={mRepeatCount} >>>");
126             }
127             get => mRepeatCount;
128         }
129
130         /// <summary>
131         /// TotalFrame of animation.
132         /// </summary>
133         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
134         [EditorBrowsable(EditorBrowsableState.Never)]
135         public int TotalFrame
136         {
137             get => TotalFrameNumber;
138         }
139
140         /// <summary>
141         /// CurrentFrame of animation.
142         /// </summary>
143         /// <returns> Returns user set value for the current frame. Cannot provide actual playing current frame. </returns>
144         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
145         [EditorBrowsable(EditorBrowsableState.Never)]
146         public int CurrentFrame
147         {
148             set
149             {
150                 tlog.Fatal(tag, $"<<< [{GetId()}] VectorAnimationView.CurrentFrame SET");
151
152                 if (mResourceURL == null || mResourceURL == String.Empty)
153                 {
154                     throw new InvalidOperationException("Resource Url not yet Set");
155                 }
156
157                 mFrameCount = TotalFrameNumber;
158
159                 if (value < 0)
160                 {
161                     value = 0;
162                 }
163                 else if (value >= mFrameCount)
164                 {
165                     value = mFrameCount - 1;
166                 }
167
168                 mCurrentFrame = value;
169                 AnimationState = AnimationStates.Paused;
170
171                 SetPlayRange(mCurrentFrame, mCurrentFrame);
172                 CurrentFrameNumber = mCurrentFrame;
173
174                 tlog.Fatal(tag, $" [{GetId()}] VectorAnimationView.CurrentFrame SET mCurrentFrame={mCurrentFrame}) >>>");
175             }
176             get => mCurrentFrame;
177         }
178
179         /// <summary>
180         /// RepeatMode of animation.
181         /// </summary>
182         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
183         [EditorBrowsable(EditorBrowsableState.Never)]
184         public RepeatModes RepeatMode
185         {
186             set
187             {
188                 tlog.Fatal(tag, $"<<< [{GetId()}] VectorAnimationView.RepeatMode SET");
189                 mRepeatMode = value;
190
191                 switch (mRepeatMode)
192                 {
193                     case RepeatModes.Restart:
194                         LoopMode = LoopingModeType.Restart;
195                         break;
196                     case RepeatModes.Reverse:
197                         LoopMode = LoopingModeType.AutoReverse;
198                         break;
199                     default:
200                         LoopMode = LoopingModeType.Restart;
201                         break;
202                 }
203
204                 tlog.Fatal(tag, $" [{GetId()}] VectorAnimationView.RepeatMode SET mRepeatMode={mRepeatMode}) >>>");
205             }
206             get => mRepeatMode;
207         }
208
209         /// <summary>
210         /// Get state of animation.
211         /// </summary>
212         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
213         [EditorBrowsable(EditorBrowsableState.Never)]
214         public AnimationStates AnimationState
215         {
216             private set;
217             get;
218         }
219         #endregion Property
220
221
222         #region Method
223         /// <summary>
224         /// Set minimum frame and maximum frame
225         /// </summary>
226         /// <param name="minFrame">minimum frame.</param>
227         /// <param name="maxFrame">maximum frame.</param>
228         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
229         [EditorBrowsable(EditorBrowsableState.Never)]
230         public void SetMinAndMaxFrame(int minFrame, int maxFrame)
231         {
232             tlog.Fatal(tag, $"<<< [{GetId()}] VectorAnimationView.SetMinAndMaxFrame({minFrame}, {maxFrame})");
233
234             mMinFrame = (minFrame) > 0 ? minFrame : 0;
235             mMaxFrame = (maxFrame) > 0 ? maxFrame : 0;
236             mIsMinMaxSet = true;
237             mFrameCount = TotalFrameNumber;
238
239             if (mMinFrame >= mFrameCount)
240             {
241                 mMinFrame = mFrameCount - 1;
242             }
243
244             if (mMaxFrame >= mFrameCount)
245             {
246                 mMaxFrame = mFrameCount - 1;
247             }
248
249             if (mMinFrame > mMaxFrame)
250             {
251                 return;
252             }
253
254             base.Stop();
255             SetPlayRange(mMinFrame, mMaxFrame);
256             CurrentFrameNumber = mMinFrame;
257
258             tlog.Fatal(tag, $" [{GetId()}] VectorAnimationView.SetMinAndMaxFrame(m: {mMinFrame}, {mMaxFrame}) >>>");
259         }
260
261         /// <summary>
262         /// Play Animation.
263         /// </summary>
264         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
265         [EditorBrowsable(EditorBrowsableState.Never)]
266         public new void Play()
267         {
268             tlog.Fatal(tag, $"<<< [{GetId()}] VectorAnimationView.Play()");
269
270             if (mResourceURL == null || mResourceURL == String.Empty)
271             {
272                 throw new InvalidOperationException("Resource Url not yet Set");
273             }
274
275             if (mIsMinMaxSet)
276             {
277                 //if(mRepeatCount > 0 )
278                 //{
279                 //    StopBehavior = StopBehaviorType.FirstFrame;
280                 //}
281                 SetPlayRange(mMinFrame, mMaxFrame);
282             }
283             else
284             {
285                 SetPlayRange(0, TotalFrameNumber - 1);
286             }
287
288             //temporal fix
289             Extents tmp = base.Margin;
290             base.Margin = tmp;
291
292             base.Play();
293             AnimationState = AnimationStates.Playing;
294
295             tlog.Fatal(tag, $" [{GetId()}] VectorAnimationView.Play(mIsMinMaxSet={mIsMinMaxSet}) >>>");
296         }
297
298         /// <summary>
299         /// Pause Animation.
300         /// </summary>
301         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
302         [EditorBrowsable(EditorBrowsableState.Never)]
303         public new void Pause()
304         {
305             tlog.Fatal(tag, $"<<< [{GetId()}] VectorAnimationView.Pause() AnimationState={AnimationState}");
306
307             if (mResourceURL == null || mResourceURL == String.Empty)
308             {
309                 throw new InvalidOperationException("Resource Url not yet Set");
310             }
311
312             base.Pause();
313             AnimationState = AnimationStates.Paused;
314
315             tlog.Fatal(tag, $" [{GetId()}] VectorAnimationView.Pause() >>>");
316         }
317
318         /// <summary>
319         /// Stop Animation.
320         /// </summary>
321         /// <param name="endAction">Defines, what should be behaviour after cancel operation
322         /// End action is Cancel, Animation Stops at the Current Frame.
323         /// End action is Discard, Animation Stops at the Min Frame
324         /// End action is StopFinal, Animation Stops at the Max Frame
325         /// </param>
326         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
327         [EditorBrowsable(EditorBrowsableState.Never)]
328         public void Stop(EndActions endAction = EndActions.Cancel)
329         {
330             tlog.Fatal(tag, $"<<< [{GetId()}] VectorAnimationView.Stop({endAction})");
331
332             if (mResourceURL == null || mResourceURL == String.Empty)
333             {
334                 throw new InvalidOperationException("Resource Url not yet Set");
335             }
336
337             if (AnimationState == AnimationStates.Stopped)
338             {
339                 return;
340             }
341
342             if (mEndAction != endAction)
343             {
344                 mEndAction = endAction;
345                 switch (endAction)
346             {
347                     case EndActions.Cancel:
348                         StopBehavior = StopBehaviorType.CurrentFrame;
349                         break;
350                     case EndActions.Discard:
351                         StopBehavior = StopBehaviorType.FirstFrame;
352                         break;
353                     case EndActions.StopFinal:
354                         StopBehavior = StopBehaviorType.LastFrame;
355                         break;
356                     default:
357                         tlog.Fatal(tag, $" [{GetId()}] no endAction : default set");
358                         break;
359             }
360             }
361             //if (endAction == EndActions.Discard)
362             //{
363             //    CurrentFrame = mMinFrame;
364             //}
365             //else if (endAction == EndActions.StopFinal)
366             //{
367             //    CurrentFrame = mMaxFrame;
368             //}
369             AnimationState = AnimationStates.Stopped;
370
371             base.Stop();
372
373             tlog.Fatal(tag, $" [{GetId()}] VectorAnimationView.Stop(endaction={endAction}) >>>");
374         }
375         #endregion Method
376
377
378         #region Event, Enum, Struct, ETC
379         /// <summary>
380         /// RepeatMode of animation.
381         /// </summary>
382         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
383         [EditorBrowsable(EditorBrowsableState.Never)]
384         public enum RepeatModes
385         {
386             /// <summary>
387             /// When the animation reaches the end and RepeatCount is nonZero, the animation restarts from the beginning. 
388             /// </summary>
389             // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
390             [EditorBrowsable(EditorBrowsableState.Never)]
391             Restart = LoopModes.Forward,
392             /// <summary>
393             /// When the animation reaches the end and RepeatCount nonZero, the animation reverses direction on every animation cycle. 
394             /// </summary>
395             // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
396             [EditorBrowsable(EditorBrowsableState.Never)]
397             Reverse = LoopModes.Backward
398         }
399
400         /// <summary>
401         /// EndActions of animation.
402         /// </summary>
403         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
404         [EditorBrowsable(EditorBrowsableState.Never)]
405         public enum EndActions
406         {
407             /// <summary> End action is Cancel, Animation Stops at the Current Frame.</summary>
408             // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
409             [EditorBrowsable(EditorBrowsableState.Never)]
410             Cancel = 0,
411             /// <summary>  End action is Discard, Animation Stops at the Min Frame</summary>
412             // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
413             [EditorBrowsable(EditorBrowsableState.Never)]
414             Discard = 1,
415             /// <summary> End action is StopFinal, Animation Stops at the Max Frame</summary>
416             // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
417             [EditorBrowsable(EditorBrowsableState.Never)]
418             StopFinal = 2
419         }
420
421         /// <summary>
422         /// AnimationStates of animation.
423         /// </summary>
424         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
425         [EditorBrowsable(EditorBrowsableState.Never)]
426         public enum AnimationStates
427         {
428             /// <summary> The animation has stopped.</summary>
429             // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
430             [EditorBrowsable(EditorBrowsableState.Never)]
431             Stopped = PlayStateType.Stopped,
432             /// <summary> The animation is playing.</summary>
433             // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
434             [EditorBrowsable(EditorBrowsableState.Never)]
435             Playing = PlayStateType.Playing,
436             /// <summary> The animation is paused.</summary>
437             // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
438             [EditorBrowsable(EditorBrowsableState.Never)]
439             Paused = PlayStateType.Paused
440         }
441         #endregion Event, Enum, Struct, ETC
442
443
444         #region Internal
445         #endregion Internal
446
447
448         #region Private
449         private string mResourceURL = null;
450         private int mRepeatCount = 0;
451         private int mFrameCount = 0;
452         private RepeatModes mRepeatMode = RepeatModes.Restart;
453         private int mMinFrame = -1, mMaxFrame = -1;
454         private bool mIsMinMaxSet = false;
455         private int mCurrentFrame = -1;
456         private EndActions mEndAction = EndActions.Cancel;
457
458         private string tag = "NUITEST";
459         #endregion Private
460     }
461 }