Refactoring of path constraints + LinearConstrainer
[platform/core/uifw/dali-core.git] / dali / internal / event / animation / linear-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/linear-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
28 namespace Dali
29 {
30
31 namespace Internal
32 {
33
34 namespace
35 {
36
37 // Properties
38 //              Name            Type   writable animatable constraint-input  enum for index-checking
39 DALI_PROPERTY_TABLE_BEGIN
40 DALI_PROPERTY( "value",        ARRAY,     true,    false,       false,        Dali::LinearConstrainer::Property::VALUE )
41 DALI_PROPERTY( "progress",     ARRAY,     true,    false,       false,        Dali::LinearConstrainer::Property::PROGRESS )
42 DALI_PROPERTY_TABLE_END( DEFAULT_OBJECT_PROPERTY_START_INDEX )
43
44 } //Unnamed namespace
45
46 LinearConstrainer* LinearConstrainer::New()
47 {
48   return new LinearConstrainer();
49 }
50
51 LinearConstrainer::LinearConstrainer()
52 : Constrainer()
53 {
54 }
55
56 LinearConstrainer::~LinearConstrainer()
57 {
58 }
59
60 unsigned int LinearConstrainer::GetDefaultPropertyCount() const
61 {
62   return DEFAULT_PROPERTY_COUNT;
63 }
64
65 void LinearConstrainer::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
66 {
67   indices.reserve( DEFAULT_PROPERTY_COUNT );
68
69   for ( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
70   {
71     indices.push_back( i );
72   }
73 }
74
75 const char* LinearConstrainer::GetDefaultPropertyName(Property::Index index) const
76 {
77   if ( ( index >= 0 ) && ( index < DEFAULT_PROPERTY_COUNT ) )
78   {
79     return DEFAULT_PROPERTY_DETAILS[index].name;
80   }
81
82   // index out of range
83   return NULL;
84 }
85
86 Property::Index LinearConstrainer::GetDefaultPropertyIndex(const std::string& name) const
87 {
88   Property::Index index = Property::INVALID_INDEX;
89
90   // Look for name in default properties
91   for( int i = 0; i < DEFAULT_PROPERTY_COUNT; ++i )
92   {
93     const Internal::PropertyDetails* property = &DEFAULT_PROPERTY_DETAILS[ i ];
94     if( 0 == strcmp( name.c_str(), property->name ) )
95     {
96       index = i;
97       break;
98     }
99   }
100   return index;
101 }
102
103 Property::Type LinearConstrainer::GetDefaultPropertyType(Property::Index index) const
104 {
105   if( index < DEFAULT_PROPERTY_COUNT )
106   {
107     return DEFAULT_PROPERTY_DETAILS[index].type;
108   }
109
110   // index out of range
111   return Property::NONE;
112 }
113
114 Property::Value LinearConstrainer::GetDefaultProperty( Property::Index index ) const
115 {
116   Property::Value value;
117   if( index == Dali::LinearConstrainer::Property::VALUE )
118   {
119     Property::Array propertyArray;
120     value = Property::Value(propertyArray);
121     size_t count( mValue.Size() );
122     for( size_t i( 0 ); i != count; ++i )
123     {
124       value.AppendItem( mValue[i] );
125     }
126   }
127   else if( index == Dali::LinearConstrainer::Property::PROGRESS )
128   {
129     Property::Array propertyArray;
130     value = Property::Value(propertyArray);
131     size_t count( mValue.Size() );
132     for( size_t i( 0 ); i != count; ++i )
133     {
134       value.AppendItem( mProgress[i] );
135     }
136   }
137
138   return value;
139 }
140
141 void LinearConstrainer::SetDefaultProperty(Property::Index index, const Property::Value& propertyValue)
142 {
143   if( index == Dali::LinearConstrainer::Property::VALUE  )
144   {
145     Property::Array propertyArray;
146     propertyValue.Get(propertyArray);
147
148     size_t propertyArrayCount = propertyArray.size();
149     mValue.Resize( propertyArrayCount );
150     for( size_t i(0); i!=propertyArrayCount; ++i )
151     {
152       propertyArray[i].Get( mValue[i]);
153     }
154   }
155   else if( index == Dali::LinearConstrainer::Property::PROGRESS  )
156   {
157     Property::Array propertyArray;
158     propertyValue.Get(propertyArray);
159
160     size_t propertyArrayCount = propertyArray.size();
161     mProgress.Resize( propertyArrayCount );
162     for( size_t i(0); i!=propertyArrayCount; ++i )
163     {
164       propertyArray[i].Get( mProgress[i]);
165     }
166   }
167 }
168
169 bool LinearConstrainer::IsDefaultPropertyWritable(Property::Index index) const
170 {
171   if( index < DEFAULT_PROPERTY_COUNT )
172   {
173     return DEFAULT_PROPERTY_DETAILS[index].writable;
174   }
175
176   return false;
177 }
178
179 bool LinearConstrainer::IsDefaultPropertyAnimatable(Property::Index index) const
180 {
181   if( index < DEFAULT_PROPERTY_COUNT )
182   {
183     return DEFAULT_PROPERTY_DETAILS[index].animatable;
184   }
185
186   return false;
187 }
188
189 bool LinearConstrainer::IsDefaultPropertyAConstraintInput( Property::Index index ) const
190 {
191   if( index < DEFAULT_PROPERTY_COUNT )
192   {
193     return DEFAULT_PROPERTY_DETAILS[index].constraintInput;
194   }
195
196   return false;
197 }
198
199 void LinearConstrainer::Apply( Property target, Property source, const Vector2& range, const Vector2& wrap)
200 {
201   Dali::Constraint constraint = Dali::Constraint::New<float>( target.object, target.propertyIndex, LinearConstraintFunctor( mValue, mProgress, range, wrap ) );
202   constraint.AddSource( Dali::Source(source.object, source.propertyIndex ) );
203
204   constraint.SetTag( reinterpret_cast<size_t>( this ) );
205   constraint.SetRemoveAction( Dali::Constraint::Discard );
206   constraint.Apply();
207
208
209   //Start observing the object
210   Observe( target.object );
211 }
212
213 } // Internal
214
215 } // Dali