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