c357e414f295f83b076739efe22d85647afb5868
[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()
67 {
68 }
69
70 Property::Value LinearConstrainer::GetDefaultProperty( Property::Index index ) const
71 {
72   if( index == Dali::LinearConstrainer::Property::VALUE )
73   {
74     Property::Value value( Property::ARRAY );
75     Property::Array* array = value.GetArray();
76     uint32_t count = static_cast<uint32_t>( mValue.Size() );
77
78     if( array )
79     {
80       array->Reserve( count );
81       for( uint32_t i( 0 ); i != count; ++i )
82       {
83         array->PushBack( mValue[i] );
84       }
85     }
86     return value;
87   }
88   else if( index == Dali::LinearConstrainer::Property::PROGRESS )
89   {
90     Property::Value value( Property::ARRAY );
91     Property::Array* array = value.GetArray();
92     uint32_t count = static_cast<uint32_t>( mProgress.Size() );
93
94     if( array )
95     {
96       array->Reserve( count );
97       for( uint32_t i( 0 ); i != count; ++i )
98       {
99         array->PushBack( mProgress[i] );
100       }
101     }
102     return value;
103   }
104
105   return Property::Value();
106 }
107
108 Property::Value LinearConstrainer::GetDefaultPropertyCurrentValue( Property::Index index ) const
109 {
110   return GetDefaultProperty( index ); // Event-side only properties
111 }
112
113 void LinearConstrainer::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
114 {
115   const Property::Array* array = propertyValue.GetArray();
116   if( array )
117   {
118     uint32_t propertyArrayCount = static_cast<uint32_t>( array->Count() );
119     if( index == Dali::LinearConstrainer::Property::VALUE  )
120     {
121       mValue.Clear(); // remove old values
122       mValue.Resize( propertyArrayCount );
123       for( uint32_t i(0); i != propertyArrayCount; ++i )
124       {
125         array->GetElementAt( i ).Get( mValue[ i ] );
126       }
127     }
128     else if( index == Dali::LinearConstrainer::Property::PROGRESS  )
129     {
130       mProgress.Clear(); // remove old values
131       mProgress.Resize( propertyArrayCount );
132       for( uint32_t i(0); i != propertyArrayCount; ++i )
133       {
134         array->GetElementAt( i ).Get( mProgress[ i ] );
135       }
136     }
137   }
138 }
139
140 void LinearConstrainer::Apply( Property target, Property source, const Vector2& range, const Vector2& wrap)
141 {
142   Dali::Constraint constraint = Dali::Constraint::New<float>( target.object, target.propertyIndex, LinearConstraintFunctor( mValue, mProgress, range, wrap ) );
143   constraint.AddSource( Dali::Source(source.object, source.propertyIndex ) );
144
145   constraint.SetTag( static_cast<uint32_t>( reinterpret_cast<uintptr_t>( this ) ) ); // taking 32bits of this as tag
146   constraint.SetRemoveAction( Dali::Constraint::DISCARD );
147   constraint.Apply();
148
149   //Start observing the object
150   Observe( target.object );
151 }
152
153 } // Internal
154
155 } // Dali