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() : base(typeof(VisualView).FullName, 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) : base(typeof(VisualView).FullName, CustomViewBehaviour.ViewBehaviourDefault | CustomViewBehaviour.RequiresTouchEventsSupport, viewStyle)
65 // static constructor registers the control type (for user can add kinds of visuals to it)
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));
74 /// Gets the total number of visuals which are added by users.
76 /// <since_tizen> 3 </since_tizen>
77 public int NumberOfVisuals
81 return _visualDictionary.Count;
86 /// Overrides the parent method.
88 /// <since_tizen> 3 </since_tizen>
89 public override void OnInitialize()
92 _visualDictionary = new Dictionary<int, VisualBase>();
93 _tranformDictionary = new Dictionary<int, PropertyMap>();
94 _animateArray = new PropertyArray();
98 /// Adds or updates a visual to visual view.
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)
105 VisualBase visual = null;
106 int visualIndex = -1;
108 /* If the visual had added, then replace it using RegisterVusal. */
109 //visual.Name = name;
110 foreach (var item in _visualDictionary)
112 if (item.Value.Name == visualName)
114 /* Find a existed visual, its key also exited. */
115 visualIndex = item.Key;
116 UnregisterVisual(visualIndex);
117 _visualDictionary.Remove(visualIndex);
118 _tranformDictionary.Remove(visualIndex);
123 if (visualIndex == -1) // The visual is a new one, create index for it. */
125 visualIndex = RegisterProperty(visualName, new PropertyValue(visualName), PropertyAccessMode.ReadWrite);
130 visual = VisualFactory.Instance.CreateVisual(visualMap.OutputVisualMap); // Create a visual for the new one.
131 visual.Name = visualName;
132 visual.DepthIndex = visualMap.DepthIndex;
134 RegisterVisual(visualIndex, visual);
136 _visualDictionary.Add(visualIndex, visual);
137 _tranformDictionary.Add(visualIndex, visualMap.OutputTransformMap);
139 visualMap.VisualIndex = visualIndex;
140 visualMap.Name = visualName;
141 visualMap.Parent = this;
148 /// Removes a visual by name.
150 /// <param name="visualName">The name of a visual to remove.</param>
151 /// <since_tizen> 3 </since_tizen>
152 public void RemoveVisual(string visualName)
154 foreach (var item in _visualDictionary)
156 if (item.Value.Name == visualName)
158 EnableVisual(item.Key, false);
159 UnregisterVisual(item.Key);
160 _tranformDictionary.Remove(item.Key);
161 _visualDictionary.Remove(item.Key);
170 /// Removes all visuals of the visual view.
172 /// <since_tizen> 3 </since_tizen>
173 public void RemoveAll()
175 foreach (var item in _visualDictionary)
177 EnableVisual(item.Key, false);
178 UnregisterVisual(item.Key);
180 _visualDictionary.Clear();
181 _tranformDictionary.Clear();
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 />
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)
197 foreach (var item in _visualDictionary)
199 item.Value.SetTransformAndSize(_tranformDictionary[item.Key], size);
200 EnableVisual(item.Key, true);
205 /// Creates a visual animation (transition) with the input parameters.
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)
218 string _alphaFunction = alphaFunction?.GetDescription();
220 foreach (var item in _visualDictionary.ToList())
222 if (item.Value.Name == target.Name)
224 PropertyMap _animator = new PropertyMap();
225 if (_alphaFunction != null) { _animator.Add("alphaFunction", new PropertyValue(_alphaFunction)); }
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));
232 StringBuilder sb = new StringBuilder(property);
233 sb[0] = (char)(sb[0] | 0x20);
234 string _str = sb.ToString();
235 if (_str == "position") { _str = "offset"; }
237 PropertyValue destVal = PropertyValue.CreateFromObject(destinationValue);
239 PropertyMap _transition = new PropertyMap();
240 _transition.Add("target", new PropertyValue(target.Name));
241 _transition.Add("property", new PropertyValue(_str));
242 if (initialValue != null)
244 PropertyValue initVal = PropertyValue.CreateFromObject(initialValue);
245 _transition.Add("initialValue", new PropertyValue(initVal));
247 _transition.Add("targetValue", destVal);
248 _transition.Add("animator", new PropertyValue(_animator));
250 TransitionData _transitionData = new TransitionData(_transition);
251 return this.CreateTransition(_transitionData);
258 /// Adds a group visual animation (transition) map with the input parameters.
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)
270 string _alphaFunction = alphaFunction?.GetDescription();
272 foreach (var item in _visualDictionary.ToList())
274 if (item.Value.Name == target.Name)
276 PropertyMap _animator = new PropertyMap();
277 if (_alphaFunction != null) { _animator.Add("alphaFunction", new PropertyValue(_alphaFunction)); }
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));
284 StringBuilder sb = new StringBuilder(property);
285 sb[0] = (char)(sb[0] | 0x20);
286 string _str = sb.ToString();
287 if (_str == "position") { _str = "offset"; }
289 PropertyValue destVal = PropertyValue.CreateFromObject(destinationValue);
291 PropertyMap _transition = new PropertyMap();
292 _transition.Add("target", new PropertyValue(target.Name));
293 _transition.Add("property", new PropertyValue(_str));
294 if (initialValue != null)
296 PropertyValue initVal = PropertyValue.CreateFromObject(initialValue);
297 _transition.Add("initialValue", new PropertyValue(initVal));
299 _transition.Add("targetValue", destVal);
300 _transition.Add("animator", new PropertyValue(_animator));
302 _animateArray.Add(new PropertyValue(_transition));
308 /// Finishes to add a visual animation (transition) map and creates a transition animation.
310 /// <returns>Animation instance.</returns>
311 /// <since_tizen> 3 </since_tizen>
312 public Animation AnimateVisualAddFinish()
314 if (_animateArray == null || _animateArray.Empty())
316 Tizen.Log.Fatal("NUI", "animate visual property array is empty!");
319 TransitionData _transitionData = new TransitionData(_animateArray);
321 return this.CreateTransition(_transitionData);
325 /// temporary fix to pass TCT.
327 /// <since_tizen> 3 </since_tizen>
328 public Animation VisualAnimate(Tizen.NUI.VisualAnimator visualMap)
330 foreach (var item in _visualDictionary.ToList())
332 if (item.Value.Name == visualMap.Target)
334 TransitionData _transitionData = new TransitionData(visualMap.OutputVisualMap);
335 return this.CreateTransition(_transitionData);
340 //temporary fix to pass TCT
342 internal void UpdateVisual(int visualIndex, string visualName, VisualMap visualMap)
344 VisualBase visual = null;
346 visual = VisualFactory.Instance.CreateVisual(visualMap.OutputVisualMap);
347 visual.Name = visualName;
348 visual.DepthIndex = visualMap.DepthIndex;
350 RegisterVisual(visualIndex, visual);
352 _visualDictionary[visualIndex] = visual;
353 _tranformDictionary[visualIndex] = visualMap.OutputTransformMap;
356 NUILog.Debug("UpdateVisual() name=" + visualName);
359 static CustomView CreateInstance()
361 return new VisualView();