f77158574015b87e6701ba2a064574a3ffe9511b
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / transition / transition-impl.cpp
1 /*
2  * Copyright (c) 2021 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 // CLASS HEADER
19 #include <dali-toolkit/internal/transition/transition-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali-toolkit/public-api/controls/control-impl.h>
23 #include <dali/devel-api/actors/actor-devel.h>
24 #include <dali/integration-api/debug.h>
25 #include <dali/public-api/common/dali-common.h>
26 #include <dali/public-api/object/type-registry.h>
27
28 namespace Dali
29 {
30 namespace Toolkit
31 {
32 namespace Internal
33 {
34 namespace
35 {
36 const Dali::AlphaFunction DEFAULT_ALPHA_FUNCTION(Dali::AlphaFunction::DEFAULT);
37
38 } // anonymous namespace
39
40 TransitionPtr Transition::New(Dali::Toolkit::Control source, Dali::Toolkit::Control destination, TimePeriod timePeriod)
41 {
42   float durationSeconds = timePeriod.durationSeconds;
43   if(durationSeconds < 0.0f)
44   {
45     DALI_LOG_WARNING("duration should be greater than 0.0f.\n");
46     durationSeconds = 0.0f;
47   }
48
49   TransitionPtr transition = new Transition(source, destination, timePeriod);
50
51   // Second-phase construction
52   transition->Initialize();
53
54   return transition;
55 }
56
57 Transition::Transition(Dali::Toolkit::Control source, Dali::Toolkit::Control destination, TimePeriod timePeriod)
58 : TransitionBase(),
59   mSourceView(source),
60   mDestinationView(destination),
61   mShowSourceAfterFinished(true)
62 {
63   SetTargetView(destination);
64   SetDuration(timePeriod.durationSeconds);
65   SetDelay(timePeriod.delaySeconds);
66 }
67
68 Transition::~Transition()
69 {
70 }
71
72 void Transition::OnPlay()
73 {
74   if(!mSourceView[Dali::Actor::Property::CONNECTED_TO_SCENE] ||
75      !mDestinationView[Dali::Actor::Property::CONNECTED_TO_SCENE])
76   {
77     DALI_LOG_ERROR("The source or destination is not added on the window\n");
78     return;
79   }
80
81   //Make startPropertyMap and finishPropertyMap to use for property animation.
82   Matrix     sourceWorldTransform = mSourceView[Dali::Actor::Property::WORLD_MATRIX];
83   Vector3    sourcePosition, sourceScale;
84   Quaternion sourceOrientation;
85   sourceWorldTransform.GetTransformComponents(sourcePosition, sourceOrientation, sourceScale);
86
87   Matrix     destinationWorldTransform = GetWorldTransform(mDestinationView);
88   Vector3    destinationPosition, destinationScale;
89   Quaternion destinationOrientation;
90   destinationWorldTransform.GetTransformComponents(destinationPosition, destinationOrientation, destinationScale);
91
92   Vector3       targetSize  = mDestinationView[Dali::Actor::Property::SIZE];
93   Vector4       targetColor = GetWorldColor(mDestinationView);
94   Property::Map startPropertyMap;
95   Property::Map finishPropertyMap;
96
97   // Use world transform if this transition requires animation of transform.
98   mDestinationView[Dali::Actor::Property::ANCHOR_POINT]               = AnchorPoint::CENTER;
99   mDestinationView[Dali::Actor::Property::PARENT_ORIGIN]              = ParentOrigin::CENTER;
100   mDestinationView[Dali::Actor::Property::POSITION_USES_ANCHOR_POINT] = true;
101   mDestinationView[Dali::Actor::Property::INHERIT_POSITION]           = false;
102   mDestinationView[Dali::Actor::Property::INHERIT_ORIENTATION]        = false;
103   mDestinationView[Dali::Actor::Property::INHERIT_SCALE]              = false;
104   mDestinationView[Dali::Actor::Property::COLOR_MODE]                 = Dali::ColorMode::USE_OWN_COLOR;
105
106   // Set animation of Transform
107   startPropertyMap.Insert(Dali::Actor::Property::POSITION, sourcePosition);
108   finishPropertyMap.Insert(Dali::Actor::Property::POSITION, destinationPosition);
109
110   startPropertyMap.Insert(Dali::Actor::Property::ORIENTATION, sourceOrientation);
111   finishPropertyMap.Insert(Dali::Actor::Property::ORIENTATION, destinationOrientation);
112
113   startPropertyMap.Insert(Dali::Actor::Property::SCALE, sourceScale);
114   finishPropertyMap.Insert(Dali::Actor::Property::SCALE, destinationScale);
115
116   Vector4 sourceColor = mSourceView.GetCurrentProperty<Vector4>(Dali::Actor::Property::WORLD_COLOR);
117   startPropertyMap.Insert(Dali::Actor::Property::COLOR, sourceColor);
118   finishPropertyMap.Insert(Dali::Actor::Property::COLOR, targetColor);
119
120   // Set animation for other properties if source and destination is different.
121   Vector3 sourceSize = mSourceView.GetCurrentProperty<Vector3>(Dali::Actor::Property::SIZE);
122   if(sourceSize != targetSize)
123   {
124     startPropertyMap.Insert(Dali::Actor::Property::SIZE, sourceSize);
125     finishPropertyMap.Insert(Dali::Actor::Property::SIZE, targetSize);
126   }
127
128   SetStartPropertyMap(startPropertyMap);
129   SetFinishPropertyMap(finishPropertyMap);
130
131   // source View becomes transparent during transition.
132   GetImplementation(mSourceView).SetTransparent(true);
133
134   Dali::Animation animation = GetAnimation();
135   if(!animation)
136   {
137     DALI_LOG_ERROR("animation is still not initialized\n");
138     return;
139   }
140   GetImplementation(mDestinationView).CreateTransitions(animation, mSourceView, GetAlphaFunction(), TimePeriod(GetDelay(), GetDuration()));
141 }
142
143 void Transition::OnFinished()
144 {
145   if(mShowSourceAfterFinished)
146   {
147     GetImplementation(mSourceView).SetTransparent(false);
148   }
149 }
150
151 void Transition::ShowSourceAfterFinished(bool showSourceAfterFinished)
152 {
153   mShowSourceAfterFinished = showSourceAfterFinished;
154 }
155
156 } // namespace Internal
157
158 } // namespace Toolkit
159
160 } // namespace Dali