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 VisualBase _imageVisual;
32 private VisualBase _primitiveVisual;
33 private VisualBase _textVisual;
35 private string _imageURL;
36 private string _maskURL;
40 static CustomView CreateInstance()
42 return new ContactView();
47 CustomViewRegistry.Instance.Register( CreateInstance, typeof(ContactView));
50 public ContactView() : base( typeof(ContactView).FullName, CustomViewBehaviour.RequiresKeyboardNavigationSupport)
56 get { return _maskURL; }
57 set { _maskURL=value; }
60 [ScriptableProperty()]
61 public string ImageURL
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;
80 [ScriptableProperty()]
81 public PropertyMap ImageVisual
85 // return _imageVisual.Creation;
89 _imageVisual = VisualFactory.Instance.CreateVisual( value );
90 RegisterVisual( GetPropertyIndex("ImageVisual"), _imageVisual );
92 // Set the depth index for Image visual
93 _imageVisual.DepthIndex = 30;
97 [ScriptableProperty()]
98 public string NameField
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;
119 [ScriptableProperty()]
120 public PropertyMap NameVisual
124 // return _textVisual.Creation;
128 _textVisual = VisualFactory.Instance.CreateVisual( value );
129 RegisterVisual( GetPropertyIndex("NameVisual"), _textVisual );
131 // Set the depth index for Text visual
132 _textVisual.DepthIndex = 30;
136 [ScriptableProperty()]
146 BackgroundColor = value;
150 [ScriptableProperty()]
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);
169 ShapeVisual = primitiveVisual.OutputVisualMap;
173 [ScriptableProperty()]
174 public PropertyMap ShapeVisual
178 // return _primitiveVisual.Creation;
182 _primitiveVisual = VisualFactory.Instance.CreateVisual( value );
183 RegisterVisual( GetPropertyIndex("ShapeVisual"), _primitiveVisual );
185 // Set the depth index for Primitive visual
186 _primitiveVisual.DepthIndex = 30;
190 public override void OnInitialize()
192 // Enable Tap gesture on ContactView
193 EnableGestureDetection(Gesture.GestureType.Tap);
196 public override void OnTap(TapGesture tap)
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 );
208 public override void OnRelayout(Vector2 size, RelayoutContainer container)
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);
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);
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);