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