Slide transition for Page transition
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / transition / slide-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/slide-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/devel-api/adaptor-framework/window-devel.h>
25 #include <dali/integration-api/debug.h>
26 #include <dali/public-api/common/dali-common.h>
27 #include <dali/public-api/object/type-registry.h>
28 #include <limits>
29
30 namespace Dali
31 {
32 namespace Toolkit
33 {
34 namespace Internal
35 {
36 SlideTransitionPtr SlideTransition::New(Dali::Toolkit::Control control, const Vector2& direction, TimePeriod timePeriod)
37 {
38   float delaySeconds = timePeriod.delaySeconds;
39   if(delaySeconds < 0.0f)
40   {
41     DALI_LOG_WARNING("delay should be greater than 0.0f.\n");
42     delaySeconds = 0.0f;
43   }
44
45   float durationSeconds = timePeriod.durationSeconds;
46   if(durationSeconds < 0.0f)
47   {
48     DALI_LOG_WARNING("duration should be greater than 0.0f.\n");
49     durationSeconds = 0.0f;
50   }
51
52   SlideTransitionPtr slideTransition = new SlideTransition(control, direction, TimePeriod(delaySeconds, durationSeconds));
53
54   // Second-phase construction
55   slideTransition->Initialize();
56
57   return slideTransition;
58 }
59
60 SlideTransition::SlideTransition(Dali::Toolkit::Control control, const Vector2& direction, TimePeriod timePeriod)
61 : TransitionBase(),
62   mTargetControl(control),
63   mDirection(direction)
64 {
65   SetTarget(control);
66   SetTimePeriod(timePeriod);
67 }
68
69 SlideTransition::~SlideTransition()
70 {
71 }
72
73 void SlideTransition::SetDirection(const Vector2& direction)
74 {
75   mDirection = direction;
76   mDirection.Normalize();
77 }
78
79 Vector2 SlideTransition::GetDirection() const
80 {
81   return mDirection;
82 }
83
84 void SlideTransition::OnPlay()
85 {
86   Dali::Toolkit::Control targetControl = mTargetControl.GetHandle();
87   if(!targetControl || !targetControl[Dali::Actor::Property::CONNECTED_TO_SCENE])
88   {
89     DALI_LOG_ERROR("The Control is not added on the window\n");
90     return;
91   }
92
93   Property::Map startPropertyMap;
94   Property::Map finishPropertyMap;
95
96   Vector3 currentPosition = targetControl[Dali::Actor::Property::POSITION];
97   Vector3 currentScale    = targetControl[Dali::Actor::Property::SCALE];
98
99   Vector3 size = targetControl[Dali::Actor::Property::SIZE];
100   size *= currentScale;
101
102   Vector2 windowSize = DevelWindow::Get(targetControl).GetSize();
103   // This checkPosition go outside of window(by following the direction), this targetControl will be out of window.
104   Vector2 checkPosition = windowSize / 2.0f +
105                           Vector2(currentPosition.x + ((mDirection.x < 0.0f) ? size.x / 2.0f : -size.x / 2.0f),
106                                   currentPosition.y + ((mDirection.y < 0.0f) ? size.y / 2.0f : -size.y / 2.0f));
107
108   float xScale = (mDirection.x == 0.0f) ? std::numeric_limits<float>::max()
109                                         : ((mDirection.x < 0.0f) ? checkPosition.x : windowSize.x - checkPosition.x) / std::abs(mDirection.x);
110   float yScale = (mDirection.y == 0.0f) ? std::numeric_limits<float>::max()
111                                         : ((mDirection.y < 0.0f) ? checkPosition.y : windowSize.y - checkPosition.y) / std::abs(mDirection.y);
112
113   Vector2 displacement = mDirection * std::min(xScale, yScale);
114
115   Vector3 startPosition;
116   Vector3 finishPosition;
117   if(IsAppearingTransition())
118   {
119     startPosition  = currentPosition + Vector3(displacement);
120     finishPosition = currentPosition;
121   }
122   else
123   {
124     startPosition  = currentPosition;
125     finishPosition = currentPosition + Vector3(displacement);
126   }
127
128   startPropertyMap.Insert(Dali::Actor::Property::POSITION, startPosition);
129   finishPropertyMap.Insert(Dali::Actor::Property::POSITION, finishPosition);
130
131   SetStartPropertyMap(startPropertyMap);
132   SetFinishPropertyMap(finishPropertyMap);
133 }
134
135 } // namespace Internal
136
137 } // namespace Toolkit
138
139 } // namespace Dali