[NUI] Modify NUI Seamless animation bug (#2334)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / FrameBroker / DefaultFrameBroker.cs
1 /*
2  * Copyright (c) 2020 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 using System.Collections.Generic;
19 using System.ComponentModel;
20 using System.Text;
21 using Tizen.NUI.BaseComponents;
22
23 namespace Tizen.NUI
24 {
25     internal class DefaultFrameBroker : FrameBrokerBase
26     {
27         private Window window;
28         private ImageView providerImage;
29         private bool isAnimating;
30
31         public delegate void AnimationEventHandler(bool direction);
32         internal event AnimationEventHandler AnimationInitialized;
33         internal event AnimationEventHandler AnimationFinished;
34
35         internal View mainView;
36         private bool direction;
37
38         internal Animation animation;
39
40         internal DefaultFrameBroker(Window window) : base(window)
41         {
42             this.window = window;
43             isAnimating = false;
44         }
45
46         protected override void OnFrameResumed(FrameData frame)
47         {
48             base.OnFrameResumed(frame);
49
50             direction = frame.DirectionForward;
51
52             if (isAnimating)
53             {
54                 return;
55             }
56             isAnimating = true;
57
58             AnimationInitialized?.Invoke(frame.DirectionForward);
59
60             if (frame.DirectionForward)
61             {
62                 PlayAnimateTo(frame, ForwardAnimation);
63             }
64             else
65             {
66                 PlayAnimateTo(frame, BackwardAnimation);
67             }
68
69             StartAnimation();
70         }
71
72         protected override void OnFramePaused()
73         {
74             base.OnFramePaused();
75             animation?.Stop();
76
77             ResetImage();
78
79             isAnimating = false;
80         }
81
82         private void PlayAnimateTo(FrameData frame, TransitionAnimation transition)
83         {
84             if (transition != null)
85             {
86                 //ResetImage();
87                 if (!providerImage)
88                 {
89                     providerImage = new ImageView(transition.DefaultImageStyle);
90                     providerImage.ParentOrigin = transition.DefaultImageStyle.ParentOrigin;
91                     providerImage.PivotPoint = transition.DefaultImageStyle.PivotPoint;
92                     providerImage.PositionUsesPivotPoint = true;
93                     providerImage.AddRenderer(GetRenderer(frame));
94                     if (mainView)
95                     {
96                         mainView.Add(providerImage);
97                         providerImage.LowerToBottom();
98                     }
99                     else
100                     {
101                         window.Add(providerImage);
102                     }
103                 }
104                 else
105                 {
106                     providerImage.ApplyStyle(transition.DefaultImageStyle.Clone());
107                 }
108
109                 providerImage.Show();
110                 int propertyCount = transition.AnimationDataList.Count;
111                 animation = new Animation(transition.DurationMilliSeconds+80);
112                 animation.Properties = new string[propertyCount];
113                 animation.DestValue = new string[propertyCount];
114                 animation.StartTime = new int[propertyCount];
115                 animation.EndTime = new int[propertyCount];
116
117                 for (int i = 0; i < propertyCount; i++)
118                 {
119                     animation.Properties[i] = transition.AnimationDataList[i].Property;
120                     animation.DestValue[i] = transition.AnimationDataList[i].DestinationValue;
121                     animation.StartTime[i] = 80+transition.AnimationDataList[i].StartTime;
122                     animation.EndTime[i] = 80+transition.AnimationDataList[i].EndTime;
123                 }
124                 animation.PlayAnimateTo(providerImage);
125                 animation.Finished += Ani_Finished;
126             }
127             else
128             {
129                 FinishAnimation();
130             }
131         }
132
133
134         private TransitionAnimation forwardAnimation;
135         internal TransitionAnimation ForwardAnimation
136         {
137             get
138             {
139                 return forwardAnimation;
140             }
141             set
142             {
143                 forwardAnimation = value;
144             }
145         }
146
147         private TransitionAnimation backwardAnimation;
148         internal TransitionAnimation BackwardAnimation
149         {
150             get
151             {
152                 return backwardAnimation;
153             }
154             set
155             {
156                 backwardAnimation = value;
157             }
158         }
159
160         private void Ani_Finished(object sender, EventArgs e)
161         {
162             FinishAnimation();
163
164             AnimationFinished?.Invoke(direction);
165         }
166
167         private void ResetImage()
168         {
169             if (providerImage != null)
170             {
171                 providerImage.Hide();
172                 //providerImage.Unparent();
173                 //providerImage.Dispose();
174                 //providerImage = null;
175             }
176         }
177     }
178 }