1 // Copyright (c) 2017 Samsung Electronics Co., Ltd.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
16 namespace Tizen.NUI.BaseComponents
20 using System.Runtime.InteropServices;
21 using System.Collections.Generic;
25 /// A visual view control if a user adds any visual to it.
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);
41 public class VisualView : CustomView
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;
48 static CustomView CreateInstance()
50 return new VisualView();
53 // static constructor registers the control type (for user can add kinds of visuals to it)
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));
61 public VisualView() : base(typeof(VisualView).FullName, CustomViewBehaviour.ViewBehaviourDefault)
66 /// Overrides the parent method.
68 public override void OnInitialize()
71 _visualDictionary = new Dictionary<int, VisualBase>();
72 _tranformDictionary = new Dictionary<int, PropertyMap>();
73 _animateArray = new PropertyArray();
77 /// Adds or updates a visual to visual view.
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)
83 VisualBase visual = null;
86 /* If the visual had added, then replace it using RegisterVusal. */
88 foreach (var item in _visualDictionary)
90 if (item.Value.Name == visualName)
92 /* Find a existed visual, its key also exited. */
93 visualIndex = item.Key;
94 UnregisterVisual(visualIndex);
95 _visualDictionary.Remove(visualIndex);
96 _tranformDictionary.Remove(visualIndex);
101 if (visualIndex == -1) // The visual is a new one, create index for it. */
103 visualIndex = RegisterProperty(visualName, new PropertyValue(visualName), PropertyAccessMode.ReadWrite);
108 visual = VisualFactory.Instance.CreateVisual(visualMap.OutputVisualMap); // Create a visual for the new one.
109 visual.Name = visualName;
110 visual.DepthIndex = visualMap.DepthIndex;
112 RegisterVisual(visualIndex, visual);
114 _visualDictionary.Add(visualIndex, visual);
115 _tranformDictionary.Add(visualIndex, visualMap.OutputTransformMap);
117 visualMap.VisualIndex = visualIndex;
118 visualMap.Name = visualName;
119 visualMap.Parent = this;
126 /// Removes a visual by name.
128 /// <param name="visualName">The name of a visual to remove.</param>
129 public void RemoveVisual(string visualName)
131 foreach (var item in _visualDictionary)
133 if (item.Value.Name == visualName)
135 EnableVisual(item.Key, false);
136 UnregisterVisual(item.Key);
137 _tranformDictionary.Remove(item.Key);
138 _visualDictionary.Remove(item.Key);
147 /// Gets the total number of visuals which are added by users.
149 public int NumberOfVisuals
153 return _visualDictionary.Count;
158 /// Removes all visuals of the visual view.
160 public void RemoveAll()
162 foreach (var item in _visualDictionary)
164 EnableVisual(item.Key, false);
165 UnregisterVisual(item.Key);
167 _visualDictionary.Clear();
168 _tranformDictionary.Clear();
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>
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)
183 foreach (var item in _visualDictionary)
185 item.Value.SetTransformAndSize(_tranformDictionary[item.Key], size);
186 EnableVisual(item.Key, true);
190 internal void UpdateVisual(int visualIndex, string visualName, VisualMap visualMap)
192 VisualBase visual = null;
194 visual = VisualFactory.Instance.CreateVisual(visualMap.OutputVisualMap);
195 visual.Name = visualName;
196 visual.DepthIndex = visualMap.DepthIndex;
198 RegisterVisual(visualIndex, visual);
200 _visualDictionary[visualIndex] = visual;
201 _tranformDictionary[visualIndex] = visualMap.OutputTransformMap;
204 NUILog.Debug("UpdateVisual() name=" + visualName);
208 /// Creates a visual animation (transition) with the input parameters.
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)
220 string _alphaFunction = null;
221 if (alphaFunction != null)
223 switch (alphaFunction)
225 case Tizen.NUI.AlphaFunction.BuiltinFunctions.Linear:
227 _alphaFunction = "LINEAR";
230 case Tizen.NUI.AlphaFunction.BuiltinFunctions.Reverse:
232 _alphaFunction = "REVERSE";
235 case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseInSquare:
237 _alphaFunction = "EASE_IN_SQUARE";
240 case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseOutSquare:
242 _alphaFunction = "EASE_OUT_SQUARE";
245 case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseIn:
247 _alphaFunction = "EASE_IN";
250 case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseOut:
252 _alphaFunction = "EASE_OUT";
255 case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseInOut:
257 _alphaFunction = "EASE_IN_OUT";
260 case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseInSine:
262 _alphaFunction = "EASE_IN_SINE";
265 case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseOutSine:
267 _alphaFunction = "EASE_OUT_SINE";
270 case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseInOutSine:
272 _alphaFunction = "EASE_IN_OUT_SINE";
275 case Tizen.NUI.AlphaFunction.BuiltinFunctions.Bounce:
277 _alphaFunction = "BOUNCE";
280 case Tizen.NUI.AlphaFunction.BuiltinFunctions.Sin:
282 _alphaFunction = "SIN";
285 case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseOutBack:
287 _alphaFunction = "EASE_OUT_BACK";
292 _alphaFunction = "DEFAULT";
298 foreach (var item in _visualDictionary.ToList())
300 if (item.Value.Name == target.Name)
302 PropertyMap _animator = new PropertyMap();
303 if ( _alphaFunction != null) {_animator.Add("alphaFunction", new PropertyValue(_alphaFunction));}
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));
310 StringBuilder sb = new StringBuilder(property);
311 sb[0] = (char)(sb[0] | 0x20);
312 string _str = sb.ToString();
313 if (_str == "position") {_str = "offset";}
315 PropertyValue destVal = PropertyValue.CreateFromObject(destinationValue);
317 PropertyMap _transition = new PropertyMap();
318 _transition.Add("target", new PropertyValue(target.Name));
319 _transition.Add("property", new PropertyValue(_str));
320 if (initialValue != null)
322 PropertyValue initVal = PropertyValue.CreateFromObject(initialValue);
323 _transition.Add("initialValue", new PropertyValue(initVal));
325 _transition.Add("targetValue", destVal);
326 _transition.Add("animator", new PropertyValue(_animator));
328 TransitionData _transitionData = new TransitionData(_transition);
329 return this.CreateTransition(_transitionData);
336 /// Adds a group visual animation (transition) map with the input parameters.
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)
347 string _alphaFunction = null;
348 if (alphaFunction != null)
350 switch (alphaFunction)
352 case Tizen.NUI.AlphaFunction.BuiltinFunctions.Linear:
354 _alphaFunction = "LINEAR";
357 case Tizen.NUI.AlphaFunction.BuiltinFunctions.Reverse:
359 _alphaFunction = "REVERSE";
362 case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseInSquare:
364 _alphaFunction = "EASE_IN_SQUARE";
367 case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseOutSquare:
369 _alphaFunction = "EASE_OUT_SQUARE";
372 case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseIn:
374 _alphaFunction = "EASE_IN";
377 case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseOut:
379 _alphaFunction = "EASE_OUT";
382 case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseInOut:
384 _alphaFunction = "EASE_IN_OUT";
387 case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseInSine:
389 _alphaFunction = "EASE_IN_SINE";
392 case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseOutSine:
394 _alphaFunction = "EASE_OUT_SINE";
397 case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseInOutSine:
399 _alphaFunction = "EASE_IN_OUT_SINE";
402 case Tizen.NUI.AlphaFunction.BuiltinFunctions.Bounce:
404 _alphaFunction = "BOUNCE";
407 case Tizen.NUI.AlphaFunction.BuiltinFunctions.Sin:
409 _alphaFunction = "SIN";
412 case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseOutBack:
414 _alphaFunction = "EASE_OUT_BACK";
419 _alphaFunction = "DEFAULT";
425 foreach (var item in _visualDictionary.ToList())
427 if (item.Value.Name == target.Name)
429 PropertyMap _animator = new PropertyMap();
430 if ( _alphaFunction != null) {_animator.Add("alphaFunction", new PropertyValue(_alphaFunction));}
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));
437 StringBuilder sb = new StringBuilder(property);
438 sb[0] = (char)(sb[0] | 0x20);
439 string _str = sb.ToString();
440 if (_str == "position") {_str = "offset";}
442 PropertyValue destVal = PropertyValue.CreateFromObject(destinationValue);
444 PropertyMap _transition = new PropertyMap();
445 _transition.Add("target", new PropertyValue(target.Name));
446 _transition.Add("property", new PropertyValue(_str));
447 if (initialValue != null)
449 PropertyValue initVal = PropertyValue.CreateFromObject(initialValue);
450 _transition.Add("initialValue", new PropertyValue(initVal));
452 _transition.Add("targetValue", destVal);
453 _transition.Add("animator", new PropertyValue(_animator));
455 _animateArray.Add(new PropertyValue(_transition));
461 /// Finishes to add a visual animation (transition) map and creates a transition animation.
463 /// <returns>Animation instance.</returns>
464 public Animation AnimateVisualAddFinish()
466 if ( _animateArray == null || _animateArray.Empty())
468 Tizen.Log.Fatal("NUI", "animate visual property array is empty!");
471 TransitionData _transitionData = new TransitionData(_animateArray);
473 return this.CreateTransition(_transitionData);
477 //temporary fix to pass TCT
478 public Animation VisualAnimate(Tizen.NUI.VisualAnimator visualMap)
480 foreach (var item in _visualDictionary.ToList())
482 if (item.Value.Name == visualMap.Target)
484 TransitionData _transitionData = new TransitionData(visualMap.OutputVisualMap);
485 return this.CreateTransition(_transitionData);
490 //temporary fix to pass TCT