Remove a few exports by getting rid of two std::vectors and two std::sets
[platform/core/uifw/dali-core.git] / dali / internal / update / animation / scene-graph-constraint.h
1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_CONSTRAINT_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_CONSTRAINT_H__
3
4 /*
5  * Copyright (c) 2014 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 // EXTERNAL INCLUDES
22 #include <boost/function.hpp>
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/animation/active-constraint.h>
26 #include <dali/public-api/animation/alpha-functions.h>
27 #include <dali/public-api/common/dali-common.h>
28 #include <dali/internal/event/animation/property-constraint-ptr.h>
29 #include <dali/internal/update/common/animatable-property.h>
30 #include <dali/internal/update/common/property-owner.h>
31 #include <dali/internal/update/animation/scene-graph-constraint-base.h>
32 #include <dali/internal/render/common/performance-monitor.h>
33
34 namespace Dali
35 {
36
37 namespace Internal
38 {
39
40 namespace SceneGraph
41 {
42
43 /**
44  * Used to constrain a property of a scene-object.
45  * The constraint function takes another scene-object property as an input.
46  */
47 template < class PropertyType, typename PropertyAccessorType >
48 class Constraint : public ConstraintBase
49 {
50 public:
51
52   typedef typename PropertyConstraintPtr< PropertyType >::Type ConstraintFunctionPtr;
53   typedef boost::function< PropertyType (const PropertyType&, const PropertyType&, float progress) > InterpolatorFunc;
54
55   /**
56    * Create a new scene-graph constraint.
57    * @param[in] targetProperty The target property.
58    * @param[in] ownerSet A set of property owners; func is connected to the properties provided by these objects.
59    * @param[in] func The function to calculate the final constrained value.
60    * @param[in] interpolator The function to interpolate between start & final value.
61    * @param[in] customWeight A custom weight property, or NULL if the constraint is using its own.
62    * @return A smart-pointer to a newly allocated constraint.
63    */
64   static ConstraintBase* New( const PropertyBase& targetProperty,
65                               PropertyOwnerContainer& ownerContainer,
66                               ConstraintFunctionPtr func,
67                               InterpolatorFunc interpolator,
68                               const AnimatableProperty<float>* customWeight )
69   {
70     // Scene-graph thread can edit these objects
71     PropertyBase& property = const_cast< PropertyBase& >( targetProperty );
72
73     return new Constraint< PropertyType, PropertyAccessorType >( property,
74                                                                  ownerContainer,
75                                                                  func,
76                                                                  interpolator,
77                                                                  customWeight );
78   }
79
80   /**
81    * Virtual destructor.
82    */
83   virtual ~Constraint()
84   {
85   }
86
87   /**
88    * Query whether the constraint needs to be applied. If a constraint depends on a set of properties,
89    * then it should be applied when any of those properties have changed.
90    */
91   bool ApplyNeeded()
92   {
93     if ( mFirstApply )
94     {
95       mFirstApply = false;
96       return true;
97     }
98
99     if ( ! mTargetProperty.IsClean() ||
100            mFunc->InputsChanged()    ||
101          ! mWeightInput->IsClean() )
102     {
103       return true;
104     }
105
106     // We don't need to reapply constraint if none of the properties changed
107     return false;
108   }
109
110   /**
111    * @copydoc Dali::Internal::SceneGraph::ConstraintBase::Apply()
112    */
113   virtual void Apply( BufferIndex updateBufferIndex )
114   {
115     if ( mDisconnected )
116     {
117       return; // Early-out when property owners have been disconnected
118     }
119
120     bool firstApply( mFirstApply );
121
122     if ( mFunc->InputsInitialized() &&
123          ApplyNeeded() )
124     {
125       const PropertyType& current = mTargetProperty.Get( updateBufferIndex );
126
127       // FINAL_WEIGHT means the constraint is fully-applied, unless weight is still being animated
128       if ( ( ! firstApply && ! mWeightInput->IsClean() ) || // We should not rely on the flag state of weight-input on first apply
129            ! Equals( Dali::ActiveConstraint::FINAL_WEIGHT, (*mWeightInput)[updateBufferIndex] ) )
130       {
131         // Constraint is not fully-applied; interpolation between start & final values
132         mTargetProperty.Set( updateBufferIndex,
133                              mInterpolator( current, mFunc->Apply( updateBufferIndex, current ), (*mWeightInput)[updateBufferIndex] ) );
134       }
135       else
136       {
137         // Constraint is fully-applied; optionally bake the final value
138         if ( Dali::Constraint::Bake == mRemoveAction )
139         {
140           mTargetProperty.Bake( updateBufferIndex, mFunc->Apply( updateBufferIndex, current ) );
141         }
142         else
143         {
144           mTargetProperty.Set( updateBufferIndex, mFunc->Apply( updateBufferIndex, current ) );
145         }
146       }
147
148       INCREASE_COUNTER(PerformanceMonitor::CONSTRAINTS_APPLIED);
149     }
150     else
151     {
152       INCREASE_COUNTER(PerformanceMonitor::CONSTRAINTS_SKIPPED);
153     }
154   }
155
156 private:
157
158   /**
159    * @copydoc Dali::Internal::SceneGraph::Constraint::New()
160    */
161   Constraint( PropertyBase& targetProperty,
162               PropertyOwnerContainer& ownerContainer,
163               ConstraintFunctionPtr func,
164               InterpolatorFunc interpolator,
165               const AnimatableProperty<float>* customWeight )
166   : ConstraintBase( ownerContainer ),
167     mTargetProperty( &targetProperty ),
168     mFunc( func ),
169     mInterpolator( interpolator ),
170     mWeightInput( customWeight ? customWeight : &mWeight)
171   {
172   }
173
174   // Undefined
175   Constraint( const Constraint& constraint );
176
177   // Undefined
178   Constraint& operator=( const Constraint& rhs );
179
180   /**
181    * @copydoc Dali::Internal::SceneGraph::ConstraintBase::OnDisconnect()
182    */
183   virtual void OnDisconnect()
184   {
185     // Discard target object/property pointers
186     mTargetProperty.Reset();
187     mFunc = NULL;
188   }
189
190 protected:
191
192   PropertyAccessorType mTargetProperty; ///< Raw-pointer to the target property. Not owned.
193
194   ConstraintFunctionPtr mFunc;
195
196   InterpolatorFunc mInterpolator;
197
198   const AnimatableProperty<float>* mWeightInput;
199 };
200
201 } // namespace SceneGraph
202
203 } // namespace Internal
204
205 } // namespace Dali
206
207 #endif // __DALI_INTERNAL_SCENE_GRAPH_CONSTRAINT_H__