[dali_2.3.25] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / public-api / animation / constraint.cpp
1 /*
2  * Copyright (c) 2020 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/public-api/animation/constraint.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/internal/event/animation/constraint-impl.h>
23 #include <dali/internal/event/animation/constraint-source-impl.h>
24 #include <dali/internal/event/animation/property-constraint-ptr.h>
25 #include <dali/internal/event/animation/property-constraint.h>
26
27 namespace Dali
28 {
29 namespace // unnamed namespace
30 {
31 template<class P>
32 Internal::PropertyConstraint<P>* CreatePropertyConstraint(CallbackBase* func)
33 {
34   return new Internal::PropertyConstraint<P>(reinterpret_cast<Dali::Constraint::Function<P>*>(func));
35 }
36
37 } // unnamed namespace
38
39 const Constraint::RemoveAction Constraint::DEFAULT_REMOVE_ACTION = Constraint::BAKE;
40
41 Constraint::Constraint() = default;
42
43 Constraint Constraint::Clone(Handle object)
44 {
45   return Constraint(GetImplementation(*this).Clone(GetImplementation(object)));
46 }
47
48 Constraint::~Constraint() = default;
49
50 Constraint::Constraint(const Constraint& constraint) = default;
51
52 Constraint& Constraint::operator=(const Constraint& rhs) = default;
53
54 Constraint::Constraint(Constraint&& rhs) noexcept = default;
55
56 Constraint& Constraint::operator=(Constraint&& rhs) noexcept = default;
57
58 Constraint Constraint::DownCast(BaseHandle baseHandle)
59 {
60   return Constraint(dynamic_cast<Dali::Internal::ConstraintBase*>(baseHandle.GetObjectPtr()));
61 }
62
63 void Constraint::AddSource(ConstraintSource source)
64 {
65   GetImplementation(*this).AddSource(Internal::Source(source));
66 }
67
68 void Constraint::Apply()
69 {
70   GetImplementation(*this).Apply();
71 }
72
73 void Constraint::ApplyPost()
74 {
75   GetImplementation(*this).ApplyPost();
76 }
77
78 void Constraint::Remove()
79 {
80   GetImplementation(*this).Remove();
81 }
82
83 Handle Constraint::GetTargetObject()
84 {
85   return GetImplementation(*this).GetTargetObject();
86 }
87
88 Property::Index Constraint::GetTargetProperty()
89 {
90   return GetImplementation(*this).GetTargetProperty();
91 }
92
93 void Constraint::SetRemoveAction(Constraint::RemoveAction action)
94 {
95   GetImplementation(*this).SetRemoveAction(action);
96 }
97
98 Constraint::RemoveAction Constraint::GetRemoveAction() const
99 {
100   return GetImplementation(*this).GetRemoveAction();
101 }
102
103 void Constraint::SetTag(const uint32_t tag)
104 {
105   GetImplementation(*this).SetTag(tag);
106 }
107
108 uint32_t Constraint::GetTag() const
109 {
110   return GetImplementation(*this).GetTag();
111 }
112
113 Constraint::Constraint(Internal::ConstraintBase* constraint)
114 : BaseHandle(constraint)
115 {
116 }
117
118 Constraint Constraint::New(Handle handle, Property::Index targetIndex, Property::Type targetType, CallbackBase* function)
119 {
120   Constraint                constraint;
121   Internal::SourceContainer sources;
122   Internal::Object&         object = GetImplementation(handle);
123
124   switch(targetType)
125   {
126     case Property::BOOLEAN:
127     {
128       Internal::PropertyConstraintPtr<bool>::Type funcPtr(CreatePropertyConstraint<bool>(function));
129
130       constraint = Dali::Constraint(Internal::Constraint<bool>::New(object,
131                                                                     targetIndex,
132                                                                     sources,
133                                                                     funcPtr));
134       break;
135     }
136
137     case Property::FLOAT:
138     {
139       Internal::PropertyConstraintPtr<float>::Type funcPtr(CreatePropertyConstraint<float>(function));
140
141       constraint = Dali::Constraint(Internal::Constraint<float>::New(object,
142                                                                      targetIndex,
143                                                                      sources,
144                                                                      funcPtr));
145       break;
146     }
147
148     case Property::INTEGER:
149     {
150       Internal::PropertyConstraintPtr<int>::Type funcPtr(CreatePropertyConstraint<int>(function));
151
152       constraint = Dali::Constraint(Internal::Constraint<int>::New(object,
153                                                                    targetIndex,
154                                                                    sources,
155                                                                    funcPtr));
156       break;
157     }
158
159     case Property::VECTOR2:
160     {
161       Internal::PropertyConstraintPtr<Vector2>::Type funcPtr(CreatePropertyConstraint<Vector2>(function));
162
163       constraint = Dali::Constraint(Internal::Constraint<Vector2>::New(object,
164                                                                        targetIndex,
165                                                                        sources,
166                                                                        funcPtr));
167       break;
168     }
169
170     case Property::VECTOR3:
171     {
172       Internal::PropertyConstraintPtr<Vector3>::Type funcPtr(CreatePropertyConstraint<Vector3>(function));
173
174       constraint = Dali::Constraint(Internal::Constraint<Vector3>::New(object,
175                                                                        targetIndex,
176                                                                        sources,
177                                                                        funcPtr));
178       break;
179     }
180
181     case Property::VECTOR4:
182     {
183       Internal::PropertyConstraintPtr<Vector4>::Type funcPtr(CreatePropertyConstraint<Vector4>(function));
184
185       constraint = Dali::Constraint(Internal::Constraint<Vector4>::New(object,
186                                                                        targetIndex,
187                                                                        sources,
188                                                                        funcPtr));
189       break;
190     }
191
192     case Property::ROTATION:
193     {
194       Internal::PropertyConstraintPtr<Quaternion>::Type funcPtr(CreatePropertyConstraint<Quaternion>(function));
195
196       constraint = Dali::Constraint(Internal::Constraint<Quaternion>::New(object,
197                                                                           targetIndex,
198                                                                           sources,
199                                                                           funcPtr));
200       break;
201     }
202
203     case Property::MATRIX:
204     {
205       Internal::PropertyConstraintPtr<Matrix>::Type funcPtr(CreatePropertyConstraint<Matrix>(function));
206
207       constraint = Dali::Constraint(Internal::Constraint<Matrix>::New(object,
208                                                                       targetIndex,
209                                                                       sources,
210                                                                       funcPtr));
211       break;
212     }
213
214     case Property::MATRIX3:
215     {
216       Internal::PropertyConstraintPtr<Matrix3>::Type funcPtr(CreatePropertyConstraint<Matrix3>(function));
217
218       constraint = Dali::Constraint(Internal::Constraint<Matrix3>::New(object,
219                                                                        targetIndex,
220                                                                        sources,
221                                                                        funcPtr));
222       break;
223     }
224
225     default:
226     {
227       DALI_ABORT("Property not constrainable");
228       break;
229     }
230   }
231
232   return constraint;
233 }
234
235 } // namespace Dali