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.
22 // A ContactView is a Custom View which consists of four visuals (Image, Primitive, Text and Color).
23 // All of these visuals can be configured via properties - ImageURL (Image), Shape (Primitive), Name (Text) and Color.
24 // Tap gesture is also enabled on the ContactView which changes the color visual to some random color when ContactView is tapped.
26 namespace VisualsUsingCustomView
28 public class ContactView : CustomView
30 private const int ColorVisualPropertyIndex = 0;
31 private const int PrimitiveVisualPropertyIndex = 1;
32 private const int ImageVisualPropertyIndex = 2;
33 private const int TextVisualPropertyIndex = 3;
34 private VisualBase _imageVisual;
35 private VisualBase _colorVisual;
36 private VisualBase _primitiveVisual;
37 private VisualBase _textVisual;
39 private string _imageURL;
43 public ContactView() : base(typeof(ContactView).Name, CustomViewBehaviour.REQUIRES_KEYBOARD_NAVIGATION_SUPPORT)
47 public string ImageURL
57 // Create and Register Image Visual
58 Dali.Property.Map imageVisual = new Dali.Property.Map();
59 imageVisual.Add( Visual.Property.Type, new Dali.Property.Value( (int)Visual.Type.Image ))
60 .Add( ImageVisualProperty.URL, new Dali.Property.Value( _imageURL ));
61 _imageVisual = VisualFactory.Get().CreateVisual( imageVisual );
62 RegisterVisual( ImageVisualPropertyIndex, _imageVisual );
64 // Set the depth index for Image visual
65 _imageVisual.SetDepthIndex(ImageVisualPropertyIndex);
79 // Create and Register Text Visual
80 Dali.Property.Map textVisual = new Dali.Property.Map();
81 textVisual.Add(Visual.Property.Type, new Dali.Property.Value((int)Visual.Type.Text))
82 .Add(TextVisualProperty.Text, new Dali.Property.Value(_name))
83 .Add(TextVisualProperty.TextColor, new Dali.Property.Value(Dali.Color.White))
84 .Add(TextVisualProperty.PointSize, new Dali.Property.Value(15))
85 .Add( TextVisualProperty.HorizontalAlignment, new Dali.Property.Value("CENTER"))
86 .Add( TextVisualProperty.VerticalAlignment, new Dali.Property.Value("CENTER"));
87 _textVisual = VisualFactory.Get().CreateVisual( textVisual );
88 RegisterVisual( TextVisualPropertyIndex, _textVisual );
90 // Set the depth index for Text visual
91 _textVisual.SetDepthIndex(TextVisualPropertyIndex);
105 // Create and Register Color Visual
106 Dali.Property.Map colorVisual = new Dali.Property.Map();
107 colorVisual.Add( Visual.Property.Type, new Dali.Property.Value( (int)Visual.Type.Color ))
108 .Add( ColorVisualProperty.MixColor, new Dali.Property.Value( _color ));
109 _colorVisual = VisualFactory.Get().CreateVisual( colorVisual );
110 RegisterVisual( ColorVisualPropertyIndex, _colorVisual );
112 // Set the depth index for Color visual
113 _colorVisual.SetDepthIndex(ColorVisualPropertyIndex);
127 // Create and Register Primitive Visual
128 Dali.Property.Map primitiveVisual = new Dali.Property.Map();
129 primitiveVisual.Add( Visual.Property.Type, new Dali.Property.Value( (int)Visual.Type.Primitive ))
130 .Add( PrimitiveVisualProperty.Shape, new Dali.Property.Value(_shape))
131 .Add( PrimitiveVisualProperty.BevelPercentage, new Dali.Property.Value(0.3f))
132 .Add( PrimitiveVisualProperty.BevelSmoothness, new Dali.Property.Value(0.0f))
133 .Add( PrimitiveVisualProperty.ScaleDimensions, new Dali.Property.Value(new Vector3(1.0f,1.0f,0.3f)))
134 .Add( PrimitiveVisualProperty.MixColor, new Dali.Property.Value(new Vector4((245.0f/255.0f), (188.0f/255.0f), (73.0f/255.0f), 1.0f)));
135 _primitiveVisual = VisualFactory.Get().CreateVisual( primitiveVisual );
136 RegisterVisual( PrimitiveVisualPropertyIndex, _primitiveVisual );
138 // Set the depth index for Primitive visual
139 _primitiveVisual.SetDepthIndex(PrimitiveVisualPropertyIndex);
143 public override void OnInitialize()
145 // Enable Tap gesture on ContactView
146 EnableGestureDetection(Gesture.GestureType.Tap);
149 public override void OnTap(TapGesture tap)
151 // Change the Color visual of ContactView with some random color
152 Random random = new Random();
153 Color = new Color((random.Next(0, 256) / 255.0f), (random.Next(0, 256) / 255.0f), (random.Next(0, 256) / 255.0f), 1.0f);
156 public override void OnRelayout(Vector2 size, RelayoutContainer container)
158 // Configure the transform and size of Image visual.
159 Dali.Property.Map imageVisualTransform = new Dali.Property.Map();
160 imageVisualTransform.Add((int)VisualTransformPropertyType.OFFSET, new Dali.Property.Value(new Vector2(10.0f, 0.0f)))
161 .Add((int)VisualTransformPropertyType.OFFSET_POLICY, new Dali.Property.Value(new Vector2((int)VisualTransformPolicyType.ABSOLUTE, (int)VisualTransformPolicyType.ABSOLUTE)))
162 .Add((int)VisualTransformPropertyType.SIZE_POLICY, new Dali.Property.Value(new Vector2((int)VisualTransformPolicyType.ABSOLUTE, (int)VisualTransformPolicyType.ABSOLUTE)))
163 .Add((int)VisualTransformPropertyType.SIZE, new Dali.Property.Value(new Vector2(40.0f, 40.0f)))
164 .Add((int)VisualTransformPropertyType.ORIGIN, new Dali.Property.Value((int)AlignType.CENTER_BEGIN))
165 .Add((int)VisualTransformPropertyType.ANCHOR_POINT, new Dali.Property.Value((int)AlignType.CENTER_BEGIN));
166 _imageVisual.SetTransformAndSize(imageVisualTransform, size);
168 // Configure the transform and size of Text visual.
169 Dali.Property.Map textVisualTransform = new Dali.Property.Map();
170 textVisualTransform.Add((int)VisualTransformPropertyType.OFFSET, new Dali.Property.Value(new Vector2(0.0f, 0.0f)))
171 .Add((int)VisualTransformPropertyType.OFFSET_POLICY, new Dali.Property.Value(new Vector2((int)VisualTransformPolicyType.RELATIVE, (int)VisualTransformPolicyType.RELATIVE)))
172 .Add((int)VisualTransformPropertyType.SIZE_POLICY, new Dali.Property.Value(new Vector2((int)VisualTransformPolicyType.ABSOLUTE, (int)VisualTransformPolicyType.ABSOLUTE)))
173 .Add((int)VisualTransformPropertyType.SIZE, new Dali.Property.Value(new Vector2(size.X - 100.0f, 50.0f)))
174 .Add((int)VisualTransformPropertyType.ORIGIN, new Dali.Property.Value((int)Align.Type.Center))
175 .Add((int)VisualTransformPropertyType.ANCHOR_POINT, new Dali.Property.Value((int)Align.Type.Center));
176 _textVisual.SetTransformAndSize(textVisualTransform, size);
178 // Configure the transform and size of Primitive visual.
179 Dali.Property.Map primitiveVisualTransform = new Dali.Property.Map();
180 primitiveVisualTransform.Add((int)VisualTransformPropertyType.OFFSET, new Dali.Property.Value(new Vector2(size.X - 60.0f, 0.0f)))
181 .Add((int)VisualTransformPropertyType.OFFSET_POLICY, new Dali.Property.Value(new Vector2((int)VisualTransformPolicyType.ABSOLUTE, (int)VisualTransformPolicyType.ABSOLUTE)))
182 .Add((int)VisualTransformPropertyType.SIZE_POLICY, new Dali.Property.Value(new Vector2((int)VisualTransformPolicyType.ABSOLUTE, (int)VisualTransformPolicyType.ABSOLUTE)))
183 .Add((int)VisualTransformPropertyType.SIZE, new Dali.Property.Value(new Vector2(40.0f, 40.0f)))
184 .Add((int)VisualTransformPropertyType.ORIGIN, new Dali.Property.Value((int)AlignType.CENTER_BEGIN))
185 .Add((int)VisualTransformPropertyType.ANCHOR_POINT, new Dali.Property.Value((int)AlignType.CENTER_BEGIN));
186 _primitiveVisual.SetTransformAndSize(primitiveVisualTransform, size);
188 // Configure the transform and size of Color visual. This is also the default value.
189 Dali.Property.Map colorVisualTransform = new Dali.Property.Map();
190 colorVisualTransform.Add( (int)VisualTransformPropertyType.OFFSET, new Dali.Property.Value(new Vector2(0.0f,0.0f)))
191 .Add((int)VisualTransformPropertyType.OFFSET_POLICY, new Dali.Property.Value(new Vector2((int)VisualTransformPolicyType.RELATIVE, (int)VisualTransformPolicyType.RELATIVE)))
192 .Add((int)VisualTransformPropertyType.SIZE_POLICY, new Dali.Property.Value(new Vector2((int)VisualTransformPolicyType.RELATIVE, (int)VisualTransformPolicyType.RELATIVE)))
193 .Add( (int)VisualTransformPropertyType.SIZE, new Dali.Property.Value(new Vector2(1.0f, 1.0f)) )
194 .Add( (int)VisualTransformPropertyType.ORIGIN, new Dali.Property.Value((int)AlignType.TOP_BEGIN) )
195 .Add( (int)VisualTransformPropertyType.ANCHOR_POINT, new Dali.Property.Value((int)AlignType.TOP_BEGIN) );
196 _colorVisual.SetTransformAndSize(colorVisualTransform, size);