Merge "fixed for Visual Studio 2017" into tizen
[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     /// <summary>
26     /// Polygon map object
27     /// </summary>
28     public class Polygon : MapObject, IDisposable
29     {
30         internal Interop.PolygonHandle handle;
31         private List<Geocoordinates> _coordinateList;
32
33         /// <summary>
34         /// Creates a polygon visual object.
35         /// </summary>
36         /// <param name="coordinates">List of geographical coordinates</param>
37         /// <param name="color">Background color</param>
38         /// <exception cref="ArgumentException">Thrown when input values are invalid.</exception>
39         public Polygon(IEnumerable<Geocoordinates> coordinates, Color color) : base()
40         {
41             var err = Interop.ErrorCode.InvalidParameter;
42             if (coordinates == null || coordinates.Count() < 3)
43             {
44                 err.ThrowIfFailed("given coordinates list should contain at least 3 coordinates");
45             }
46             _coordinateList = coordinates.ToList();
47             var geocoordinateList = new GeocoordinatesList(_coordinateList, false);
48             handle = new Interop.PolygonHandle(geocoordinateList.handle, color);
49         }
50
51         /// <summary>
52         /// Adds or removes clicked event handlers.
53         /// </summary>
54         public event EventHandler Clicked;
55
56         /// <summary>
57         /// Gets or sets visibility for the polygon.
58         /// </summary>
59         public override bool IsVisible
60         {
61             get { return handle.IsVisible; }
62             set { handle.IsVisible = value; }
63         }
64
65         /// <summary>
66         /// Gets or sets a list of geographical coordinates of polygon vertices.
67         /// </summary>
68         public IEnumerable<Geocoordinates> Coordinates
69         {
70             get
71             {
72                 return _coordinateList;
73             }
74             set
75             {
76                 var coordinates = value.ToList();
77                 var err = Interop.ErrorCode.InvalidParameter;
78                 if (coordinates == null || coordinates.Count() < 3)
79                 {
80                     err.ThrowIfFailed("given coordinates list should contain at least 3 coordinates");
81                 }
82
83                 var geocoordinateList = new GeocoordinatesList(coordinates, false);
84                 if (handle.SetPolygon(geocoordinateList.handle).IsSuccess())
85                 {
86                     _coordinateList = coordinates;
87                 }
88             }
89         }
90
91         /// <summary>
92         /// Gets or sets background color to fill the polygon.
93         /// </summary>
94         public Color FillColor
95         {
96             get
97             {
98                 return handle.FillColor;
99             }
100             set
101             {
102                 handle.FillColor = value;
103             }
104         }
105
106         internal override void HandleClickedEvent()
107         {
108             Clicked?.Invoke(this, EventArgs.Empty);
109         }
110
111         internal override void InvalidateMapObject()
112         {
113             handle = null;
114         }
115
116         internal override Interop.ViewObjectHandle GetHandle()
117         {
118             return handle;
119         }
120
121         #region IDisposable Support
122         private bool _disposedValue = false;
123
124         protected virtual void Dispose(bool disposing)
125         {
126             if (!_disposedValue)
127             {
128                 if (disposing)
129                 {
130                     _coordinateList.Clear();
131                 }
132                 handle.Dispose();
133                 _disposedValue = true;
134             }
135         }
136
137         /// <summary>
138         /// Releases all resources used by this object.
139         /// </summary>
140         public void Dispose()
141         {
142             Dispose(true);
143             GC.SuppressFinalize(this);
144         }
145         #endregion
146     }
147 }