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