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