Merge "Adds transition effect" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / transition / transition-base-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-base-impl.h>
20
21 // INTERNAL INCLUDES
22 #include <dali-toolkit/public-api/controls/control-impl.h>
23
24 // EXTERNAL INCLUDES
25 #include <dali/devel-api/actors/actor-devel.h>
26 #include <dali/integration-api/debug.h>
27 #include <dali/public-api/animation/key-frames.h>
28 #include <dali/public-api/common/dali-common.h>
29 #include <dali/public-api/object/type-registry.h>
30
31 namespace Dali
32 {
33 namespace Toolkit
34 {
35 namespace Internal
36 {
37 namespace
38 {
39 const Dali::AlphaFunction DEFAULT_ALPHA_FUNCTION(Dali::AlphaFunction::DEFAULT);
40
41 Property::Map GetOriginalProperties(Dali::Toolkit::Control control)
42 {
43   Property::Map propertyMap;
44   propertyMap.Insert(Dali::Actor::Property::ANCHOR_POINT, control[Dali::Actor::Property::ANCHOR_POINT]);
45   propertyMap.Insert(Dali::Actor::Property::PARENT_ORIGIN, control[Dali::Actor::Property::PARENT_ORIGIN]);
46   propertyMap.Insert(Dali::Actor::Property::POSITION_USES_ANCHOR_POINT, control[Dali::Actor::Property::POSITION_USES_ANCHOR_POINT]);
47   propertyMap.Insert(Dali::Actor::Property::INHERIT_POSITION, control[Dali::Actor::Property::INHERIT_POSITION]);
48   propertyMap.Insert(Dali::Actor::Property::INHERIT_ORIENTATION, control[Dali::Actor::Property::INHERIT_ORIENTATION]);
49   propertyMap.Insert(Dali::Actor::Property::INHERIT_SCALE, control[Dali::Actor::Property::INHERIT_SCALE]);
50   propertyMap.Insert(Dali::Actor::Property::COLOR_MODE, control[Dali::Actor::Property::COLOR_MODE]);
51   propertyMap.Insert(Dali::Actor::Property::HEIGHT_RESIZE_POLICY, control[Dali::Actor::Property::HEIGHT_RESIZE_POLICY]);
52   propertyMap.Insert(Dali::Actor::Property::WIDTH_RESIZE_POLICY, control[Dali::Actor::Property::WIDTH_RESIZE_POLICY]);
53   propertyMap.Insert(Dali::Actor::Property::POSITION, control[Dali::Actor::Property::POSITION]);
54   propertyMap.Insert(Dali::Actor::Property::ORIENTATION, control[Dali::Actor::Property::ORIENTATION]);
55   propertyMap.Insert(Dali::Actor::Property::SCALE, control[Dali::Actor::Property::SCALE]);
56   propertyMap.Insert(Dali::Actor::Property::COLOR, control[Dali::Actor::Property::COLOR]);
57
58   return propertyMap;
59 }
60
61 /**
62  * @brief Computes and center position by using transform properties.
63  * @param[in] anchorPoint anchorPoint of an actor.
64  * @param[in] positionUsesAnchorPoint positionUsesAnchorPoint of an actor.
65  * @param[in] size size of an actor.
66  * @param[in] scale scale of an actor.
67  * @param[in] orientation orientation of an actor.
68  */
69 Vector3 CalculateCenterPosition(
70   const Vector3&    anchorPoint,
71   const bool        positionUsesAnchorPoint,
72   const Vector3&    size,
73   const Vector3&    scale,
74   const Quaternion& orientation)
75 {
76   Vector3       centerPosition;
77   const Vector3 half(0.5f, 0.5f, 0.5f);
78   const Vector3 topLeft(0.0f, 0.0f, 0.5f);
79   // Calculate the center-point by applying the scale and rotation on the anchor point.
80   centerPosition = (half - anchorPoint) * size * scale;
81   centerPosition *= orientation;
82
83   // If the position is ignoring the anchor-point, then remove the anchor-point shift from the position.
84   if(!positionUsesAnchorPoint)
85   {
86     centerPosition -= (topLeft - anchorPoint) * size;
87   }
88   return centerPosition;
89 }
90
91 } // anonymous namespace
92
93 TransitionBasePtr TransitionBase::New()
94 {
95   TransitionBasePtr transition = new TransitionBase();
96
97   // Second-phase construction
98   transition->Initialize();
99
100   return transition;
101 }
102
103 TransitionBase::TransitionBase()
104 : mAlphaFunction(DEFAULT_ALPHA_FUNCTION),
105   mTimePeriod(TimePeriod(0.0f)),
106   mTransitionWithChild(false),
107   mMoveTargetChildren(false)
108 {
109 }
110
111 void TransitionBase::Initialize()
112 {
113   RegisterObject();
114 }
115
116 void TransitionBase::SetTimePeriod(const Dali::TimePeriod& timePeriod)
117 {
118   if(timePeriod.durationSeconds < 0.0f)
119   {
120     DALI_LOG_WARNING("Duration should be greater than 0.0f.\n");
121   }
122   else
123   {
124     mTimePeriod.durationSeconds = timePeriod.durationSeconds;
125   }
126
127   if(timePeriod.delaySeconds < 0.0f)
128   {
129     DALI_LOG_WARNING("Delay should be greater than 0.0f.\n");
130     return;
131   }
132   else
133   {
134     mTimePeriod.delaySeconds = timePeriod.delaySeconds;
135   }
136 }
137
138 Dali::TimePeriod TransitionBase::GetTimePeriod() const
139 {
140   return mTimePeriod;
141 }
142
143 void TransitionBase::TransitionWithChild(bool transitionWithChild)
144 {
145   mTransitionWithChild = transitionWithChild;
146 }
147
148 void TransitionBase::PreProcess(Dali::Animation animation)
149 {
150   mAnimation           = animation;
151   // Retrieve original property map of mTarget to backup and to reset after transition is finished.
152   mOriginalPropertyMap = GetOriginalProperties(mTarget);
153   mMoveTargetChildren  = false;
154   if(!mTransitionWithChild && mTarget.GetChildCount() > 0)
155   {
156     mMoveTargetChildren = true;
157     CopyTarget();
158   }
159   GetImplementation(mTarget).SetTransparent(false);
160 }
161
162 void TransitionBase::Play()
163 {
164   if(!mTarget[Dali::Actor::Property::CONNECTED_TO_SCENE])
165   {
166     DALI_LOG_ERROR("The target is not added on the window\n");
167     return;
168   }
169
170   OnPlay();
171
172   SetAnimation();
173 }
174
175 void TransitionBase::SetAnimation()
176 {
177   if(!mAnimation)
178   {
179     DALI_LOG_ERROR("animation is not initialized\n");
180     return;
181   }
182
183   for(uint32_t i = 0; i < mStartPropertyMap.Count(); ++i)
184   {
185     Property::Value* initialValuePointer = mInitialPropertyMap.Find(mStartPropertyMap.GetKeyAt(i).indexKey);
186     Property::Value* finishValue = mFinishPropertyMap.Find(mStartPropertyMap.GetKeyAt(i).indexKey);
187     if(finishValue)
188     {
189       Property::Value initialValue = mStartPropertyMap.GetValue(i);
190       if(initialValuePointer)
191       {
192         initialValue = *initialValuePointer;
193       }
194       AnimateBetween(mTarget, mStartPropertyMap.GetKeyAt(i).indexKey, initialValue, mStartPropertyMap.GetValue(i), *finishValue);
195     }
196   }
197 }
198
199 void TransitionBase::AnimateBetween(Dali::Toolkit::Control target, Property::Index index, Property::Value initialValue, Property::Value sourceValue, Property::Value destinationValue)
200 {
201   if(mAnimation)
202   {
203     if(mTimePeriod.delaySeconds>0.0f)
204     {
205       Dali::KeyFrames initialKeyframes = Dali::KeyFrames::New();
206       initialKeyframes.Add(0.0f, initialValue);
207       initialKeyframes.Add(1.0f, initialValue);
208       mAnimation.AnimateBetween(Property(target, index), initialKeyframes, TimePeriod(mTimePeriod.delaySeconds));
209     }
210     Dali::KeyFrames keyframes = Dali::KeyFrames::New();
211     keyframes.Add(0.0f, sourceValue);
212     keyframes.Add(1.0f, destinationValue);
213     mAnimation.AnimateBetween(Property(target, index), keyframes, mAlphaFunction, mTimePeriod);
214   }
215 }
216
217 void TransitionBase::CopyTarget()
218 {
219   mCopiedActor = Dali::Actor::New();
220   mTarget.GetParent().Add(mCopiedActor);
221   mCopiedActor[Dali::DevelActor::Property::SIBLING_ORDER] = static_cast<int32_t>(mTarget[Dali::DevelActor::Property::SIBLING_ORDER]) + 1;
222   for(uint32_t i = 0; i < mTarget.GetChildCount(); ++i)
223   {
224     Dali::Actor child = mTarget.GetChildAt(i);
225     Dali::DevelActor::SwitchParent(child, mCopiedActor);
226   }
227
228   // Copy Size property to mCopiedActor because Size is not included mOriginalPropertyMap.
229   mCopiedActor[Dali::Actor::Property::SIZE] = mTarget.GetProperty<Vector3>(Dali::Actor::Property::SIZE);
230   mCopiedActor.SetProperties(mOriginalPropertyMap);
231 }
232
233 void TransitionBase::TransitionFinished()
234 {
235   OnFinished();
236
237   mTarget.SetProperties(mOriginalPropertyMap);
238   if(mMoveTargetChildren)
239   {
240     for(uint32_t i = 0; i < mCopiedActor.GetChildCount(); ++i)
241     {
242       Dali::Actor child = mCopiedActor.GetChildAt(i);
243       Dali::DevelActor::SwitchParent(child, mTarget);
244     }
245     mCopiedActor.Unparent();
246     mCopiedActor.Reset();
247   }
248   mAnimation.Reset();
249 }
250
251 Matrix TransitionBase::GetWorldTransform(Dali::Actor actor)
252 {
253   enum InheritanceMode
254   {
255     DONT_INHERIT_TRANSFORM = 0,
256     INHERIT_POSITION       = 1,
257     INHERIT_SCALE          = 2,
258     INHERIT_ORIENTATION    = 4,
259     INHERIT_ALL            = INHERIT_POSITION | INHERIT_SCALE | INHERIT_ORIENTATION,
260   };
261
262   std::vector<Dali::Actor>     descentList;
263   std::vector<InheritanceMode> inheritanceModeList;
264   Dali::Actor                  currentActor = actor;
265   int                          inheritance  = 0;
266   do
267   {
268     inheritance = (static_cast<int>(currentActor.GetProperty<bool>(Dali::Actor::Property::INHERIT_ORIENTATION)) << 2) +
269                   (static_cast<int>(currentActor.GetProperty<bool>(Dali::Actor::Property::INHERIT_SCALE)) << 1) +
270                   static_cast<int>(currentActor.GetProperty<bool>(Dali::Actor::Property::INHERIT_POSITION));
271     inheritanceModeList.push_back(static_cast<InheritanceMode>(inheritance));
272     descentList.push_back(currentActor);
273     currentActor = currentActor.GetParent();
274   } while(inheritance != DONT_INHERIT_TRANSFORM && currentActor);
275
276   Matrix  worldMatrix;
277   Vector3 localPosition;
278   for(unsigned int i(descentList.size() - 1); i < descentList.size(); --i)
279   {
280     Vector3    anchorPoint             = descentList[i].GetProperty<Vector3>(Dali::Actor::Property::ANCHOR_POINT);
281     Vector3    parentOrigin            = descentList[i].GetProperty<Vector3>(Dali::Actor::Property::PARENT_ORIGIN);
282     bool       positionUsesAnchorPoint = descentList[i].GetProperty<bool>(Dali::Actor::Property::POSITION_USES_ANCHOR_POINT);
283     Vector3    size                    = descentList[i].GetProperty<Vector3>(Dali::Actor::Property::SIZE);
284     Vector3    actorPosition           = descentList[i].GetProperty<Vector3>(Dali::Actor::Property::POSITION);
285     Quaternion localOrientation        = descentList[i].GetProperty<Quaternion>(Dali::Actor::Property::ORIENTATION);
286     Vector3    localScale              = descentList[i].GetProperty<Vector3>(Dali::Actor::Property::SCALE);
287
288     Vector3 centerPosition = CalculateCenterPosition(anchorPoint, positionUsesAnchorPoint, size, localScale, localOrientation);
289     if(inheritanceModeList[i] != DONT_INHERIT_TRANSFORM && descentList[i].GetParent())
290     {
291       Matrix  localMatrix;
292       Vector3 parentSize = descentList[i + 1].GetProperty<Vector3>(Dali::Actor::Property::SIZE);
293       if(inheritanceModeList[i] == INHERIT_ALL)
294       {
295         localPosition = actorPosition + centerPosition + (parentOrigin - Vector3(0.5f, 0.5f, 0.5f)) * parentSize;
296         localMatrix.SetTransformComponents(localScale, localOrientation, localPosition);
297
298         //Update the world matrix
299         Matrix tempMatrix;
300         Matrix::Multiply(tempMatrix, localMatrix, worldMatrix);
301         worldMatrix = tempMatrix;
302       }
303       else
304       {
305         Vector3    parentPosition, parentScale;
306         Quaternion parentOrientation;
307         worldMatrix.GetTransformComponents(parentPosition, parentOrientation, parentScale);
308
309         if((inheritanceModeList[i] & INHERIT_SCALE) == 0)
310         {
311           //Don't inherit scale
312           localScale /= parentScale;
313         }
314
315         if((inheritanceModeList[i] & INHERIT_ORIENTATION) == 0)
316         {
317           //Don't inherit orientation
318           parentOrientation.Invert();
319           localOrientation = parentOrientation * localOrientation;
320         }
321
322         if((inheritanceModeList[i] & INHERIT_POSITION) == 0)
323         {
324           localMatrix.SetTransformComponents(localScale, localOrientation, Vector3::ZERO);
325           Matrix tempMatrix;
326           Matrix::Multiply(tempMatrix, localMatrix, worldMatrix);
327           worldMatrix = tempMatrix;
328           worldMatrix.SetTranslation(actorPosition + centerPosition);
329         }
330         else
331         {
332           localPosition = actorPosition + centerPosition + (parentOrigin - Vector3(0.5f, 0.5f, 0.5f)) * parentSize;
333           localMatrix.SetTransformComponents(localScale, localOrientation, localPosition);
334           Matrix tempMatrix;
335           Matrix::Multiply(tempMatrix, localMatrix, worldMatrix);
336           worldMatrix = tempMatrix;
337         }
338       }
339     }
340     else
341     {
342       localPosition = actorPosition + centerPosition;
343       worldMatrix.SetTransformComponents(localScale, localOrientation, localPosition);
344     }
345   }
346
347   return worldMatrix;
348 }
349
350 Vector4 TransitionBase::GetWorldColor(Dali::Actor actor)
351 {
352   std::vector<Dali::Actor>     descentList;
353   std::vector<Dali::ColorMode> inheritanceModeList;
354   Dali::Actor                  currentActor = actor;
355   Dali::ColorMode              inheritance  = Dali::ColorMode::USE_OWN_MULTIPLY_PARENT_ALPHA;
356   do
357   {
358     inheritance = currentActor.GetProperty<Dali::ColorMode>(Dali::Actor::Property::COLOR_MODE);
359     inheritanceModeList.push_back(inheritance);
360     descentList.push_back(currentActor);
361     currentActor = currentActor.GetParent();
362   } while(inheritance != Dali::ColorMode::USE_OWN_COLOR && currentActor);
363
364   Vector4 worldColor;
365   for(unsigned int i(descentList.size() - 1); i < descentList.size(); --i)
366   {
367     if(inheritanceModeList[i] == USE_OWN_COLOR || i == descentList.size() - 1)
368     {
369       worldColor = descentList[i].GetProperty<Vector4>(Dali::Actor::Property::COLOR);
370     }
371     else if(inheritanceModeList[i] == USE_OWN_MULTIPLY_PARENT_ALPHA)
372     {
373       Vector4 ownColor = descentList[i].GetProperty<Vector4>(Dali::Actor::Property::COLOR);
374       worldColor       = Vector4(ownColor.r, ownColor.g, ownColor.b, ownColor.a * worldColor.a);
375     }
376     else if(inheritanceModeList[i] == USE_OWN_MULTIPLY_PARENT_COLOR)
377     {
378       Vector4 ownColor = descentList[i].GetProperty<Vector4>(Dali::Actor::Property::COLOR);
379       worldColor *= ownColor;
380     }
381   }
382
383   return worldColor;
384 }
385
386 } // namespace Internal
387
388 } // namespace Toolkit
389
390 } // namespace Dali