7e5111c55ff570ea145129790e568b51a097530d
[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( "control-points", 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   Property::Value value;
120   if( index == Dali::PathConstrainer::Property::FORWARD )
121   {
122     value = Property::Value( mForward );
123   }
124   else if( index == Dali::PathConstrainer::Property::POINTS )
125   {
126     value = Property::Value(Property::ARRAY);
127     const Dali::Vector<Vector3>& point = mPath->GetPoints();
128     size_t pointCount( point.Size() );
129     for( size_t i( 0 ); i != pointCount; ++i )
130     {
131       value.AppendItem( point[i] );
132     }
133   }
134   else if( index == Dali::PathConstrainer::Property::CONTROL_POINTS )
135   {
136     value = Property::Value(Property::ARRAY);
137     const Dali::Vector<Vector3>& point = mPath->GetControlPoints();
138     size_t pointCount( point.Size() );
139     for( size_t i( 0 ); i != pointCount; ++i )
140     {
141       value.AppendItem( point[i] );
142     }
143   }
144
145   return value;
146 }
147
148 void PathConstrainer::SetDefaultProperty(Property::Index index, const Property::Value& propertyValue)
149 {
150   if( index == Dali::PathConstrainer::Property::FORWARD )
151   {
152     propertyValue.Get(mForward);
153   }
154   else if( index == Dali::PathConstrainer::Property::POINTS  )
155   {
156     size_t propertyArrayCount = propertyValue.GetSize();
157     Dali::Vector<Vector3> point;
158     point.Resize( propertyArrayCount );
159     for( size_t i(0); i != propertyArrayCount; ++i )
160     {
161       propertyValue.GetItem(i).Get( point[i] );
162     }
163     mPath->SetPoints( point );
164   }
165   else if( index == Dali::PathConstrainer::Property::CONTROL_POINTS )
166   {
167     size_t propertyArrayCount = propertyValue.GetSize();
168     Dali::Vector<Vector3> point;
169     point.Resize( propertyArrayCount );
170     for( size_t i(0); i != propertyArrayCount; ++i )
171     {
172       propertyValue.GetItem(i).Get( point[i] );
173     }
174     mPath->SetControlPoints( point );
175   }
176 }
177
178 bool PathConstrainer::IsDefaultPropertyWritable(Property::Index index) const
179 {
180   if( index < DEFAULT_PROPERTY_COUNT )
181   {
182     return DEFAULT_PROPERTY_DETAILS[index].writable;
183   }
184
185   return false;
186 }
187
188 bool PathConstrainer::IsDefaultPropertyAnimatable(Property::Index index) const
189 {
190   if( index < DEFAULT_PROPERTY_COUNT )
191   {
192     return DEFAULT_PROPERTY_DETAILS[index].animatable;
193   }
194
195   return false;
196 }
197
198 bool PathConstrainer::IsDefaultPropertyAConstraintInput( Property::Index index ) const
199 {
200   if( index < DEFAULT_PROPERTY_COUNT )
201   {
202     return DEFAULT_PROPERTY_DETAILS[index].constraintInput;
203   }
204
205   return false;
206 }
207
208 void PathConstrainer::Apply( Property target, Property source, const Vector2& range, const Vector2& wrap)
209 {
210   Dali::Property::Type propertyType = target.object.GetPropertyType( target.propertyIndex);
211   if( propertyType == Dali::Property::VECTOR3)
212   {
213     // If property type is Vector3, constrain its value to the position of the path
214     Dali::Constraint constraint = Dali::Constraint::New<Vector3>( target.object, target.propertyIndex, PathConstraintFunctor( mPath, range, wrap ) );
215     constraint.AddSource( Dali::Source(source.object, source.propertyIndex ) );
216
217     constraint.SetTag( reinterpret_cast<size_t>( this ) );
218     constraint.SetRemoveAction( Dali::Constraint::Discard );
219     constraint.Apply();
220   }
221   else if( propertyType == Dali::Property::ROTATION )
222   {
223     // If property type is Rotation, constrain its value to align the forward vector to the tangent of the path
224     Dali::Constraint constraint = Dali::Constraint::New<Quaternion>( target.object, target.propertyIndex, PathConstraintFunctor( mPath, range, mForward, wrap) );
225     constraint.AddSource( Dali::Source(source.object, source.propertyIndex ) );
226
227     constraint.SetTag( reinterpret_cast<size_t>( this ) );
228     constraint.SetRemoveAction( Dali::Constraint::Discard );
229     constraint.Apply();
230   }
231
232   //Start observing the object
233   Observe( target.object );
234 }
235
236 } // Internal
237
238 } // Dali