[Tizen] Not execute the remove callback
[platform/core/uifw/dali-core.git] / dali / internal / event / animation / property-constraint.h
1 #ifndef DALI_PROPERTY_CONSTRAINT_H
2 #define DALI_PROPERTY_CONSTRAINT_H
3
4 /*
5  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // INTERNAL INCLUDES
22 #include <dali/internal/event/animation/property-input-accessor.h>
23 #include <dali/internal/event/animation/property-input-indexer.h>
24 #include <dali/internal/event/common/property-input-impl.h>
25 #include <dali/public-api/animation/constraint.h>
26 #include <dali/public-api/common/dali-vector.h>
27 #include <dali/public-api/common/vector-wrapper.h>
28
29 namespace Dali
30 {
31 namespace Internal
32 {
33 /**
34  * A class for connecting properties to a constraint function.
35  */
36 template<typename PropertyType>
37 class PropertyConstraint
38 {
39 public:
40   using ConstraintFunction    = Dali::Constraint::Function<PropertyType>;
41   using InputContainer        = std::vector<PropertyInputAccessor>;
42   using InputIndexerContainer = std::vector<PropertyInputIndexer<PropertyInputAccessor> >;
43
44   /**
45    * Create a property constraint.
46    *
47    * @param[in]  func  A constraint function. Ownership of this callback-function is passed to this object.
48    */
49   PropertyConstraint(ConstraintFunction* func)
50   : mFunction(func),
51     mInputs(),
52     mInputsInitialized(false)
53   {
54   }
55
56   /**
57    * Constructor.
58    * @param [in]  func    A constraint function. Ownership of this callback-function is passed to this object.
59    * @param [in]  inputs  Property inputs.
60    */
61   PropertyConstraint(ConstraintFunction*   func,
62                      const InputContainer& inputs)
63   : mFunction(func),
64     mInputs(inputs),
65     mInputsInitialized(false)
66   {
67   }
68
69   /**
70    * Non virtual destructor.
71    */
72   ~PropertyConstraint()
73   {
74     delete mFunction;
75   }
76
77   /**
78    * Clone a property constraint.
79    *
80    * @return The clone of the property-constraint.
81    *
82    * @note This function will create a copy of the stored constraint function for the clone.
83    */
84   PropertyConstraint<PropertyType>* Clone()
85   {
86     return new PropertyConstraint<PropertyType>(reinterpret_cast<ConstraintFunction*>(mFunction->Clone()), mInputs);
87   }
88
89   /**
90    * Set the input for one of the property constraint parameters.
91    * @param [in] input The interface for receiving a property value.
92    * @param [in] componentIndex Component index.
93    */
94   void AddInput(const PropertyInputImpl* input, int32_t componentIndex)
95   {
96     mInputs.push_back(PropertyInputAccessor{input, componentIndex});
97   }
98
99   /**
100    * Retrieve the input for one of the property constraint parameters.
101    * @param [in] index The parameter index.
102    * @return The property input, or nullptr if no input exists with this index.
103    */
104   const PropertyInputImpl* GetInput(uint32_t index) const
105   {
106     if(index < mInputs.size())
107     {
108       return mInputs[index].GetInput();
109     }
110
111     return nullptr;
112   }
113
114   /**
115    * Query whether all of the inputs have been initialized.
116    * @return True if all of the inputs have been initialized.
117    */
118   bool InputsInitialized()
119   {
120     if(!mInputsInitialized)
121     {
122       // Check whether the inputs are initialized yet
123       uint32_t index(0u);
124       for(const PropertyInputImpl* input = GetInput(index);
125           nullptr != input;
126           input = GetInput(++index))
127       {
128         if(!input->InputInitialized())
129         {
130           return false;
131         }
132       }
133
134       // All inputs are now initialized
135       mInputsInitialized = true;
136     }
137
138     return true;
139   }
140
141   /**
142    * Query whether any of the inputs have changed
143    * @return True if any of the inputs have changed.
144    */
145   bool InputsChanged()
146   {
147     uint32_t index(0u);
148     for(const PropertyInputImpl* input = GetInput(index);
149         nullptr != input;
150         input = GetInput(++index))
151     {
152       if(input->InputChanged())
153       {
154         // At least one of the inputs has changed
155         return true;
156       }
157     }
158
159     return false;
160   }
161
162   /**
163    * Apply the constraint.
164    * @param [in] bufferIndex The current update buffer index.
165    * @param [in,out] current The current property value, will be set to the constrained value upon return.
166    */
167   void Apply(BufferIndex bufferIndex, PropertyType& current)
168   {
169     InputIndexerContainer  inputIndices;
170     PropertyInputContainer indices;
171     const uint32_t         noOfInputs = static_cast<uint32_t>(mInputs.size());
172
173     inputIndices.reserve(noOfInputs);
174     indices.Reserve(noOfInputs);
175
176     const auto&& endIter = mInputs.end();
177     uint32_t     index   = 0;
178     for(auto&& iter = mInputs.begin(); iter != endIter; ++iter, ++index)
179     {
180       DALI_ASSERT_DEBUG(nullptr != iter->GetInput());
181       inputIndices.push_back(PropertyInputIndexer<PropertyInputAccessor>(bufferIndex, &*iter));
182       indices.PushBack(&inputIndices[index]);
183     }
184
185     CallbackBase::Execute<PropertyType&, const PropertyInputContainer&>(*mFunction, current, indices);
186   }
187
188 private:
189   // Undefined
190   PropertyConstraint()                          = delete;
191   PropertyConstraint(const PropertyConstraint&) = delete;
192   PropertyConstraint& operator=(const PropertyConstraint& rhs) = delete;
193
194 private:
195   ConstraintFunction* mFunction;
196   InputContainer      mInputs;
197   bool                mInputsInitialized;
198 };
199
200 } // namespace Internal
201
202 } // namespace Dali
203
204 #endif // DALI_PROPERTY_CONSTRAINT_H