Purge underscored header file barriers
[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) 2019 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/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>
28
29 namespace Dali
30 {
31
32 namespace Internal
33 {
34
35 /**
36  * A class for connecting properties to a constraint function.
37  */
38 template < typename PropertyType >
39 class PropertyConstraint
40 {
41 public:
42
43   using ConstraintFunction = Dali::Constraint::Function< PropertyType >;
44   using InputContainer = std::vector < PropertyInputAccessor >;
45   using InputIndexerContainer = std::vector< PropertyInputIndexer< PropertyInputAccessor > >;
46
47   /**
48    * Create a property constraint.
49    *
50    * @param[in]  func  A constraint function. Ownership of this callback-function is passed to this object.
51    */
52   PropertyConstraint( ConstraintFunction* func )
53   : mFunction( func ),
54     mInputs(),
55     mInputsInitialized( false )
56   {
57   }
58
59   /**
60    * Constructor.
61    * @param [in]  func    A constraint function. Ownership of this callback-function is passed to this object.
62    * @param [in]  inputs  Property inputs.
63    */
64   PropertyConstraint( ConstraintFunction* func,
65                       const InputContainer& inputs )
66   : mFunction( func ),
67     mInputs( inputs ),
68     mInputsInitialized( false )
69   {
70   }
71
72   /**
73    * Non virtual destructor.
74    */
75   ~PropertyConstraint()
76   {
77     delete mFunction;
78   }
79
80   /**
81    * Clone a property constraint.
82    *
83    * @return The clone of the property-constraint.
84    *
85    * @note This function will create a copy of the stored constraint function for the clone.
86    */
87   PropertyConstraint< PropertyType >* Clone()
88   {
89     return new PropertyConstraint< PropertyType >( reinterpret_cast< ConstraintFunction* >( mFunction->Clone() ), mInputs );
90   }
91
92   /**
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.
96    */
97   void AddInput( const PropertyInputImpl* input, int32_t componentIndex )
98   {
99     mInputs.push_back( PropertyInputAccessor{ input, componentIndex } );
100   }
101
102   /**
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.
106    */
107   const PropertyInputImpl* GetInput( uint32_t index ) const
108   {
109     if ( index < mInputs.size() )
110     {
111       return mInputs[ index ].GetInput();
112     }
113
114     return nullptr;
115   }
116
117   /**
118    * Query whether all of the inputs have been initialized.
119    * @return True if all of the inputs have been initialized.
120    */
121   bool InputsInitialized()
122   {
123     if ( !mInputsInitialized )
124     {
125       // Check whether the inputs are initialized yet
126       uint32_t index( 0u );
127       for ( const PropertyInputImpl* input = GetInput( index );
128             nullptr != input;
129             input = GetInput( ++index ) )
130       {
131         if ( !input->InputInitialized() )
132         {
133           return false;
134         }
135       }
136
137       // All inputs are now initialized
138       mInputsInitialized = true;
139     }
140
141     return true;
142   }
143
144   /**
145    * Query whether any of the inputs have changed
146    * @return True if any of the inputs have changed.
147    */
148   bool InputsChanged()
149   {
150     uint32_t index( 0u );
151     for ( const PropertyInputImpl* input = GetInput( index );
152           nullptr != input;
153           input = GetInput( ++index ) )
154     {
155       if ( input->InputChanged() )
156       {
157         // At least one of the inputs has changed
158         return true;
159       }
160     }
161
162     return false;
163   }
164
165   /**
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.
169    */
170   void Apply( BufferIndex bufferIndex, PropertyType& current )
171   {
172     InputIndexerContainer inputIndices;
173     PropertyInputContainer indices;
174     const uint32_t noOfInputs = static_cast<uint32_t>( mInputs.size() );
175
176     inputIndices.reserve( noOfInputs );
177     indices.Reserve( noOfInputs );
178
179     const auto&& endIter = mInputs.end();
180     uint32_t index = 0;
181     for ( auto&& iter = mInputs.begin(); iter != endIter; ++iter, ++index )
182     {
183       DALI_ASSERT_DEBUG( nullptr != iter->GetInput() );
184       inputIndices.push_back( PropertyInputIndexer< PropertyInputAccessor >( bufferIndex, &*iter ) );
185       indices.PushBack( &inputIndices[ index ] );
186     }
187
188     CallbackBase::Execute< PropertyType&, const PropertyInputContainer& >( *mFunction, current, indices );
189   }
190
191 private:
192
193   // Undefined
194   PropertyConstraint() = delete;
195   PropertyConstraint( const PropertyConstraint& ) = delete;
196   PropertyConstraint& operator=( const PropertyConstraint& rhs ) = delete;
197
198 private:
199
200   ConstraintFunction* mFunction;
201   InputContainer mInputs;
202   bool mInputsInitialized;
203
204 };
205
206 } // namespace Internal
207
208 } // namespace Dali
209
210 #endif // DALI_PROPERTY_CONSTRAINT_H