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