Merge "Remove std::vector from public api" into tizen
[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.PushBack( 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     value = Property::Value(Property::ARRAY);
120     size_t count( mValue.Size() );
121     for( size_t i( 0 ); i != count; ++i )
122     {
123       value.AppendItem( mValue[i] );
124     }
125   }
126   else if( index == Dali::LinearConstrainer::Property::PROGRESS )
127   {
128     value = Property::Value(Property::ARRAY);
129     size_t count( mValue.Size() );
130     for( size_t i( 0 ); i != count; ++i )
131     {
132       value.AppendItem( mProgress[i] );
133     }
134   }
135
136   return value;
137 }
138
139 void LinearConstrainer::SetDefaultProperty(Property::Index index, const Property::Value& propertyValue)
140 {
141   if( index == Dali::LinearConstrainer::Property::VALUE  )
142   {
143     size_t propertyArrayCount = propertyValue.GetSize();
144     mValue.Resize( propertyArrayCount );
145     for( size_t i(0); i != propertyArrayCount; ++i )
146     {
147       propertyValue.GetItem(i).Get( mValue[i] );
148     }
149   }
150   else if( index == Dali::LinearConstrainer::Property::PROGRESS  )
151   {
152     size_t propertyArrayCount = propertyValue.GetSize();
153     mProgress.Resize( propertyArrayCount );
154     for( size_t i(0); i != propertyArrayCount; ++i )
155     {
156       propertyValue.GetItem(i).Get( mProgress[i] );
157     }
158   }
159 }
160
161 bool LinearConstrainer::IsDefaultPropertyWritable(Property::Index index) const
162 {
163   if( index < DEFAULT_PROPERTY_COUNT )
164   {
165     return DEFAULT_PROPERTY_DETAILS[index].writable;
166   }
167
168   return false;
169 }
170
171 bool LinearConstrainer::IsDefaultPropertyAnimatable(Property::Index index) const
172 {
173   if( index < DEFAULT_PROPERTY_COUNT )
174   {
175     return DEFAULT_PROPERTY_DETAILS[index].animatable;
176   }
177
178   return false;
179 }
180
181 bool LinearConstrainer::IsDefaultPropertyAConstraintInput( Property::Index index ) const
182 {
183   if( index < DEFAULT_PROPERTY_COUNT )
184   {
185     return DEFAULT_PROPERTY_DETAILS[index].constraintInput;
186   }
187
188   return false;
189 }
190
191 void LinearConstrainer::Apply( Property target, Property source, const Vector2& range, const Vector2& wrap)
192 {
193   Dali::Constraint constraint = Dali::Constraint::New<float>( target.object, target.propertyIndex, LinearConstraintFunctor( mValue, mProgress, range, wrap ) );
194   constraint.AddSource( Dali::Source(source.object, source.propertyIndex ) );
195
196   constraint.SetTag( reinterpret_cast<size_t>( this ) );
197   constraint.SetRemoveAction( Dali::Constraint::Discard );
198   constraint.Apply();
199
200
201   //Start observing the object
202   Observe( target.object );
203 }
204
205 } // Internal
206
207 } // Dali