Add focus transition and other fixes.
[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, Actor gainedChild, Actor lostChild )
41   : layoutItem( &layoutItem )
42   , layoutTransitionType( layoutTransitionType )
43   , gainedChild( gainedChild )
44   , lostChild( lostChild )
45   {
46   }
47
48   LayoutTransition()
49   : layoutTransitionType( -1 )
50   {
51   }
52
53   bool operator==( const LayoutTransition& rhs )
54   {
55     return ( layoutItem.Get() == rhs.layoutItem.Get()
56         && layoutTransitionType == rhs.layoutTransitionType
57         && gainedChild == rhs.gainedChild
58         && lostChild == rhs.lostChild );
59   }
60
61   LayoutItemPtr layoutItem;
62   int layoutTransitionType;
63   Actor gainedChild;
64   Actor lostChild;
65 };
66
67 const float DEFAULT_TRANSITION_DURATION( 0.5f );
68
69 struct LayoutDataAnimator
70 {
71    enum class AnimatorType
72    {
73      ANIMATE_TO,
74      ANIMATE_BY,
75      ANIMATE_BETWEEN,
76      ANIMATE_PATH
77    };
78
79    LayoutDataAnimator()
80    : animatorType( AnimatorType::ANIMATE_TO ),
81      alphaFunction( AlphaFunction::LINEAR ),
82      timePeriod( 0.0f, DEFAULT_TRANSITION_DURATION ),
83      interpolation( Animation::Linear )
84    {
85    }
86
87    std::string name;
88    AnimatorType animatorType;
89    AlphaFunction alphaFunction;
90    TimePeriod timePeriod;
91
92    KeyFrames keyFrames;
93    Animation::Interpolation interpolation;
94
95    Path path;
96    Vector3 forward;
97 };
98
99 using LayoutAnimatorArray = std::vector< LayoutDataAnimator >;
100
101 struct LayoutPositionData
102 {
103   LayoutPositionData( Handle handle, float left, float top, float right, float bottom, bool animated ) :
104       handle( handle ), left( left ), top( top ), right( right ), bottom( bottom ), animated( animated )
105   {
106   };
107
108   BaseHandle handle;
109   float left;
110   float top;
111   float right;
112   float bottom;
113   bool animated;
114 };
115
116 using LayoutPositionDataArray = std::vector< LayoutPositionData >;
117
118 struct LayoutDataElement
119 {
120   LayoutDataElement()
121   : propertyIndex( Property::INVALID_KEY ),
122     animatorIndex( -1 ),
123     positionDataIndex(-1 ),
124     condition( Dali::Toolkit::LayoutTransitionData::Condition::NONE )
125   {
126   };
127
128   BaseHandle handle;
129   Property::Index propertyIndex;
130   Property::Value initialValue;
131   Property::Value targetValue;
132   int animatorIndex;
133   int positionDataIndex;
134   int condition;
135 };
136
137 class LayoutTransitionData;
138 using LayoutTransitionDataPtr = IntrusivePtr<LayoutTransitionData>;
139
140 /**
141  * LayoutTransitionData implementation class.
142  */
143 class DALI_TOOLKIT_API LayoutTransitionData : public BaseObject
144 {
145 public:
146   struct PropertyAnimator
147   {
148     PropertyAnimator();
149     PropertyAnimator( Actor actor, Property::Map map );
150     PropertyAnimator( Actor actor, Property::Map map, Path path, Vector3 forward );
151     PropertyAnimator( Actor actor, Property::Map map, KeyFrames keyFrames, Animation::Interpolation interpolation );
152
153     BaseHandle handle;
154
155     Property::Map map;
156
157     KeyFrames keyFrames;
158     Animation::Interpolation interpolation;
159
160     Path path;
161     Vector3 forward;
162   };
163
164   using PropertyAnimatorArray = std::vector< PropertyAnimator >;
165
166   static LayoutTransitionDataPtr New();
167
168   LayoutTransitionData( const LayoutTransitionData& ) = delete;
169   LayoutTransitionData& operator=( const LayoutTransitionData& ) = delete;
170
171   /**
172    * @brief Add a property animator for an actor in the transition
173    * @param[in] actor The actor
174    * @param[in] map The map containing the transition animator keys
175    *
176    * This will add the property animator to the list of animators related to this transition
177    */
178   void AddPropertyAnimator( Actor actor, Property::Map map );
179
180   /**
181    * @brief Add a property animator for an actor in the transition
182    * @param[in] actor The actor
183    * @param[in] map The map containing the transition animator keys
184    * @param[in] keyFrames The key frames used by the property animator
185    * @param[in] interpolation The interpolation used by the property animator
186    *
187    * This will add the property animator to the list of animators related to this transition
188    */
189   void AddPropertyAnimator( Actor actor, Property::Map map, KeyFrames keyFrames, Animation::Interpolation interpolation );
190
191   /**
192    * @brief Add a property animator for an actor in the transition
193    * @param[in] actor The actor
194    * @param[in] map The map containing the transition animator keys
195    * @param[in] path The path for the property animator
196    * @param[in] forward The forward vector for the property animator
197    *
198    * This will add the property animator to the list of animators related to this transition
199    */
200   void AddPropertyAnimator( Actor actor, Property::Map map, Path path, Vector3 forward );
201
202   /**
203    * @brief Convert the transition property animators to the layout data elements
204    * @param[in] actor The actor the transition property animators are applied to
205    * @param[in] layoutData The layout data containing layout data elements array for the layout update
206    *
207    * This will parse the property animators and add the layout data elements to the layout data elements array
208    */
209   void ConvertToLayoutDataElements( Actor, LayoutData& layoutData );
210
211   /**
212    * @brief Convert the transition children property animators to the layout data elements
213    * @param[in] actor The actor the transition property animators are applied to
214    * @param[in] layoutData The layout data containing layout data elements array for the layout update
215    *
216    * This will parse the children property animators and add the layout data elements to the layout data elements array
217    */
218   static void ConvertChildrenAnimatorsToLayoutDataElements( Actor, LayoutData& layoutData );
219
220   /**
221    * @copydoc Dali::Toolkit::LayoutTransitionData::FinishedSignal()
222    */
223   Dali::Toolkit::LayoutTransitionData::LayoutTransitionSignalType& FinishedSignal();
224
225   /**
226    * @brief Emit the transition finish signal
227    * @param[in] layoutTransitionType The transition type
228    */
229   void EmitSignalFinish( int layoutTransitionType );
230
231 private:
232   /**
233    * @brief Convert the property animator data to the layout data animator
234    * @param[in] animatorData The animator data map
235    * @param[in] propertyAnimator The property animator
236    * @param[in] layoutAnimator The layout animator
237    *
238    * This will parse the property animator data and add the layout data element animator
239    */
240   static bool ConvertToLayoutAnimator( const Property::Map& animatorData, const PropertyAnimator& propertyAnimator, LayoutDataAnimator& layoutAnimator );
241
242   /**
243    * @brief Convert the property animator to the layout data element
244    * @param[in] propertyAnimator The property animator
245    * @param[in] layoutDataElement The layout data element
246    * @param[in] layoutDataElement The layout data
247    *
248    * This will parse the children property animators and add the layout data elements to the layout data elements array
249    */
250   static bool ConvertToLayoutDataElement( const PropertyAnimator& propertyAnimator, LayoutDataElement& layoutDataElement, LayoutData& layoutData );
251
252   PropertyAnimatorArray mPropertyAnimators;
253
254   /**
255    * Ref counted object - Only allow construction via New().
256    */
257   LayoutTransitionData();
258
259 protected:
260   /**
261    *  A ref counted object may only be deleted by calling Unreference
262    */
263   virtual ~LayoutTransitionData();
264
265   Dali::Toolkit::LayoutTransitionData::LayoutTransitionSignalType mFinishedSignal;
266 };
267
268 using PropertyAnimatorArray = std::vector< LayoutTransitionData::PropertyAnimator >;
269 using LayoutTransitionDataArray = std::vector< LayoutTransitionDataPtr >;
270 using LayoutDataArray = std::vector< LayoutDataElement >;
271
272 struct LayoutData
273 {
274   LayoutData( LayoutTransition& layoutTransition, LayoutPositionDataArray& layoutPositionDataArray, LayoutDataArray& layoutDataArray,
275       LayoutAnimatorArray& layoutAnimatorArray, PropertyAnimatorArray& childrenPropertyAnimators )
276   : speculativeLayout( false ),
277     layoutTransition( layoutTransition ),
278     layoutPositionDataArray( layoutPositionDataArray ),
279     layoutDataArray( layoutDataArray),
280     layoutAnimatorArray( layoutAnimatorArray ),
281     childrenPropertyAnimators( childrenPropertyAnimators )
282   {
283   };
284
285   bool speculativeLayout;
286   LayoutTransition& layoutTransition;
287   LayoutPositionDataArray& layoutPositionDataArray;
288   LayoutDataArray& layoutDataArray;
289   LayoutAnimatorArray& layoutAnimatorArray;
290   PropertyAnimatorArray& childrenPropertyAnimators;
291 };
292
293 } //namespace Internal
294
295 inline Internal::LayoutTransitionData& GetImplementation( Dali::Toolkit::LayoutTransitionData& handle )
296 {
297   DALI_ASSERT_ALWAYS( handle && "LayoutTransitionData handle is empty" );
298   BaseObject& object = handle.GetBaseObject();
299   return static_cast< Internal::LayoutTransitionData& >( object );
300 }
301
302 inline const Internal::LayoutTransitionData& GetImplementation( const Dali::Toolkit::LayoutTransitionData& handle )
303 {
304   DALI_ASSERT_ALWAYS( handle && "LayoutTransitionData handle is empty" );
305   const BaseObject& object = handle.GetBaseObject();
306   return static_cast< const Internal::LayoutTransitionData& >( object );
307 }
308
309 } //namespace Toolkit
310 } //namespace Dali
311
312 #endif // DALI_TOOLKIT_INTERNAL_LAYOUTING_LAYOUT_TRANSITION_DATA_IMPL_H