Merge "Clean up the code to build successfully on macOS" into devel/master
[platform/core/uifw/dali-core.git] / dali / internal / event / animation / linear-constrainer-impl.cpp
1 /*
2  * Copyright (c) 2018 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/public-api/animation/constraint.h>
26 #include <dali/public-api/object/property-array.h>
27 #include <dali/public-api/object/type-registry.h>
28 #include <dali/internal/event/common/property-helper.h>
29
30
31 namespace Dali
32 {
33
34 namespace Internal
35 {
36
37 namespace
38 {
39
40 // Properties
41 //              Name            Type   writable animatable constraint-input  enum for index-checking
42 DALI_PROPERTY_TABLE_BEGIN
43 DALI_PROPERTY( "value",        ARRAY,     true,    false,       false,        Dali::LinearConstrainer::Property::VALUE )
44 DALI_PROPERTY( "progress",     ARRAY,     true,    false,       false,        Dali::LinearConstrainer::Property::PROGRESS )
45 DALI_PROPERTY_TABLE_END( DEFAULT_OBJECT_PROPERTY_START_INDEX, LinearConstrainerDefaultProperties )
46
47 BaseHandle Create()
48 {
49   return Dali::LinearConstrainer::New();
50 }
51
52 TypeRegistration mType( typeid( Dali::LinearConstrainer ), typeid( Dali::Handle ), Create, LinearConstrainerDefaultProperties );
53
54 } //Unnamed namespace
55
56 LinearConstrainer* LinearConstrainer::New()
57 {
58   return new LinearConstrainer();
59 }
60
61 LinearConstrainer::LinearConstrainer()
62 : Constrainer()
63 {
64 }
65
66 LinearConstrainer::~LinearConstrainer() = default;
67
68 Property::Value LinearConstrainer::GetDefaultProperty( Property::Index index ) const
69 {
70   if( index == Dali::LinearConstrainer::Property::VALUE )
71   {
72     Property::Value value( Property::ARRAY );
73     Property::Array* array = value.GetArray();
74     uint32_t count = static_cast<uint32_t>( mValue.Size() );
75
76     if( array )
77     {
78       array->Reserve( count );
79       for( uint32_t i( 0 ); i != count; ++i )
80       {
81         array->PushBack( mValue[i] );
82       }
83     }
84     return value;
85   }
86   else if( index == Dali::LinearConstrainer::Property::PROGRESS )
87   {
88     Property::Value value( Property::ARRAY );
89     Property::Array* array = value.GetArray();
90     uint32_t count = static_cast<uint32_t>( mProgress.Size() );
91
92     if( array )
93     {
94       array->Reserve( count );
95       for( uint32_t i( 0 ); i != count; ++i )
96       {
97         array->PushBack( mProgress[i] );
98       }
99     }
100     return value;
101   }
102
103   return Property::Value();
104 }
105
106 Property::Value LinearConstrainer::GetDefaultPropertyCurrentValue( Property::Index index ) const
107 {
108   return GetDefaultProperty( index ); // Event-side only properties
109 }
110
111 void LinearConstrainer::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
112 {
113   const Property::Array* array = propertyValue.GetArray();
114   if( array )
115   {
116     uint32_t propertyArrayCount = static_cast<uint32_t>( array->Count() );
117     if( index == Dali::LinearConstrainer::Property::VALUE  )
118     {
119       mValue.Clear(); // remove old values
120       mValue.Resize( propertyArrayCount );
121       for( uint32_t i(0); i != propertyArrayCount; ++i )
122       {
123         array->GetElementAt( i ).Get( mValue[ i ] );
124       }
125     }
126     else if( index == Dali::LinearConstrainer::Property::PROGRESS  )
127     {
128       mProgress.Clear(); // remove old values
129       mProgress.Resize( propertyArrayCount );
130       for( uint32_t i(0); i != propertyArrayCount; ++i )
131       {
132         array->GetElementAt( i ).Get( mProgress[ i ] );
133       }
134     }
135   }
136 }
137
138 void LinearConstrainer::Apply( Property target, Property source, const Vector2& range, const Vector2& wrap)
139 {
140   Dali::Constraint constraint = Dali::Constraint::New<float>( target.object, target.propertyIndex, LinearConstraintFunctor( mValue, mProgress, range, wrap ) );
141   constraint.AddSource( Dali::Source(source.object, source.propertyIndex ) );
142
143   constraint.SetTag( static_cast<uint32_t>( reinterpret_cast<uintptr_t>( this ) ) ); // taking 32bits of this as tag
144   constraint.SetRemoveAction( Dali::Constraint::DISCARD );
145   constraint.Apply();
146
147   //Start observing the object
148   Observe( target.object );
149 }
150
151 } // Internal
152
153 } // Dali