Revert "[Tizen] fix LineWrap GET error"
[platform/core/csapi/nui.git] / NUISamples / NUISamples / NUISamples.Tizen / examples / visuals-using-custom-view / ContactView.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 Tizen.NUI;
20 using Tizen.NUI.Constants;
21 using Tizen.NUI.BaseComponents;
22
23 // A ContactView is a Custom View which consists of four visuals (Image, Primitive, Text and Color).
24 // All of these visuals can be configured via properties - ImageURL (Image), Shape (Primitive), Name (Text) and Color.
25 // Tap gesture is also enabled on the ContactView which changes the color visual to some random color when ContactView is tapped.
26
27 namespace VisualsUsingCustomView
28 {
29     public class ContactView : CustomView
30     {
31         private VisualBase _imageVisual;
32         private VisualBase _primitiveVisual;
33         private VisualBase _textVisual;
34         private int _shape;
35         private string _imageURL;
36         private string _maskURL;
37         private string _name;
38         private Color _color;
39
40         static CustomView CreateInstance()
41         {
42             return new ContactView();
43         }
44
45         static ContactView()
46         {
47             CustomViewRegistry.Instance.Register( CreateInstance, typeof(ContactView));
48         }
49
50         public ContactView() : base( typeof(ContactView).FullName, CustomViewBehaviour.RequiresKeyboardNavigationSupport)
51         {
52         }
53
54         public string MaskURL
55         {
56             get { return _maskURL; }
57             set { _maskURL=value; }
58         }
59
60         [ScriptableProperty()]
61         public string ImageURL
62         {
63             get
64             {
65                 return _imageURL;
66             }
67             set
68             {
69                 _imageURL = value;
70
71                 ImageVisual imageVisual = new ImageVisual();
72                 imageVisual.URL = value;
73                 imageVisual.AlphaMaskURL = _maskURL;
74                 //imageVisual.MaskContentScale = 1.6f;
75                 //imageVisual.CropToMask = true;
76                 ImageVisual = imageVisual.OutputVisualMap;
77             }
78         }
79
80         [ScriptableProperty()]
81         public PropertyMap ImageVisual
82         {
83             //get
84             //{
85             //    return _imageVisual.Creation;
86             //}
87             set
88             {
89                 _imageVisual =  VisualFactory.Instance.CreateVisual( value );
90                 RegisterVisual( GetPropertyIndex("ImageVisual"), _imageVisual );
91
92                 // Set the depth index for Image visual
93                 _imageVisual.DepthIndex = 30;
94             }
95         }
96
97         [ScriptableProperty()]
98         public string NameField
99         {
100             get
101             {
102                 return _name;
103             }
104             set
105             {
106                 _name = value;
107
108                 // Create and Register Text Visual
109                 TextVisual textVisual = new TextVisual();
110                 textVisual.Text = _name;
111                 textVisual.TextColor = Color.Black;
112                 textVisual.PointSize = 12;
113                 textVisual.HorizontalAlignment = HorizontalAlignment.Center;
114                 textVisual.VerticalAlignment = VerticalAlignment.Center;
115                 NameVisual = textVisual.OutputVisualMap;
116             }
117         }
118
119         [ScriptableProperty()]
120         public PropertyMap NameVisual
121         {
122             //get
123             //{
124             //    return _textVisual.Creation;
125             //}
126             set
127             {
128                 _textVisual =  VisualFactory.Instance.CreateVisual( value );
129                 RegisterVisual( GetPropertyIndex("NameVisual"), _textVisual );
130
131                 // Set the depth index for Text visual
132                 _textVisual.DepthIndex = 30;
133             }
134         }
135
136         [ScriptableProperty()]
137         public Color Color
138         {
139             get
140             {
141                 return _color;
142             }
143             set
144             {
145                 _color = value;
146                 BackgroundColor = value;
147             }
148         }
149
150         [ScriptableProperty()]
151         public int Shape
152         {
153             get
154             {
155                 return _shape;
156             }
157             set
158             {
159                 _shape = value;
160
161                 // Create and Register Primitive Visual
162                 var primitiveVisual = new PrimitiveVisual();
163                 primitiveVisual.Shape = (PrimitiveVisualShapeType)_shape;
164                 primitiveVisual.BevelPercentage = 0.3f;
165                 primitiveVisual.BevelSmoothness = 0.0f;
166                 primitiveVisual.ScaleDimensions = new Vector3( 1.0f, 1.0f, 0.3f );
167                 primitiveVisual.MixColor = new Vector4( (245.0f/255.0f), (188.0f/255.0f), (73.0f/255.0f), 1.0f);
168
169                 ShapeVisual = primitiveVisual.OutputVisualMap;
170             }
171         }
172
173         [ScriptableProperty()]
174         public PropertyMap ShapeVisual
175         {
176             //get
177             //{
178             //    return _primitiveVisual.Creation;
179             //}
180             set
181             {
182                 _primitiveVisual =  VisualFactory.Instance.CreateVisual( value );
183                 RegisterVisual( GetPropertyIndex("ShapeVisual"), _primitiveVisual );
184
185                 // Set the depth index for Primitive visual
186                 _primitiveVisual.DepthIndex = 30;
187             }
188         }
189
190         public override void OnInitialize()
191         {
192             // Enable Tap gesture on ContactView
193             EnableGestureDetection(Gesture.GestureType.Tap);
194         }
195
196         public override void OnTap(TapGesture tap)
197         {
198             // Change the Color visual of ContactView with some random color
199             Random random = new Random();
200             float nextRed   = (random.Next(0, 256) / 255.0f);
201             float nextGreen = (random.Next(0, 256) / 255.0f);
202             float nextBlue  = (random.Next(0, 256) / 255.0f);
203             Animation anim = AnimateBackgroundColor( new Color( nextRed, nextGreen, nextBlue, 1.0f), 0, 2000 );
204             if( anim )
205                 anim.Play();
206         }
207
208         public override void OnRelayout(Vector2 size, RelayoutContainer container)
209         {
210             // Configure the transform and size of Image visual.
211             PropertyMap imageVisualTransform = new PropertyMap();
212             imageVisualTransform.Add((int)VisualTransformPropertyType.Offset, new PropertyValue(new Vector2(10.0f, 0.0f)))
213                 .Add((int)VisualTransformPropertyType.OffsetPolicy, new PropertyValue(new Vector2((int)VisualTransformPolicyType.Absolute, (int)VisualTransformPolicyType.Absolute)))
214                 .Add((int)VisualTransformPropertyType.SizePolicy, new PropertyValue(new Vector2((int)VisualTransformPolicyType.Absolute, (int)VisualTransformPolicyType.Absolute)))
215                 .Add((int)VisualTransformPropertyType.Size, new PropertyValue(new Vector2(40.0f, 40.0f)))
216                 .Add((int)VisualTransformPropertyType.Origin, new PropertyValue((int)Visual.AlignType.CenterBegin))
217                 .Add((int)VisualTransformPropertyType.AnchorPoint, new PropertyValue((int)Visual.AlignType.CenterBegin));
218             _imageVisual.SetTransformAndSize(imageVisualTransform, size);
219
220             // Configure the transform and size of Text visual.
221             PropertyMap textVisualTransform = new PropertyMap();
222             textVisualTransform.Add((int)VisualTransformPropertyType.Offset, new PropertyValue(new Vector2(0.0f, 0.0f)))
223                 .Add((int)VisualTransformPropertyType.OffsetPolicy, new PropertyValue(new Vector2((int)VisualTransformPolicyType.Relative, (int)VisualTransformPolicyType.Relative)))
224                 .Add((int)VisualTransformPropertyType.SizePolicy, new PropertyValue(new Vector2((int)VisualTransformPolicyType.Absolute, (int)VisualTransformPolicyType.Absolute)))
225                 .Add((int)VisualTransformPropertyType.Size, new PropertyValue(new Vector2(size.X - 100.0f, 50.0f)))
226                 .Add((int)VisualTransformPropertyType.Origin, new PropertyValue((int)Visual.AlignType.Center))
227                 .Add((int)VisualTransformPropertyType.AnchorPoint, new PropertyValue((int)Visual.AlignType.Center));
228             _textVisual.SetTransformAndSize(textVisualTransform, size);
229
230             // Configure the transform and size of Primitive visual.
231             PropertyMap primitiveVisualTransform = new PropertyMap();
232             primitiveVisualTransform.Add((int)VisualTransformPropertyType.Offset, new PropertyValue(new Vector2(size.X - 60.0f, 0.0f)))
233                 .Add((int)VisualTransformPropertyType.OffsetPolicy, new PropertyValue(new Vector2((int)VisualTransformPolicyType.Absolute, (int)VisualTransformPolicyType.Absolute)))
234                 .Add((int)VisualTransformPropertyType.SizePolicy, new PropertyValue(new Vector2((int)VisualTransformPolicyType.Absolute, (int)VisualTransformPolicyType.Absolute)))
235                 .Add((int)VisualTransformPropertyType.Size, new PropertyValue(new Vector2(40.0f, 40.0f)))
236                 .Add((int)VisualTransformPropertyType.Origin, new PropertyValue((int)Visual.AlignType.CenterBegin))
237                 .Add((int)VisualTransformPropertyType.AnchorPoint, new PropertyValue((int)Visual.AlignType.CenterBegin));
238             _primitiveVisual.SetTransformAndSize(primitiveVisualTransform, size);
239         }
240     }
241 }