98cf30ff4d1a519ab81796e3e01faa8dcfd01266
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / 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     /// Hidden-API which is usually used as Inhouse-API. If required to be opened as Public-API, ACR process is needed.
28     [EditorBrowsable(EditorBrowsableState.Never)]
29     public class TransitionList : List<LayoutTransition> {}
30
31     /// <summary>
32     /// The properties that can be animated.
33     /// </summary>
34     /// Hidden-API which is usually used as Inhouse-API. If required to be opened as Public-API, ACR process is needed.
35     [EditorBrowsable(EditorBrowsableState.Never)]
36     public enum AnimatableProperties
37     {
38         /// <summary>
39         /// Position property.
40         /// </summary>
41         /// Hidden-API which is usually used as Inhouse-API. If required to be opened as Public-API, ACR process is needed.
42         [EditorBrowsable(EditorBrowsableState.Never)]
43         Position,
44         /// <summary>
45         /// Size property.
46         /// </summary>
47         /// Hidden-API which is usually used as Inhouse-API. If required to be opened as Public-API, ACR process is needed.
48         [EditorBrowsable(EditorBrowsableState.Never)]
49         Size,
50         /// <summary>
51         /// Opacity property.
52         /// </summary>
53         /// Hidden-API which is usually used as Inhouse-API. If required to be opened as Public-API, ACR process is needed.
54         [EditorBrowsable(EditorBrowsableState.Never)]
55         Opacity
56     }
57
58     /// <summary>
59     /// Parts of the transition that can be configured to provide a custom effect.
60     /// </summary>
61     /// Hidden-API which is usually used as Inhouse-API. If required to be opened as Public-API, ACR process is needed.
62     [EditorBrowsable(EditorBrowsableState.Never)]
63     public class TransitionComponents
64     {
65         /// <summary>
66         /// TransitionComponents default constructor.
67         /// </summary>
68         /// Hidden-API which is usually used as Inhouse-API. If required to be opened as Public-API, ACR process is needed.
69         [EditorBrowsable(EditorBrowsableState.Never)]
70         public TransitionComponents()
71         {
72             Delay = 0;
73             Duration = 100;
74             AlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.Linear);
75         }
76
77         /// <summary>
78         /// TransitionComponents constructor. Stores delay, duration and AlphaFunction.
79         /// </summary>
80         /// <param name="delay">The delay before the animator starts.</param>
81         /// <param name="duration">the duration fo the animator.</param>
82         /// <param name="alphaFunction">alpha function to use .</param>
83         /// Hidden-API which is usually used as Inhouse-API. If required to be opened as Public-API, ACR process is needed.
84         [EditorBrowsable(EditorBrowsableState.Never)]
85         public TransitionComponents(int delay, int duration, AlphaFunction alphaFunction)
86         {
87             Delay = delay;
88             Duration = duration;
89             AlphaFunction = alphaFunction;
90         }
91
92         /// <summary>
93         /// Time the transition should execute. Milliseconds.
94         /// </summary>
95         /// Hidden-API which is usually used as Inhouse-API. If required to be opened as Public-API, ACR process is needed.
96         [EditorBrowsable(EditorBrowsableState.Never)]
97         public int Duration;
98         /// <summary>
99         /// Delay before the transition executes. Milliseconds.
100         /// </summary>
101         /// Hidden-API which is usually used as Inhouse-API. If required to be opened as Public-API, ACR process is needed.
102         [EditorBrowsable(EditorBrowsableState.Never)]
103         public int Delay;
104         /// <summary>
105         /// Function to alter the transition path over time.
106         /// </summary>
107         /// Hidden-API which is usually used as Inhouse-API. If required to be opened as Public-API, ACR process is needed.
108         [EditorBrowsable(EditorBrowsableState.Never)]
109         public AlphaFunction AlphaFunction;
110     }
111
112     /// <summary>
113     /// LayoutTransition stores the aninmation setting for a transition conidition.
114     /// </summary>
115     /// Hidden-API which is usually used as Inhouse-API. If required to be opened as Public-API, ACR process is needed.
116     [EditorBrowsable(EditorBrowsableState.Never)]
117     public class LayoutTransition
118     {
119         /// <summary>
120         /// LayoutTransition default constructor.
121         /// </summary>
122         /// Hidden-API which is usually used as Inhouse-API. If required to be opened as Public-API, ACR process is needed.
123         [EditorBrowsable(EditorBrowsableState.Never)]
124         public LayoutTransition()
125         {
126           Condition = TransitionCondition.Unspecified;
127           AnimatableProperty = AnimatableProperties.Position;
128           Animator = null;
129           TargetValue = 0;
130         }
131         /// <summary>
132         /// LayoutTransition constructor.
133         /// </summary>
134         /// <param name="condition">The animatable condition.</param>
135         /// <param name="animatableProperty">the property to animate.</param>
136         /// <param name="targetValue">target value of the property.</param>
137         /// <param name="animator">Components to define the animator.</param>
138         /// Hidden-API which is usually used as Inhouse-API. If required to be opened as Public-API, ACR process is needed.
139         [EditorBrowsable(EditorBrowsableState.Never)]
140         public LayoutTransition( TransitionCondition condition,
141                                  AnimatableProperties animatableProperty,
142                                  object targetValue,
143                                  TransitionComponents animator)
144         {
145             Condition = condition;
146             AnimatableProperty = animatableProperty;
147             Animator = animator;
148             TargetValue = targetValue;
149         }
150
151         /// <summary>
152         /// Condition for this Transition
153         /// </summary>
154         /// Hidden-API which is usually used as Inhouse-API. If required to be opened as Public-API, ACR process is needed.
155         [EditorBrowsable(EditorBrowsableState.Never)]
156         public TransitionCondition Condition{get; set;}
157         /// <summary>
158         /// Property to animate.
159         /// </summary>
160         /// Hidden-API which is usually used as Inhouse-API. If required to be opened as Public-API, ACR process is needed.
161         [EditorBrowsable(EditorBrowsableState.Never)]
162         public AnimatableProperties AnimatableProperty{get; set;}
163         /// <summary>
164         /// Components of the Animator.
165         /// </summary>
166         /// Hidden-API which is usually used as Inhouse-API. If required to be opened as Public-API, ACR process is needed.
167         [EditorBrowsable(EditorBrowsableState.Never)]
168         public TransitionComponents Animator{get; set;}
169         /// <summary>
170         /// Target value to animate to.
171         /// </summary>
172         /// Hidden-API which is usually used as Inhouse-API. If required to be opened as Public-API, ACR process is needed.
173         [EditorBrowsable(EditorBrowsableState.Never)]
174         public object TargetValue{get; set;}
175     }
176
177
178     /// <summary>
179     /// Class to help manage the adding and updating of transitions.
180     /// </summary>
181     internal static class LayoutTransitionsHelper
182     {
183         /// <summary>
184         /// Adds the given transition and condition to a transition list.
185         /// </summary>
186         /// <param name="targetTransitionList">The list to add the transition to.</param>
187         /// <param name="condition">Condition for the transition.</param>
188         /// <param name="transition">The transition to add.</param>
189         /// <param name="explicitlySet">True is set explicitly, false if inherited.</param>
190         static public void AddTransitionForCondition(
191                               Dictionary<TransitionCondition, TransitionList> targetTransitionList,
192                               TransitionCondition condition,
193                               LayoutTransition transition,
194                               bool explicitlySet)
195         {
196             bool replaced = false;
197             bool conditionNotInDictionary = false;
198             TransitionList transitionListMatchingCondition;
199             if (targetTransitionList.TryGetValue(condition, out transitionListMatchingCondition))
200             {
201                 for (var index=0; index < transitionListMatchingCondition?.Count; index++ )
202                 {
203                     if (transitionListMatchingCondition[index].AnimatableProperty == transition.AnimatableProperty)
204                     {
205                         if (explicitlySet)
206                         {
207                             transitionListMatchingCondition[index] = transition;
208                             replaced = true;
209                             continue;
210                         }
211                     }
212                 }
213             }
214             else
215             {
216                 conditionNotInDictionary = true;
217             }
218
219             if (replaced == false)
220             {
221                 if (transitionListMatchingCondition == null)
222                 {
223                     transitionListMatchingCondition = new TransitionList();
224                 }
225                 transitionListMatchingCondition.Add(transition);
226                 // Update dictionary with new or replaced entry.
227                 if (conditionNotInDictionary)
228                 {
229                     targetTransitionList.Add(condition, transitionListMatchingCondition); // new entry
230                 }
231                 else
232                 {
233                     targetTransitionList[condition] = transitionListMatchingCondition; // replaced
234                 }
235             }
236         }
237
238         /// <summary>
239         /// Retreive the transition list for the given condition.
240         /// </summary>
241         /// <param name="sourceTransitionCollection">The source collection of transition lists to retrieve.</param>
242         /// <param name="condition">Condition for the transition.</param>
243         /// <param name="transitionsForCondition">transition list to return as out parameter.</param>
244         /// <returns>True if a transition list found for the given condition></returns>
245         static public bool GetTransitionsListForCondition(
246                               Dictionary<TransitionCondition, TransitionList> sourceTransitionCollection,
247                               TransitionCondition condition,
248                               TransitionList transitionsForCondition )
249         {
250             TransitionCondition resolvedCondition = condition;
251             bool matched = false;
252             // LayoutChanged transitions overrides ChangeOnAdd and ChangeOnRemove as siblings will
253             // reposition to the new layout not to the insertion/removal of a sibling.
254             if ((condition & TransitionCondition.LayoutChanged) == TransitionCondition.LayoutChanged)
255             {
256                 resolvedCondition = TransitionCondition.LayoutChanged;
257             }
258
259             if (sourceTransitionCollection.TryGetValue(resolvedCondition, out transitionsForCondition))
260             {
261                 matched = true;
262             }
263
264             return matched;
265         }
266
267         /// <summary>
268         /// Copy the transitions in the source list to the target list
269         /// </summary>
270         /// <param name="sourceTransitionList">The source transition list.</param>
271         /// <param name="targetTransitionList">The target transition list to copy to.</param>
272         static public void CopyTransitions( TransitionList sourceTransitionList,
273                                             TransitionList targetTransitionList )
274         {
275             targetTransitionList.Clear();
276             foreach (LayoutTransition transitionToApply in sourceTransitionList)
277             {
278                 // Overwite existing transitions
279                 targetTransitionList.Add(transitionToApply);
280             }
281
282         }
283     }
284
285 } // namespace Tizen.NUI