Merge "Fix prevent issue - Unsigned compared against 0" into devel/master
[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 #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     DALI_ASSERT_DEBUG( array ); // should always exist in this case
123     size_t count( mValue.Size() );
124     array->Reserve( count );
125     for( size_t i( 0 ); i != count; ++i )
126     {
127       array->PushBack( mValue[i] );
128     }
129     return value;
130   }
131   else if( index == Dali::LinearConstrainer::Property::PROGRESS )
132   {
133     Property::Value value( Property::ARRAY );
134     Property::Array* array = value.GetArray();
135     DALI_ASSERT_DEBUG( array ); // should always exist in this case
136     size_t count( mValue.Size() );
137     array->Reserve( count );
138     for( size_t i( 0 ); i != count; ++i )
139     {
140       array->PushBack( mProgress[i] );
141     }
142     return value;
143   }
144
145   return Property::Value();
146 }
147
148 void LinearConstrainer::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
149 {
150   const Property::Array* array = propertyValue.GetArray();
151   if( array )
152   {
153     size_t propertyArrayCount = array->Count();
154     if( index == Dali::LinearConstrainer::Property::VALUE  )
155     {
156       mValue.Clear(); // remove old values
157       mValue.Resize( propertyArrayCount );
158       for( size_t i(0); i != propertyArrayCount; ++i )
159       {
160         array->GetElementAt( i ).Get( mValue[ i ] );
161       }
162     }
163     else if( index == Dali::LinearConstrainer::Property::PROGRESS  )
164     {
165       mProgress.Clear(); // remove old values
166       mProgress.Resize( propertyArrayCount );
167       for( size_t i(0); i != propertyArrayCount; ++i )
168       {
169         array->GetElementAt( i ).Get( mProgress[ i ] );
170       }
171     }
172   }
173 }
174
175 bool LinearConstrainer::IsDefaultPropertyWritable(Property::Index index) const
176 {
177   if( index < DEFAULT_PROPERTY_COUNT )
178   {
179     return DEFAULT_PROPERTY_DETAILS[index].writable;
180   }
181
182   return false;
183 }
184
185 bool LinearConstrainer::IsDefaultPropertyAnimatable(Property::Index index) const
186 {
187   if( index < DEFAULT_PROPERTY_COUNT )
188   {
189     return DEFAULT_PROPERTY_DETAILS[index].animatable;
190   }
191
192   return false;
193 }
194
195 bool LinearConstrainer::IsDefaultPropertyAConstraintInput( Property::Index index ) const
196 {
197   if( index < DEFAULT_PROPERTY_COUNT )
198   {
199     return DEFAULT_PROPERTY_DETAILS[index].constraintInput;
200   }
201
202   return false;
203 }
204
205 void LinearConstrainer::Apply( Property target, Property source, const Vector2& range, const Vector2& wrap)
206 {
207   Dali::Constraint constraint = Dali::Constraint::New<float>( target.object, target.propertyIndex, LinearConstraintFunctor( mValue, mProgress, range, wrap ) );
208   constraint.AddSource( Dali::Source(source.object, source.propertyIndex ) );
209
210   constraint.SetTag( reinterpret_cast<size_t>( this ) );
211   constraint.SetRemoveAction( Dali::Constraint::Discard );
212   constraint.Apply();
213
214
215   //Start observing the object
216   Observe( target.object );
217 }
218
219 } // Internal
220
221 } // Dali