[NUI] Layouting API made public (#1026)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Layouting / LayoutTransition.cs
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 using System.Collections.Generic;
19 using System;
20
21 namespace Tizen.NUI
22 {
23     /// <summary>
24     /// Define a List of LayoutTransitions
25     /// </summary>
26     /// <since_tizen> 6 </since_tizen>
27     public class TransitionList : List<LayoutTransition> {}
28
29     /// <summary>
30     /// The conditions for transitions.
31     /// </summary>
32     /// <since_tizen> 6 </since_tizen>
33     [FlagsAttribute] public enum TransitionCondition
34     {
35         /// <summary>
36         /// Default when a condition has not been set.
37         /// </summary>
38         /// <since_tizen> 6 </since_tizen>
39         Unspecified = 0,
40         /// <summary>
41         /// Animate changing layout to another layout.
42         /// </summary>
43         /// <since_tizen> 6 </since_tizen>
44
45         LayoutChanged = 1,
46         /// <summary>
47         /// Animate adding item.
48         /// </summary>
49         /// <since_tizen> 6 </since_tizen>
50
51         Add = 2,
52         /// <summary>
53         /// Animate removing item.
54         /// </summary>
55         /// <since_tizen> 6 </since_tizen>
56
57         Remove = 4,
58         /// <summary>
59         /// Animation when an item changes due to a  being added.
60         /// </summary>
61         /// <since_tizen> 6 </since_tizen>
62
63         ChangeOnAdd = 8,
64         /// <summary>
65         /// Animation when an item changes due to a  being removed.
66         /// </summary>
67         /// <since_tizen> 6 </since_tizen>
68
69         ChangeOnRemove = 16
70     }
71
72     /// <summary>
73     /// The properties that can be animated.
74     /// </summary>
75     /// <since_tizen> 6 </since_tizen>
76     public enum AnimatableProperties
77     {
78         /// <summary>
79         /// Position property.
80         /// </summary>
81         /// <since_tizen> 6 </since_tizen>
82
83         Position,
84         /// <summary>
85         /// Size property.
86         /// </summary>
87         /// <since_tizen> 6 </since_tizen>
88
89         Size,
90         /// <summary>
91         /// Opacity property.
92         /// </summary>
93         /// <since_tizen> 6 </since_tizen>
94
95         Opacity
96     }
97
98     /// <summary>
99     /// Parts of the transition that can be configured to provide a custom effect.
100     /// </summary>
101     /// <since_tizen> 6 </since_tizen>
102     public class TransitionComponents
103     {
104         /// <summary>
105         /// TransitionComponents default constructor.
106         /// </summary>
107         /// <since_tizen> 6 </since_tizen>
108         public TransitionComponents()
109         {
110             Delay = 0;
111             Duration = 100;
112             AlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.Linear);
113         }
114
115         /// <summary>
116         /// TransitionComponents constructor. Stores delay, duration and AlphaFunction.
117         /// </summary>
118         /// <param name="delay">The delay before the animator starts.</param>
119         /// <param name="duration">the duration of the animator.</param>
120         /// <param name="alphaFunction">alpha function to use .</param>
121         /// <since_tizen> 6 </since_tizen>
122
123         public TransitionComponents(int delay, int duration, AlphaFunction alphaFunction)
124         {
125             Delay = delay;
126             Duration = duration;
127             AlphaFunction = alphaFunction;
128         }
129
130         /// <summary>
131         /// Get, Set the time transition should execute for . Milliseconds.
132         /// </summary>
133         /// <since_tizen> 6 </since_tizen>
134         public int Duration;
135         /// <summary>
136         /// Get, Set the delay before the transition executes. Milliseconds.
137         /// </summary>
138         /// <since_tizen> 6 </since_tizen>
139         public int Delay;
140         /// <summary>
141         /// Get, Set the function to alter the transition path over time.
142         /// </summary>
143         /// <since_tizen> 6 </since_tizen>
144         public AlphaFunction AlphaFunction;
145     }
146
147     /// <summary>
148     /// LayoutTransition stores the animation setting for a transition conidition.
149     /// </summary>
150     public class LayoutTransition
151     {
152         /// <summary>
153         /// LayoutTransition default constructor.
154         /// </summary>
155         /// <since_tizen> 6 </since_tizen>
156         public LayoutTransition()
157         {
158           Condition = TransitionCondition.Unspecified;
159           AnimatableProperty = AnimatableProperties.Position;
160           Animator = null;
161           TargetValue = 0;
162         }
163         /// <summary>
164         /// LayoutTransition constructor.
165         /// </summary>
166         /// <param name="condition">The animatable condition.</param>
167         /// <param name="animatableProperty">the property to animate.</param>
168         /// <param name="targetValue">target value of the property.</param>
169         /// <param name="animator">Components to define the animator.</param>
170         /// <since_tizen> 6 </since_tizen>
171         public LayoutTransition( TransitionCondition condition,
172                                  AnimatableProperties animatableProperty,
173                                  object targetValue,
174                                  TransitionComponents animator)
175         {
176             Condition = condition;
177             AnimatableProperty = animatableProperty;
178             Animator = animator;
179             TargetValue = targetValue;
180         }
181
182         /// <summary>
183         /// Condition for this Transition
184         /// </summary>
185         /// <since_tizen> 6 </since_tizen>
186
187         public TransitionCondition Condition{get; set;}
188         /// <summary>
189         /// Property to animate.
190         /// </summary>
191         /// <since_tizen> 6 </since_tizen>
192
193         public AnimatableProperties AnimatableProperty{get; set;}
194         /// <summary>
195         /// Components of the Animator.
196         /// </summary>
197         /// <since_tizen> 6 </since_tizen>
198
199         public TransitionComponents Animator{get; set;}
200         /// <summary>
201         /// Target value to animate to.
202         /// </summary>
203         /// <since_tizen> 6 </since_tizen>
204
205         public object TargetValue{get; set;}
206     }
207
208
209     /// <summary>
210     /// Class to help manage the adding and updating of transitions.
211     /// </summary>
212     internal static class LayoutTransitionsHelper
213     {
214         /// <summary>
215         /// Adds the given transition and condition to a transition list.
216         /// </summary>
217         /// <param name="targetTransitionList">The list to add the transition to.</param>
218         /// <param name="condition">Condition for the transition.</param>
219         /// <param name="transition">The transition to add.</param>
220         /// <param name="explicitlySet">True is set explicitly, false if inherited.</param>
221         static public void AddTransitionForCondition(
222                               Dictionary<TransitionCondition, TransitionList> targetTransitionList,
223                               TransitionCondition condition,
224                               LayoutTransition transition,
225                               bool explicitlySet)
226         {
227             bool replaced = false;
228             bool conditionNotInDictionary = false;
229             TransitionList transitionListMatchingCondition;
230             if (targetTransitionList.TryGetValue(condition, out transitionListMatchingCondition))
231             {
232                 for (var index=0; index < transitionListMatchingCondition?.Count; index++ )
233                 {
234                     if (transitionListMatchingCondition[index].AnimatableProperty == transition.AnimatableProperty)
235                     {
236                         if (explicitlySet)
237                         {
238                             transitionListMatchingCondition[index] = transition;
239                             replaced = true;
240                             continue;
241                         }
242                     }
243                 }
244             }
245             else
246             {
247                 conditionNotInDictionary = true;
248             }
249
250             if (replaced == false)
251             {
252                 if (transitionListMatchingCondition == null)
253                 {
254                     transitionListMatchingCondition = new TransitionList();
255                 }
256                 transitionListMatchingCondition.Add(transition);
257                 // Update dictionary with new or replaced entry.
258                 if (conditionNotInDictionary)
259                 {
260                     targetTransitionList.Add(condition, transitionListMatchingCondition); // new entry
261                 }
262                 else
263                 {
264                     targetTransitionList[condition] = transitionListMatchingCondition; // replaced
265                 }
266             }
267         }
268
269         /// <summary>
270         /// Retreive the transition list for the given condition.
271         /// </summary>
272         /// <param name="sourceTransitionCollection">The source collection of transition lists to retrieve.</param>
273         /// <param name="condition">Condition for the transition.</param>
274         /// <param name="transitionsForCondition">transition list to return as out parameter.</param>
275         /// <returns>True if a transition list found for the given condition></returns>
276         static public bool GetTransitionsListForCondition(
277                               Dictionary<TransitionCondition, TransitionList> sourceTransitionCollection,
278                               TransitionCondition condition,
279                               TransitionList transitionsForCondition )
280         {
281             TransitionCondition resolvedCondition = condition;
282             bool matched = false;
283             // LayoutChanged transitions overrides ChangeOnAdd and ChangeOnRemove as siblings will
284             // reposition to the new layout not to the insertion/removal of a sibling.
285             if ((condition & TransitionCondition.LayoutChanged) == TransitionCondition.LayoutChanged)
286             {
287                 resolvedCondition = TransitionCondition.LayoutChanged;
288             }
289
290             if (sourceTransitionCollection.TryGetValue(resolvedCondition, out transitionsForCondition))
291             {
292                 matched = true;
293             }
294
295             return matched;
296         }
297
298         /// <summary>
299         /// Copy the transitions in the source list to the target list
300         /// </summary>
301         /// <param name="sourceTransitionList">The source transition list.</param>
302         /// <param name="targetTransitionList">The target transition list to copy to.</param>
303         static public void CopyTransitions( TransitionList sourceTransitionList,
304                                             TransitionList targetTransitionList )
305         {
306             targetTransitionList.Clear();
307             foreach (LayoutTransition transitionToApply in sourceTransitionList)
308             {
309                 // Overwite existing transitions
310                 targetTransitionList.Add(transitionToApply);
311             }
312
313         }
314     }
315
316 } // namespace Tizen.NUI