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
19 using System.Runtime.InteropServices;
20 using System.Collections.Generic;
24 /// A visual view control for user add any visual to it.
29 /// VisualView _visualView = new VisualView();
30 /// ImageVisualMap imageVisualMap1 = new ImageVisualMap();
31 /// imageVisualMap1.URL = "./NUISample/res/images/image-1.jpg";
32 /// imageVisualMap1.VisualSize = new Vector2( 300.0f, 300.0f );
33 /// imageVisualMap1.Offset = new Vector2( 50.0f, 50.0f );
34 /// imageVisualMap1.OffsetSizeMode = new Vector4( 1.0f, 1.0f, 1.0f, 1.0f );
35 /// imageVisualMap1.Origin = AlignType.TOP_BEGIN;
36 /// imageVisualMap1.AnchorPoint = AlignType.TOP_BEGIN;
37 /// _visualView.AddVisual("imageVisual1", imageVisualMap1);
40 public class VisualView : CustomView
42 //private LinkedList<VisualBase> _visualList = null;
43 private Dictionary<int, VisualBase> _visualDictionary = null;
44 private Dictionary<int, PropertyMap> _tranformDictionary = null;
46 static CustomView CreateInstance()
48 return new VisualView();
51 // static constructor registers the control type (for user can add kinds of visuals to it)
54 // ViewRegistry registers control type with DALi type registery
55 // also uses introspection to find any properties that need to be registered with type registry
56 ViewRegistry.Instance.Register(CreateInstance, typeof(VisualView));
59 public VisualView() : base(typeof(VisualView).Name, CustomViewBehaviour.ViewBehaviourDefault)
64 /// Override the parent method.
66 public override void OnInitialize()
69 _visualDictionary = new Dictionary<int, VisualBase>();
70 _tranformDictionary = new Dictionary<int, PropertyMap>();
74 /// Add or update a visual to visual view.
76 /// <param name="visualName"> The name of visual to add. If add a existed visual name, the visual will be replaced. </param>
77 /// <param name="visualMap"> The property map of visual to create. </param>
78 public void AddVisual(string visualName, VisualMap visualMap)
80 VisualBase visual = null;
83 /* If the visual had added, then replace it using RegisterVusal. */
85 foreach (var item in _visualDictionary)
87 if (item.Value.Name == visualName)
89 /* Find a existed visual, its key also exited. */
90 visualIndex = item.Key;
91 UnregisterVisual(visualIndex);
92 _visualDictionary.Remove(visualIndex);
93 _tranformDictionary.Remove(visualIndex);
98 if (visualIndex == -1) // The visual is a new one, create index for it. */
100 visualIndex = RegisterProperty(visualName, new PropertyValue(visualName), PropertyAccessMode.ReadWrite);
105 visual = VisualFactory.Get().CreateVisual(visualMap.OutputVisualMap); // Create a visual for the new one.
106 visual.Name = visualName;
107 visual.DepthIndex = visualMap.DepthIndex;
109 RegisterVisual(visualIndex, visual);
111 _visualDictionary.Add(visualIndex, visual);
112 _tranformDictionary.Add(visualIndex, visualMap.OutputTransformMap);
114 visualMap.VisualIndex = visualIndex;
115 visualMap.Name = visualName;
116 visualMap.Parent = this;
123 /// Remove a visual by name.
125 /// <param name="visualName"> The name of visual to remove. </param>
126 public void RemoveVisual(string visualName)
128 foreach (var item in _visualDictionary)
130 if (item.Value.Name == visualName)
132 EnableVisual(item.Key, false);
133 UnregisterVisual(item.Key);
134 _tranformDictionary.Remove(item.Key);
135 _visualDictionary.Remove(item.Key);
144 /// Get the total number of Visuals which are added by users
146 public int NumberOfVisuals
150 return _visualDictionary.Count;
155 /// Remove all visuals of visual view.
157 public void RemoveAll()
159 foreach (var item in _visualDictionary)
161 EnableVisual(item.Key, false);
162 UnregisterVisual(item.Key);
164 _visualDictionary.Clear();
165 _tranformDictionary.Clear();
170 /// Override method of OnRelayout() for CustomView class.<br>
171 /// Called after the size negotiation has been finished for this control.<br>
172 /// The control is expected to assign this given size to itself/its children.<br>
173 /// 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>
175 /// <remarks>As this function is called from inside the size negotiation algorithm, you cannot call RequestRelayout (the call would just be ignored)</remarks>
176 /// <param name="size">The allocated size</param>
177 /// <param name="container">The control should add actors to this container that it is not able to allocate a size for.</param>
178 public override void OnRelayout(Vector2 size, RelayoutContainer container)
180 foreach (var item in _visualDictionary)
182 item.Value.SetTransformAndSize(_tranformDictionary[item.Key], size);
183 EnableVisual(item.Key, true);
187 internal void UpdateVisual(int visualIndex, string visualName, VisualMap visualMap)
189 VisualBase visual = null;
191 visual = VisualFactory.Get().CreateVisual(visualMap.OutputVisualMap);
192 visual.Name = visualName;
193 visual.DepthIndex = visualMap.DepthIndex;
195 RegisterVisual(visualIndex, visual);
197 _visualDictionary[visualIndex] = visual;
198 _tranformDictionary[visualIndex] = visualMap.OutputTransformMap;
202 Tizen.Log.Debug("NUI", "UpdateVisual() name=" + visualName);
207 /// Create visual animation (transition) with the input parameters.
209 /// <param name="target"> The visual map to animation.</param>
210 /// <param name="property"> The property of visual to animation.</param>
211 /// <param name="destinationValue"> The destination value of property after animation.</param>
212 /// <param name="startTime"> The start time of visual animation.</param>
213 /// <param name="endTime"> The end time of visual animation.</param>
214 /// <param name="alphaFunction"> The alpha function of visual animation</param>
215 /// <returns>Animation instance</returns>
216 public Animation AnimateVisual(VisualMap target, string property, object destinationValue, int startTime, int endTime, AlphaFunction.BuiltinFunctions alphaFunction)
218 string _alphaFunction = "";
219 switch (alphaFunction)
221 case Tizen.NUI.AlphaFunction.BuiltinFunctions.Linear:
223 _alphaFunction = "LINEAR";
226 case Tizen.NUI.AlphaFunction.BuiltinFunctions.Reverse:
228 _alphaFunction = "REVERSE";
231 case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseInSquare:
233 _alphaFunction = "EASE_IN_SQUARE";
236 case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseOutSquare:
238 _alphaFunction = "EASE_OUT_SQUARE";
241 case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseIn:
243 _alphaFunction = "EASE_IN";
246 case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseOut:
248 _alphaFunction = "EASE_OUT";
251 case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseInOut:
253 _alphaFunction = "EASE_IN_OUT";
256 case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseInSine:
258 _alphaFunction = "EASE_IN_SINE";
261 case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseOutSine:
263 _alphaFunction = "EASE_OUT_SINE";
266 case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseInOutSine:
268 _alphaFunction = "EASE_IN_OUT_SINE";
271 case Tizen.NUI.AlphaFunction.BuiltinFunctions.Bounce:
273 _alphaFunction = "BOUNCE";
276 case Tizen.NUI.AlphaFunction.BuiltinFunctions.Sin:
278 _alphaFunction = "SIN";
281 case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseOutBack:
283 _alphaFunction = "EASE_OUT_BACK";
288 _alphaFunction = "DEFAULT";
293 foreach (var item in _visualDictionary.ToList())
295 if (item.Value.Name == target.Name)
297 PropertyMap _animator = new PropertyMap();
298 _animator.Add("alphaFunction", new PropertyValue(_alphaFunction));
300 PropertyMap _timePeriod = new PropertyMap();
301 _timePeriod.Add("duration", new PropertyValue((endTime - startTime) / 1000.0f));
302 _timePeriod.Add("delay", new PropertyValue(startTime / 1000.0f));
303 _animator.Add("timePeriod", new PropertyValue(_timePeriod));
305 string _str1 = property.Substring(0, 1);
306 string _str2 = property.Substring(1);
307 string _str = _str1.ToLower() + _str2;
309 PropertyValue val = PropertyValue.CreateFromObject(destinationValue);
311 PropertyMap _transition = new PropertyMap();
312 _transition.Add("target", new PropertyValue(target.Name));
313 _transition.Add("property", new PropertyValue(_str));
314 _transition.Add("targetValue", val);
315 _transition.Add("animator", new PropertyValue(_animator));
317 TransitionData _transitionData = new TransitionData(_transition);
318 return this.CreateTransition(_transitionData);