[3.0] Clipping API feature in Actor
[platform/core/uifw/dali-core.git] / dali / internal / event / animation / path-constrainer-impl.cpp
1 /*
2  * Copyright (c) 2015 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/internal/event/animation/path-constrainer-impl.h>
20
21 //EXTRENAL INCLUDES
22 #include <cstring> // for strcmp
23
24 // INTERNAL INCLUDES
25 #include <dali/internal/event/common/property-helper.h>
26 #include <dali/public-api/animation/constraint.h>
27 #include <dali/public-api/object/property-array.h>
28
29 namespace Dali
30 {
31
32 namespace Internal
33 {
34
35 namespace
36 {
37
38 // Properties
39 //              Name             Type   writable animatable constraint-input  enum for index-checking
40 DALI_PROPERTY_TABLE_BEGIN
41 DALI_PROPERTY( "forward",       VECTOR3,   true,    false,       false,        Dali::PathConstrainer::Property::FORWARD )
42 DALI_PROPERTY( "points",         ARRAY,    true,    false,       false,        Dali::PathConstrainer::Property::POINTS )
43 DALI_PROPERTY( "controlPoints",  ARRAY,    true,    false,       false,        Dali::PathConstrainer::Property::CONTROL_POINTS )
44 DALI_PROPERTY_TABLE_END( DEFAULT_OBJECT_PROPERTY_START_INDEX )
45
46 } //Unnamed namespace
47
48 PathConstrainer* PathConstrainer::New()
49 {
50   return new PathConstrainer();
51 }
52
53 PathConstrainer::PathConstrainer()
54 : Constrainer(),
55   mPath( Path::New() )
56 {
57 }
58
59 PathConstrainer::~PathConstrainer()
60 {
61 }
62
63 unsigned int PathConstrainer::GetDefaultPropertyCount() const
64 {
65   return DEFAULT_PROPERTY_COUNT;
66 }
67
68 void PathConstrainer::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
69 {
70   indices.Reserve( DEFAULT_PROPERTY_COUNT );
71
72   for ( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
73   {
74     indices.PushBack( i );
75   }
76 }
77
78 const char* PathConstrainer::GetDefaultPropertyName(Property::Index index) const
79 {
80   if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
81   {
82     return DEFAULT_PROPERTY_DETAILS[index].name;
83   }
84
85   // index out of range
86   return NULL;
87 }
88
89 Property::Index PathConstrainer::GetDefaultPropertyIndex(const std::string& name) const
90 {
91   Property::Index index = Property::INVALID_INDEX;
92
93   // Look for name in default properties
94   for( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
95   {
96     const Internal::PropertyDetails* property = &DEFAULT_PROPERTY_DETAILS[ i ];
97     if( 0 == strcmp( name.c_str(), property->name ) ) // dont want to convert rhs to string
98     {
99       index = i;
100       break;
101     }
102   }
103   return index;
104 }
105
106 Property::Type PathConstrainer::GetDefaultPropertyType(Property::Index index) const
107 {
108   if( index < DEFAULT_PROPERTY_COUNT )
109   {
110     return DEFAULT_PROPERTY_DETAILS[index].type;
111   }
112
113   // index out of range
114   return Property::NONE;
115 }
116
117 Property::Value PathConstrainer::GetDefaultProperty( Property::Index index ) const
118 {
119   if( index == Dali::PathConstrainer::Property::FORWARD )
120   {
121     return Property::Value( mForward );
122   }
123   else
124   {
125     if( index == Dali::PathConstrainer::Property::POINTS )
126     {
127       Property::Value value( Property::ARRAY );
128       Property::Array* array = value.GetArray();
129       const Dali::Vector<Vector3>& point = mPath->GetPoints();
130       Property::Array::SizeType pointCount = point.Size();
131
132       if( array )
133       {
134         array->Reserve( pointCount );
135         for( Property::Array::SizeType i = 0; i < pointCount; ++i )
136         {
137           array->PushBack( point[i] );
138         }
139       }
140       return value;
141     }
142     else if( index == Dali::PathConstrainer::Property::CONTROL_POINTS )
143     {
144       Property::Value value( Property::ARRAY );
145       Property::Array* array = value.GetArray();
146       const Dali::Vector<Vector3>& point = mPath->GetControlPoints();
147       Property::Array::SizeType pointCount = point.Size();
148
149       if( array )
150       {
151         array->Reserve( pointCount );
152         for( Property::Array::SizeType i = 0; i < pointCount; ++i )
153         {
154           array->PushBack( point[i] );
155         }
156       }
157       return value;
158     }
159   }
160
161   return Property::Value();
162 }
163
164 void PathConstrainer::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
165 {
166   if( index == Dali::PathConstrainer::Property::FORWARD )
167   {
168     propertyValue.Get(mForward);
169   }
170   else if( index == Dali::PathConstrainer::Property::POINTS  )
171   {
172     const Property::Array* array = propertyValue.GetArray();
173     mPath->ClearPoints();
174     if( array )
175     {
176       for( Property::Array::SizeType i = 0, count = array->Count(); i < count; ++i )
177       {
178         Vector3 point;
179         array->GetElementAt( i ).Get( point );
180         mPath->AddPoint( point );
181       }
182     }
183   }
184   else if( index == Dali::PathConstrainer::Property::CONTROL_POINTS )
185   {
186     const Property::Array* array = propertyValue.GetArray();
187     mPath->ClearControlPoints();
188     if( array )
189     {
190       for( Property::Array::SizeType i = 0, count = array->Count(); i < count; ++i )
191       {
192         Vector3 point;
193         array->GetElementAt( i ).Get( point );
194         mPath->AddControlPoint( point );
195       }
196     }
197   }
198 }
199
200 bool PathConstrainer::IsDefaultPropertyWritable(Property::Index index) const
201 {
202   if( index < DEFAULT_PROPERTY_COUNT )
203   {
204     return DEFAULT_PROPERTY_DETAILS[index].writable;
205   }
206
207   return false;
208 }
209
210 bool PathConstrainer::IsDefaultPropertyAnimatable(Property::Index index) const
211 {
212   if( index < DEFAULT_PROPERTY_COUNT )
213   {
214     return DEFAULT_PROPERTY_DETAILS[index].animatable;
215   }
216
217   return false;
218 }
219
220 bool PathConstrainer::IsDefaultPropertyAConstraintInput( Property::Index index ) const
221 {
222   if( index < DEFAULT_PROPERTY_COUNT )
223   {
224     return DEFAULT_PROPERTY_DETAILS[index].constraintInput;
225   }
226
227   return false;
228 }
229
230 void PathConstrainer::Apply( Property target, Property source, const Vector2& range, const Vector2& wrap)
231 {
232   Dali::Property::Type propertyType = target.object.GetPropertyType( target.propertyIndex);
233   if( propertyType == Dali::Property::VECTOR3)
234   {
235     // If property type is Vector3, constrain its value to the position of the path
236     Dali::Constraint constraint = Dali::Constraint::New<Vector3>( target.object, target.propertyIndex, PathConstraintFunctor( mPath, range, wrap ) );
237     constraint.AddSource( Dali::Source(source.object, source.propertyIndex ) );
238
239     constraint.SetTag( reinterpret_cast<size_t>( this ) );
240     constraint.SetRemoveAction( Dali::Constraint::Discard );
241     constraint.Apply();
242   }
243   else if( propertyType == Dali::Property::ROTATION )
244   {
245     // If property type is Rotation, constrain its value to align the forward vector to the tangent of the path
246     Dali::Constraint constraint = Dali::Constraint::New<Quaternion>( target.object, target.propertyIndex, PathConstraintFunctor( mPath, range, mForward, wrap) );
247     constraint.AddSource( Dali::Source(source.object, source.propertyIndex ) );
248
249     constraint.SetTag( reinterpret_cast<size_t>( this ) );
250     constraint.SetRemoveAction( Dali::Constraint::Discard );
251     constraint.Apply();
252   }
253
254   //Start observing the object
255   Observe( target.object );
256 }
257
258 } // Internal
259
260 } // Dali