1 // Copyright (c) 2019 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.
18 using System.Runtime.InteropServices;
19 using System.Collections.Generic;
21 using System.ComponentModel;
23 namespace Tizen.NUI.BaseComponents
26 /// A visual view control if a user adds any visual to it.
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);
42 /// <since_tizen> 3 </since_tizen>
43 public class VisualView : CustomView
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;
53 /// <since_tizen> 3 </since_tizen>
54 public VisualView() : this(CustomViewBehaviour.ViewBehaviourDefault | CustomViewBehaviour.RequiresTouchEventsSupport)
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) : this(CustomViewBehaviour.ViewBehaviourDefault | CustomViewBehaviour.RequiresTouchEventsSupport, viewStyle)
65 /// <summary>Create a VisualView with specified behaviour.</summary>
66 /// <param name="behaviour">CustomView behaviour</param>
67 [EditorBrowsable(EditorBrowsableState.Never)]
68 public VisualView(CustomViewBehaviour behaviour) : base(typeof(VisualView).FullName, behaviour)
72 /// <summary>Create a VisualView with specified behaviour and a style.</summary>
73 /// <param name="behaviour">CustomView behaviour</param>
74 /// <param name="viewStyle">The ViewStyle.</param>
75 [EditorBrowsable(EditorBrowsableState.Never)]
76 public VisualView(CustomViewBehaviour behaviour, ViewStyle viewStyle) : base(typeof(VisualView).FullName, behaviour, viewStyle)
80 // static constructor registers the control type (for user can add kinds of visuals to it)
83 // ViewRegistry registers control type with DALi type registry
84 // also uses introspection to find any properties that need to be registered with type registry
85 CustomViewRegistry.Instance.Register(CreateInstance, typeof(VisualView));
89 /// Gets the total number of visuals which are added by users.
91 /// <since_tizen> 3 </since_tizen>
92 public int NumberOfVisuals
96 return visualDictionary.Count;
101 /// Overrides the parent method.
103 /// <since_tizen> 3 </since_tizen>
104 public override void OnInitialize()
109 visualDictionary = new Dictionary<int, VisualBase>();
110 tranformDictionary = new Dictionary<int, PropertyMap>();
111 animateArray = new PropertyArray();
115 /// Adds or updates a visual to visual view.
117 /// <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>
118 /// <param name="visualMap">The property map of a visual to create.</param>
119 /// <exception cref="ArgumentNullException"> Thrown when visualMap is null. </exception>
120 /// <since_tizen> 3 </since_tizen>
121 public void AddVisual(string visualName, VisualMap visualMap)
123 VisualBase visual = null;
124 int visualIndex = -1;
126 /* If the visual had added, then replace it using RegisterVusal. */
127 //visual.Name = name;
128 foreach (var item in visualDictionary)
130 if (item.Value.Name == visualName)
132 /* Find a existed visual, its key also exited. */
133 visualIndex = item.Key;
134 UnregisterVisual(visualIndex);
135 visualDictionary.Remove(visualIndex);
136 tranformDictionary.Remove(visualIndex);
141 if (visualIndex == -1) // The visual is a new one, create index for it. */
143 using (var temp = new PropertyValue(visualName))
145 visualIndex = RegisterProperty(visualName, temp, PropertyAccessMode.ReadWrite);
151 if (visualMap == null)
153 throw new ArgumentNullException(nameof(visualMap));
155 visual = VisualFactory.Instance.CreateVisual(visualMap.OutputVisualMap); // Create a visual for the new one.
156 visual.Name = visualName;
157 visual.DepthIndex = visualMap.DepthIndex;
159 RegisterVisual(visualIndex, visual);
161 visualDictionary.Add(visualIndex, visual);
162 tranformDictionary.Add(visualIndex, visualMap.OutputTransformMap);
164 visualMap.VisualIndex = visualIndex;
165 visualMap.Name = visualName;
166 visualMap.Parent = this;
173 /// Removes a visual by name.
175 /// <param name="visualName">The name of a visual to remove.</param>
176 /// <since_tizen> 3 </since_tizen>
177 public void RemoveVisual(string visualName)
179 foreach (var item in visualDictionary)
181 if (item.Value.Name == visualName)
183 EnableVisual(item.Key, false);
184 UnregisterVisual(item.Key);
185 tranformDictionary.Remove(item.Key);
186 visualDictionary.Remove(item.Key);
195 /// Removes all visuals of the visual view.
197 /// <since_tizen> 3 </since_tizen>
198 public void RemoveAll()
200 foreach (var item in visualDictionary)
202 EnableVisual(item.Key, false);
203 UnregisterVisual(item.Key);
205 visualDictionary.Clear();
206 tranformDictionary.Clear();
211 /// Overrides the method of OnRelayout() for CustomView class.<br />
212 /// Called after the size negotiation has been finished for this control.<br />
213 /// The control is expected to assign this given size to itself or its children.<br />
214 /// 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 />
216 /// <remarks>As this function is called from inside the size negotiation algorithm, you cannot call RequestRelayout (the call would just be ignored).</remarks>
217 /// <param name="size">The allocated size.</param>
218 /// <param name="container">The control should add actors to this container that it is not able to allocate a size for.</param>
219 /// <since_tizen> 3 </since_tizen>
220 public override void OnRelayout(Vector2 size, RelayoutContainer container)
222 foreach (var item in visualDictionary)
224 item.Value.SetTransformAndSize(tranformDictionary[item.Key], size);
225 EnableVisual(item.Key, true);
230 /// Creates a visual animation (transition) with the input parameters.
232 /// <param name="target">The visual map to animation.</param>
233 /// <param name="property">The property of visual to animation.</param>
234 /// <param name="destinationValue">The destination value of property after animation.</param>
235 /// <param name="startTime">The start time of visual animation.</param>
236 /// <param name="endTime">The end time of visual animation.</param>
237 /// <param name="alphaFunction">The alpha function of visual animation.</param>
238 /// <param name="initialValue">The initial property value of visual animation.</param>
239 /// <returns>Animation instance</returns>
240 /// <exception cref="ArgumentNullException"> Thrown when target is null. </exception>
241 /// <since_tizen> 3 </since_tizen>
242 public Animation AnimateVisual(VisualMap target, string property, object destinationValue, int startTime, int endTime, AlphaFunction.BuiltinFunctions? alphaFunction = null, object initialValue = null)
246 throw new ArgumentNullException(nameof(target));
249 string strAlpha = alphaFunction?.GetDescription();
251 foreach (var item in visualDictionary.ToList())
253 if (item.Value.Name == target.Name)
255 using (PropertyMap animator = new PropertyMap())
256 using (PropertyMap timePeriod = new PropertyMap())
257 using (PropertyValue pvDuration = new PropertyValue((endTime - startTime) / 1000.0f))
258 using (PropertyValue pvDelay = new PropertyValue(startTime / 1000.0f))
259 using (PropertyValue destVal = PropertyValue.CreateFromObject(destinationValue))
260 using (PropertyMap transition = new PropertyMap())
261 using (PropertyValue pvTarget = new PropertyValue(target.Name))
263 if (strAlpha != null)
265 using (PropertyValue pvAlpha = new PropertyValue(strAlpha))
267 animator.Add("alphaFunction", pvAlpha);
270 timePeriod.Add("duration", pvDuration);
271 timePeriod.Add("delay", pvDelay);
272 using (PropertyValue pvTimePeriod = new PropertyValue(timePeriod))
274 animator.Add("timePeriod", pvTimePeriod);
277 StringBuilder sb = new StringBuilder(property);
278 sb[0] = (char)(sb[0] | 0x20);
279 string _str = sb.ToString();
280 if (_str == "position") { _str = "offset"; }
282 transition.Add("target", pvTarget);
283 using (PropertyValue pvStr = new PropertyValue(_str))
285 transition.Add("property", pvStr);
288 if (initialValue != null)
290 using (PropertyValue initVal = PropertyValue.CreateFromObject(initialValue))
291 using (PropertyValue pvInitialValue = new PropertyValue(initVal))
293 transition.Add("initialValue", pvInitialValue);
296 transition.Add("targetValue", destVal);
297 using (PropertyValue pvAnimator = new PropertyValue(animator))
299 transition.Add("animator", pvAnimator);
302 using (TransitionData transitionData = new TransitionData(transition))
304 return this.CreateTransition(transitionData);
313 /// Adds a group visual animation (transition) map with the input parameters.
315 /// <param name="target">The visual map to animation.</param>
316 /// <param name="property">The property of visual to animation.</param>
317 /// <param name="destinationValue">The destination value of property after animation.</param>
318 /// <param name="startTime">The start time of visual animation.</param>
319 /// <param name="endTime">The end time of visual animation.</param>
320 /// <param name="alphaFunction">The alpha function of visual animation.</param>
321 /// <param name="initialValue">The initial property value of visual animation.</param>
322 /// <exception cref="ArgumentNullException"> Thrown when target is null. </exception>
323 /// <since_tizen> 3 </since_tizen>
324 public void AnimateVisualAdd(VisualMap target, string property, object destinationValue, int startTime, int endTime, AlphaFunction.BuiltinFunctions? alphaFunction = null, object initialValue = null)
328 throw new ArgumentNullException(nameof(target));
331 string strAlpha = alphaFunction?.GetDescription();
333 foreach (var item in visualDictionary.ToList())
335 if (item.Value.Name == target.Name)
337 using (PropertyMap animator = new PropertyMap())
338 using (PropertyMap timePeriod = new PropertyMap())
339 using (PropertyValue pvDuration = new PropertyValue((endTime - startTime) / 1000.0f))
340 using (PropertyValue pvDelay = new PropertyValue(startTime / 1000.0f))
341 using (PropertyValue destVal = PropertyValue.CreateFromObject(destinationValue))
342 using (PropertyMap transition = new PropertyMap())
343 using (PropertyValue pvTarget = new PropertyValue(target.Name))
345 if (strAlpha != null)
347 using (PropertyValue pvStrAlpha = new PropertyValue(strAlpha))
349 animator.Add("alphaFunction", pvStrAlpha);
353 timePeriod.Add("duration", pvDuration);
354 timePeriod.Add("delay", pvDelay);
355 using (PropertyValue pvTimePeriod = new PropertyValue(timePeriod))
357 animator.Add("timePeriod", pvTimePeriod);
360 StringBuilder sb = new StringBuilder(property);
361 sb[0] = (char)(sb[0] | 0x20);
362 string _str = sb.ToString();
363 if (_str == "position") { _str = "offset"; }
365 transition.Add("target", pvTarget);
366 using (PropertyValue pvStr = new PropertyValue(_str))
368 transition.Add("property", pvStr);
371 if (initialValue != null)
373 using (PropertyValue initVal = PropertyValue.CreateFromObject(initialValue))
374 using (PropertyValue pvInitialValue = new PropertyValue(initVal))
376 transition.Add("initialValue", pvInitialValue);
379 transition.Add("targetValue", destVal);
380 using (PropertyValue pvAnimator = new PropertyValue(animator))
382 transition.Add("animator", pvAnimator);
385 using (PropertyValue pvTransition = new PropertyValue(transition))
387 PropertyArray temp = animateArray.Add(pvTransition);
396 /// Finishes to add a visual animation (transition) map and creates a transition animation.
398 /// <returns>Animation instance.</returns>
399 /// <since_tizen> 3 </since_tizen>
400 public Animation AnimateVisualAddFinish()
402 if (animateArray == null || animateArray.Empty())
404 Tizen.Log.Fatal("NUI", "animate visual property array is empty!");
407 TransitionData transitionData = new TransitionData(animateArray);
408 Animation ret = this.CreateTransition(transitionData);
409 transitionData.Dispose();
414 /// temporary fix to pass TCT.
416 /// <exception cref="ArgumentNullException"> Thrown when visualMap is null. </exception>
417 /// <since_tizen> 3 </since_tizen>
418 public Animation VisualAnimate(Tizen.NUI.VisualAnimator visualMap)
420 if (visualMap == null)
422 throw new ArgumentNullException(nameof(visualMap));
425 foreach (var item in visualDictionary.ToList())
427 if (item.Value.Name == visualMap.Target)
429 using (TransitionData transitionData = new TransitionData(visualMap.OutputVisualMap))
431 return this.CreateTransition(transitionData);
437 //temporary fix to pass TCT
439 internal void UpdateVisual(int visualIndex, string visualName, VisualMap visualMap)
441 VisualBase visual = null;
443 visual = VisualFactory.Instance.CreateVisual(visualMap.OutputVisualMap);
444 visual.Name = visualName;
445 visual.DepthIndex = visualMap.DepthIndex;
447 RegisterVisual(visualIndex, visual);
449 visualDictionary[visualIndex] = visual;
450 tranformDictionary[visualIndex] = visualMap.OutputTransformMap;
453 NUILog.Debug("UpdateVisual() name=" + visualName);
456 static CustomView CreateInstance()
458 return new VisualView();