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