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