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 } // anonymous namespace
43 TransitionBasePtr TransitionBase::New()
45 TransitionBasePtr transition = new TransitionBase();
47 // Second-phase construction
48 transition->Initialize();
53 TransitionBase::TransitionBase()
54 : mAlphaFunction(DEFAULT_ALPHA_FUNCTION),
55 mDurationSeconds(0.0f),
57 mTransitionWithChild(false),
58 mMoveTargetChildren(false)
62 void TransitionBase::Initialize()
67 void TransitionBase::SetDuration(float seconds)
71 DALI_LOG_WARNING("duration should be greater than 0.0f.\n");
75 mDurationSeconds = seconds;
78 float TransitionBase::GetDuration() const
80 // This is not animatable; the cached value is up-to-date.
81 return mDurationSeconds;
84 void TransitionBase::SetDelay(float seconds)
88 DALI_LOG_WARNING("delay should be greater than 0.0f.\n");
92 mDelaySeconds = seconds;
95 float TransitionBase::GetDelay() const
97 // This is not animatable; the cached value is up-to-date.
101 void TransitionBase::SetTimePeriod(const Dali::TimePeriod& timePeriod)
103 if(timePeriod.durationSeconds <= 0.0f || timePeriod.delaySeconds <= 0.0f)
105 DALI_LOG_WARNING("duration and delay should be greater than 0.0f.\n");
109 SetDuration(timePeriod.durationSeconds);
110 SetDelay(timePeriod.delaySeconds);
113 void TransitionBase::TransitionWithChild(bool transitionWithChild)
115 mTransitionWithChild = transitionWithChild;
118 void TransitionBase::SetPlay(Dali::Toolkit::TransitionSet transitionSet)
120 mTransitionSet = transitionSet;
123 void TransitionBase::PreProcess(Dali::Animation animation)
125 mAnimation = animation;
126 mOriginialPropertyMap = GetProperties(mTargetView);
127 mMoveTargetChildren = false;
128 if(!mTransitionWithChild && mTargetView.GetChildCount() > 0)
130 mMoveTargetChildren = true;
133 GetImplementation(mTargetView).SetTransparent(false);
136 void TransitionBase::Play()
138 if(!mTargetView[Dali::Actor::Property::CONNECTED_TO_SCENE])
140 DALI_LOG_ERROR("The target view is not added on the window\n");
149 Property::Map TransitionBase::GetProperties(Dali::Toolkit::Control control)
151 Property::Map propertyMap;
152 propertyMap.Insert(Dali::Actor::Property::ANCHOR_POINT, control[Dali::Actor::Property::ANCHOR_POINT]);
153 propertyMap.Insert(Dali::Actor::Property::PARENT_ORIGIN, control[Dali::Actor::Property::PARENT_ORIGIN]);
154 propertyMap.Insert(Dali::Actor::Property::POSITION_USES_ANCHOR_POINT, control[Dali::Actor::Property::POSITION_USES_ANCHOR_POINT]);
155 propertyMap.Insert(Dali::Actor::Property::INHERIT_POSITION, control[Dali::Actor::Property::INHERIT_POSITION]);
156 propertyMap.Insert(Dali::Actor::Property::INHERIT_ORIENTATION, control[Dali::Actor::Property::INHERIT_ORIENTATION]);
157 propertyMap.Insert(Dali::Actor::Property::INHERIT_SCALE, control[Dali::Actor::Property::INHERIT_SCALE]);
158 propertyMap.Insert(Dali::Actor::Property::COLOR_MODE, control[Dali::Actor::Property::COLOR_MODE]);
159 propertyMap.Insert(Dali::Actor::Property::POSITION, control[Dali::Actor::Property::POSITION]);
160 propertyMap.Insert(Dali::Actor::Property::ORIENTATION, control[Dali::Actor::Property::ORIENTATION]);
161 propertyMap.Insert(Dali::Actor::Property::SCALE, control[Dali::Actor::Property::SCALE]);
162 propertyMap.Insert(Dali::Actor::Property::COLOR, control[Dali::Actor::Property::COLOR]);
167 void TransitionBase::SetAnimation()
171 DALI_LOG_ERROR("animation is not initialized\n");
175 for(uint32_t i = 0; i < mStartPropertyMap.Count(); ++i)
177 Property::Value* finishValue = mFinishPropertyMap.Find(mStartPropertyMap.GetKeyAt(i).indexKey);
180 AnimateBetween(mTargetView, mStartPropertyMap.GetKeyAt(i).indexKey, mStartPropertyMap.GetValue(i), *finishValue);
185 void TransitionBase::AnimateBetween(Dali::Toolkit::Control target, Property::Index index, Property::Value sourceValue, Property::Value destinationValue)
189 Dali::KeyFrames keyframes = Dali::KeyFrames::New();
190 keyframes.Add(0.0f, sourceValue);
191 keyframes.Add(1.0f, destinationValue);
192 mAnimation.AnimateBetween(Property(target, index), keyframes, mAlphaFunction, TimePeriod(mDelaySeconds, mDurationSeconds));
196 void TransitionBase::CopyTarget()
198 mCopiedActor = Dali::Actor::New();
199 mTargetView.GetParent().Add(mCopiedActor);
200 mCopiedActor[Dali::DevelActor::Property::SIBLING_ORDER] = static_cast<int32_t>(mTargetView[Dali::DevelActor::Property::SIBLING_ORDER]) + 1;
201 for(uint32_t i = 0; i < mTargetView.GetChildCount(); ++i)
203 Dali::Actor child = mTargetView.GetChildAt(i);
204 Dali::DevelActor::SwitchParent(child, mCopiedActor);
207 // Animation to set property instantly without SetProperty.
208 mCopiedActor[Dali::Actor::Property::SIZE] = mTargetView.GetProperty<Vector3>(Dali::Actor::Property::SIZE);
209 mCopiedActor.SetProperties(mOriginialPropertyMap);
212 void TransitionBase::TransitionFinished()
216 mTargetView.SetProperties(mOriginialPropertyMap);
217 if(mMoveTargetChildren)
219 for(uint32_t i = 0; i < mCopiedActor.GetChildCount(); ++i)
221 Dali::Actor child = mCopiedActor.GetChildAt(i);
222 Dali::DevelActor::SwitchParent(child, mTargetView);
224 mCopiedActor.Unparent();
225 mCopiedActor.Reset();
227 mTransitionSet.Reset();
231 Matrix TransitionBase::GetWorldTransform(Dali::Actor actor)
235 DONT_INHERIT_TRANSFORM = 0,
236 INHERIT_POSITION = 1,
238 INHERIT_ORIENTATION = 4,
239 INHERIT_ALL = INHERIT_POSITION | INHERIT_SCALE | INHERIT_ORIENTATION,
242 std::vector<Dali::Actor> descentList;
243 std::vector<InheritanceMode> inheritanceModeList;
244 Dali::Actor currentActor = actor;
248 inheritance = (static_cast<int>(currentActor.GetProperty<bool>(Dali::Actor::Property::INHERIT_ORIENTATION)) << 2) +
249 (static_cast<int>(currentActor.GetProperty<bool>(Dali::Actor::Property::INHERIT_SCALE)) << 1) +
250 static_cast<int>(currentActor.GetProperty<bool>(Dali::Actor::Property::INHERIT_POSITION));
251 inheritanceModeList.push_back(static_cast<InheritanceMode>(inheritance));
252 descentList.push_back(currentActor);
253 currentActor = currentActor.GetParent();
254 } while(inheritance != DONT_INHERIT_TRANSFORM && currentActor);
257 Vector3 localPosition;
258 for(unsigned int i(descentList.size() - 1); i < descentList.size(); --i)
260 Vector3 anchorPoint = descentList[i].GetProperty<Vector3>(Dali::Actor::Property::ANCHOR_POINT);
261 Vector3 parentOrigin = descentList[i].GetProperty<Vector3>(Dali::Actor::Property::PARENT_ORIGIN);
262 bool positionUsesAnchorPoint = descentList[i].GetProperty<bool>(Dali::Actor::Property::POSITION_USES_ANCHOR_POINT);
263 Vector3 size = descentList[i].GetProperty<Vector3>(Dali::Actor::Property::SIZE);
264 Vector3 actorPosition = descentList[i].GetProperty<Vector3>(Dali::Actor::Property::POSITION);
265 Quaternion localOrientation = descentList[i].GetProperty<Quaternion>(Dali::Actor::Property::ORIENTATION);
266 Vector3 localScale = descentList[i].GetProperty<Vector3>(Dali::Actor::Property::SCALE);
268 Vector3 centerPosition = CalculateCenterPosition(anchorPoint, positionUsesAnchorPoint, size, localScale, localOrientation);
269 if(inheritanceModeList[i] != DONT_INHERIT_TRANSFORM && descentList[i].GetParent())
272 Vector3 parentSize = descentList[i + 1].GetProperty<Vector3>(Dali::Actor::Property::SIZE);
273 if(inheritanceModeList[i] == INHERIT_ALL)
275 localPosition = actorPosition + centerPosition + (parentOrigin - Vector3(0.5f, 0.5f, 0.5f)) * parentSize;
276 localMatrix.SetTransformComponents(localScale, localOrientation, localPosition);
278 //Update the world matrix
280 Matrix::Multiply(tempMatrix, localMatrix, worldMatrix);
281 worldMatrix = tempMatrix;
285 Vector3 parentPosition, parentScale;
286 Quaternion parentOrientation;
287 worldMatrix.GetTransformComponents(parentPosition, parentOrientation, parentScale);
289 if((inheritanceModeList[i] & INHERIT_SCALE) == 0)
291 //Don't inherit scale
292 localScale /= parentScale;
295 if((inheritanceModeList[i] & INHERIT_ORIENTATION) == 0)
297 //Don't inherit orientation
298 parentOrientation.Invert();
299 localOrientation = parentOrientation * localOrientation;
302 if((inheritanceModeList[i] & INHERIT_POSITION) == 0)
304 localMatrix.SetTransformComponents(localScale, localOrientation, Vector3::ZERO);
306 Matrix::Multiply(tempMatrix, localMatrix, worldMatrix);
307 worldMatrix = tempMatrix;
308 worldMatrix.SetTranslation(actorPosition + centerPosition);
312 localPosition = actorPosition + centerPosition + (parentOrigin - Vector3(0.5f, 0.5f, 0.5f)) * parentSize;
313 localMatrix.SetTransformComponents(localScale, localOrientation, localPosition);
315 Matrix::Multiply(tempMatrix, localMatrix, worldMatrix);
316 worldMatrix = tempMatrix;
322 localPosition = actorPosition + centerPosition;
323 worldMatrix.SetTransformComponents(localScale, localOrientation, localPosition);
330 Vector4 TransitionBase::GetWorldColor(Dali::Actor actor)
332 std::vector<Dali::Actor> descentList;
333 std::vector<Dali::ColorMode> inheritanceModeList;
334 Dali::Actor currentActor = actor;
335 Dali::ColorMode inheritance = Dali::ColorMode::USE_OWN_MULTIPLY_PARENT_ALPHA;
338 inheritance = currentActor.GetProperty<Dali::ColorMode>(Dali::Actor::Property::COLOR_MODE);
339 inheritanceModeList.push_back(inheritance);
340 descentList.push_back(currentActor);
341 currentActor = currentActor.GetParent();
342 } while(inheritance != Dali::ColorMode::USE_OWN_COLOR && currentActor);
345 for(unsigned int i(descentList.size() - 1); i < descentList.size(); --i)
347 if(inheritanceModeList[i] == USE_OWN_COLOR || i == descentList.size() - 1)
349 worldColor = descentList[i].GetProperty<Vector4>(Dali::Actor::Property::COLOR);
351 else if(inheritanceModeList[i] == USE_OWN_MULTIPLY_PARENT_ALPHA)
353 Vector4 ownColor = descentList[i].GetProperty<Vector4>(Dali::Actor::Property::COLOR);
354 worldColor = Vector4(ownColor.r, ownColor.g, ownColor.b, ownColor.a * worldColor.a);
356 else if(inheritanceModeList[i] == USE_OWN_MULTIPLY_PARENT_COLOR)
358 Vector4 ownColor = descentList[i].GetProperty<Vector4>(Dali::Actor::Property::COLOR);
359 worldColor *= ownColor;
366 Vector3 TransitionBase::CalculateCenterPosition(
367 const Vector3& anchorPoint,
368 const bool positionUsesAnchorPoint,
370 const Vector3& scale,
371 const Quaternion& orientation)
373 Vector3 centerPosition;
374 const Vector3 half(0.5f, 0.5f, 0.5f);
375 const Vector3 topLeft(0.0f, 0.0f, 0.5f);
376 // Calculate the center-point by applying the scale and rotation on the anchor point.
377 centerPosition = (half - anchorPoint) * size * scale;
378 centerPosition *= orientation;
380 // If the position is ignoring the anchor-point, then remove the anchor-point shift from the position.
381 if(!positionUsesAnchorPoint)
383 centerPosition -= (topLeft - anchorPoint) * size;
385 return centerPosition;
388 } // namespace Internal
390 } // namespace Toolkit