1 #ifndef DALI_INTERNAL_ANIMATOR_CONNECTOR_BASE_H
2 #define DALI_INTERNAL_ANIMATOR_CONNECTOR_BASE_H
5 * Copyright (c) 2019 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/alpha-function.h>
23 #include <dali/public-api/animation/time-period.h>
24 #include <dali/public-api/common/dali-common.h>
25 #include <dali/internal/event/common/object-impl.h>
26 #include <dali/internal/update/common/property-resetter.h>
27 #include <dali/internal/update/manager/update-manager.h>
39 * An abstract base class to create animator connectors and property re-setters for them when needed.
41 * The scene-graph objects are created by a Object e.g. Actor is a proxy for SceneGraph::Node.
42 * AnimatorConnectorBase observes the proxy object, in order to detect when a scene-graph object is created
43 * to avoid having unnecessary animations on the scene-graph and allow apps to create animations in initialisation
45 class AnimatorConnectorBase: public Object::Observer
52 AnimatorConnectorBase(
54 Property::Index propertyIndex,
55 int32_t componentIndex,
56 Internal::AnimatorFunctionBase* animatorFunction,
58 const TimePeriod& period)
62 mAnimatorFunction( animatorFunction ),
63 mAlphaFunction(alpha),
65 mPropertyIndex( propertyIndex ),
66 mComponentIndex( componentIndex )
68 object.AddObserver( *this );
74 ~AnimatorConnectorBase() override
78 mObject->RemoveObserver( *this );
81 // If there is not a SceneGraph::Animator, the AnimatorConnector is responsible for deleting the mAnimatorFunction
82 // otherwise, the animator function ownership is transferred to the SceneGraph::Animator
85 delete mAnimatorFunction;
90 * Helper function to create a Scenegraph::Animator and PropertyResetter and add it to its correspondent
91 * SceneGraph::Animation.
93 * @note This function will only be called the first time the object is added to the scene or at creation time if
94 * the object was already in the scene
98 DALI_ASSERT_DEBUG( mAnimator == nullptr );
99 DALI_ASSERT_DEBUG( mAnimatorFunction != nullptr );
100 DALI_ASSERT_DEBUG( mParent != nullptr );
102 //Get the PropertyOwner the animator is going to animate
103 const SceneGraph::PropertyOwner& propertyOwner = mObject->GetSceneObject();
105 // Get SceneGraph::BaseProperty
106 const SceneGraph::PropertyBase* baseProperty = mObject->GetSceneObjectAnimatableProperty( mPropertyIndex );
107 DALI_ASSERT_ALWAYS( baseProperty && "Property is not animatable" );
109 // Check if property is a component of another property
110 const int32_t componentIndex = mObject->GetPropertyComponentIndex( mPropertyIndex );
111 if( componentIndex != Property::INVALID_COMPONENT_INDEX )
113 mComponentIndex = componentIndex;
116 // call the type specific method to create the concrete animator
117 bool resetterRequired = DoCreateAnimator( propertyOwner, *baseProperty );
119 DALI_ASSERT_DEBUG( mAnimator != nullptr );
121 // Add the new SceneGraph::Animator to its correspondent SceneGraph::Animation via message
122 const SceneGraph::Animation* animation = mParent->GetSceneObject();
123 DALI_ASSERT_DEBUG( nullptr != animation );
124 AddAnimatorMessage( mParent->GetEventThreadServices(), *animation, *mAnimator );
126 // Add the new SceneGraph::PropertyResetter to the update manager via message
127 if( resetterRequired )
129 OwnerPointer<SceneGraph::PropertyResetterBase> resetter = SceneGraph::AnimatorResetter::New( propertyOwner, *baseProperty, *mAnimator );
130 AddResetterMessage( mParent->GetEventThreadServices().GetUpdateManager(), resetter );
135 * Type specific extension of animator creation
137 virtual bool DoCreateAnimator( const SceneGraph::PropertyOwner& propertyOwner, const SceneGraph::PropertyBase& baseProperty ) = 0;
140 * Set the parent of the AnimatorConnector.
141 * @pre The connector does not already have a parent.
142 * @param [in] parent The parent object.
144 void SetParent( Animation& parent )
146 DALI_ASSERT_ALWAYS( mParent == nullptr && "AnimationConnector already has a parent" );
156 * Retrieve the parent of the AnimatorConnector.
157 * @return The parent object, or nullptr.
159 Animation* GetParent() const
164 Object* GetObject() const
169 Property::Index GetPropertyIndex() const
171 return mPropertyIndex;
174 int32_t GetComponentIndex() const
176 return mComponentIndex;
182 * From Object::Observer
184 void SceneObjectAdded( Object& object ) final
186 // If the animator has not been created yet, create it now.
187 if( !mAnimator && mObject )
194 * From Object::Observer
196 void SceneObjectRemoved( Object& object ) final
201 * From Object::Observer
203 void ObjectDestroyed( Object& object ) override
210 Animation* mParent; ///< The parent owns the connector.
211 Object* mObject; ///< Not owned by the animator connector. Valid until ObjectDestroyed() is called.
212 SceneGraph::AnimatorBase* mAnimator;
213 Internal::AnimatorFunctionBase* mAnimatorFunction; ///< Owned by the animator connector until an Scenegraph::Animator is created
215 AlphaFunction mAlphaFunction;
216 TimePeriod mTimePeriod;
218 Property::Index mPropertyIndex;
219 int32_t mComponentIndex;
223 } // namespace Internal
227 #endif // DALI_INTERNAL_ANIMATOR_CONNECTOR_BASE_H