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