1 #ifndef __DALI_PROPERTY_CONSTRAINT_H__
2 #define __DALI_PROPERTY_CONSTRAINT_H__
5 * Copyright (c) 2018 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/public-api/animation/constraint.h>
23 #include <dali/public-api/common/dali-vector.h>
24 #include <dali/public-api/common/vector-wrapper.h>
25 #include <dali/internal/event/animation/property-input-accessor.h>
26 #include <dali/internal/event/animation/property-input-indexer.h>
27 #include <dali/internal/event/common/property-input-impl.h>
36 * A class for connecting properties to a constraint function.
38 template < typename PropertyType >
39 class PropertyConstraint
43 using ConstraintFunction = Dali::Constraint::Function< PropertyType >;
44 using InputContainer = std::vector < PropertyInputAccessor >;
45 using InputIndexerContainer = std::vector< PropertyInputIndexer< PropertyInputAccessor > >;
48 * Create a property constraint.
50 * @param[in] func A constraint function. Ownership of this callback-function is passed to this object.
52 PropertyConstraint( ConstraintFunction* func )
55 mInputsInitialized( false )
61 * @param [in] func A constraint function. Ownership of this callback-function is passed to this object.
62 * @param [in] inputs Property inputs.
64 PropertyConstraint( ConstraintFunction* func,
65 const InputContainer& inputs )
68 mInputsInitialized( false )
73 * Non virtual destructor.
81 * Clone a property constraint.
83 * @return The clone of the property-constraint.
85 * @note This function will create a copy of the stored constraint function for the clone.
87 PropertyConstraint< PropertyType >* Clone()
89 return new PropertyConstraint< PropertyType >( reinterpret_cast< ConstraintFunction* >( mFunction->Clone() ), mInputs );
93 * Set the input for one of the property constraint parameters.
94 * @param [in] input The interface for receiving a property value.
95 * @param [in] componentIndex Component index.
97 void AddInput( const PropertyInputImpl* input, int32_t componentIndex )
99 mInputs.push_back( PropertyInputAccessor{ input, componentIndex } );
103 * Retrieve the input for one of the property constraint parameters.
104 * @param [in] index The parameter index.
105 * @return The property input, or nullptr if no input exists with this index.
107 const PropertyInputImpl* GetInput( uint32_t index ) const
109 if ( index < mInputs.size() )
111 return mInputs[ index ].GetInput();
118 * Query whether all of the inputs have been initialized.
119 * @return True if all of the inputs have been initialized.
121 bool InputsInitialized()
123 if ( !mInputsInitialized )
125 // Check whether the inputs are initialized yet
126 uint32_t index( 0u );
127 for ( const PropertyInputImpl* input = GetInput( index );
129 input = GetInput( ++index ) )
131 if ( !input->InputInitialized() )
137 // All inputs are now initialized
138 mInputsInitialized = true;
145 * Query whether any of the inputs have changed
146 * @return True if any of the inputs have changed.
150 uint32_t index( 0u );
151 for ( const PropertyInputImpl* input = GetInput( index );
153 input = GetInput( ++index ) )
155 if ( input->InputChanged() )
157 // At least one of the inputs has changed
166 * Apply the constraint.
167 * @param [in] bufferIndex The current update buffer index.
168 * @param [in,out] current The current property value, will be set to the constrained value upon return.
170 void Apply( BufferIndex bufferIndex, PropertyType& current )
172 InputIndexerContainer inputIndices;
173 PropertyInputContainer indices;
174 const uint32_t noOfInputs = static_cast<uint32_t>( mInputs.size() );
176 inputIndices.reserve( noOfInputs );
177 indices.Reserve( noOfInputs );
179 const auto&& endIter = mInputs.end();
181 for ( auto&& iter = mInputs.begin(); iter != endIter; ++iter, ++index )
183 DALI_ASSERT_DEBUG( nullptr != iter->GetInput() );
184 inputIndices.push_back( PropertyInputIndexer< PropertyInputAccessor >( bufferIndex, &*iter ) );
185 indices.PushBack( &inputIndices[ index ] );
188 CallbackBase::Execute< PropertyType&, const PropertyInputContainer& >( *mFunction, current, indices );
194 PropertyConstraint() = delete;
195 PropertyConstraint( const PropertyConstraint& ) = delete;
196 PropertyConstraint& operator=( const PropertyConstraint& rhs ) = delete;
200 ConstraintFunction* mFunction;
201 InputContainer mInputs;
202 bool mInputsInitialized;
206 } // namespace Internal
210 #endif // __DALI_PROPERTY_CONSTRAINT_H__