[dali_1.2.43] Merge branch 'devel/master'
[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) 2017 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/common/dali-common.h>
23 #include <dali/public-api/signals/callback.h>
24 #include <dali/internal/event/animation/property-constraint-ptr.h>
25 #include <dali/internal/update/common/animatable-property.h>
26 #include <dali/internal/update/common/property-owner.h>
27 #include <dali/internal/update/animation/scene-graph-constraint-base.h>
28 #include <dali/internal/render/common/performance-monitor.h>
29
30 namespace Dali
31 {
32
33 namespace Internal
34 {
35
36 namespace SceneGraph
37 {
38
39 /**
40  * Used to constrain a property of a scene-object.
41  * The constraint function takes another scene-object property as an input.
42  */
43 template < class PropertyType, typename PropertyAccessorType >
44 class Constraint : public ConstraintBase
45 {
46 public:
47
48   typedef typename PropertyConstraintPtr< PropertyType >::Type ConstraintFunctionPtr;
49
50   /**
51    * Create a new scene-graph constraint.
52    * @param[in] targetProperty The target property.
53    * @param[in] ownerSet A set of property owners; func is connected to the properties provided by these objects.
54    * @param[in] func The function to calculate the final constrained value.
55    * @param[in] removeAction Remove action to perform when constraint is removed
56    * @return A smart-pointer to a newly allocated constraint.
57    */
58   static ConstraintBase* New( const PropertyBase& targetProperty,
59                               PropertyOwnerContainer& ownerContainer,
60                               ConstraintFunctionPtr func,
61                               RemoveAction removeAction )
62   {
63     // Scene-graph thread can edit these objects
64     PropertyBase& property = const_cast< PropertyBase& >( targetProperty );
65
66     return new Constraint< PropertyType, PropertyAccessorType >( property,
67                                                                  ownerContainer,
68                                                                  func,
69                                                                  removeAction );
70   }
71
72   /**
73    * Virtual destructor.
74    */
75   virtual ~Constraint()
76   {
77   }
78
79   /**
80    * @copydoc Dali::Internal::SceneGraph::ConstraintBase::Apply()
81    */
82   virtual void Apply( BufferIndex updateBufferIndex )
83   {
84     if ( mDisconnected )
85     {
86       return; // Early-out when property owners have been disconnected
87     }
88
89     if ( mFunc->InputsInitialized() )
90     {
91       PropertyType current = mTargetProperty.Get( updateBufferIndex );
92       mFunc->Apply( updateBufferIndex, current );
93
94       // Optionally bake the final value
95       if ( Dali::Constraint::Bake == mRemoveAction )
96       {
97         mTargetProperty.Bake( updateBufferIndex, current );
98       }
99       else
100       {
101         mTargetProperty.Set( updateBufferIndex, current );
102       }
103
104       INCREASE_COUNTER(PerformanceMonitor::CONSTRAINTS_APPLIED);
105     }
106     else
107     {
108       INCREASE_COUNTER(PerformanceMonitor::CONSTRAINTS_SKIPPED);
109     }
110   }
111
112 private:
113
114   /**
115    * @copydoc Dali::Internal::SceneGraph::Constraint::New()
116    */
117   Constraint( PropertyBase& targetProperty,
118               PropertyOwnerContainer& ownerContainer,
119               ConstraintFunctionPtr func,
120               RemoveAction removeAction )
121   : ConstraintBase( ownerContainer, removeAction ),
122     mTargetProperty( &targetProperty ),
123     mFunc( func )
124   {
125   }
126
127   // Undefined
128   Constraint( const Constraint& constraint );
129
130   // Undefined
131   Constraint& operator=( const Constraint& rhs );
132
133   /**
134    * @copydoc Dali::Internal::SceneGraph::ConstraintBase::OnDisconnect()
135    */
136   virtual void OnDisconnect()
137   {
138     // Discard target object/property pointers
139     mTargetProperty.Reset();
140     mFunc = NULL;
141   }
142
143 protected:
144
145   PropertyAccessorType mTargetProperty; ///< Raw-pointer to the target property. Not owned.
146
147   ConstraintFunctionPtr mFunc;
148 };
149
150 } // namespace SceneGraph
151
152 } // namespace Internal
153
154 } // namespace Dali
155
156 #endif // __DALI_INTERNAL_SCENE_GRAPH_CONSTRAINT_H__