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 Dictionary<int, string> visualNameDictionary = 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 visualNameDictionary = new Dictionary<int, string>();
110 visualDictionary = new Dictionary<int, VisualBase>();
111 tranformDictionary = new Dictionary<int, PropertyMap>();
112 animateArray = new PropertyArray();
116 /// Adds or updates a visual to visual view.
118 /// <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>
119 /// <param name="visualMap">The property map of a visual to create.</param>
120 /// <exception cref="ArgumentNullException"> Thrown when visualMap is null. </exception>
121 /// <since_tizen> 3 </since_tizen>
122 public void AddVisual(string visualName, VisualMap visualMap)
124 int visualIndex = -1;
126 /* If the visual had added, then replace it using RegisterVusal. */
127 //visual.Name = name;
128 foreach (var item in visualNameDictionary)
130 if (item.Value == visualName)
132 /* Find a existed visual, its key also exited. */
133 visualIndex = item.Key;
134 UnregisterVisual(visualIndex);
135 visualNameDictionary.Remove(visualIndex);
136 visualDictionary.Remove(visualIndex);
137 tranformDictionary.Remove(visualIndex);
142 if (visualIndex == -1) // The visual is a new one, create index for it. */
144 using (var temp = new PropertyValue(visualName))
146 visualIndex = RegisterProperty(visualName, temp, PropertyAccessMode.ReadWrite);
152 if (visualMap == null)
154 throw new ArgumentNullException(nameof(visualMap));
157 visualMap.VisualIndex = visualIndex;
158 visualMap.Name = visualName;
159 visualMap.Parent = this;
161 // Register index and name
162 UpdateVisual(visualIndex, visualName, visualMap);
167 /// Removes a visual by name.
169 /// <param name="visualName">The name of a visual to remove.</param>
170 /// <since_tizen> 3 </since_tizen>
171 public void RemoveVisual(string visualName)
173 foreach (var item in visualNameDictionary)
175 if (item.Value == visualName)
177 EnableVisual(item.Key, false);
178 UnregisterVisual(item.Key);
179 tranformDictionary.Remove(item.Key);
180 visualDictionary.Remove(item.Key);
181 visualNameDictionary.Remove(item.Key);
190 /// Removes all visuals of the visual view.
192 /// <since_tizen> 3 </since_tizen>
193 public void RemoveAll()
195 foreach (var item in visualNameDictionary)
197 EnableVisual(item.Key, false);
198 UnregisterVisual(item.Key);
200 visualDictionary.Clear();
201 tranformDictionary.Clear();
202 visualNameDictionary.Clear();
207 /// Overrides the method of OnRelayout() for CustomView class.<br />
208 /// Called after the size negotiation has been finished for this control.<br />
209 /// The control is expected to assign this given size to itself or its children.<br />
210 /// 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 />
212 /// <remarks>As this function is called from inside the size negotiation algorithm, you cannot call RequestRelayout (the call would just be ignored).</remarks>
213 /// <param name="size">The allocated size.</param>
214 /// <param name="container">The control should add actors to this container that it is not able to allocate a size for.</param>
215 /// <since_tizen> 3 </since_tizen>
216 public override void OnRelayout(Vector2 size, RelayoutContainer container)
218 foreach (var item in visualDictionary)
220 if(item.Value != null)
222 item.Value.SetTransformAndSize(tranformDictionary[item.Key], size);
223 EnableVisual(item.Key, true);
229 /// Creates a visual animation (transition) with the input parameters.
231 /// <param name="target">The visual map to animation.</param>
232 /// <param name="property">The property of visual to animation.</param>
233 /// <param name="destinationValue">The destination value of property after animation.</param>
234 /// <param name="startTime">The start time of visual animation.</param>
235 /// <param name="endTime">The end time of visual animation.</param>
236 /// <param name="alphaFunction">The alpha function of visual animation.</param>
237 /// <param name="initialValue">The initial property value of visual animation.</param>
238 /// <returns>Animation instance</returns>
239 /// <exception cref="ArgumentNullException"> Thrown when target is null. </exception>
240 /// <since_tizen> 3 </since_tizen>
241 public Animation AnimateVisual(VisualMap target, string property, object destinationValue, int startTime, int endTime, AlphaFunction.BuiltinFunctions? alphaFunction = null, object initialValue = null)
245 throw new ArgumentNullException(nameof(target));
248 string strAlpha = alphaFunction?.GetDescription();
250 foreach (var item in visualNameDictionary.ToList())
252 if (item.Value == target.Name)
254 using (PropertyMap animator = new PropertyMap())
255 using (PropertyMap timePeriod = new PropertyMap())
256 using (PropertyValue pvDuration = new PropertyValue((endTime - startTime) / 1000.0f))
257 using (PropertyValue pvDelay = new PropertyValue(startTime / 1000.0f))
258 using (PropertyValue destVal = PropertyValue.CreateFromObject(destinationValue))
259 using (PropertyMap transition = new PropertyMap())
260 using (PropertyValue pvTarget = new PropertyValue(target.Name))
262 if (strAlpha != null)
264 using (PropertyValue pvAlpha = new PropertyValue(strAlpha))
266 animator.Add("alphaFunction", pvAlpha);
269 timePeriod.Add("duration", pvDuration);
270 timePeriod.Add("delay", pvDelay);
271 using (PropertyValue pvTimePeriod = new PropertyValue(timePeriod))
273 animator.Add("timePeriod", pvTimePeriod);
276 StringBuilder sb = new StringBuilder(property);
277 sb[0] = (char)(sb[0] | 0x20);
278 string _str = sb.ToString();
279 if (_str == "position") { _str = "offset"; }
281 transition.Add("target", pvTarget);
282 using (PropertyValue pvStr = new PropertyValue(_str))
284 transition.Add("property", pvStr);
287 if (initialValue != null)
289 using (PropertyValue initVal = PropertyValue.CreateFromObject(initialValue))
290 using (PropertyValue pvInitialValue = new PropertyValue(initVal))
292 transition.Add("initialValue", pvInitialValue);
295 transition.Add("targetValue", destVal);
296 using (PropertyValue pvAnimator = new PropertyValue(animator))
298 transition.Add("animator", pvAnimator);
301 using (TransitionData transitionData = new TransitionData(transition))
303 return this.CreateTransition(transitionData);
312 /// Adds a group visual animation (transition) map with the input parameters.
314 /// <param name="target">The visual map to animation.</param>
315 /// <param name="property">The property of visual to animation.</param>
316 /// <param name="destinationValue">The destination value of property after animation.</param>
317 /// <param name="startTime">The start time of visual animation.</param>
318 /// <param name="endTime">The end time of visual animation.</param>
319 /// <param name="alphaFunction">The alpha function of visual animation.</param>
320 /// <param name="initialValue">The initial property value of visual animation.</param>
321 /// <exception cref="ArgumentNullException"> Thrown when target is null. </exception>
322 /// <since_tizen> 3 </since_tizen>
323 public void AnimateVisualAdd(VisualMap target, string property, object destinationValue, int startTime, int endTime, AlphaFunction.BuiltinFunctions? alphaFunction = null, object initialValue = null)
327 throw new ArgumentNullException(nameof(target));
330 string strAlpha = alphaFunction?.GetDescription();
332 foreach (var item in visualNameDictionary.ToList())
334 if (item.Value == target.Name)
336 using (PropertyMap animator = new PropertyMap())
337 using (PropertyMap timePeriod = new PropertyMap())
338 using (PropertyValue pvDuration = new PropertyValue((endTime - startTime) / 1000.0f))
339 using (PropertyValue pvDelay = new PropertyValue(startTime / 1000.0f))
340 using (PropertyValue destVal = PropertyValue.CreateFromObject(destinationValue))
341 using (PropertyMap transition = new PropertyMap())
342 using (PropertyValue pvTarget = new PropertyValue(target.Name))
344 if (strAlpha != null)
346 using (PropertyValue pvStrAlpha = new PropertyValue(strAlpha))
348 animator.Add("alphaFunction", pvStrAlpha);
352 timePeriod.Add("duration", pvDuration);
353 timePeriod.Add("delay", pvDelay);
354 using (PropertyValue pvTimePeriod = new PropertyValue(timePeriod))
356 animator.Add("timePeriod", pvTimePeriod);
359 StringBuilder sb = new StringBuilder(property);
360 sb[0] = (char)(sb[0] | 0x20);
361 string _str = sb.ToString();
362 if (_str == "position") { _str = "offset"; }
364 transition.Add("target", pvTarget);
365 using (PropertyValue pvStr = new PropertyValue(_str))
367 transition.Add("property", pvStr);
370 if (initialValue != null)
372 using (PropertyValue initVal = PropertyValue.CreateFromObject(initialValue))
373 using (PropertyValue pvInitialValue = new PropertyValue(initVal))
375 transition.Add("initialValue", pvInitialValue);
378 transition.Add("targetValue", destVal);
379 using (PropertyValue pvAnimator = new PropertyValue(animator))
381 transition.Add("animator", pvAnimator);
384 using (PropertyValue pvTransition = new PropertyValue(transition))
386 PropertyArray temp = animateArray.Add(pvTransition);
395 /// Finishes to add a visual animation (transition) map and creates a transition animation.
397 /// <returns>Animation instance.</returns>
398 /// <since_tizen> 3 </since_tizen>
399 public Animation AnimateVisualAddFinish()
401 if (animateArray == null || animateArray.Empty())
403 Tizen.Log.Fatal("NUI", "animate visual property array is empty!");
406 TransitionData transitionData = new TransitionData(animateArray);
407 Animation ret = this.CreateTransition(transitionData);
408 transitionData.Dispose();
413 /// temporary fix to pass TCT.
415 /// <exception cref="ArgumentNullException"> Thrown when visualMap is null. </exception>
416 /// <since_tizen> 3 </since_tizen>
417 public Animation VisualAnimate(Tizen.NUI.VisualAnimator visualMap)
419 if (visualMap == null)
421 throw new ArgumentNullException(nameof(visualMap));
424 foreach (var item in visualNameDictionary.ToList())
426 if (item.Value == visualMap.Target)
428 using (TransitionData transitionData = new TransitionData(visualMap.OutputVisualMap))
430 return this.CreateTransition(transitionData);
436 //temporary fix to pass TCT
438 internal void UpdateVisual(int visualIndex, string visualName, VisualMap visualMap)
440 VisualBase visual = VisualFactory.Instance.CreateVisual(visualMap.OutputVisualMap);
442 visualNameDictionary[visualIndex] = visualName;
443 visualDictionary[visualIndex] = visual;
444 tranformDictionary[visualIndex] = visualMap.OutputTransformMap;
448 visual.Name = visualName;
449 visual.DepthIndex = visualMap.DepthIndex;
451 RegisterVisual(visualIndex, visual);
454 NUILog.Debug("UpdateVisual() name=" + visualName);
458 NUILog.Debug("UpdateVisual() FAIL! visual create failed name=" + visualName);
462 static CustomView CreateInstance()
464 return new VisualView();