Merge "Clean up the code to build successfully on macOS" into devel/master
[platform/core/uifw/dali-core.git] / dali / internal / event / animation / animator-connector-base.h
1 #ifndef DALI_INTERNAL_ANIMATOR_CONNECTOR_BASE_H
2 #define DALI_INTERNAL_ANIMATOR_CONNECTOR_BASE_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/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>
28
29
30 namespace Dali
31 {
32
33 namespace Internal
34 {
35
36 class Animation;
37
38 /**
39  * An abstract base class to create animator connectors and property re-setters for them when needed.
40  *
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
44  */
45 class AnimatorConnectorBase: public Object::Observer
46 {
47 public:
48
49   /**
50    * Constructor.
51    */
52   AnimatorConnectorBase(Object&           object,
53                         Property::Index   propertyIndex,
54                         int32_t           componentIndex,
55                         AlphaFunction     alpha,
56                         const TimePeriod& period)
57   : mObject(&object),
58     mAlphaFunction(alpha),
59     mTimePeriod(period),
60     mPropertyIndex(propertyIndex),
61     mComponentIndex(componentIndex)
62   {
63     object.AddObserver( *this );
64   }
65
66   /**
67    * Virtual destructor.
68    */
69   ~AnimatorConnectorBase() override
70   {
71     if( mObject )
72     {
73       mObject->RemoveObserver( *this );
74     }
75   }
76
77   /**
78    * Helper function to create a Scenegraph::Animator and PropertyResetter and add it to its correspondent
79    * SceneGraph::Animation.
80    *
81    * @note This function will only be called the first time the object is added to the scene or at creation time if
82    * the object was already in the scene
83    */
84   void CreateAnimator()
85   {
86     DALI_ASSERT_DEBUG( mAnimator == nullptr );
87     DALI_ASSERT_DEBUG( mParent != nullptr );
88
89     //Get the PropertyOwner the animator is going to animate
90     const SceneGraph::PropertyOwner& propertyOwner = mObject->GetSceneObject();
91
92     // Get SceneGraph::BaseProperty
93     const SceneGraph::PropertyBase* baseProperty = mObject->GetSceneObjectAnimatableProperty( mPropertyIndex );
94     DALI_ASSERT_ALWAYS( baseProperty && "Property is not animatable" );
95
96     // Check if property is a component of another property
97     const int32_t componentIndex = mObject->GetPropertyComponentIndex( mPropertyIndex );
98     if( componentIndex != Property::INVALID_COMPONENT_INDEX )
99     {
100       mComponentIndex = componentIndex;
101     }
102
103     // call the type specific method to create the concrete animator
104     bool resetterRequired = DoCreateAnimator( propertyOwner, *baseProperty );
105
106     DALI_ASSERT_DEBUG( mAnimator != nullptr );
107
108     // Add the new SceneGraph::Animator to its correspondent SceneGraph::Animation via message
109     const SceneGraph::Animation* animation = mParent->GetSceneObject();
110     DALI_ASSERT_DEBUG( nullptr != animation );
111     AddAnimatorMessage( mParent->GetEventThreadServices(), *animation, *mAnimator );
112
113     // Add the new SceneGraph::PropertyResetter to the update manager via message
114     if( resetterRequired )
115     {
116       OwnerPointer<SceneGraph::PropertyResetterBase> resetter = SceneGraph::AnimatorResetter::New( propertyOwner, *baseProperty, *mAnimator );
117       AddResetterMessage( mParent->GetEventThreadServices().GetUpdateManager(), resetter );
118     }
119   }
120
121   /**
122    * Type specific extension of animator creation
123    */
124   virtual bool DoCreateAnimator( const SceneGraph::PropertyOwner& propertyOwner, const SceneGraph::PropertyBase& baseProperty ) = 0;
125
126   /**
127    * Set the parent of the AnimatorConnector.
128    * @pre The connector does not already have a parent.
129    * @param [in] parent The parent object.
130    */
131   void SetParent( Animation& parent )
132   {
133     DALI_ASSERT_ALWAYS( mParent == nullptr && "AnimationConnector already has a parent" );
134     mParent = &parent;
135
136     if( mObject )
137     {
138       CreateAnimator();
139     }
140   }
141
142   /**
143    * Retrieve the parent of the AnimatorConnector.
144    * @return The parent object, or nullptr.
145    */
146   Animation* GetParent() const
147   {
148     return mParent;
149   }
150
151   Object* GetObject() const
152   {
153     return mObject;
154   }
155
156   Property::Index GetPropertyIndex() const
157   {
158     return mPropertyIndex;
159   }
160
161   int32_t GetComponentIndex() const
162   {
163     return mComponentIndex;
164   }
165
166 private:
167
168   /**
169    * From Object::Observer
170    */
171   void SceneObjectAdded( Object& object ) final
172   {
173     // If the animator has not been created yet, create it now.
174     if( !mAnimator && mObject )
175     {
176       CreateAnimator();
177     }
178   }
179
180   /**
181    * From Object::Observer
182    */
183   void SceneObjectRemoved( Object& object ) final
184   {
185   }
186
187   /**
188    * From Object::Observer
189    */
190   void ObjectDestroyed( Object& object ) override
191   {
192     mObject = nullptr;
193   }
194
195 protected:
196   Animation*                mParent{nullptr}; ///< The parent owns the connector.
197   Object*                   mObject;          ///< Not owned by the animator connector. Valid until ObjectDestroyed() is called.
198   SceneGraph::AnimatorBase* mAnimator{nullptr};
199
200   AlphaFunction mAlphaFunction;
201   TimePeriod    mTimePeriod;
202
203   Property::Index mPropertyIndex;
204   int32_t         mComponentIndex;
205 };
206
207 } // namespace Internal
208
209 } // namespace Dali
210
211 #endif // DALI_INTERNAL_ANIMATOR_CONNECTOR_BASE_H