Merge "[NUI-252] change string type of property to enum type of property" into tizen
[platform/core/csapi/tizenfx.git] / src / 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.
25     /// </summary>
26     /// <example>
27     /// Example:
28     /// <code>
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);
38     /// </code>
39     /// </example>
40     public class VisualView : CustomView
41     {
42         //private LinkedList<VisualBase> _visualList = null;
43         private Dictionary<int, VisualBase> _visualDictionary = null;
44         private Dictionary<int, PropertyMap> _tranformDictionary = null;
45
46         static CustomView CreateInstance()
47         {
48             return new VisualView();
49         }
50
51         // static constructor registers the control type (for user can add kinds of visuals to it)
52         static VisualView()
53         {
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));
57         }
58
59         public VisualView() : base(typeof(VisualView).Name, CustomViewBehaviour.ViewBehaviourDefault)
60         {
61         }
62
63         /// <summary>
64         /// Override the parent method.
65         /// </summary>
66         public override void OnInitialize()
67         {
68             //Initialize empty
69             _visualDictionary = new Dictionary<int, VisualBase>();
70             _tranformDictionary = new Dictionary<int, PropertyMap>();
71         }
72
73         /// <summary>
74         /// Add or update a visual to visual view.
75         /// </summary>
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)
79         {
80             VisualBase visual = null;
81             int visualIndex = -1;
82
83             /* If the visual had added, then replace it using RegisterVusal. */
84             //visual.Name = name;
85             foreach (var item in _visualDictionary)
86             {
87                 if (item.Value.Name == visualName)
88                 {
89                     /* Find a existed visual, its key also exited. */
90                     visualIndex = item.Key;
91                     UnregisterVisual(visualIndex);
92                     _visualDictionary.Remove(visualIndex);
93                     _tranformDictionary.Remove(visualIndex);
94                     break;
95                 }
96             }
97
98             if (visualIndex == -1) // The visual is a new one, create index for it. */
99             {
100                 visualIndex = RegisterProperty(visualName, new PropertyValue(visualName), PropertyAccessMode.ReadWrite);
101             }
102
103             if (visualIndex > 0)
104             {
105                 visual = VisualFactory.Get().CreateVisual(visualMap.OutputVisualMap); // Create a visual for the new one.
106                 visual.Name = visualName;
107                 visual.DepthIndex = visualMap.DepthIndex;
108
109                 RegisterVisual(visualIndex, visual);
110
111                 _visualDictionary.Add(visualIndex, visual);
112                 _tranformDictionary.Add(visualIndex, visualMap.OutputTransformMap);
113
114                 visualMap.VisualIndex = visualIndex;
115                 visualMap.Name = visualName;
116                 visualMap.Parent = this;
117
118                 RelayoutRequest();
119             }
120         }
121
122         /// <summary>
123         /// Remove a visual by name.
124         /// </summary>
125         /// <param name="visualName"> The name of visual to remove. </param>
126         public void RemoveVisual(string visualName)
127         {
128             foreach (var item in _visualDictionary.ToList())
129             {
130                 if (item.Value.Name == visualName)
131                 {
132                     EnableVisual(item.Key, false);
133                     UnregisterVisual(item.Key);
134                     _visualDictionary.Remove(item.Key);
135                 }
136             }
137         }
138
139         /// <summary>
140         ///  Get the total number of Visuals which are added by users
141         /// </summary>
142         public int NumberOfVisuals
143         {
144             get
145             {
146                 return _visualDictionary.Count;
147             }
148         }
149
150
151         /// <summary>
152         /// Remove all visuals of visual view.
153         /// </summary>
154         public void RemoveAll()
155         {
156             _visualDictionary.Clear();
157         }
158
159         /// <summary>
160         /// Override method of OnRelayout() for CustomView class.<br>
161         /// Called after the size negotiation has been finished for this control.<br>
162         /// The control is expected to assign this given size to itself/its children.<br>
163         /// 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>
164         /// </summary>
165         /// <remarks>As this function is called from inside the size negotiation algorithm, you cannot call RequestRelayout (the call would just be ignored)</remarks>
166         /// <param name="size">The allocated size</param>
167         /// <param name="container">The control should add actors to this container that it is not able to allocate a size for.</param>
168         public override void OnRelayout(Vector2 size, RelayoutContainer container)
169         {
170             foreach (var item in _visualDictionary)
171             {
172                 item.Value.SetTransformAndSize(_tranformDictionary[item.Key], size);
173                 EnableVisual(item.Key, true);
174             }
175         }
176
177         internal void UpdateVisual(int visualIndex, string visualName, VisualMap visualMap)
178         {
179             VisualBase visual = null;
180
181             visual = VisualFactory.Get().CreateVisual(visualMap.OutputVisualMap);
182             visual.Name = visualName;
183             visual.DepthIndex = visualMap.DepthIndex;
184
185             RegisterVisual(visualIndex, visual);
186
187             _visualDictionary[visualIndex] = visual;
188             _tranformDictionary[visualIndex] = visualMap.OutputTransformMap;
189
190             RelayoutRequest();
191
192             Tizen.Log.Debug("NUI", "UpdateVisual() name=" + visualName);
193         }
194
195         /// <summary>
196         /// Create visual animation (transition) with the input property map
197         /// </summary>
198         /// <param name="visualMap">property map to define visual animation</param>
199         /// <returns>Animation instance</returns>
200         public Animation AnimateVisual(AnimatorVisual visualMap)
201         {
202             foreach (var item in _visualDictionary.ToList())
203             {
204                 if (item.Value.Name == visualMap.Target)
205                 {
206                     TransitionData _transitionData = new TransitionData(visualMap.OutputVisualMap);
207                     return this.CreateTransition(_transitionData);
208                 }
209             }
210             return null;
211         }
212
213     }
214 }