Visual transition, SVG/AGIF added
[platform/core/csapi/nui.git] / Tizen.NUI / src / public / CustomView / VisualView.cs
1 // Copyright (c) 2017 Samsung Electronics Co., Ltd.
2 //
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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14 //
15
16 namespace Tizen.NUI
17 {
18     using System;
19     using System.Runtime.InteropServices;
20     using System.Collections.Generic;
21     using System.Linq;
22
23     /// <summary>
24     /// A visual view control for user add any visual to it.<br>
25     /// Example:<br>
26     /// VisualView _visualView = new VisualView();<br>
27     /// ImageVisualMap imageVisualMap1 = new ImageVisualMap();<br>
28     /// imageVisualMap1.URL = "./NUISample/res/images/image-1.jpg";<br>
29     /// imageVisualMap1.VisualSize = new Vector2( 300.0f, 300.0f );<br>
30     /// imageVisualMap1.Offset = new Vector2( 50.0f, 50.0f );<br>
31     /// imageVisualMap1.OffsetSizeMode = new Vector4( 1.0f, 1.0f, 1.0f, 1.0f );<br>
32     /// imageVisualMap1.Origin = AlignType.TOP_BEGIN;<br>
33     /// imageVisualMap1.AnchorPoint = AlignType.TOP_BEGIN;<br>
34     /// _visualView.AddVisual("imageVisual1", imageVisualMap1);<br>
35     /// </summary>
36     public class VisualView : CustomView
37     {
38         //private LinkedList<VisualBase> _visualList = null;
39         private Dictionary<int, VisualBase> _visualDictionary = null;
40         private Dictionary<int, PropertyMap> _tranformDictionary = null;
41
42         static CustomView CreateInstance()
43         {
44             return new VisualView();
45         }
46
47         // static constructor registers the control type (for user can add kinds of visuals to it)
48         static VisualView()
49         {
50             // ViewRegistry registers control type with DALi type registery
51             // also uses introspection to find any properties that need to be registered with type registry
52             ViewRegistry.Instance.Register(CreateInstance, typeof(VisualView));
53         }
54
55         public VisualView() : base(typeof(VisualView).Name, CustomViewBehaviour.ViewBehaviourDefault)
56         {
57         }
58
59         /// <summary>
60         /// Override the parent method.
61         /// </summary>
62         public override void OnInitialize()
63         {
64             //Initialize empty
65             _visualDictionary = new Dictionary<int, VisualBase>();
66             _tranformDictionary = new Dictionary<int, PropertyMap>();
67         }
68
69         /// <summary>
70         /// Add or update a visual to visual view.
71         /// </summary>
72         /// <param name="visualName"> The name of visual to add. If add a existed visual name, the visual will be replaced. </param>
73         /// <param name="visualMap"> The property map of visual to create.  </param>
74         public void AddVisual(string visualName, VisualMap visualMap)
75         {
76             VisualBase visual = null;
77             int visualIndex = -1;
78
79             /* If the visual had added, then replace it using RegisterVusal. */
80             //visual.Name = name;
81             foreach (var item in _visualDictionary)
82             {
83                 if (item.Value.Name == visualName)
84                 {
85                     /* Find a existed visual, its key also exited. */
86                     visualIndex = item.Key;
87                     UnregisterVisual(visualIndex);
88                     _visualDictionary.Remove(visualIndex);
89                     _tranformDictionary.Remove(visualIndex);
90                     break;
91                 }
92             }
93
94             if (visualIndex == -1) // The visual is a new one, create index for it. */
95             {
96                 visualIndex = RegisterProperty(visualName, new PropertyValue(visualName), PropertyAccessMode.ReadWrite);
97             }
98
99             if (visualIndex > 0)
100             {
101                 visual = VisualFactory.Get().CreateVisual(visualMap.OutputVisualMap); // Create a visual for the new one.
102                 visual.Name = visualName;
103                 visual.DepthIndex = visualMap.DepthIndex;
104
105                 RegisterVisual(visualIndex, visual);
106
107                 _visualDictionary.Add(visualIndex, visual);
108                 _tranformDictionary.Add(visualIndex, visualMap.OutputTransformMap);
109
110                 visualMap.VisualIndex = visualIndex;
111                 visualMap.Name = visualName;
112                 visualMap.Parent = this;
113
114                 RelayoutRequest();
115             }
116         }
117
118         /// <summary>
119         /// Remove a visual by name.
120         /// </summary>
121         /// <param name="visualName"> The name of visual to remove. </param>
122         public void RemoveVisual(string visualName)
123         {
124             foreach (var item in _visualDictionary.ToList())
125             {
126                 if (item.Value.Name == visualName)
127                 {
128                     EnableVisual(item.Key, false);
129                     UnregisterVisual(item.Key);
130                     _visualDictionary.Remove(item.Key);
131                 }
132             }
133         }
134
135         /// <summary>
136         ///  Get the total number of Visuals which are added by users
137         /// </summary>
138         public int NumberOfVisuals
139         {
140             get
141             {
142                 return _visualDictionary.Count;
143             }
144         }
145
146
147         /// <summary>
148         /// Remove all visuals of visual view.
149         /// </summary>
150         public void RemoveAll()
151         {
152             _visualDictionary.Clear();
153         }
154
155         /// <summary>
156         /// Override method of OnRelayout() for CustomView class.<br>
157         /// Called after the size negotiation has been finished for this control.<br>
158         /// The control is expected to assign this given size to itself/its children.<br>
159         /// 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>
160         /// Note! As this function is called from inside the size negotiation algorithm, you cannot call RequestRelayout (the call would just be ignored).<br>
161         /// </summary>
162         /// <param name="size">The allocated size</param>
163         /// <param name="container">The control should add actors to this container that it is not able to allocate a size for.</param>
164         public override void OnRelayout(Vector2 size, RelayoutContainer container)
165         {
166             foreach (var item in _visualDictionary)
167             {
168                 item.Value.SetTransformAndSize(_tranformDictionary[item.Key], size);
169                 EnableVisual(item.Key, true);
170             }
171         }
172
173         internal void UpdateVisual(int visualIndex, string visualName, VisualMap visualMap)
174         {
175             VisualBase visual = null;
176
177             visual = VisualFactory.Get().CreateVisual(visualMap.OutputVisualMap);
178             visual.Name = visualName;
179             visual.DepthIndex = visualMap.DepthIndex;
180
181             RegisterVisual(visualIndex, visual);
182
183             _visualDictionary[visualIndex] = visual;
184             _tranformDictionary[visualIndex] = visualMap.OutputTransformMap;
185
186             RelayoutRequest();
187
188             Tizen.Log.Debug("NUI", "UpdateVisual() name=" + visualName);
189         }
190
191         /// <summary>
192         /// Create visual animation (transition) with the input property map
193         /// </summary>
194         /// <param name="visualMap">property map to define visual animation</param>
195         /// <returns>Animation instance</returns>
196         public Animation AnimateVisual(AnimatorVisual visualMap)
197         {
198             foreach (var item in _visualDictionary.ToList())
199             {
200                 if (item.Value.Name == visualMap.Target)
201                 {
202                     TransitionData _transitionData = new TransitionData(visualMap.OutputVisualMap);
203                     return this.CreateTransition(_transitionData);
204     }
205             }
206             return null;
207         }
208
209     }
210 }