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