2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali-toolkit/internal/transition/transition-base-impl.h>
22 #include <dali-toolkit/public-api/controls/control-impl.h>
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>
39 const Dali::AlphaFunction DEFAULT_ALPHA_FUNCTION(Dali::AlphaFunction::DEFAULT);
41 Property::Map GetOriginalProperties(Dali::Toolkit::Control control)
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]);
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.
69 Vector3 CalculateCenterPosition(
70 const Vector3& anchorPoint,
71 const bool positionUsesAnchorPoint,
74 const Quaternion& orientation)
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;
83 // If the position is ignoring the anchor-point, then remove the anchor-point shift from the position.
84 if(!positionUsesAnchorPoint)
86 centerPosition -= (topLeft - anchorPoint) * size;
88 return centerPosition;
91 } // anonymous namespace
93 TransitionBasePtr TransitionBase::New()
95 TransitionBasePtr transition = new TransitionBase();
97 // Second-phase construction
98 transition->Initialize();
103 TransitionBase::TransitionBase()
104 : mAlphaFunction(DEFAULT_ALPHA_FUNCTION),
105 mTimePeriod(TimePeriod(0.0f)),
106 mTransitionWithChild(false),
107 mMoveTargetChildren(false),
108 mIsAppearingTransition(true)
112 void TransitionBase::Initialize()
117 void TransitionBase::SetTimePeriod(const Dali::TimePeriod& timePeriod)
119 if(timePeriod.durationSeconds < 0.0f)
121 DALI_LOG_WARNING("Duration should be greater than 0.0f.\n");
125 mTimePeriod.durationSeconds = timePeriod.durationSeconds;
128 if(timePeriod.delaySeconds < 0.0f)
130 DALI_LOG_WARNING("Delay should be greater than 0.0f.\n");
135 mTimePeriod.delaySeconds = timePeriod.delaySeconds;
139 Dali::TimePeriod TransitionBase::GetTimePeriod() const
144 void TransitionBase::TransitionWithChild(bool transitionWithChild)
146 mTransitionWithChild = transitionWithChild;
149 void TransitionBase::PreProcess(Dali::Animation animation)
151 mAnimation = animation;
152 // Retrieve original property map of mTarget to backup and to reset after transition is finished.
153 mOriginalPropertyMap = GetOriginalProperties(mTarget);
154 mMoveTargetChildren = false;
155 if(!mTransitionWithChild && mTarget.GetChildCount() > 0)
157 mMoveTargetChildren = true;
160 GetImplementation(mTarget).SetTransparent(false);
163 void TransitionBase::Play()
165 if(!mTarget[Dali::Actor::Property::CONNECTED_TO_SCENE])
167 DALI_LOG_ERROR("The target is not added on the window\n");
176 void TransitionBase::SetAnimation()
180 DALI_LOG_ERROR("animation is not initialized\n");
184 for(uint32_t i = 0; i < mStartPropertyMap.Count(); ++i)
186 Property::Value* initialValuePointer = mInitialPropertyMap.Find(mStartPropertyMap.GetKeyAt(i).indexKey);
187 Property::Value* finishValue = mFinishPropertyMap.Find(mStartPropertyMap.GetKeyAt(i).indexKey);
190 Property::Value initialValue = mStartPropertyMap.GetValue(i);
191 if(initialValuePointer)
193 initialValue = *initialValuePointer;
195 AnimateBetween(mTarget, mStartPropertyMap.GetKeyAt(i).indexKey, initialValue, mStartPropertyMap.GetValue(i), *finishValue);
200 void TransitionBase::AnimateBetween(Dali::Toolkit::Control target, Property::Index index, Property::Value initialValue, Property::Value sourceValue, Property::Value destinationValue)
204 if(mTimePeriod.delaySeconds>0.0f)
206 Dali::KeyFrames initialKeyframes = Dali::KeyFrames::New();
207 initialKeyframes.Add(0.0f, initialValue);
208 initialKeyframes.Add(1.0f, initialValue);
209 mAnimation.AnimateBetween(Property(target, index), initialKeyframes, TimePeriod(mTimePeriod.delaySeconds));
211 Dali::KeyFrames keyframes = Dali::KeyFrames::New();
212 keyframes.Add(0.0f, sourceValue);
213 keyframes.Add(1.0f, destinationValue);
214 mAnimation.AnimateBetween(Property(target, index), keyframes, mAlphaFunction, mTimePeriod);
218 void TransitionBase::CopyTarget()
220 mCopiedActor = Dali::Actor::New();
221 mTarget.GetParent().Add(mCopiedActor);
223 mCopiedActor[Dali::DevelActor::Property::SIBLING_ORDER] = static_cast<int32_t>(mTarget[Dali::DevelActor::Property::SIBLING_ORDER]) + 1;
224 while(mTarget.GetChildCount() > 0)
226 Dali::Actor child = mTarget.GetChildAt(0);
227 Dali::DevelActor::SwitchParent(child, mCopiedActor);
230 // Copy Size property to mCopiedActor because Size is not included mOriginalPropertyMap.
231 mCopiedActor[Dali::Actor::Property::SIZE] = mTarget.GetProperty<Vector3>(Dali::Actor::Property::SIZE);
232 mCopiedActor.SetProperties(mOriginalPropertyMap);
235 void TransitionBase::TransitionFinished()
239 mTarget.SetProperties(mOriginalPropertyMap);
240 if(mMoveTargetChildren)
242 while(mCopiedActor.GetChildCount() > 0)
244 Dali::Actor child = mCopiedActor.GetChildAt(0);
245 Dali::DevelActor::SwitchParent(child, mTarget);
247 mCopiedActor.Unparent();
248 mCopiedActor.Reset();
253 Matrix TransitionBase::GetWorldTransform(Dali::Actor actor)
257 DONT_INHERIT_TRANSFORM = 0,
258 INHERIT_POSITION = 1,
260 INHERIT_ORIENTATION = 4,
261 INHERIT_ALL = INHERIT_POSITION | INHERIT_SCALE | INHERIT_ORIENTATION,
264 std::vector<Dali::Actor> descentList;
265 std::vector<InheritanceMode> inheritanceModeList;
266 Dali::Actor currentActor = actor;
270 inheritance = (static_cast<int>(currentActor.GetProperty<bool>(Dali::Actor::Property::INHERIT_ORIENTATION)) << 2) +
271 (static_cast<int>(currentActor.GetProperty<bool>(Dali::Actor::Property::INHERIT_SCALE)) << 1) +
272 static_cast<int>(currentActor.GetProperty<bool>(Dali::Actor::Property::INHERIT_POSITION));
273 inheritanceModeList.push_back(static_cast<InheritanceMode>(inheritance));
274 descentList.push_back(currentActor);
275 currentActor = currentActor.GetParent();
276 } while(inheritance != DONT_INHERIT_TRANSFORM && currentActor);
279 Vector3 localPosition;
280 for(unsigned int i(descentList.size() - 1); i < descentList.size(); --i)
282 Vector3 anchorPoint = descentList[i].GetProperty<Vector3>(Dali::Actor::Property::ANCHOR_POINT);
283 Vector3 parentOrigin = descentList[i].GetProperty<Vector3>(Dali::Actor::Property::PARENT_ORIGIN);
284 bool positionUsesAnchorPoint = descentList[i].GetProperty<bool>(Dali::Actor::Property::POSITION_USES_ANCHOR_POINT);
285 Vector3 size = descentList[i].GetProperty<Vector3>(Dali::Actor::Property::SIZE);
286 Vector3 actorPosition = descentList[i].GetProperty<Vector3>(Dali::Actor::Property::POSITION);
287 Quaternion localOrientation = descentList[i].GetProperty<Quaternion>(Dali::Actor::Property::ORIENTATION);
288 Vector3 localScale = descentList[i].GetProperty<Vector3>(Dali::Actor::Property::SCALE);
290 Vector3 centerPosition = CalculateCenterPosition(anchorPoint, positionUsesAnchorPoint, size, localScale, localOrientation);
291 if(inheritanceModeList[i] != DONT_INHERIT_TRANSFORM && descentList[i].GetParent())
294 Vector3 parentSize = descentList[i + 1].GetProperty<Vector3>(Dali::Actor::Property::SIZE);
295 if(inheritanceModeList[i] == INHERIT_ALL)
297 localPosition = actorPosition + centerPosition + (parentOrigin - Vector3(0.5f, 0.5f, 0.5f)) * parentSize;
298 localMatrix.SetTransformComponents(localScale, localOrientation, localPosition);
300 //Update the world matrix
302 Matrix::Multiply(tempMatrix, localMatrix, worldMatrix);
303 worldMatrix = tempMatrix;
307 Vector3 parentPosition, parentScale;
308 Quaternion parentOrientation;
309 worldMatrix.GetTransformComponents(parentPosition, parentOrientation, parentScale);
311 if((inheritanceModeList[i] & INHERIT_SCALE) == 0)
313 //Don't inherit scale
314 localScale /= parentScale;
317 if((inheritanceModeList[i] & INHERIT_ORIENTATION) == 0)
319 //Don't inherit orientation
320 parentOrientation.Invert();
321 localOrientation = parentOrientation * localOrientation;
324 if((inheritanceModeList[i] & INHERIT_POSITION) == 0)
326 localMatrix.SetTransformComponents(localScale, localOrientation, Vector3::ZERO);
328 Matrix::Multiply(tempMatrix, localMatrix, worldMatrix);
329 worldMatrix = tempMatrix;
330 worldMatrix.SetTranslation(actorPosition + centerPosition);
334 localPosition = actorPosition + centerPosition + (parentOrigin - Vector3(0.5f, 0.5f, 0.5f)) * parentSize;
335 localMatrix.SetTransformComponents(localScale, localOrientation, localPosition);
337 Matrix::Multiply(tempMatrix, localMatrix, worldMatrix);
338 worldMatrix = tempMatrix;
344 localPosition = actorPosition + centerPosition;
345 worldMatrix.SetTransformComponents(localScale, localOrientation, localPosition);
352 Vector4 TransitionBase::GetWorldColor(Dali::Actor actor)
354 std::vector<Dali::Actor> descentList;
355 std::vector<Dali::ColorMode> inheritanceModeList;
356 Dali::Actor currentActor = actor;
357 Dali::ColorMode inheritance = Dali::ColorMode::USE_OWN_MULTIPLY_PARENT_ALPHA;
360 inheritance = currentActor.GetProperty<Dali::ColorMode>(Dali::Actor::Property::COLOR_MODE);
361 inheritanceModeList.push_back(inheritance);
362 descentList.push_back(currentActor);
363 currentActor = currentActor.GetParent();
364 } while(inheritance != Dali::ColorMode::USE_OWN_COLOR && currentActor);
367 for(unsigned int i(descentList.size() - 1); i < descentList.size(); --i)
369 if(inheritanceModeList[i] == USE_OWN_COLOR || i == descentList.size() - 1)
371 worldColor = descentList[i].GetProperty<Vector4>(Dali::Actor::Property::COLOR);
373 else if(inheritanceModeList[i] == USE_OWN_MULTIPLY_PARENT_ALPHA)
375 Vector4 ownColor = descentList[i].GetProperty<Vector4>(Dali::Actor::Property::COLOR);
376 worldColor = Vector4(ownColor.r, ownColor.g, ownColor.b, ownColor.a * worldColor.a);
378 else if(inheritanceModeList[i] == USE_OWN_MULTIPLY_PARENT_COLOR)
380 Vector4 ownColor = descentList[i].GetProperty<Vector4>(Dali::Actor::Property::COLOR);
381 worldColor *= ownColor;
388 } // namespace Internal
390 } // namespace Toolkit