Tizen.Maps Setup
[platform/core/csapi/tizenfx.git] / src / Tizen.Maps / Tizen.Maps / Polygon.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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 using System;
18 using System.Collections.Generic;
19 using System.Linq;
20
21 using Color = ElmSharp.Color;
22
23 namespace Tizen.Maps
24 {
25     public class Polygon : MapObject
26     {
27         private List<Geocoordinates> _coordinateList;
28
29         public Polygon(IEnumerable<Geocoordinates> coordinates, Color color) : base(CreateNativeHandle(coordinates, color))
30         {
31             var err = Interop.ErrorCode.InvalidParameter;
32             if (coordinates == null || coordinates.Count() < 3)
33             {
34                 err.ThrowIfFailed("given coordinates list should contain at least 3 coordinates");
35             }
36             _coordinateList = coordinates.ToList();
37         }
38
39         internal Polygon(Interop.ViewObjectHandle nativeHandle) : base(nativeHandle)
40         {
41         }
42
43         public IEnumerable<Geocoordinates> Coordinates
44         {
45             get
46             {
47                 if (_coordinateList == null)
48                 {
49                     _coordinateList = new List<Geocoordinates>();
50                     Interop.ViewObject.CoordinatesCallback callback = (index, nativeHandle, userData) =>
51                     {
52                         _coordinateList.Add(new Geocoordinates(nativeHandle));
53                         return true;
54                     };
55                     Interop.ViewObject.PolygonForeachPoint(handle, callback, IntPtr.Zero);
56                 }
57                 return _coordinateList;
58             }
59             set
60             {
61                 var coordinates = value.ToList();
62
63                 var err = Interop.ErrorCode.InvalidParameter;
64                 if (coordinates == null || coordinates.Count() < 3)
65                 {
66                     err.ThrowIfFailed("given coordinates list should contain at least 3 coordinates");
67                 }
68
69                 var coordinateList = new GeocoordinatesList(coordinates, false);
70                 if (Interop.ViewObject.PolygonSetPolygon(handle, coordinateList.handle).IsSuccess())
71                 {
72                     _coordinateList = coordinates;
73                 }
74             }
75         }
76
77         public Color FillColor
78         {
79             get
80             {
81                 byte r, g, b, a;
82                 Interop.ViewObject.PolygonGetFillColor(handle, out r, out g, out b, out a);
83                 return new Color(r, g, b, a);
84             }
85             set
86             {
87                 Interop.ViewObject.PolygonSetFillColor(handle, (byte)value.R, (byte)value.G, (byte)value.B, (byte)value.A);
88             }
89         }
90
91         private static Interop.ViewObjectHandle CreateNativeHandle(IEnumerable<Geocoordinates> coordinates, Color color)
92         {
93             if (coordinates == null || coordinates.Count() < 3) return new Interop.ViewObjectHandle(IntPtr.Zero);
94
95             IntPtr nativeHandle;
96             var geocoordinateList = new GeocoordinatesList(coordinates.ToList(), false);
97             var err = Interop.ViewObject.CreatePolygon(geocoordinateList.handle, (byte)color.R, (byte)color.G, (byte)color.B, (byte)color.A, out nativeHandle);
98             err.ThrowIfFailed("Failed to create native handle for polygon");
99             return new Interop.ViewObjectHandle(nativeHandle);
100         }
101     }
102 }