Merge "Add BuildPickingRay to devel api" into 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::Remove()
74 {
75   GetImplementation(*this).Remove();
76 }
77
78 Handle Constraint::GetTargetObject()
79 {
80   return GetImplementation(*this).GetTargetObject();
81 }
82
83 Property::Index Constraint::GetTargetProperty()
84 {
85   return GetImplementation(*this).GetTargetProperty();
86 }
87
88 void Constraint::SetRemoveAction(Constraint::RemoveAction action)
89 {
90   GetImplementation(*this).SetRemoveAction(action);
91 }
92
93 Constraint::RemoveAction Constraint::GetRemoveAction() const
94 {
95   return GetImplementation(*this).GetRemoveAction();
96 }
97
98 void Constraint::SetTag(const uint32_t tag)
99 {
100   GetImplementation(*this).SetTag(tag);
101 }
102
103 uint32_t Constraint::GetTag() const
104 {
105   return GetImplementation(*this).GetTag();
106 }
107
108 Constraint::Constraint(Internal::ConstraintBase* constraint)
109 : BaseHandle(constraint)
110 {
111 }
112
113 Constraint Constraint::New(Handle handle, Property::Index targetIndex, Property::Type targetType, CallbackBase* function)
114 {
115   Constraint                constraint;
116   Internal::SourceContainer sources;
117   Internal::Object&         object = GetImplementation(handle);
118
119   switch(targetType)
120   {
121     case Property::BOOLEAN:
122     {
123       Internal::PropertyConstraintPtr<bool>::Type funcPtr(CreatePropertyConstraint<bool>(function));
124
125       constraint = Dali::Constraint(Internal::Constraint<bool>::New(object,
126                                                                     targetIndex,
127                                                                     sources,
128                                                                     funcPtr));
129       break;
130     }
131
132     case Property::FLOAT:
133     {
134       Internal::PropertyConstraintPtr<float>::Type funcPtr(CreatePropertyConstraint<float>(function));
135
136       constraint = Dali::Constraint(Internal::Constraint<float>::New(object,
137                                                                      targetIndex,
138                                                                      sources,
139                                                                      funcPtr));
140       break;
141     }
142
143     case Property::INTEGER:
144     {
145       Internal::PropertyConstraintPtr<int>::Type funcPtr(CreatePropertyConstraint<int>(function));
146
147       constraint = Dali::Constraint(Internal::Constraint<int>::New(object,
148                                                                    targetIndex,
149                                                                    sources,
150                                                                    funcPtr));
151       break;
152     }
153
154     case Property::VECTOR2:
155     {
156       Internal::PropertyConstraintPtr<Vector2>::Type funcPtr(CreatePropertyConstraint<Vector2>(function));
157
158       constraint = Dali::Constraint(Internal::Constraint<Vector2>::New(object,
159                                                                        targetIndex,
160                                                                        sources,
161                                                                        funcPtr));
162       break;
163     }
164
165     case Property::VECTOR3:
166     {
167       Internal::PropertyConstraintPtr<Vector3>::Type funcPtr(CreatePropertyConstraint<Vector3>(function));
168
169       constraint = Dali::Constraint(Internal::Constraint<Vector3>::New(object,
170                                                                        targetIndex,
171                                                                        sources,
172                                                                        funcPtr));
173       break;
174     }
175
176     case Property::VECTOR4:
177     {
178       Internal::PropertyConstraintPtr<Vector4>::Type funcPtr(CreatePropertyConstraint<Vector4>(function));
179
180       constraint = Dali::Constraint(Internal::Constraint<Vector4>::New(object,
181                                                                        targetIndex,
182                                                                        sources,
183                                                                        funcPtr));
184       break;
185     }
186
187     case Property::ROTATION:
188     {
189       Internal::PropertyConstraintPtr<Quaternion>::Type funcPtr(CreatePropertyConstraint<Quaternion>(function));
190
191       constraint = Dali::Constraint(Internal::Constraint<Quaternion>::New(object,
192                                                                           targetIndex,
193                                                                           sources,
194                                                                           funcPtr));
195       break;
196     }
197
198     case Property::MATRIX:
199     {
200       Internal::PropertyConstraintPtr<Matrix>::Type funcPtr(CreatePropertyConstraint<Matrix>(function));
201
202       constraint = Dali::Constraint(Internal::Constraint<Matrix>::New(object,
203                                                                       targetIndex,
204                                                                       sources,
205                                                                       funcPtr));
206       break;
207     }
208
209     case Property::MATRIX3:
210     {
211       Internal::PropertyConstraintPtr<Matrix3>::Type funcPtr(CreatePropertyConstraint<Matrix3>(function));
212
213       constraint = Dali::Constraint(Internal::Constraint<Matrix3>::New(object,
214                                                                        targetIndex,
215                                                                        sources,
216                                                                        funcPtr));
217       break;
218     }
219
220     default:
221     {
222       DALI_ABORT("Property not constrainable");
223       break;
224     }
225   }
226
227   return constraint;
228 }
229
230 } // namespace Dali