Merge "Enable Property::LABEL visual of a button when CreateVisuals" into devel/master
[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/object/weak-handle.h>
26 #include <dali/public-api/actors/actor-enumerations.h>
27 #include <dali/public-api/animation/animation.h>
28
29 #include <dali-toolkit/devel-api/layouting/layout-item-impl.h>
30 #include <dali-toolkit/devel-api/layouting/layout-transition-data.h>
31
32 namespace Dali
33 {
34 namespace Toolkit
35 {
36 namespace Internal
37 {
38
39 struct LayoutTransition
40 {
41   LayoutTransition( LayoutItem& layoutItem, int layoutTransitionType, Actor gainedChild, Actor lostChild )
42   : layoutItem( &layoutItem )
43   , layoutTransitionType( layoutTransitionType )
44   , gainedChild( gainedChild )
45   , lostChild( lostChild )
46   {
47   }
48
49   LayoutTransition()
50   : layoutTransitionType( -1 )
51   {
52   }
53
54   bool operator==( const LayoutTransition& rhs )
55   {
56     return ( layoutItem.Get() == rhs.layoutItem.Get()
57         && layoutTransitionType == rhs.layoutTransitionType
58         && gainedChild == rhs.gainedChild
59         && lostChild == rhs.lostChild );
60   }
61
62   LayoutItemPtr layoutItem;
63   int layoutTransitionType;
64   WeakHandle<Actor> gainedChild;
65   WeakHandle<Actor> lostChild;
66 };
67
68 const float DEFAULT_TRANSITION_DURATION( 0.5f );
69
70 struct LayoutDataAnimator
71 {
72    LayoutDataAnimator()
73    : animatorType( Toolkit::LayoutTransitionData::Animator::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    Toolkit::LayoutTransitionData::Animator::Type 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( Actor handle, float left, float top, float right, float bottom, bool animated ) :
97       handle( handle ), left( left ), top( top ), right( right ), bottom( bottom ), animated( animated ), updateWithCurrentSize(false)
98   {
99   };
100
101   WeakHandle<Actor> handle;
102   float left;
103   float top;
104   float right;
105   float bottom;
106   bool animated;
107   bool updateWithCurrentSize;
108 };
109
110 using LayoutPositionDataArray = std::vector< LayoutPositionData >;
111
112 struct LayoutDataElement
113 {
114   LayoutDataElement()
115   : propertyIndex( Property::INVALID_KEY ),
116     animatorIndex( -1 ),
117     positionDataIndex(-1 ),
118     condition( Dali::Toolkit::LayoutTransitionData::Condition::NONE ),
119     updateMeasuredSize( false )
120   {
121   };
122
123   bool AdjustMeasuredSize( float& width, float& height, Toolkit::LayoutTransitionData::Animator::Type animatorType );
124   void UpdatePropertyIndex();
125   void UpdateAnimatorIndex( const LayoutAnimatorArray& animators );
126   void UpdatePositionDataIndex( LayoutData& layoutData );
127
128   WeakHandle<Actor> handle;
129   std::string propertyName;
130   Property::Index propertyIndex;
131   Property::Value initialValue;
132   Property::Value targetValue;
133   std::string animatorName;
134   int animatorIndex;
135   int positionDataIndex;
136   int condition;
137   bool updateMeasuredSize;
138 };
139
140 using LayoutDataArray = std::vector< LayoutDataElement >;
141
142 class LayoutTransitionData;
143 using LayoutTransitionDataPtr = IntrusivePtr<LayoutTransitionData>;
144
145 /**
146  * LayoutTransitionData implementation class.
147  */
148 class DALI_TOOLKIT_API LayoutTransitionData : public BaseObject
149 {
150 public:
151   struct PropertyAnimator
152   {
153     PropertyAnimator();
154     PropertyAnimator( Actor actor, Property::Map map );
155     PropertyAnimator( Actor actor, Property::Map map, Path path, Vector3 forward );
156     PropertyAnimator( Actor actor, Property::Map map, KeyFrames keyFrames, Animation::Interpolation interpolation );
157
158     WeakHandle<Actor> handle;
159
160     Property::Map map;
161
162     KeyFrames keyFrames;
163     Animation::Interpolation interpolation;
164
165     Path path;
166     Vector3 forward;
167   };
168   using PropertyAnimatorArray = std::vector< PropertyAnimator >;
169
170   static LayoutTransitionDataPtr New();
171
172   LayoutTransitionData( const LayoutTransitionData& ) = delete;
173   LayoutTransitionData& operator=( const LayoutTransitionData& ) = delete;
174
175   /**
176    * @brief Add a property animator for an actor in the transition
177    * @param[in] actor The actor
178    * @param[in] map The map containing the transition animator keys
179    *
180    * This will parse the property animator map and add the layout data element to the array of layout data elements related to this transition
181    */
182   void AddPropertyAnimator( Actor actor, Property::Map map );
183
184   /**
185    * @brief Add a property animator for an actor in the transition
186    * @param[in] actor The actor
187    * @param[in] map The map containing the transition animator keys
188    * @param[in] keyFrames The key frames used by the property animator
189    * @param[in] interpolation The interpolation used by the property animator
190    *
191    * This will parse the property animator map and add the layout data element to the array of layout data elements related to this transition
192    */
193   void AddPropertyAnimator( Actor actor, Property::Map map, KeyFrames keyFrames, Animation::Interpolation interpolation );
194
195   /**
196    * @brief Add a property animator for an actor in the transition
197    * @param[in] actor The actor
198    * @param[in] map The map containing the transition animator keys
199    * @param[in] path The path for the property animator
200    * @param[in] forward The forward vector for the property animator
201    *
202    * This will parse the property animator map and add the layout data element to the array of layout data elements related to this transition
203    */
204   void AddPropertyAnimator( Actor actor, Property::Map map, Path path, Vector3 forward );
205
206   /**
207    * @brief Collect the transition layout data elements
208    * @param[in] actor The actor the transition property animators are applied to
209    * @param[in] layoutData The layout data containing layout data elements array for the layout update
210    *
211    * This will copy the transition layout data elements to the layout data elements array
212    */
213   void CollectLayoutDataElements( Actor, LayoutData& layoutData );
214
215   /**
216    * @brief Collect the transition children layout data elements
217    * @param[in] actor The actor the transition property animators are applied to
218    * @param[in] layoutData The layout data containing layout data elements array for the layout update
219    *
220    * This will copy the children transition layout data elements to the layout data elements array
221    */
222   static void CollectChildrenLayoutDataElements( Actor, LayoutData& layoutData );
223
224   /**
225    * @copydoc Dali::Toolkit::LayoutTransitionData::FinishedSignal()
226    */
227   Dali::Toolkit::LayoutTransitionData::LayoutTransitionSignalType& FinishedSignal();
228
229   /**
230    * @brief Emit the transition finish signal
231    * @param[in] layoutTransitionType The transition type
232    */
233   void EmitSignalFinish( int layoutTransitionType );
234
235   /**
236    * @brief Check if one of the layout data elements has updateMeasuredSize flag set
237    */
238   bool HasUpdateMeasuredSize();
239
240 private:
241   /**
242    * @brief Convert the property animator data to the layout data animator
243    * @param[in] animatorData The animator data map
244    * @param[in] propertyAnimator The property animator
245    * @param[in] layoutAnimator The layout animator
246    *
247    * This will parse the property animator map and add the layout data element animator to the layout animators array
248    */
249   bool ConvertToLayoutAnimator( const Property::Map& animatorData, const PropertyAnimator& propertyAnimator, LayoutDataAnimator& layoutAnimator );
250
251   /**
252    * @brief Convert the property animator to the layout data element
253    * @param[in] propertyAnimator The property animator
254    * @param[in] layoutDataElement The layout data element
255    * @param[in] layoutDataElement The layout data
256    *
257    * This will parse the property animator map and add the layout data element to the layout data elements array
258    */
259   bool ConvertToLayoutDataElement( const PropertyAnimator& propertyAnimator, LayoutDataElement& layoutDataElement );
260
261   void UpdateAnimatorsIndices();
262
263   bool mUpdateMeasuredSize;
264   LayoutAnimatorArray mLayoutAnimators;
265   LayoutDataArray mLayoutDataElements;
266
267   /**
268    * Ref counted object - Only allow construction via New().
269    */
270   LayoutTransitionData();
271
272 protected:
273   /**
274    *  A ref counted object may only be deleted by calling Unreference
275    */
276   virtual ~LayoutTransitionData();
277
278   Dali::Toolkit::LayoutTransitionData::LayoutTransitionSignalType mFinishedSignal;
279 };
280
281 using PropertyAnimatorArray = std::vector< LayoutTransitionData::PropertyAnimator >;
282 using LayoutTransitionDataArray = std::vector< LayoutTransitionDataPtr >;
283 using LayoutDataArray = std::vector< LayoutDataElement >;
284
285 struct LayoutData
286 {
287   LayoutData( LayoutTransition& layoutTransition, LayoutPositionDataArray& layoutPositionDataArray, LayoutAnimatorArray& layoutAnimatorArray,
288       LayoutDataArray& layoutDataArray, LayoutDataArray& childrenLayoutDataArray )
289   : speculativeLayout( false ),
290     updateMeasuredSize( false ),
291     layoutTransition( layoutTransition ),
292     layoutPositionDataArray( layoutPositionDataArray ),
293     layoutAnimatorArray( layoutAnimatorArray ),
294     layoutDataArray( layoutDataArray),
295     childrenLayoutDataArray( childrenLayoutDataArray )
296   {
297   };
298
299   bool speculativeLayout;
300   bool updateMeasuredSize;
301   LayoutTransition& layoutTransition;
302   LayoutPositionDataArray& layoutPositionDataArray;
303   LayoutAnimatorArray& layoutAnimatorArray;
304   LayoutDataArray& layoutDataArray;
305   LayoutDataArray& childrenLayoutDataArray;
306 };
307
308 } //namespace Internal
309
310 inline Internal::LayoutTransitionData& GetImplementation( Dali::Toolkit::LayoutTransitionData& handle )
311 {
312   DALI_ASSERT_ALWAYS( handle && "LayoutTransitionData handle is empty" );
313   BaseObject& object = handle.GetBaseObject();
314   return static_cast< Internal::LayoutTransitionData& >( object );
315 }
316
317 inline const Internal::LayoutTransitionData& GetImplementation( const Dali::Toolkit::LayoutTransitionData& handle )
318 {
319   DALI_ASSERT_ALWAYS( handle && "LayoutTransitionData handle is empty" );
320   const BaseObject& object = handle.GetBaseObject();
321   return static_cast< const Internal::LayoutTransitionData& >( object );
322 }
323
324 } //namespace Toolkit
325 } //namespace Dali
326
327 #endif // DALI_TOOLKIT_INTERNAL_LAYOUTING_LAYOUT_TRANSITION_DATA_IMPL_H