1 #ifndef DALI_PROPERTY_CONSTRAINT_H
2 #define DALI_PROPERTY_CONSTRAINT_H
5 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
34 * A class for connecting properties to a constraint function.
36 template<typename PropertyType>
37 class PropertyConstraint
40 using ConstraintFunction = Dali::Constraint::Function<PropertyType>;
41 using InputContainer = std::vector<PropertyInputAccessor>;
42 using InputIndexerContainer = std::vector<PropertyInputIndexer<PropertyInputAccessor> >;
45 * Create a property constraint.
47 * @param[in] func A constraint function. Ownership of this callback-function is passed to this object.
49 PropertyConstraint(ConstraintFunction* func)
52 mInputsInitialized(false)
58 * @param [in] func A constraint function. Ownership of this callback-function is passed to this object.
59 * @param [in] inputs Property inputs.
61 PropertyConstraint(ConstraintFunction* func,
62 const InputContainer& inputs)
65 mInputsInitialized(false)
70 * Non virtual destructor.
78 * Clone a property constraint.
80 * @return The clone of the property-constraint.
82 * @note This function will create a copy of the stored constraint function for the clone.
84 PropertyConstraint<PropertyType>* Clone()
86 return new PropertyConstraint<PropertyType>(reinterpret_cast<ConstraintFunction*>(mFunction->Clone()), mInputs);
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.
94 void AddInput(const PropertyInputImpl* input, int32_t componentIndex)
96 mInputs.push_back(PropertyInputAccessor{input, componentIndex});
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.
104 const PropertyInputImpl* GetInput(uint32_t index) const
106 if(index < mInputs.size())
108 return mInputs[index].GetInput();
115 * Query whether all of the inputs have been initialized.
116 * @return True if all of the inputs have been initialized.
118 bool InputsInitialized()
120 if(!mInputsInitialized)
122 // Check whether the inputs are initialized yet
124 for(const PropertyInputImpl* input = GetInput(index);
126 input = GetInput(++index))
128 if(!input->InputInitialized())
134 // All inputs are now initialized
135 mInputsInitialized = true;
142 * Query whether any of the inputs have changed
143 * @return True if any of the inputs have changed.
148 for(const PropertyInputImpl* input = GetInput(index);
150 input = GetInput(++index))
152 if(input->InputChanged())
154 // At least one of the inputs has changed
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.
167 void Apply(BufferIndex bufferIndex, PropertyType& current)
169 InputIndexerContainer inputIndices;
170 PropertyInputContainer indices;
171 const uint32_t noOfInputs = static_cast<uint32_t>(mInputs.size());
173 inputIndices.reserve(noOfInputs);
174 indices.Reserve(noOfInputs);
176 const auto&& endIter = mInputs.end();
178 for(auto&& iter = mInputs.begin(); iter != endIter; ++iter, ++index)
180 DALI_ASSERT_DEBUG(nullptr != iter->GetInput());
181 inputIndices.push_back(PropertyInputIndexer<PropertyInputAccessor>(bufferIndex, &*iter));
182 indices.PushBack(&inputIndices[index]);
185 CallbackBase::Execute<PropertyType&, const PropertyInputContainer&>(*mFunction, current, indices);
190 PropertyConstraint() = delete;
191 PropertyConstraint(const PropertyConstraint&) = delete;
192 PropertyConstraint& operator=(const PropertyConstraint& rhs) = delete;
195 ConstraintFunction* mFunction;
196 InputContainer mInputs;
197 bool mInputsInitialized;
200 } // namespace Internal
204 #endif // DALI_PROPERTY_CONSTRAINT_H