1 #ifndef DALI_INTERNAL_SCENE_GRAPH_CONSTRAINT_H
2 #define DALI_INTERNAL_SCENE_GRAPH_CONSTRAINT_H
5 * Copyright (c) 2023 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/internal/event/animation/property-constraint-ptr.h>
23 #include <dali/internal/render/common/performance-monitor.h>
24 #include <dali/internal/update/animation/scene-graph-constraint-base.h>
25 #include <dali/internal/update/common/animatable-property.h>
26 #include <dali/internal/update/common/property-owner.h>
27 #include <dali/public-api/common/dali-common.h>
28 #include <dali/public-api/signals/callback.h>
37 * Used to constrain a property of a scene-object.
38 * The constraint function takes another scene-object property as an input.
40 template<class PropertyType, typename PropertyAccessorType>
41 class Constraint : public ConstraintBase
44 using ConstraintFunctionPtr = typename PropertyConstraintPtr<PropertyType>::Type;
47 * Create a new scene-graph constraint.
48 * @param[in] targetProperty The target property.
49 * @param[in] ownerSet A set of property owners; func is connected to the properties provided by these objects.
50 * @param[in] func The function to calculate the final constrained value.
51 * @param[in] removeAction Remove action to perform when constraint is removed
52 * @return A smart-pointer to a newly allocated constraint.
54 static ConstraintBase* New(const PropertyBase& targetProperty,
55 PropertyOwnerContainer& ownerContainer,
56 ConstraintFunctionPtr func,
57 RemoveAction removeAction)
59 // Scene-graph thread can edit these objects
60 PropertyBase& property = const_cast<PropertyBase&>(targetProperty);
62 return new Constraint<PropertyType, PropertyAccessorType>(property,
71 ~Constraint() override = default;
74 * @copydoc Dali::Internal::SceneGraph::ConstraintBase::Apply()
76 void Apply(BufferIndex updateBufferIndex) override
80 if(mFunc->InputsInitialized())
82 PropertyType current = mTargetProperty.Get(updateBufferIndex);
83 PropertyType old = mTargetProperty.Get(!updateBufferIndex);
85 mFunc->Apply(updateBufferIndex, current);
87 // Compare with value of the previous frame
88 if constexpr(std::is_same_v<PropertyType, float>)
90 if(!Equals(old, current))
92 if(!mObservedOwners.Empty())
94 // The first observer is the target of the constraint
95 mObservedOwners[0]->SetUpdated(true);
103 if(!mObservedOwners.Empty())
105 // The first observer is the target of the constraint
106 mObservedOwners[0]->SetUpdated(true);
111 // Optionally bake the final value
112 if(Dali::Constraint::BAKE == mRemoveAction)
114 mTargetProperty.Bake(updateBufferIndex, current);
118 mTargetProperty.Set(updateBufferIndex, current);
121 INCREASE_COUNTER(PerformanceMonitor::CONSTRAINTS_APPLIED);
125 INCREASE_COUNTER(PerformanceMonitor::CONSTRAINTS_SKIPPED);
132 * @copydoc Dali::Internal::SceneGraph::Constraint::New()
134 Constraint(PropertyBase& targetProperty,
135 PropertyOwnerContainer& ownerContainer,
136 ConstraintFunctionPtr func,
137 RemoveAction removeAction)
138 : ConstraintBase(ownerContainer, removeAction),
139 mTargetProperty(&targetProperty),
145 Constraint() = delete;
146 Constraint(const Constraint& constraint) = delete;
147 Constraint& operator=(const Constraint& rhs) = delete;
150 * @copydoc Dali::Internal::SceneGraph::ConstraintBase::OnDisconnect()
152 void OnDisconnect() override
154 // Discard target object/property pointers
155 mTargetProperty.Reset();
160 PropertyAccessorType mTargetProperty; ///< Raw-pointer to the target property. Not owned.
162 ConstraintFunctionPtr mFunc;
165 } // namespace SceneGraph
167 } // namespace Internal
171 #endif // DALI_INTERNAL_SCENE_GRAPH_CONSTRAINT_H