f86584881d46bfe0eb4a78e4f7e78d37b5605694
[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 using System.ComponentModel;
21
22 namespace Tizen.NUI
23 {
24     /// <summary>
25     /// Define a List of LayoutTransitions
26     /// </summary>
27     /// <since_tizen> 6 </since_tizen>
28     public class TransitionList : List<LayoutTransition> { }
29
30     /// <summary>
31     /// The conditions for transitions.
32     /// </summary>
33     /// <since_tizen> 6 </since_tizen>
34     [FlagsAttribute]
35     public enum TransitionCondition
36     {
37         /// <summary>
38         /// Default when a condition has not been set.
39         /// </summary>
40         /// <since_tizen> 6 </since_tizen>
41         Unspecified = 0,
42         /// <summary>
43         /// Animate changing layout to another layout.
44         /// </summary>
45         /// <since_tizen> 6 </since_tizen>
46
47         LayoutChanged = 1,
48         /// <summary>
49         /// Animate adding item.
50         /// </summary>
51         /// <since_tizen> 6 </since_tizen>
52
53         Add = 2,
54         /// <summary>
55         /// Animate removing item.
56         /// </summary>
57         /// <since_tizen> 6 </since_tizen>
58
59         Remove = 4,
60         /// <summary>
61         /// Animation when an item changes due to a  being added.
62         /// </summary>
63         /// <since_tizen> 6 </since_tizen>
64
65         ChangeOnAdd = 8,
66         /// <summary>
67         /// Animation when an item changes due to a  being removed.
68         /// </summary>
69         /// <since_tizen> 6 </since_tizen>
70
71         ChangeOnRemove = 16
72     }
73
74     /// <summary>
75     /// The properties that can be animated.
76     /// </summary>
77     /// <since_tizen> 6 </since_tizen>
78     [System.Diagnostics.CodeAnalysis.SuppressMessage("Naming", "CA1717:Only FlagsAttribute enums should have plural names")]
79     public enum AnimatableProperties
80     {
81         /// <summary>
82         /// Position property.
83         /// </summary>
84         /// <since_tizen> 6 </since_tizen>
85
86         Position,
87         /// <summary>
88         /// Size property.
89         /// </summary>
90         /// <since_tizen> 6 </since_tizen>
91
92         Size,
93         /// <summary>
94         /// Opacity property.
95         /// </summary>
96         /// <since_tizen> 6 </since_tizen>
97
98         Opacity
99     }
100
101     /// <summary>
102     /// Parts of the transition that can be configured to provide a custom effect.
103     /// </summary>
104     /// <since_tizen> 6 </since_tizen>
105     public class TransitionComponents : IDisposable
106     {
107         private bool disposed = false;
108         /// <summary>
109         /// TransitionComponents default constructor.
110         /// </summary>
111         /// <since_tizen> 6 </since_tizen>
112         public TransitionComponents()
113         {
114             Delay = 0;
115             Duration = 100;
116             AlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.Linear);
117         }
118
119         /// <summary>
120         /// TransitionComponents constructor. Stores delay, duration and AlphaFunction.
121         /// </summary>
122         /// <param name="delay">The delay before the animator starts.</param>
123         /// <param name="duration">the duration of the animator.</param>
124         /// <param name="alphaFunction">alpha function to use .</param>
125         /// <since_tizen> 6 </since_tizen>
126
127         public TransitionComponents(int delay, int duration, AlphaFunction alphaFunction)
128         {
129             Delay = delay;
130             Duration = duration;
131             AlphaFunction = alphaFunction;
132         }
133
134         /// <summary>
135         /// Get, Set the time transition should execute for . Milliseconds.
136         /// </summary>
137         /// <since_tizen> 6 </since_tizen>
138         public int Duration;
139         /// <summary>
140         /// Get, Set the delay before the transition executes. Milliseconds.
141         /// </summary>
142         /// <since_tizen> 6 </since_tizen>
143         public int Delay;
144         /// <summary>
145         /// Get, Set the function to alter the transition path over time.
146         /// </summary>
147         /// <since_tizen> 6 </since_tizen>
148         public AlphaFunction AlphaFunction;
149
150
151         [EditorBrowsable(EditorBrowsableState.Never)]
152         protected virtual void Dispose(bool disposing)
153         {
154             if (disposed)
155             {
156                 return;
157             }
158             if (disposing)
159             {
160                 AlphaFunction?.Dispose();
161             }
162             disposed = true;
163         }
164
165         [EditorBrowsable(EditorBrowsableState.Never)]
166         public void Dispose()
167         {
168             Dispose(true);
169             global::System.GC.SuppressFinalize(this);
170         }
171     }
172
173     /// <summary>
174     /// LayoutTransition stores the animation setting for a transition conidition.
175     /// </summary>
176     public class LayoutTransition
177     {
178         /// <summary>
179         /// LayoutTransition default constructor.
180         /// </summary>
181         /// <since_tizen> 6 </since_tizen>
182         public LayoutTransition()
183         {
184             Condition = TransitionCondition.Unspecified;
185             AnimatableProperty = AnimatableProperties.Position;
186             Animator = null;
187             TargetValue = 0;
188         }
189         /// <summary>
190         /// LayoutTransition constructor.
191         /// </summary>
192         /// <param name="condition">The animatable condition.</param>
193         /// <param name="animatableProperty">the property to animate.</param>
194         /// <param name="targetValue">target value of the property.</param>
195         /// <param name="animator">Components to define the animator.</param>
196         /// <since_tizen> 6 </since_tizen>
197         public LayoutTransition(TransitionCondition condition,
198                                  AnimatableProperties animatableProperty,
199                                  object targetValue,
200                                  TransitionComponents animator)
201         {
202             Condition = condition;
203             AnimatableProperty = animatableProperty;
204             Animator = animator;
205             TargetValue = targetValue;
206         }
207
208         /// <summary>
209         /// Condition for this Transition
210         /// </summary>
211         /// <since_tizen> 6 </since_tizen>
212
213         public TransitionCondition Condition { get; set; }
214         /// <summary>
215         /// Property to animate.
216         /// </summary>
217         /// <since_tizen> 6 </since_tizen>
218
219         public AnimatableProperties AnimatableProperty { get; set; }
220         /// <summary>
221         /// Components of the Animator.
222         /// </summary>
223         /// <since_tizen> 6 </since_tizen>
224
225         public TransitionComponents Animator { get; set; }
226         /// <summary>
227         /// Target value to animate to.
228         /// </summary>
229         /// <since_tizen> 6 </since_tizen>
230
231         public object TargetValue { get; set; }
232     }
233
234
235     /// <summary>
236     /// Class to help manage the adding and updating of transitions.
237     /// </summary>
238     internal static class LayoutTransitionsHelper
239     {
240         /// <summary>
241         /// Adds the given transition and condition to a transition list.
242         /// </summary>
243         /// <param name="targetTransitionList">The list to add the transition to.</param>
244         /// <param name="condition">Condition for the transition.</param>
245         /// <param name="transition">The transition to add.</param>
246         /// <param name="explicitlySet">True is set explicitly, false if inherited.</param>
247         static public void AddTransitionForCondition(
248                               Dictionary<TransitionCondition, TransitionList> targetTransitionList,
249                               TransitionCondition condition,
250                               LayoutTransition transition,
251                               bool explicitlySet)
252         {
253             bool replaced = false;
254             bool conditionNotInDictionary = false;
255             TransitionList transitionListMatchingCondition;
256             if (targetTransitionList.TryGetValue(condition, out transitionListMatchingCondition))
257             {
258                 if (transitionListMatchingCondition != null)
259                 {
260                     for (var index = 0; index < transitionListMatchingCondition.Count; index++)
261                     {
262                         if (transitionListMatchingCondition[index].AnimatableProperty == transition.AnimatableProperty)
263                         {
264                             if (explicitlySet)
265                             {
266                                 transitionListMatchingCondition[index] = transition;
267                                 replaced = true;
268                                 continue;
269                             }
270                         }
271                     }
272                 }
273             }
274             else
275             {
276                 conditionNotInDictionary = true;
277             }
278
279             if (replaced == false)
280             {
281                 if (transitionListMatchingCondition == null)
282                 {
283                     transitionListMatchingCondition = new TransitionList();
284                 }
285                 transitionListMatchingCondition.Add(transition);
286                 // Update dictionary with new or replaced entry.
287                 if (conditionNotInDictionary)
288                 {
289                     targetTransitionList.Add(condition, transitionListMatchingCondition); // new entry
290                 }
291                 else
292                 {
293                     targetTransitionList[condition] = transitionListMatchingCondition; // replaced
294                 }
295             }
296         }
297
298         /// <summary>
299         /// Retreive the transition list for the given condition.
300         /// </summary>
301         /// <param name="sourceTransitionCollection">The source collection of transition lists to retrieve.</param>
302         /// <param name="condition">Condition for the transition.</param>
303         /// <param name="transitionsForCondition">transition list to return as out parameter.</param>
304         /// <returns>True if a transition list found for the given condition></returns>
305         static public bool GetTransitionsListForCondition(
306                               Dictionary<TransitionCondition, TransitionList> sourceTransitionCollection,
307                               TransitionCondition condition,
308                               TransitionList transitionsForCondition)
309         {
310             TransitionCondition resolvedCondition = condition;
311             bool matched = false;
312             // LayoutChanged transitions overrides ChangeOnAdd and ChangeOnRemove as siblings will
313             // reposition to the new layout not to the insertion/removal of a sibling.
314             if ((condition & TransitionCondition.LayoutChanged) == TransitionCondition.LayoutChanged)
315             {
316                 resolvedCondition = TransitionCondition.LayoutChanged;
317             }
318
319             if (sourceTransitionCollection.TryGetValue(resolvedCondition, out transitionsForCondition))
320             {
321                 matched = true;
322             }
323
324             return matched;
325         }
326
327         /// <summary>
328         /// Copy the transitions in the source list to the target list
329         /// </summary>
330         /// <param name="sourceTransitionList">The source transition list.</param>
331         /// <param name="targetTransitionList">The target transition list to copy to.</param>
332         static public void CopyTransitions(TransitionList sourceTransitionList,
333                                             TransitionList targetTransitionList)
334         {
335             targetTransitionList.Clear();
336             foreach (LayoutTransition transitionToApply in sourceTransitionList)
337             {
338                 // Overwite existing transitions
339                 targetTransitionList.Add(transitionToApply);
340             }
341
342         }
343     }
344
345 } // namespace Tizen.NUI