Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / BaseComponents / VisualView.cs
1 // Copyright (c) 2017 Samsung Electronics Co., Ltd.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15
16 namespace Tizen.NUI.BaseComponents
17 {
18     using System;
19     using System.Runtime.InteropServices;
20     using System.Collections.Generic;
21     using System.Linq;
22
23     /// <summary>
24     /// A visual view control for user add any visual to it.
25     /// </summary>
26     /// <example>
27     /// Example:
28     /// <code>
29     /// VisualView _visualView = new VisualView();
30     /// ImageVisualMap imageVisualMap1 = new ImageVisualMap();
31     /// imageVisualMap1.URL = "./NUISample/res/images/image-1.jpg";
32     /// imageVisualMap1.VisualSize = new Vector2( 300.0f, 300.0f );
33     /// imageVisualMap1.Offset = new Vector2( 50.0f, 50.0f );
34     /// imageVisualMap1.OffsetSizeMode = new Vector4( 1.0f, 1.0f, 1.0f, 1.0f );
35     /// imageVisualMap1.Origin = AlignType.TOP_BEGIN;
36     /// imageVisualMap1.AnchorPoint = AlignType.TOP_BEGIN;
37     /// _visualView.AddVisual("imageVisual1", imageVisualMap1);
38     /// </code>
39     /// </example>
40     public class VisualView : CustomView
41     {
42         //private LinkedList<VisualBase> _visualList = null;
43         private Dictionary<int, VisualBase> _visualDictionary = null;
44         private Dictionary<int, PropertyMap> _tranformDictionary = null;
45         private PropertyArray _animateArray = null;
46
47         static CustomView CreateInstance()
48         {
49             return new VisualView();
50         }
51
52         // static constructor registers the control type (for user can add kinds of visuals to it)
53         static VisualView()
54         {
55             // ViewRegistry registers control type with DALi type registery
56             // also uses introspection to find any properties that need to be registered with type registry
57             CustomViewRegistry.Instance.Register(CreateInstance, typeof(VisualView));
58         }
59
60         public VisualView() : base(typeof(VisualView).Name, CustomViewBehaviour.ViewBehaviourDefault)
61         {
62         }
63
64         /// <summary>
65         /// Override the parent method.
66         /// </summary>
67         public override void OnInitialize()
68         {
69             //Initialize empty
70             _visualDictionary = new Dictionary<int, VisualBase>();
71             _tranformDictionary = new Dictionary<int, PropertyMap>();
72             _animateArray = new PropertyArray();
73         }
74
75         /// <summary>
76         /// Add or update a visual to visual view.
77         /// </summary>
78         /// <param name="visualName"> The name of visual to add. If add a existed visual name, the visual will be replaced. </param>
79         /// <param name="visualMap"> The property map of visual to create.  </param>
80         public void AddVisual(string visualName, VisualMap visualMap)
81         {
82             VisualBase visual = null;
83             int visualIndex = -1;
84
85             /* If the visual had added, then replace it using RegisterVusal. */
86             //visual.Name = name;
87             foreach (var item in _visualDictionary)
88             {
89                 if (item.Value.Name == visualName)
90                 {
91                     /* Find a existed visual, its key also exited. */
92                     visualIndex = item.Key;
93                     UnregisterVisual(visualIndex);
94                     _visualDictionary.Remove(visualIndex);
95                     _tranformDictionary.Remove(visualIndex);
96                     break;
97                 }
98             }
99
100             if (visualIndex == -1) // The visual is a new one, create index for it. */
101             {
102                 visualIndex = RegisterProperty(visualName, new PropertyValue(visualName), PropertyAccessMode.ReadWrite);
103             }
104
105             if (visualIndex > 0)
106             {
107                 visual = VisualFactory.Instance.CreateVisual(visualMap.OutputVisualMap); // Create a visual for the new one.
108                 visual.Name = visualName;
109                 visual.DepthIndex = visualMap.DepthIndex;
110
111                 RegisterVisual(visualIndex, visual);
112
113                 _visualDictionary.Add(visualIndex, visual);
114                 _tranformDictionary.Add(visualIndex, visualMap.OutputTransformMap);
115
116                 visualMap.VisualIndex = visualIndex;
117                 visualMap.Name = visualName;
118                 visualMap.Parent = this;
119
120                 RelayoutRequest();
121             }
122         }
123
124         /// <summary>
125         /// Remove a visual by name.
126         /// </summary>
127         /// <param name="visualName"> The name of visual to remove. </param>
128         public void RemoveVisual(string visualName)
129         {
130             foreach (var item in _visualDictionary)
131             {
132                 if (item.Value.Name == visualName)
133                 {
134                     EnableVisual(item.Key, false);
135                     UnregisterVisual(item.Key);
136                     _tranformDictionary.Remove(item.Key);
137                     _visualDictionary.Remove(item.Key);
138
139                     RelayoutRequest();
140                     break;
141                 }
142             }
143         }
144
145         /// <summary>
146         ///  Get the total number of Visuals which are added by users
147         /// </summary>
148         public int NumberOfVisuals
149         {
150             get
151             {
152                 return _visualDictionary.Count;
153             }
154         }
155
156         /// <summary>
157         /// Remove all visuals of visual view.
158         /// </summary>
159         public void RemoveAll()
160         {
161             foreach (var item in _visualDictionary)
162             {
163                 EnableVisual(item.Key, false);
164                 UnregisterVisual(item.Key);
165             }
166             _visualDictionary.Clear();
167             _tranformDictionary.Clear();
168             RelayoutRequest();
169         }
170
171         /// <summary>
172         /// Override method of OnRelayout() for CustomView class.<br>
173         /// Called after the size negotiation has been finished for this control.<br>
174         /// The control is expected to assign this given size to itself/its children.<br>
175         /// Should be overridden by derived classes if they need to layout actors differently after certain operations like add or remove actors, resize or after changing specific properties.<br>
176         /// </summary>
177         /// <remarks>As this function is called from inside the size negotiation algorithm, you cannot call RequestRelayout (the call would just be ignored)</remarks>
178         /// <param name="size">The allocated size</param>
179         /// <param name="container">The control should add actors to this container that it is not able to allocate a size for.</param>
180         public override void OnRelayout(Vector2 size, RelayoutContainer container)
181         {
182             foreach (var item in _visualDictionary)
183             {
184                 item.Value.SetTransformAndSize(_tranformDictionary[item.Key], size);
185                 EnableVisual(item.Key, true);
186             }
187         }
188
189         internal void UpdateVisual(int visualIndex, string visualName, VisualMap visualMap)
190         {
191             VisualBase visual = null;
192
193             visual = VisualFactory.Instance.CreateVisual(visualMap.OutputVisualMap);
194             visual.Name = visualName;
195             visual.DepthIndex = visualMap.DepthIndex;
196
197             RegisterVisual(visualIndex, visual);
198
199             _visualDictionary[visualIndex] = visual;
200             _tranformDictionary[visualIndex] = visualMap.OutputTransformMap;
201
202             RelayoutRequest();
203             NUILog.Debug("UpdateVisual() name=" + visualName);
204         }
205
206         /// <summary>
207         /// Create visual animation (transition) with the input parameters.
208         /// </summary>
209         /// <param name="target"> The visual map to animation.</param>
210         /// <param name="property"> The property of visual to animation.</param>
211         /// <param name="destinationValue"> The destination value of property after animation.</param>
212         /// <param name="startTime"> The start time of visual animation.</param>
213         /// <param name="endTime"> The end time of visual animation.</param>
214         /// <param name="alphaFunction"> The alpha function of visual animation</param>
215         /// <param name="initialValue"> The initial property value of visual animation </param>
216         /// <returns>Animation instance</returns>
217         public Animation AnimateVisual(VisualMap target, string property, object destinationValue, int startTime, int endTime, AlphaFunction.BuiltinFunctions? alphaFunction = null, object initialValue = null)
218         {
219             string _alphaFunction = null;
220             if (alphaFunction != null)
221             {
222                 switch (alphaFunction)
223                 {
224                     case Tizen.NUI.AlphaFunction.BuiltinFunctions.Linear:
225                     {
226                         _alphaFunction = "LINEAR";
227                         break;
228                     }
229                     case Tizen.NUI.AlphaFunction.BuiltinFunctions.Reverse:
230                     {
231                         _alphaFunction = "REVERSE";
232                         break;
233                     }
234                     case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseInSquare:
235                     {
236                         _alphaFunction = "EASE_IN_SQUARE";
237                         break;
238                     }
239                     case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseOutSquare:
240                     {
241                         _alphaFunction = "EASE_OUT_SQUARE";
242                         break;
243                     }
244                     case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseIn:
245                     {
246                         _alphaFunction = "EASE_IN";
247                         break;
248                     }
249                     case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseOut:
250                     {
251                         _alphaFunction = "EASE_OUT";
252                         break;
253                     }
254                     case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseInOut:
255                     {
256                         _alphaFunction = "EASE_IN_OUT";
257                         break;
258                     }
259                     case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseInSine:
260                     {
261                         _alphaFunction = "EASE_IN_SINE";
262                         break;
263                     }
264                     case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseOutSine:
265                     {
266                         _alphaFunction = "EASE_OUT_SINE";
267                         break;
268                     }
269                     case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseInOutSine:
270                     {
271                         _alphaFunction = "EASE_IN_OUT_SINE";
272                         break;
273                     }
274                     case Tizen.NUI.AlphaFunction.BuiltinFunctions.Bounce:
275                     {
276                         _alphaFunction = "BOUNCE";
277                         break;
278                     }
279                     case Tizen.NUI.AlphaFunction.BuiltinFunctions.Sin:
280                     {
281                         _alphaFunction = "SIN";
282                         break;
283                     }
284                     case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseOutBack:
285                     {
286                         _alphaFunction = "EASE_OUT_BACK";
287                         break;
288                     }
289                     default:
290                     {
291                         _alphaFunction = "DEFAULT";
292                         break;
293                     }
294                 }
295             }
296
297             foreach (var item in _visualDictionary.ToList())
298             {
299                 if (item.Value.Name == target.Name)
300                 {
301                     PropertyMap _animator = new PropertyMap();
302                     if ( _alphaFunction != null) {_animator.Add("alphaFunction", new PropertyValue(_alphaFunction));}
303
304                     PropertyMap _timePeriod = new PropertyMap();
305                     _timePeriod.Add("duration", new PropertyValue((endTime - startTime) / 1000.0f));
306                     _timePeriod.Add("delay", new PropertyValue(startTime / 1000.0f));
307                     _animator.Add("timePeriod", new PropertyValue(_timePeriod));
308
309                     string _str1 = property.Substring(0, 1);
310                     string _str2 = property.Substring(1);
311                     string _str = _str1.ToLower() + _str2;
312                     if (_str == "position") {_str = "offset";}
313
314                     PropertyValue destVal = PropertyValue.CreateFromObject(destinationValue);
315
316                     PropertyMap _transition = new PropertyMap();
317                     _transition.Add("target", new PropertyValue(target.Name));
318                     _transition.Add("property", new PropertyValue(_str));
319                     if (initialValue != null)
320                     {
321                         PropertyValue initVal = PropertyValue.CreateFromObject(initialValue);
322                         _transition.Add("initialValue", new PropertyValue(initVal));
323                     }
324                     _transition.Add("targetValue", destVal);
325                     _transition.Add("animator", new PropertyValue(_animator));
326
327                     TransitionData _transitionData = new TransitionData(_transition);
328                     return this.CreateTransition(_transitionData);
329                 }
330             }
331             return null;
332         }
333
334         /// <summary>
335         /// Add group visual animation (transition) map with the input parameters.
336         /// </summary>
337         /// <param name="target"> The visual map to animation.</param>
338         /// <param name="property"> The property of visual to animation.</param>
339         /// <param name="destinationValue"> The destination value of property after animation.</param>
340         /// <param name="startTime"> The start time of visual animation.</param>
341         /// <param name="endTime"> The end time of visual animation.</param>
342         /// <param name="alphaFunction"> The alpha function of visual animation</param>
343         /// <param name="initialValue"> The initial property value of visual animation </param>
344         public void AnimateVisualAdd(VisualMap target, string property, object destinationValue, int startTime, int endTime, AlphaFunction.BuiltinFunctions? alphaFunction = null, object initialValue = null)
345         {
346             string _alphaFunction = null;
347             if (alphaFunction != null)
348             {
349                 switch (alphaFunction)
350                 {
351                     case Tizen.NUI.AlphaFunction.BuiltinFunctions.Linear:
352                     {
353                         _alphaFunction = "LINEAR";
354                         break;
355                     }
356                     case Tizen.NUI.AlphaFunction.BuiltinFunctions.Reverse:
357                     {
358                         _alphaFunction = "REVERSE";
359                         break;
360                     }
361                     case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseInSquare:
362                     {
363                         _alphaFunction = "EASE_IN_SQUARE";
364                         break;
365                     }
366                     case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseOutSquare:
367                     {
368                         _alphaFunction = "EASE_OUT_SQUARE";
369                         break;
370                     }
371                     case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseIn:
372                     {
373                         _alphaFunction = "EASE_IN";
374                         break;
375                     }
376                     case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseOut:
377                     {
378                         _alphaFunction = "EASE_OUT";
379                         break;
380                     }
381                     case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseInOut:
382                     {
383                         _alphaFunction = "EASE_IN_OUT";
384                         break;
385                     }
386                     case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseInSine:
387                     {
388                         _alphaFunction = "EASE_IN_SINE";
389                         break;
390                     }
391                     case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseOutSine:
392                     {
393                         _alphaFunction = "EASE_OUT_SINE";
394                         break;
395                     }
396                     case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseInOutSine:
397                     {
398                         _alphaFunction = "EASE_IN_OUT_SINE";
399                         break;
400                     }
401                     case Tizen.NUI.AlphaFunction.BuiltinFunctions.Bounce:
402                     {
403                         _alphaFunction = "BOUNCE";
404                         break;
405                     }
406                     case Tizen.NUI.AlphaFunction.BuiltinFunctions.Sin:
407                     {
408                         _alphaFunction = "SIN";
409                         break;
410                     }
411                     case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseOutBack:
412                     {
413                         _alphaFunction = "EASE_OUT_BACK";
414                         break;
415                     }
416                     default:
417                     {
418                         _alphaFunction = "DEFAULT";
419                         break;
420                     }
421                 }
422             }
423
424             foreach (var item in _visualDictionary.ToList())
425             {
426                 if (item.Value.Name == target.Name)
427                 {
428                     PropertyMap _animator = new PropertyMap();
429                     if ( _alphaFunction != null) {_animator.Add("alphaFunction", new PropertyValue(_alphaFunction));}
430
431                     PropertyMap _timePeriod = new PropertyMap();
432                     _timePeriod.Add("duration", new PropertyValue((endTime - startTime) / 1000.0f));
433                     _timePeriod.Add("delay", new PropertyValue(startTime / 1000.0f));
434                     _animator.Add("timePeriod", new PropertyValue(_timePeriod));
435
436                     string _str1 = property.Substring(0, 1);
437                     string _str2 = property.Substring(1);
438                     string _str = _str1.ToLower() + _str2;
439                     if (_str == "position") {_str = "offset";}
440
441                     PropertyValue destVal = PropertyValue.CreateFromObject(destinationValue);
442
443                     PropertyMap _transition = new PropertyMap();
444                     _transition.Add("target", new PropertyValue(target.Name));
445                     _transition.Add("property", new PropertyValue(_str));
446                     if (initialValue != null)
447                     {
448                         PropertyValue initVal = PropertyValue.CreateFromObject(initialValue);
449                         _transition.Add("initialValue", new PropertyValue(initVal));
450                     }
451                     _transition.Add("targetValue", destVal);
452                     _transition.Add("animator", new PropertyValue(_animator));
453
454                     _animateArray.Add(new PropertyValue(_transition));
455                 }
456             }
457         }
458
459         /// <summary>
460         /// Finish to add Visual animation (transition) map and create transition animation.
461         /// </summary>
462         /// <returns>Animation instance</returns>
463         public Animation AnimateVisualAddFinish()
464         {
465             if ( _animateArray == null || _animateArray.Empty())
466             {
467                 Tizen.Log.Fatal("NUI", "animate visual property array is empty!");
468                 return null;
469             }
470             TransitionData _transitionData = new TransitionData(_animateArray);
471
472             return this.CreateTransition(_transitionData);
473         }
474
475
476         //temporary fix to pass TCT
477         public Animation VisualAnimate(Tizen.NUI.VisualAnimator visualMap)
478         {
479             foreach (var item in _visualDictionary.ToList())
480             {
481                 if (item.Value.Name == visualMap.Target)
482                 {
483                     TransitionData _transitionData = new TransitionData(visualMap.OutputVisualMap);
484                     return this.CreateTransition(_transitionData);
485                 }
486             }
487             return null;
488         }
489         //temporary fix to pass TCT
490
491     }
492 }