[NUI] TCSACR-226 code change (#1032)
[platform/core/csapi/tizenfx.git] / src / ElmSharp / ElmSharp / AnimationView.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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 using System;
18
19 namespace ElmSharp
20 {
21     /// <summary>
22     /// Enumeration for the AnimationView state
23     /// </summary>
24     /// <since_tizen> preview </since_tizen>
25     public enum AnimationViewState
26     {
27         /// <summary>
28         /// Animation is not ready to play.
29         /// </summary>
30         NotReady,
31
32         /// <summary>
33         /// Animation is on playing.
34         /// </summary>
35         Play,
36
37         /// <summary>
38         /// Animation is on reverse playing.
39         /// </summary>
40         ReversedPlay,
41
42         /// <summary>
43         /// Animation has been paused.
44         /// </summary>
45         Pause,
46
47         /// <summary>
48         /// AnimationView successfully loaded a file then readied for playing. Otherwise after finished animation or stopped forcely by request.
49         /// </summary>
50         Stop
51     }
52
53     /// <summary>
54     /// The AnimationView is designed to show and play animation of vector graphics based content.
55     /// Currently ElmSharp AnimationView is supporting only json format (known for Lottie file as well).
56     /// </summary>
57     /// <since_tizen> preview </since_tizen>
58     public class AnimationView : EvasObject
59     {
60         private SmartEvent _started;
61         private SmartEvent _repeated;
62         private SmartEvent _finished;
63         private SmartEvent _paused;
64         private SmartEvent _resumed;
65         private SmartEvent _stopped;
66         private SmartEvent _updated;
67
68         /// <summary>
69         /// Creates and initializes a new instance of the AnimationView class.
70         /// </summary>
71         /// <param name="parent">The parent is a given container, which will be attached by AnimationView as a child. It's <see cref="EvasObject"/> type.</param>
72         /// <since_tizen> preview </since_tizen>
73         public AnimationView(EvasObject parent) : base(parent)
74         {
75             _started = new SmartEvent(this, this.Handle, "play,start");
76             _repeated = new SmartEvent(this, this.Handle, "play,repeat");
77             _finished = new SmartEvent(this, this.Handle, "play,done");
78             _paused = new SmartEvent(this, this.Handle, "play,pause");
79             _resumed = new SmartEvent(this, this.Handle, "play,resume");
80             _stopped = new SmartEvent(this, this.Handle, "play,stop");
81             _updated = new SmartEvent(this, this.Handle, "play,update");
82
83             _started.On += (sender, e) =>
84             {
85                 Started?.Invoke(this, EventArgs.Empty);
86             };
87
88             _repeated.On += (sender, e) =>
89             {
90                 Repeated?.Invoke(this, EventArgs.Empty);
91             };
92
93             _finished.On += (sender, e) =>
94             {
95                 Finished?.Invoke(this, EventArgs.Empty);
96             };
97
98             _paused.On += (sender, e) =>
99             {
100                 Paused?.Invoke(this, EventArgs.Empty);
101             };
102
103             _resumed.On += (sender, e) =>
104             {
105                 Resumed?.Invoke(this, EventArgs.Empty);
106             };
107
108             _stopped.On += (sender, e) =>
109             {
110                 Stopped?.Invoke(this, EventArgs.Empty);
111             };
112
113             _updated.On += (sender, e) =>
114             {
115                 Updated?.Invoke(this, EventArgs.Empty);
116             };
117         }
118
119         /// <summary>
120         /// It occurs when the animation is just started.
121         /// </summary>
122         /// <since_tizen> preview </since_tizen>
123         public event EventHandler Started;
124
125         /// <summary>
126         /// It occurs when the animation is just repeated.
127         /// </summary>
128         /// <since_tizen> preview </since_tizen>
129         public event EventHandler Repeated;
130
131         /// <summary>
132         /// It occurs when the animation is just finished.
133         /// </summary>
134         /// <since_tizen> preview </since_tizen>
135         public event EventHandler Finished;
136
137         /// <summary>
138         /// It occurs when the animation is just paused.
139         /// </summary>
140         /// <since_tizen> preview </since_tizen>
141         public event EventHandler Paused;
142
143         /// <summary>
144         /// It occurs when the animation is just resumed.
145         /// </summary>
146         /// <since_tizen> preview </since_tizen>
147         public event EventHandler Resumed;
148
149         /// <summary>
150         /// It occurs when the animation is just stopped.
151         /// </summary>
152         /// <since_tizen> preview </since_tizen>
153         public event EventHandler Stopped;
154
155         /// <summary>
156         /// It occurs when the animation is updated to the next frame.
157         /// </summary>
158         /// <since_tizen> preview </since_tizen>
159         public event EventHandler Updated;
160
161         /// <summary>
162         /// Sets or gets whether to play animation automatically.
163         /// <remarks>
164         /// If AutoPlay is true, animation will be started when it's readied.
165         /// The condition of AutoPlay is when AnimationView opened file successfully, yet to play it plus when the object is visible.
166         /// If AnimationView is disabled, invisible, it turns to pause state then resume animation when it's visible again.
167         /// This AutoPlay will be only affected to the next animation source. So must be called before SetAnimation()
168         /// </remarks>
169         /// </summary>
170         /// <since_tizen> preview </since_tizen>
171         public bool AutoPlay
172         {
173             get
174             {
175                 return Interop.Elementary.elm_animation_view_auto_play_get(Handle);
176             }
177             set
178             {
179                 Interop.Elementary.elm_animation_view_auto_play_set(Handle, value);
180             }
181         }
182
183         /// <summary>
184         /// Sets or gets whether to turn on/off animation looping.
185         /// <remarks>
186         /// If AutoRepeat is true, it repeats animation when animation frame is reached to end.
187         /// This AutoRepeat mode is valid to both Play and ReversePlay cases.
188         /// </remarks>
189         /// </summary>
190         /// <since_tizen> preview </since_tizen>
191         public bool AutoRepeat
192         {
193             get
194             {
195                 return Interop.Elementary.elm_animation_view_auto_repeat_get(Handle);
196             }
197             set
198             {
199                 Interop.Elementary.elm_animation_view_auto_repeat_set(Handle, value);
200             }
201         }
202
203         /// <summary>
204         /// Sets or gets the animation speed.
205         /// <remarks>
206         /// Control animation speed by multiplying Speed value.
207         /// If you want to play animation double-time faster, you can give Speed 2.
208         /// If you want to play animation double-time slower, you can give Speed 0.5.
209         /// Speed must be greater than zero.
210         /// </remarks>
211         /// </summary>
212         /// <since_tizen> preview </since_tizen>
213         public double Speed
214         {
215             get
216             {
217                 return Interop.Elementary.elm_animation_view_speed_get(Handle);
218             }
219             set
220             {
221                 Interop.Elementary.elm_animation_view_speed_set(Handle, value);
222             }
223         }
224
225         /// <summary>
226         /// Get the duration of animation in seconds.
227         /// </summary>
228         /// <remarks>
229         /// Returns total duration time of current animation in the seconds.
230         /// If current animation source isn't animatable, it returns zero.
231         /// </remarks>
232         /// <since_tizen> preview </since_tizen>
233         public double DurationTime
234         {
235             get
236             {
237                 return Interop.Elementary.elm_animation_view_duration_time_get(Handle);
238             }
239         }
240
241         /// <summary>
242         /// Sets or gets current progress position of animation view.
243         /// <remarks>
244         /// When you required to jump on a certain progress instantly,
245         /// you can change current position by using this property
246         /// The range of progress is 0 ~ 1.
247         /// </remarks>
248         /// </summary>
249         /// <since_tizen> preview </since_tizen>
250         public double Progress
251         {
252             get
253             {
254                 return Interop.Elementary.elm_animation_view_progress_get(Handle);
255             }
256             set
257             {
258                 Interop.Elementary.elm_animation_view_progress_set(Handle, value);
259             }
260         }
261
262         /// <summary>
263         /// Sets or gets current frame position of animation view.
264         /// <remarks>
265         /// The range of frame is from 0 to FrameCount - 1
266         /// </remarks>
267         /// </summary>
268         /// <since_tizen> preview </since_tizen>
269         public int Frame
270         {
271             get
272             {
273                 return Interop.Elementary.elm_animation_view_frame_get(Handle);
274             }
275             set
276             {
277                 Interop.Elementary.elm_animation_view_frame_set(Handle, value);
278             }
279         }
280
281         /// <summary>
282         /// Get the default view size that specified from vector resource.
283         /// </summary>
284         /// <since_tizen> preview </since_tizen>
285         public Size DefaultSize
286         {
287             get
288             {
289                 Interop.Elementary.elm_animation_view_default_size_get(Handle, out int w, out int h);
290                 return new Size(w, h);
291             }
292         }
293
294         /// <summary>
295         /// Get current animation view state.
296         /// </summary>
297         /// <since_tizen> preview </since_tizen>
298         public AnimationViewState State
299         {
300             get
301             {
302                 return (AnimationViewState)Interop.Elementary.elm_animation_view_state_get(Handle);
303             }
304         }
305
306         /// <summary>
307         /// Get the status whether current animation is on playing forward or backward.
308         /// </summary>
309         /// <remarks>
310         /// If AnimationView is not on playing, it will return False.
311         /// </remarks>
312         /// <since_tizen> preview </since_tizen>
313         public bool IsReversedPlaying
314         {
315             get
316             {
317                 return Interop.Elementary.elm_animation_view_is_playing_back(Handle);
318             }
319         }
320
321         /// <summary>
322         /// Get the index of end frame of the AnimationView, if it's animated.
323         /// </summary>
324         /// <remarks>
325         /// Frame number starts with 0.
326         /// </remarks>
327         /// <since_tizen> preview </since_tizen>
328         public int FrameCount
329         {
330             get
331             {
332                 return Interop.Elementary.elm_animation_view_frame_count_get(Handle);
333             }
334         }
335
336         /// <summary>
337         /// Sets or Gets the start progress of the play
338         /// </summary>
339         /// <remarks>
340         /// Default value is 0.
341         /// </remarks>
342         /// <since_tizen> preview </since_tizen>
343         public double MinProgress
344         {
345             get
346             {
347                 return Interop.Elementary.elm_animation_view_min_progress_get(Handle);
348             }
349             set
350             {
351                 Interop.Elementary.elm_animation_view_min_progress_set(Handle, value);
352             }
353         }
354
355         /// <summary>
356         /// Sets or Gets the last progress of the play
357         /// </summary>
358         /// <remarks>
359         /// Default value is 1.
360         /// </remarks>
361         /// <since_tizen> preview </since_tizen>
362         public double MaxProgress
363         {
364             get
365             {
366                 return Interop.Elementary.elm_animation_view_max_progress_get(Handle);
367             }
368             set
369             {
370                 Interop.Elementary.elm_animation_view_max_progress_set(Handle, value);
371             }
372         }
373
374         /// <summary>
375         /// Sets or Gets the start frame of the play
376         /// </summary>
377         /// <remarks>
378         /// Default value is 0.
379         /// </remarks>
380         /// <since_tizen> preview </since_tizen>
381         public int MinFrame
382         {
383             get
384             {
385                 return Interop.Elementary.elm_animation_view_min_frame_get(Handle);
386             }
387             set
388             {
389                 Interop.Elementary.elm_animation_view_min_frame_set(Handle, value);
390             }
391         }
392
393         /// <summary>
394         /// Sets or Gets the last frame of the play
395         /// </summary>
396         /// <remarks>
397         /// Default value is FrameCount -1.
398         /// </remarks>
399         /// <since_tizen> preview </since_tizen>
400         public int MaxFrame
401         {
402             get
403             {
404                 return Interop.Elementary.elm_animation_view_max_frame_get(Handle);
405             }
406             set
407             {
408                 Interop.Elementary.elm_animation_view_max_frame_set(Handle, value);
409             }
410         }
411
412         /// <summary>
413         /// Sets the animation source file.
414         /// </summary>
415         /// <param name="file">The animation file path.</param>
416         /// <since_tizen> preview </since_tizen>
417         public void SetAnimation(string file)
418         {
419             Interop.Elementary.elm_animation_view_file_set(Handle, file, null);
420         }
421
422         /// <summary>
423         /// Play animation one time instantly when it's available.
424         /// <remarks>
425         /// If current keyframe is on a certain position by playing reverse, this will play forward from there.
426         /// Play request will be ignored if animation source is not set yet or animation is paused state or it's already on playing.
427         /// </remarks>
428         /// </summary>
429         /// <since_tizen> preview </since_tizen>
430         public void Play()
431         {
432             Interop.Elementary.elm_animation_view_play(Handle);
433         }
434
435         /// <summary>
436         /// Play animation one time instantly when it's available.
437         /// <remarks>
438         /// If current keyframe is on a certain position by playing reverse and isReverse is ture, this will play forward from there.
439         /// And if current keyframe is on a certain position by playing and isReverse is false, this will play backward from there.
440         /// Play request will be ignored if animation source is not set yet or animation is paused state or it's already on playing.
441         /// </remarks>
442         /// </summary>
443         /// <param name="isReverse">Whether the animation play or reverse play.</param>
444         /// <since_tizen> preview </since_tizen>
445         public void Play(bool isReverse)
446         {
447             if (!isReverse)
448             {
449                 Interop.Elementary.elm_animation_view_play(Handle);
450             }
451             else
452             {
453                 Interop.Elementary.elm_animation_view_play_back(Handle);
454             }
455         }
456
457         /// <summary>
458         /// Pause current animation instantly.
459         /// <remarks>
460         /// Once animation is paused, animation view must get resume to play continue again.
461         /// Animation must be on playing or playing back status.
462         /// </remarks>
463         /// </summary>
464         /// <since_tizen> preview </since_tizen>
465         public void Pause()
466         {
467             Interop.Elementary.elm_animation_view_pause(Handle);
468         }
469
470         /// <summary>
471         /// Resume paused animation to continue animation.
472         /// </summary>
473         /// <remarks>
474         /// This resume must be called on animation paused status.
475         /// </remarks>
476         /// <since_tizen> preview </since_tizen>
477         public void Resume()
478         {
479             Interop.Elementary.elm_animation_view_resume(Handle);
480         }
481
482         /// <summary>
483         /// Stop playing animation.
484         /// <remarks>
485         /// Stop animation instatly regardless of it's status and reset to
486         /// show first frame of animation.Even though current animation is paused,
487         /// the animation status will be stopped.
488         /// </remarks>
489         /// </summary>
490         /// <since_tizen> preview </since_tizen>
491         public void Stop()
492         {
493             Interop.Elementary.elm_animation_view_stop(Handle);
494         }
495
496         /// <summary>
497         /// Creates a AnimationView handle.
498         /// </summary>
499         /// <param name="parent">Parent EvasObject.</param>
500         /// <returns>Handle IntPtr.</returns>
501         /// <since_tizen> preview </since_tizen>
502         protected override IntPtr CreateHandle(EvasObject parent)
503         {
504             return Interop.Elementary.elm_animation_view_add(parent.Handle);
505         }
506     }
507 }