[NFC][Non-ACR] Fix NFC Csharp TC failed issue (#1849)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / BaseComponents / VisualView.cs
1 // Copyright (c) 2019 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 using System.ComponentModel;
22
23 namespace Tizen.NUI.BaseComponents
24 {
25     /// <summary>
26     /// A visual view control if a user adds any visual to it.
27     /// </summary>
28     /// <example>
29     /// Example:
30     /// <code>
31     /// VisualView _visualView = new VisualView();
32     /// ImageVisualMap imageVisualMap1 = new ImageVisualMap();
33     /// imageVisualMap1.URL = "./NUISample/res/images/image-1.jpg";
34     /// imageVisualMap1.VisualSize = new Vector2( 300.0f, 300.0f );
35     /// imageVisualMap1.Offset = new Vector2( 50.0f, 50.0f );
36     /// imageVisualMap1.OffsetSizeMode = new Vector4( 1.0f, 1.0f, 1.0f, 1.0f );
37     /// imageVisualMap1.Origin = AlignType.TOP_BEGIN;
38     /// imageVisualMap1.AnchorPoint = AlignType.TOP_BEGIN;
39     /// _visualView.AddVisual("imageVisual1", imageVisualMap1);
40     /// </code>
41     /// </example>
42     /// <since_tizen> 3 </since_tizen>
43     public class VisualView : CustomView
44     {
45         //private LinkedList<VisualBase> _visualList = null;
46         private Dictionary<int, VisualBase> _visualDictionary = null;
47         private Dictionary<int, PropertyMap> _tranformDictionary = null;
48         private PropertyArray _animateArray = null;
49
50         /// <summary>
51         /// Constructor.
52         /// </summary>
53         /// <since_tizen> 3 </since_tizen>
54         public VisualView() : base(typeof(VisualView).FullName, CustomViewBehaviour.ViewBehaviourDefault | CustomViewBehaviour.RequiresTouchEventsSupport)
55         {
56         }
57
58         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
59         [EditorBrowsable(EditorBrowsableState.Never)]
60         public VisualView(ViewStyle viewStyle) : base(typeof(VisualView).FullName, CustomViewBehaviour.ViewBehaviourDefault | CustomViewBehaviour.RequiresTouchEventsSupport, viewStyle)
61         {
62
63         }
64
65         // static constructor registers the control type (for user can add kinds of visuals to it)
66         static VisualView()
67         {
68             // ViewRegistry registers control type with DALi type registery
69             // also uses introspection to find any properties that need to be registered with type registry
70             CustomViewRegistry.Instance.Register(CreateInstance, typeof(VisualView));
71         }
72
73         /// <summary>
74         /// Gets the total number of visuals which are added by users.
75         /// </summary>
76         /// <since_tizen> 3 </since_tizen>
77         public int NumberOfVisuals
78         {
79             get
80             {
81                 return _visualDictionary.Count;
82             }
83         }
84
85         /// <summary>
86         /// Overrides the parent method.
87         /// </summary>
88         /// <since_tizen> 3 </since_tizen>
89         public override void OnInitialize()
90         {
91             //Initialize empty
92             _visualDictionary = new Dictionary<int, VisualBase>();
93             _tranformDictionary = new Dictionary<int, PropertyMap>();
94             _animateArray = new PropertyArray();
95         }
96
97         /// <summary>
98         /// Adds or updates a visual to visual view.
99         /// </summary>
100         /// <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>
101         /// <param name="visualMap">The property map of a visual to create.</param>
102         /// <since_tizen> 3 </since_tizen>
103         public void AddVisual(string visualName, VisualMap visualMap)
104         {
105             VisualBase visual = null;
106             int visualIndex = -1;
107
108             /* If the visual had added, then replace it using RegisterVusal. */
109             //visual.Name = name;
110             foreach (var item in _visualDictionary)
111             {
112                 if (item.Value.Name == visualName)
113                 {
114                     /* Find a existed visual, its key also exited. */
115                     visualIndex = item.Key;
116                     UnregisterVisual(visualIndex);
117                     _visualDictionary.Remove(visualIndex);
118                     _tranformDictionary.Remove(visualIndex);
119                     break;
120                 }
121             }
122
123             if (visualIndex == -1) // The visual is a new one, create index for it. */
124             {
125                 visualIndex = RegisterProperty(visualName, new PropertyValue(visualName), PropertyAccessMode.ReadWrite);
126             }
127
128             if (visualIndex > 0)
129             {
130                 visual = VisualFactory.Instance.CreateVisual(visualMap.OutputVisualMap); // Create a visual for the new one.
131                 visual.Name = visualName;
132                 visual.DepthIndex = visualMap.DepthIndex;
133
134                 RegisterVisual(visualIndex, visual);
135
136                 _visualDictionary.Add(visualIndex, visual);
137                 _tranformDictionary.Add(visualIndex, visualMap.OutputTransformMap);
138
139                 visualMap.VisualIndex = visualIndex;
140                 visualMap.Name = visualName;
141                 visualMap.Parent = this;
142
143                 RelayoutRequest();
144             }
145         }
146
147         /// <summary>
148         /// Removes a visual by name.
149         /// </summary>
150         /// <param name="visualName">The name of a visual to remove.</param>
151         /// <since_tizen> 3 </since_tizen>
152         public void RemoveVisual(string visualName)
153         {
154             foreach (var item in _visualDictionary)
155             {
156                 if (item.Value.Name == visualName)
157                 {
158                     EnableVisual(item.Key, false);
159                     UnregisterVisual(item.Key);
160                     _tranformDictionary.Remove(item.Key);
161                     _visualDictionary.Remove(item.Key);
162
163                     RelayoutRequest();
164                     break;
165                 }
166             }
167         }
168
169         /// <summary>
170         /// Removes all visuals of the visual view.
171         /// </summary>
172         /// <since_tizen> 3 </since_tizen>
173         public void RemoveAll()
174         {
175             foreach (var item in _visualDictionary)
176             {
177                 EnableVisual(item.Key, false);
178                 UnregisterVisual(item.Key);
179             }
180             _visualDictionary.Clear();
181             _tranformDictionary.Clear();
182             RelayoutRequest();
183         }
184
185         /// <summary>
186         /// Overrides the method of OnRelayout() for CustomView class.<br />
187         /// Called after the size negotiation has been finished for this control.<br />
188         /// The control is expected to assign this given size to itself or its children.<br />
189         /// 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 />
190         /// </summary>
191         /// <remarks>As this function is called from inside the size negotiation algorithm, you cannot call RequestRelayout (the call would just be ignored).</remarks>
192         /// <param name="size">The allocated size.</param>
193         /// <param name="container">The control should add actors to this container that it is not able to allocate a size for.</param>
194         /// <since_tizen> 3 </since_tizen>
195         public override void OnRelayout(Vector2 size, RelayoutContainer container)
196         {
197             foreach (var item in _visualDictionary)
198             {
199                 item.Value.SetTransformAndSize(_tranformDictionary[item.Key], size);
200                 EnableVisual(item.Key, true);
201             }
202         }
203
204         /// <summary>
205         /// Creates a visual animation (transition) with the input parameters.
206         /// </summary>
207         /// <param name="target">The visual map to animation.</param>
208         /// <param name="property">The property of visual to animation.</param>
209         /// <param name="destinationValue">The destination value of property after animation.</param>
210         /// <param name="startTime">The start time of visual animation.</param>
211         /// <param name="endTime">The end time of visual animation.</param>
212         /// <param name="alphaFunction">The alpha function of visual animation.</param>
213         /// <param name="initialValue">The initial property value of visual animation.</param>
214         /// <returns>Animation instance</returns>
215         /// <since_tizen> 3 </since_tizen>
216         public Animation AnimateVisual(VisualMap target, string property, object destinationValue, int startTime, int endTime, AlphaFunction.BuiltinFunctions? alphaFunction = null, object initialValue = null)
217         {
218             string _alphaFunction = alphaFunction?.GetDescription();
219
220             foreach (var item in _visualDictionary.ToList())
221             {
222                 if (item.Value.Name == target.Name)
223                 {
224                     PropertyMap _animator = new PropertyMap();
225                     if (_alphaFunction != null) { _animator.Add("alphaFunction", new PropertyValue(_alphaFunction)); }
226
227                     PropertyMap _timePeriod = new PropertyMap();
228                     _timePeriod.Add("duration", new PropertyValue((endTime - startTime) / 1000.0f));
229                     _timePeriod.Add("delay", new PropertyValue(startTime / 1000.0f));
230                     _animator.Add("timePeriod", new PropertyValue(_timePeriod));
231
232                     StringBuilder sb = new StringBuilder(property);
233                     sb[0] = (char)(sb[0] | 0x20);
234                     string _str = sb.ToString();
235                     if (_str == "position") { _str = "offset"; }
236
237                     PropertyValue destVal = PropertyValue.CreateFromObject(destinationValue);
238
239                     PropertyMap _transition = new PropertyMap();
240                     _transition.Add("target", new PropertyValue(target.Name));
241                     _transition.Add("property", new PropertyValue(_str));
242                     if (initialValue != null)
243                     {
244                         PropertyValue initVal = PropertyValue.CreateFromObject(initialValue);
245                         _transition.Add("initialValue", new PropertyValue(initVal));
246                     }
247                     _transition.Add("targetValue", destVal);
248                     _transition.Add("animator", new PropertyValue(_animator));
249
250                     TransitionData _transitionData = new TransitionData(_transition);
251                     return this.CreateTransition(_transitionData);
252                 }
253             }
254             return null;
255         }
256
257         /// <summary>
258         /// Adds a group visual animation (transition) map with the input parameters.
259         /// </summary>
260         /// <param name="target">The visual map to animation.</param>
261         /// <param name="property">The property of visual to animation.</param>
262         /// <param name="destinationValue">The destination value of property after animation.</param>
263         /// <param name="startTime">The start time of visual animation.</param>
264         /// <param name="endTime">The end time of visual animation.</param>
265         /// <param name="alphaFunction">The alpha function of visual animation.</param>
266         /// <param name="initialValue">The initial property value of visual animation.</param>
267         /// <since_tizen> 3 </since_tizen>
268         public void AnimateVisualAdd(VisualMap target, string property, object destinationValue, int startTime, int endTime, AlphaFunction.BuiltinFunctions? alphaFunction = null, object initialValue = null)
269         {
270             string _alphaFunction = alphaFunction?.GetDescription();
271
272             foreach (var item in _visualDictionary.ToList())
273             {
274                 if (item.Value.Name == target.Name)
275                 {
276                     PropertyMap _animator = new PropertyMap();
277                     if (_alphaFunction != null) { _animator.Add("alphaFunction", new PropertyValue(_alphaFunction)); }
278
279                     PropertyMap _timePeriod = new PropertyMap();
280                     _timePeriod.Add("duration", new PropertyValue((endTime - startTime) / 1000.0f));
281                     _timePeriod.Add("delay", new PropertyValue(startTime / 1000.0f));
282                     _animator.Add("timePeriod", new PropertyValue(_timePeriod));
283
284                     StringBuilder sb = new StringBuilder(property);
285                     sb[0] = (char)(sb[0] | 0x20);
286                     string _str = sb.ToString();
287                     if (_str == "position") { _str = "offset"; }
288
289                     PropertyValue destVal = PropertyValue.CreateFromObject(destinationValue);
290
291                     PropertyMap _transition = new PropertyMap();
292                     _transition.Add("target", new PropertyValue(target.Name));
293                     _transition.Add("property", new PropertyValue(_str));
294                     if (initialValue != null)
295                     {
296                         PropertyValue initVal = PropertyValue.CreateFromObject(initialValue);
297                         _transition.Add("initialValue", new PropertyValue(initVal));
298                     }
299                     _transition.Add("targetValue", destVal);
300                     _transition.Add("animator", new PropertyValue(_animator));
301
302                     _animateArray.Add(new PropertyValue(_transition));
303                 }
304             }
305         }
306
307         /// <summary>
308         /// Finishes to add a visual animation (transition) map and creates a transition animation.
309         /// </summary>
310         /// <returns>Animation instance.</returns>
311         /// <since_tizen> 3 </since_tizen>
312         public Animation AnimateVisualAddFinish()
313         {
314             if (_animateArray == null || _animateArray.Empty())
315             {
316                 Tizen.Log.Fatal("NUI", "animate visual property array is empty!");
317                 return null;
318             }
319             TransitionData _transitionData = new TransitionData(_animateArray);
320
321             return this.CreateTransition(_transitionData);
322         }
323
324         /// <summary>
325         /// temporary fix to pass TCT.
326         /// </summary>
327         /// <since_tizen> 3 </since_tizen>
328         public Animation VisualAnimate(Tizen.NUI.VisualAnimator visualMap)
329         {
330             foreach (var item in _visualDictionary.ToList())
331             {
332                 if (item.Value.Name == visualMap.Target)
333                 {
334                     TransitionData _transitionData = new TransitionData(visualMap.OutputVisualMap);
335                     return this.CreateTransition(_transitionData);
336                 }
337             }
338             return null;
339         }
340         //temporary fix to pass TCT
341
342         internal void UpdateVisual(int visualIndex, string visualName, VisualMap visualMap)
343         {
344             VisualBase visual = null;
345
346             visual = VisualFactory.Instance.CreateVisual(visualMap.OutputVisualMap);
347             visual.Name = visualName;
348             visual.DepthIndex = visualMap.DepthIndex;
349
350             RegisterVisual(visualIndex, visual);
351
352             _visualDictionary[visualIndex] = visual;
353             _tranformDictionary[visualIndex] = visualMap.OutputTransformMap;
354
355             RelayoutRequest();
356             NUILog.Debug("UpdateVisual() name=" + visualName);
357         }
358
359         static CustomView CreateInstance()
360         {
361             return new VisualView();
362         }
363
364     }
365 }