[Tizen] Fixed many issues.
[platform/core/csapi/tizenfx.git] / NUISamples / NUISamples / NUISamples.TizenTV / 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 const int PROPERTY_REGISTRATION_START_INDEX = 10001000;
32         private const int ColorVisualPropertyIndex = PROPERTY_REGISTRATION_START_INDEX+1 ;
33         private const int PrimitiveVisualPropertyIndex = PROPERTY_REGISTRATION_START_INDEX+2;
34         private const int ImageVisualPropertyIndex = PROPERTY_REGISTRATION_START_INDEX+3;
35         private const int TextVisualPropertyIndex = PROPERTY_REGISTRATION_START_INDEX+4;
36         private VisualBase _imageVisual;
37         private VisualBase _colorVisual;
38         private VisualBase _primitiveVisual;
39         private VisualBase _textVisual;
40         private int _shape;
41         private string _imageURL;
42         private string _maskURL;
43         private string _name;
44         private Color _color;
45
46         static CustomView CreateInstance()
47         {
48             return new ContactView();
49         }
50
51         static ContactView()
52         {
53             CustomViewRegistry.Instance.Register( CreateInstance, typeof(ContactView));
54         }
55
56         public ContactView() : base(typeof(ContactView).FullName, CustomViewBehaviour.RequiresKeyboardNavigationSupport)
57         {
58         }
59
60         public string MaskURL
61         {
62             get { return _maskURL; }
63             set { _maskURL=value; }
64         }
65
66         [ScriptableProperty()]
67         public string ImageURL
68         {
69             get
70             {
71                 return _imageURL;
72             }
73             set
74             {
75                 _imageURL = value;
76
77                 // Create and Register Image Visual
78                 PropertyMap imageVisual = new PropertyMap();
79                 imageVisual.Add( Visual.Property.Type, new PropertyValue( (int)Visual.Type.Image ))
80                     .Add( ImageVisualProperty.URL, new PropertyValue( _imageURL ) )
81                     .Add( ImageVisualProperty.AlphaMaskURL, new PropertyValue( _maskURL ));
82                 _imageVisual =  VisualFactory.Instance.CreateVisual( imageVisual );
83
84                 RegisterVisual( GetPropertyIndex("ImageURL"), _imageVisual );
85
86                 // Set the depth index for Image visual
87                 _imageVisual.DepthIndex = ImageVisualPropertyIndex;
88             }
89         }
90
91         [ScriptableProperty()]
92         public string Name
93         {
94             get
95             {
96                 return _name;
97             }
98             set
99             {
100                 _name = value;
101
102                 // Create and Register Text Visual
103                 PropertyMap textVisual = new PropertyMap();
104                 textVisual.Add(Visual.Property.Type, new PropertyValue((int)Visual.Type.Text))
105                     .Add(TextVisualProperty.Text, new PropertyValue(_name))
106                     .Add(TextVisualProperty.TextColor, new PropertyValue(Color.White))
107                     .Add(TextVisualProperty.PointSize, new PropertyValue(7))
108                     .Add( TextVisualProperty.HorizontalAlignment, new PropertyValue("CENTER"))
109                     .Add( TextVisualProperty.VerticalAlignment, new PropertyValue("CENTER"));
110                 _textVisual =  VisualFactory.Instance.CreateVisual( textVisual );
111
112                 RegisterVisual( GetPropertyIndex("Name"), _textVisual );
113
114                 // Set the depth index for Text visual
115                 _textVisual.DepthIndex = TextVisualPropertyIndex;
116             }
117         }
118
119         [ScriptableProperty()]
120         public Color Color
121         {
122             get
123             {
124                 return _color;
125             }
126             set
127             {
128                 _color = value;
129                 BackgroundColor = value;
130             }
131         }
132
133         [ScriptableProperty()]
134         public int Shape
135         {
136             get
137             {
138                 return _shape;
139             }
140             set
141             {
142                 _shape = value;
143
144                 // Create and Register Primitive Visual
145                 PropertyMap primitiveVisual = new PropertyMap();
146                 primitiveVisual.Add( Visual.Property.Type, new PropertyValue( (int)Visual.Type.Primitive ))
147                     .Add( PrimitiveVisualProperty.Shape, new PropertyValue(_shape))
148                     .Add( PrimitiveVisualProperty.BevelPercentage, new PropertyValue(0.3f))
149                     .Add( PrimitiveVisualProperty.BevelSmoothness, new PropertyValue(0.0f))
150                     .Add( PrimitiveVisualProperty.ScaleDimensions, new PropertyValue(new Vector3(1.0f,1.0f,0.3f)))
151                     .Add( PrimitiveVisualProperty.MixColor, new PropertyValue(new Vector4((245.0f/255.0f), (188.0f/255.0f), (73.0f/255.0f), 1.0f)));
152                 _primitiveVisual =  VisualFactory.Instance.CreateVisual( primitiveVisual );
153                 RegisterVisual( GetPropertyIndex("Shape"), _primitiveVisual );
154
155                 // Set the depth index for Primitive visual
156                 _primitiveVisual.DepthIndex = PrimitiveVisualPropertyIndex;
157             }
158         }
159
160         public override void OnInitialize()
161         {
162             // Enable Tap gesture on ContactView
163             EnableGestureDetection(Gesture.GestureType.Tap);
164         }
165
166         public override void OnTap(TapGesture tap)
167         {
168             // Change the Color visual of ContactView with some random color
169             Random random = new Random();
170             float nextRed   = (random.Next(0, 256) / 255.0f);
171             float nextGreen = (random.Next(0, 256) / 255.0f);
172             float nextBlue  = (random.Next(0, 256) / 255.0f);
173             Animation anim = AnimateBackgroundColor( new Color( nextRed, nextGreen, nextBlue, 1.0f), 0, 2000 );
174             anim.Play();
175         }
176
177         public override void OnRelayout(Vector2 size, RelayoutContainer container)
178         {
179             // Configure the transform and size of Image visual.
180             PropertyMap imageVisualTransform = new PropertyMap();
181             imageVisualTransform.Add((int)VisualTransformPropertyType.Offset, new PropertyValue(new Vector2(10.0f, 0.0f)))
182                 .Add((int)VisualTransformPropertyType.OffsetPolicy, new PropertyValue(new Vector2((int)VisualTransformPolicyType.Absolute, (int)VisualTransformPolicyType.Absolute)))
183                 .Add((int)VisualTransformPropertyType.SizePolicy, new PropertyValue(new Vector2((int)VisualTransformPolicyType.Absolute, (int)VisualTransformPolicyType.Absolute)))
184                 .Add((int)VisualTransformPropertyType.Size, new PropertyValue(new Vector2(40.0f, 40.0f)))
185                 .Add((int)VisualTransformPropertyType.Origin, new PropertyValue((int)Visual.AlignType.CenterBegin))
186                 .Add((int)VisualTransformPropertyType.AnchorPoint, new PropertyValue((int)Visual.AlignType.CenterBegin));
187             _imageVisual.SetTransformAndSize(imageVisualTransform, size);
188
189             // Configure the transform and size of Text visual.
190             PropertyMap textVisualTransform = new PropertyMap();
191             textVisualTransform.Add((int)VisualTransformPropertyType.Offset, new PropertyValue(new Vector2(0.0f, 0.0f)))
192                 .Add((int)VisualTransformPropertyType.OffsetPolicy, new PropertyValue(new Vector2((int)VisualTransformPolicyType.Relative, (int)VisualTransformPolicyType.Relative)))
193                 .Add((int)VisualTransformPropertyType.SizePolicy, new PropertyValue(new Vector2((int)VisualTransformPolicyType.Absolute, (int)VisualTransformPolicyType.Absolute)))
194                 .Add((int)VisualTransformPropertyType.Size, new PropertyValue(new Vector2(size.X - 100.0f, 50.0f)))
195                 .Add((int)VisualTransformPropertyType.Origin, new PropertyValue((int)Visual.AlignType.Center))
196                 .Add((int)VisualTransformPropertyType.AnchorPoint, new PropertyValue((int)Visual.AlignType.Center));
197             _textVisual.SetTransformAndSize(textVisualTransform, size);
198
199             // Configure the transform and size of Primitive visual.
200             PropertyMap primitiveVisualTransform = new PropertyMap();
201             primitiveVisualTransform.Add((int)VisualTransformPropertyType.Offset, new PropertyValue(new Vector2(size.X - 60.0f, 0.0f)))
202                 .Add((int)VisualTransformPropertyType.OffsetPolicy, new PropertyValue(new Vector2((int)VisualTransformPolicyType.Absolute, (int)VisualTransformPolicyType.Absolute)))
203                 .Add((int)VisualTransformPropertyType.SizePolicy, new PropertyValue(new Vector2((int)VisualTransformPolicyType.Absolute, (int)VisualTransformPolicyType.Absolute)))
204                 .Add((int)VisualTransformPropertyType.Size, new PropertyValue(new Vector2(40.0f, 40.0f)))
205                 .Add((int)VisualTransformPropertyType.Origin, new PropertyValue((int)Visual.AlignType.CenterBegin))
206                 .Add((int)VisualTransformPropertyType.AnchorPoint, new PropertyValue((int)Visual.AlignType.CenterBegin));
207             _primitiveVisual.SetTransformAndSize(primitiveVisualTransform, size);
208         }
209     }
210 }