Add Layout complex animation.
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / layouting / layout-transition-data-impl.h
1 #ifndef DALI_TOOLKIT_INTERNAL_LAYOUTING_LAYOUT_TRANSITION_DATA_IMPL_H
2 #define DALI_TOOLKIT_INTERNAL_LAYOUTING_LAYOUT_TRANSITION_DATA_IMPL_H
3 /*
4  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #include <memory>
20
21 #include <dali/public-api/common/intrusive-ptr.h>
22 #include <dali/public-api/object/base-object.h>
23 #include <dali/public-api/object/property-map.h>
24 #include <dali/public-api/object/type-registry.h>
25 #include <dali/public-api/actors/actor-enumerations.h>
26 #include <dali/public-api/animation/animation.h>
27
28 #include <dali-toolkit/devel-api/layouting/layout-item-impl.h>
29 #include <dali-toolkit/devel-api/layouting/layout-transition-data.h>
30
31 namespace Dali
32 {
33 namespace Toolkit
34 {
35 namespace Internal
36 {
37
38 struct LayoutTransition
39 {
40   LayoutTransition( LayoutItem& layoutItem, int layoutTransitionType )
41   : layoutItem( &layoutItem )
42   , layoutTransitionType( layoutTransitionType )
43   {
44   }
45
46   LayoutTransition()
47   : layoutTransitionType( -1 )
48   {
49   }
50
51   bool operator==( const LayoutTransition& rhs )
52   {
53     return ( ( layoutItem.Get() == rhs.layoutItem.Get() ) && layoutTransitionType == rhs.layoutTransitionType );
54   }
55
56   LayoutItemPtr layoutItem;
57   int layoutTransitionType;
58 };
59
60 const float DEFAULT_TRANSITION_DURATION( 0.5f );
61
62 struct LayoutDataAnimator
63 {
64    enum class AnimatorType
65    {
66      ANIMATE_TO,
67      ANIMATE_BY,
68      ANIMATE_BETWEEN,
69      ANIMATE_PATH
70    };
71
72    LayoutDataAnimator()
73    : animatorType( AnimatorType::ANIMATE_TO ),
74      alphaFunction( AlphaFunction::LINEAR ),
75      timePeriod( 0.0f, DEFAULT_TRANSITION_DURATION ),
76      interpolation( Animation::Linear )
77    {
78    }
79
80    std::string name;
81    AnimatorType animatorType;
82    AlphaFunction alphaFunction;
83    TimePeriod timePeriod;
84
85    KeyFrames keyFrames;
86    Animation::Interpolation interpolation;
87
88    Path path;
89    Vector3 forward;
90 };
91
92 using LayoutAnimatorArray = std::vector< LayoutDataAnimator >;
93
94 struct LayoutPositionData
95 {
96   LayoutPositionData( Handle handle, float left, float top, float right, float bottom, bool animated ) :
97       handle( handle ), left( left ), top( top ), right( right ), bottom( bottom ), animated( animated )
98   {
99   };
100
101   BaseHandle handle;
102   float left;
103   float top;
104   float right;
105   float bottom;
106   bool animated;
107 };
108
109 using LayoutPositionDataArray = std::vector< LayoutPositionData >;
110
111 struct LayoutDataElement
112 {
113   LayoutDataElement()
114   : propertyIndex( Property::INVALID_KEY ), animatorIndex( -1 ), positionDataIndex(-1 )
115   {
116   };
117
118   LayoutDataElement( Actor actor, Property::Index propertyIndex, Property::Value value )
119   : handle( actor ),
120     propertyIndex( propertyIndex ),
121     targetValue( value ),
122     animatorIndex( -1 ),
123     positionDataIndex( -1 )
124   {
125   };
126
127   BaseHandle handle;
128   Property::Index propertyIndex;
129   Property::Value initialValue;
130   Property::Value targetValue;
131   int animatorIndex;
132   int positionDataIndex;
133 };
134
135 class LayoutTransitionData;
136 using LayoutTransitionDataPtr = IntrusivePtr<LayoutTransitionData>;
137
138 /**
139  * LayoutTransitionData implementation class.
140  */
141 class DALI_TOOLKIT_API LayoutTransitionData : public BaseObject
142 {
143 public:
144   struct PropertyAnimator
145   {
146     PropertyAnimator();
147     PropertyAnimator( Actor actor, Property::Map map );
148     PropertyAnimator( Actor actor, Property::Map map, Path path, Vector3 forward );
149     PropertyAnimator( Actor actor, Property::Map map, KeyFrames keyFrames, Animation::Interpolation interpolation );
150
151     BaseHandle handle;
152
153     Property::Map map;
154
155     KeyFrames keyFrames;
156     Animation::Interpolation interpolation;
157
158     Path path;
159     Vector3 forward;
160   };
161
162   using PropertyAnimatorArray = std::vector< PropertyAnimator >;
163
164   static LayoutTransitionDataPtr New();
165
166   LayoutTransitionData( const LayoutTransitionData& ) = delete;
167   LayoutTransitionData& operator=( const LayoutTransitionData& ) = delete;
168
169   void AddPropertyAnimator( Actor actor, Property::Map map );
170   void AddPropertyAnimator( Actor actor, Property::Map map, KeyFrames keyFrames, Animation::Interpolation interpolation );
171   void AddPropertyAnimator( Actor actor, Property::Map map, Path path, Vector3 forward );
172
173   void ConvertToLayoutDataElements( Actor, LayoutData& layoutData );
174
175   /**
176    * @copydoc Dali::Animation::FinishedSignal()
177    */
178   Dali::Toolkit::LayoutTransitionData::LayoutTransitionSignalType& FinishedSignal();
179
180   void EmitSignalFinish( int layoutTransitionType );
181
182 private:
183   bool ConvertToLayoutAnimator( const Property::Map& animatorData, const PropertyAnimator& propertyAnimator, LayoutDataAnimator& layoutAnimator );
184   bool ConvertToLayoutDataElement( const PropertyAnimator& propertyAnimator, LayoutDataElement& layoutDataElement, LayoutData& layoutData );
185
186   PropertyAnimatorArray mPropertyAnimators;
187
188   /**
189    * Ref counted object - Only allow construction via New().
190    */
191   LayoutTransitionData();
192
193 protected:
194   /**
195    *  A ref counted object may only be deleted by calling Unreference
196    */
197   virtual ~LayoutTransitionData();
198
199   Dali::Toolkit::LayoutTransitionData::LayoutTransitionSignalType mFinishedSignal;
200 };
201
202 using PropertyAnimatorArray = std::vector< LayoutTransitionData::PropertyAnimator >;
203 using LayoutTransitionDataArray = std::vector< LayoutTransitionDataPtr >;
204 using LayoutDataArray = std::vector< LayoutDataElement >;
205
206 struct LayoutData
207 {
208   LayoutData( LayoutTransition& layoutTransition, LayoutPositionDataArray& layoutPositionDataArray, LayoutDataArray& layoutDataArray,
209       LayoutAnimatorArray& layoutAnimatorArray, PropertyAnimatorArray& childrenPropertyAnimators )
210   : speculativeLayout( false ),
211     layoutTransition( layoutTransition ),
212     layoutPositionDataArray( layoutPositionDataArray ),
213     layoutDataArray( layoutDataArray),
214     layoutAnimatorArray( layoutAnimatorArray ),
215     childrenPropertyAnimators( childrenPropertyAnimators )
216   {
217   };
218
219   bool speculativeLayout;
220   LayoutTransition& layoutTransition;
221   LayoutPositionDataArray& layoutPositionDataArray;
222   LayoutDataArray& layoutDataArray;
223   LayoutAnimatorArray& layoutAnimatorArray;
224   PropertyAnimatorArray& childrenPropertyAnimators;
225 };
226
227 } //namespace Internal
228
229 inline Internal::LayoutTransitionData& GetImplementation( Dali::Toolkit::LayoutTransitionData& handle )
230 {
231   DALI_ASSERT_ALWAYS( handle && "LayoutTransitionData handle is empty" );
232   BaseObject& object = handle.GetBaseObject();
233   return static_cast< Internal::LayoutTransitionData& >( object );
234 }
235
236 inline const Internal::LayoutTransitionData& GetImplementation( const Dali::Toolkit::LayoutTransitionData& handle )
237 {
238   DALI_ASSERT_ALWAYS( handle && "LayoutTransitionData handle is empty" );
239   const BaseObject& object = handle.GetBaseObject();
240   return static_cast< const Internal::LayoutTransitionData& >( object );
241 }
242
243 } //namespace Toolkit
244 } //namespace Dali
245
246 #endif // DALI_TOOLKIT_INTERNAL_LAYOUTING_LAYOUT_TRANSITION_DATA_IMPL_H