2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 using Tizen.NUI.Constants;
21 using Tizen.NUI.BaseComponents;
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.
27 namespace VisualsUsingCustomView
29 public class ContactView : CustomView
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;
41 private string _imageURL;
42 private string _maskURL;
46 static CustomView CreateInstance()
48 return new ContactView();
53 CustomViewRegistry.Instance.Register( CreateInstance, typeof(ContactView));
56 public ContactView() : base(typeof(ContactView).FullName, CustomViewBehaviour.RequiresKeyboardNavigationSupport)
62 get { return _maskURL; }
63 set { _maskURL=value; }
66 [ScriptableProperty()]
67 public string ImageURL
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 );
84 RegisterVisual( GetPropertyIndex("ImageURL"), _imageVisual );
86 // Set the depth index for Image visual
87 _imageVisual.DepthIndex = ImageVisualPropertyIndex;
91 [ScriptableProperty()]
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 );
112 RegisterVisual( GetPropertyIndex("Name"), _textVisual );
114 // Set the depth index for Text visual
115 _textVisual.DepthIndex = TextVisualPropertyIndex;
119 [ScriptableProperty()]
129 BackgroundColor = value;
133 [ScriptableProperty()]
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 );
155 // Set the depth index for Primitive visual
156 _primitiveVisual.DepthIndex = PrimitiveVisualPropertyIndex;
160 public override void OnInitialize()
162 // Enable Tap gesture on ContactView
163 EnableGestureDetection(Gesture.GestureType.Tap);
166 public override void OnTap(TapGesture tap)
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 );
177 public override void OnRelayout(Vector2 size, RelayoutContainer container)
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);
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);
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);