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