1 #ifndef __DALI_PROPERTY_CONSTRAINT_H__
2 #define __DALI_PROPERTY_CONSTRAINT_H__
5 * Copyright (c) 2014 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 typedef std::vector < PropertyInputAccessor > InputContainer;
44 typedef typename InputContainer::iterator InputContainerIter;
45 typedef typename InputContainer::const_iterator InputContainerConstIter;
47 typedef std::vector< PropertyInputIndexer< PropertyInputAccessor > > InputIndexerContainer;
49 typedef Dali::Constraint::Function< PropertyType > ConstraintFunction;
52 * Create a property constraint.
54 * @param[in] func A constraint function. Ownership of this callback-function is passed to this object.
56 PropertyConstraint( Dali::Constraint::Function< PropertyType >* func )
57 : mInputsInitialized( false ),
65 * @param [in] func A constraint function. Ownership of this callback-function is passed to this object.
66 * @param [in] inputs Property inputs.
68 PropertyConstraint( Dali::Constraint::Function< PropertyType >* func,
69 const InputContainer& inputs )
70 : mInputsInitialized( false ),
77 * Non virtual destructor.
85 * Clone a property constraint.
87 * @return The clone of the property-constraint.
89 * @note This function will create a copy of the stored constraint function for the clone.
91 PropertyConstraint< PropertyType >* Clone()
93 return new PropertyConstraint< PropertyType >( reinterpret_cast< ConstraintFunction* >( mFunction->Clone() ), mInputs );
97 * Set the input for one of the property constraint parameters.
98 * @param [in] index The parameter index.
99 * @param [in] input The interface for receiving a property value.
101 void SetInput( unsigned int index, int componentIndex, const PropertyInputImpl& input )
103 if ( index >= mInputs.size() )
105 mInputs.push_back( PropertyInputAccessor() );
108 mInputs[ index ].SetInput( input, componentIndex );
112 * Retrieve the input for one of the property constraint parameters.
113 * @param [in] index The parameter index.
114 * @return The property input, or NULL if no input exists with this index.
116 const PropertyInputImpl* GetInput( unsigned int index ) const
118 if ( index < mInputs.size() )
120 return mInputs[ index ].GetInput();
127 * Query whether all of the inputs have been initialized.
128 * @return True if all of the inputs have been initialized.
130 bool InputsInitialized()
132 if ( !mInputsInitialized )
134 // Check whether the inputs are initialized yet
135 unsigned int index( 0u );
136 for ( const PropertyInputImpl* input = GetInput( index );
138 input = GetInput( ++index ) )
140 if ( !input->InputInitialized() )
146 // All inputs are now initialized
147 mInputsInitialized = true;
154 * Query whether any of the inputs have changed
155 * @return True if any of the inputs have changed.
159 unsigned int index( 0u );
160 for ( const PropertyInputImpl* input = GetInput( index );
162 input = GetInput( ++index ) )
164 if ( input->InputChanged() )
166 // At least one of the inputs has changed
175 * Apply the constraint.
176 * @param [in] bufferIndex The current update buffer index.
177 * @param [in,out] current The current property value, will be set to the constrained value upon return.
179 void Apply( BufferIndex bufferIndex, PropertyType& current )
181 InputIndexerContainer mInputIndices;
182 PropertyInputContainer mIndices;
183 const unsigned int noOfInputs = mInputs.size();
185 mInputIndices.reserve( noOfInputs );
186 mIndices.Reserve( noOfInputs );
188 const InputContainerConstIter endIter = mInputs.end();
189 unsigned int index = 0;
190 for ( InputContainerConstIter iter = mInputs.begin(); iter != endIter; ++iter, ++index )
192 DALI_ASSERT_DEBUG( NULL != iter->GetInput() );
193 mInputIndices.push_back( PropertyInputIndexer< PropertyInputAccessor >( bufferIndex, &*iter ) );
194 mIndices.PushBack( &mInputIndices[ index ] );
197 CallbackBase::Execute< PropertyType&, const PropertyInputContainer& >( *mFunction, current, mIndices );
203 PropertyConstraint( const PropertyConstraint& );
206 PropertyConstraint& operator=( const PropertyConstraint& rhs );
210 bool mInputsInitialized;
212 ConstraintFunction* mFunction;
214 InputContainer mInputs;
217 } // namespace Internal
221 #endif // __DALI_PROPERTY_CONSTRAINT_H__