Visual example, NUIApplication, Size/Position
[platform/core/csapi/nui.git] / Tizen.NUI / src / public / ContentView.cs
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 using System;
19 using System.Runtime.InteropServices;
20 using System.Collections.Generic;
21 using System.Linq;
22 using Tizen.NUI;
23
24 // A contentView control (for use can add image or text to it.)
25
26 namespace Tizen.NUI
27 {
28     public class ContentView : CustomView
29     {
30         private int _iconVisualIndex;
31         private string _iconVisualUrl;
32         private bool _activeContent = false;
33         private bool _transitionInProgress = false;
34
35         private VisualBase _iconVisual;
36         private Animation _animation;
37         private TransitionData _growTransitionData;
38         private TransitionData _shrinkTransitionData;\r
39 \r
40
41         // Called by DALi Builder if it finds a ContentView control in a JSON file
42         static CustomView CreateInstance()
43         {
44             return new ContentView();
45         }
46
47         // static constructor registers the control type (only runs once)
48         static ContentView()
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(ContentView));
53         }
54
55         public ContentView() : base(typeof(ContentView).Name, ViewBehaviour.ViewBehaviourDefault | ViewBehaviour.RequiresKeyboardNavigationSupport)
56         {
57
58         }
59
60         public override void OnInitialize()
61         {
62             _iconVisualIndex = RegisterProperty("iconVisual", new PropertyValue("./image/application-icon-0.png"), PropertyAccessMode.ReadWrite);
63
64             PropertyMap animator = new PropertyMap();
65             animator.Add("alphaFunction", new PropertyValue("LINEAR"));
66
67             PropertyMap timePeriod = new PropertyMap();
68             timePeriod.Add("duration", new PropertyValue(0.8f));
69             timePeriod.Add("delay", new PropertyValue(0.0f));
70             animator.Add("timePeriod", new PropertyValue(timePeriod));
71
72             PropertyMap growTransition = new PropertyMap();
73             growTransition.Add("target", new PropertyValue("iconVisual"));
74             growTransition.Add("property", new PropertyValue("size"));
75             growTransition.Add("targetValue", new PropertyValue(new Vector2(100.0f, 100.0f)));
76             growTransition.Add("animator", new PropertyValue(animator));
77             _growTransitionData = new TransitionData(growTransition);
78
79
80             PropertyMap shrinkTransition = new PropertyMap();
81             shrinkTransition.Add("target", new PropertyValue("iconVisual"));
82             shrinkTransition.Add("property", new PropertyValue("size"));
83             shrinkTransition.Add("targetValue", new PropertyValue(new Vector2(50.0f, 50.0f)));
84             shrinkTransition.Add("animator", new PropertyValue(animator));
85             _shrinkTransitionData = new TransitionData(shrinkTransition);\r
86         }
87
88         /*public virtual void OnRelayout(Vector2 size, RelayoutContainer container)
89         {
90           Console.WriteLine ("--------------OnRelayout.");
91           RelayoutVisuals( size);
92         }*/
93
94         private void RelayoutVisuals(Vector2 size)
95         {
96             if (!_transitionInProgress)
97             {
98                 if (_iconVisual)
99                 {
100                     PropertyMap iconVisualTransform = new PropertyMap();
101                     iconVisualTransform.Add((int)VisualTransformPropertyType.SIZE, new PropertyValue(new Vector2(50.0f, 50.0f)));
102                     iconVisualTransform.Add((int)VisualTransformPropertyType.OFFSET, new PropertyValue(new Vector2(5.0f, 5.0f)));
103                     iconVisualTransform.Add((int)VisualTransformPropertyType.OFFSET_SIZE_MODE, new PropertyValue(new Vector4(1.0f, 1.0f, 1.0f, 1.0f)));
104                     iconVisualTransform.Add((int)VisualTransformPropertyType.ORIGIN, new PropertyValue((int)AlignType.TOP_BEGIN));
105                     iconVisualTransform.Add((int)VisualTransformPropertyType.ANCHOR_POINT, new PropertyValue((int)AlignType.TOP_BEGIN));
106                     _iconVisual.SetTransformAndSize(iconVisualTransform, size);
107                 }
108             }
109         }
110
111         [ScriptableProperty()]
112         public string IconVisual
113         {
114             get
115             {
116                 return _iconVisualUrl;
117             }
118             set
119             {
120                 _iconVisualUrl = value;
121                 if (_iconVisual)
122                 {
123                     //RelayoutRequest();
124                     EnableVisual(_iconVisualIndex, false);
125                     UnregisterVisual(_iconVisualIndex);
126                 }
127
128                 PropertyMap iconVisualMap = new PropertyMap(); ;
129                 iconVisualMap.Add(Tizen.NUI.Constants.Visual.Property.Type, new PropertyValue((int)Tizen.NUI.Constants.Visual.Type.Image));
130                 iconVisualMap.Add(Tizen.NUI.Constants.ImageVisualProperty.URL, new PropertyValue(_iconVisualUrl));
131                 _iconVisual = VisualFactory.Get().CreateVisual(iconVisualMap);
132                 _iconVisual.Name = ("iconVisual");
133
134                 RelayoutVisuals(new Vector2(250.0f, 250.0f));
135                 RegisterVisual(_iconVisualIndex, _iconVisual);
136                 _iconVisual.DepthIndex = (1.0f);
137             }
138
139         }
140
141
142
143         [ScriptableProperty()]
144         public bool ActiveContent
145         {
146             get
147             {
148                 return _activeContent;
149             }
150             set
151             {
152                 _activeContent = value;
153                 StartTransition(_activeContent);
154             }
155         }
156
157         private void StartTransition(bool activate)
158         {
159             if (_animation)
160             {
161                 _animation.Stop();
162                 _animation.Finished += OnTransitionFinished;
163             }
164             if (activate)
165             {
166                 _animation = CreateTransition(_growTransitionData);
167             }
168             else
169             {
170                 _animation = CreateTransition(_shrinkTransitionData);
171             }
172
173             if (_animation)
174             {
175                 _animation.Finished += OnTransitionFinished;
176                 _transitionInProgress = true;
177                 _animation.Play();
178             }
179         }
180
181         private void OnTransitionFinished(object sender, EventArgs e)
182         {
183             _transitionInProgress = false;
184             if (_animation)
185             {
186                 _animation.Finished += OnTransitionFinished;
187                 _animation.Reset();
188             }
189         }
190
191         public ImageVisualMap ImageVisual\r
192         {\r
193             set\r
194             {\r
195                 if (value.URL != "")
196                 {\r
197                     VisualBase _imageVisual;\r
198                     int _imageVisualIndex = RegisterProperty("ImageVisual", new PropertyValue(value.URL), PropertyAccessMode.ReadWrite);\r
199                     _imageVisual = VisualFactory.Get().CreateVisual(value.OutputMap);\r
200                     RegisterVisual(_imageVisualIndex, _imageVisual);\r
201                     _imageVisual.DepthIndex = value.DepthIndex;
202                 }\r
203             }\r
204         }\r
205 \r
206         public TextVisualMap TextVisual\r
207         {\r
208             set\r
209             {\r
210                 if (value.Text != "" && value.PointSize != 0)
211                 {\r
212                     Tizen.Log.Debug("NUI", "p2 !!!!!");\r
213 \r
214                     PropertyMap _outputMap = new PropertyMap(); ;\r
215                     _outputMap.Add(Tizen.NUI.Constants.Visual.Property.Type, new PropertyValue((int)Tizen.NUI.Constants.Visual.Type.WireFrame + 1));\r
216                     _outputMap.Add(Tizen.NUI.Constants.TextVisualProperty.Text, new PropertyValue("ABCD"));\r
217                     _outputMap.Add(Tizen.NUI.Constants.TextVisualProperty.PointSize, new PropertyValue(10.0f));\r
218 \r
219                     VisualBase _textVisual;\r
220                     int _textVisualIndex = RegisterProperty("TextVisual", new PropertyValue(_outputMap), PropertyAccessMode.ReadWrite);\r
221                     _textVisual = VisualFactory.Get().CreateVisual(_outputMap);\r
222                     RegisterVisual(_textVisualIndex, _textVisual, true);\r
223                     _textVisual.DepthIndex = value.DepthIndex;
224                     Tizen.Log.Debug("NUI", "p2 !!!!! _textVisualIndex=" + _textVisualIndex + "is enabled?=" + IsVisualEnabled(_textVisualIndex));
225                 }\r
226             }\r
227         }
228
229     }
230 }