forked from Mapsui/Mapsui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewport.cs
More file actions
337 lines (290 loc) · 10.7 KB
/
Copy pathViewport.cs
File metadata and controls
337 lines (290 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// Copyright 2012 - Paul den Dulk (Geodan)
//
// This file is part of SharpMap.
// Mapsui is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// SharpMap is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public License
// along with SharpMap; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using Mapsui.Geometries;
using Mapsui.Utilities;
using System.Runtime.CompilerServices;
using System.ComponentModel;
namespace Mapsui
{
/// <summary>
/// Viewport holds all informations about the visible part of the map.
/// </summary>
/// <remarks>
/// Viewport is the connection between Map and MapControl. It tells MapControl,
/// which part of Map should be displayed on screen.
/// </remarks>
public class Viewport : IViewport
{
public event PropertyChangedEventHandler ViewportChanged;
private readonly BoundingBox _extent;
private Quad _windowExtent;
private double _height;
private double _resolution = Constants.DefaultResolution;
private double _width;
private double _rotation;
private ReadOnlyPoint _center = new ReadOnlyPoint(0, 0);
private bool _modified = true;
/// <summary>
/// Create a new viewport
/// </summary>
public Viewport()
{
_extent = new BoundingBox(0, 0, 0, 0);
_windowExtent = new Quad();
}
/// <summary>
/// Create a new viewport from another viewport
/// </summary>
/// <param name="viewport">Viewport from which to copy all values</param>
public Viewport(Viewport viewport) : this()
{
_resolution = viewport._resolution;
_width = viewport._width;
_height = viewport._height;
_rotation = viewport._rotation;
_center = new ReadOnlyPoint(viewport._center);
if (viewport.Extent != null) _extent = new BoundingBox(viewport.Extent);
if (viewport.WindowExtent != null) _windowExtent = new Quad(viewport.WindowExtent);
UpdateExtent();
}
public bool HasSize => !_width.IsNanOrInfOrZero() && !_height.IsNanOrInfOrZero();
/// <inheritdoc />
public ReadOnlyPoint Center
{
get => _center;
set
{
// todo: Consider making setters private or removeing Set methods
_center = value;
OnViewportChanged();
}
}
/// <inheritdoc />
public double Resolution
{
get => _resolution;
set
{
_resolution = value;
OnViewportChanged();
}
}
/// <inheritdoc />
public double Width
{
get => _width;
set
{
_width = value;
OnViewportChanged();
}
}
/// <inheritdoc />
public double Height
{
get => _height;
set
{
_height = value;
OnViewportChanged();
}
}
/// <inheritdoc />
public double Rotation
{
get => _rotation;
set
{
// normalize the value to be [0, 360)
_rotation = value % 360.0;
if (_rotation < 0)
_rotation += 360.0;
OnViewportChanged();
}
}
/// <inheritdoc />
public bool IsRotated =>
!double.IsNaN(_rotation) && _rotation > Constants.Epsilon && _rotation < 360 - Constants.Epsilon;
/// <inheritdoc />
public BoundingBox Extent
{
get
{
if (_modified) UpdateExtent();
return _extent;
}
}
/// <inheritdoc />
public Quad WindowExtent
{
get
{
if (_modified) UpdateExtent();
return _windowExtent;
}
}
/// <inheritdoc />
public Point WorldToScreen(Point worldPosition)
{
return WorldToScreen(worldPosition.X, worldPosition.Y);
}
/// <inheritdoc />
public Point WorldToScreenUnrotated(Point worldPosition)
{
return WorldToScreenUnrotated(worldPosition.X, worldPosition.Y);
}
/// <inheritdoc />
public Point ScreenToWorld(Point position)
{
return ScreenToWorld(position.X, position.Y);
}
/// <inheritdoc />
public Point WorldToScreen(double worldX, double worldY)
{
var p = WorldToScreenUnrotated(worldX, worldY);
if (IsRotated)
{
var screenCenterX = Width / 2.0;
var screenCenterY = Height / 2.0;
p = p.Rotate(-_rotation, screenCenterX, screenCenterY);
}
return p;
}
/// <inheritdoc />
public Point WorldToScreenUnrotated(double worldX, double worldY)
{
var screenCenterX = Width / 2.0;
var screenCenterY = Height / 2.0;
var screenX = (worldX - Center.X) / _resolution + screenCenterX;
var screenY = (Center.Y - worldY) / _resolution + screenCenterY;
return new Point(screenX, screenY);
}
/// <inheritdoc />
public Point ScreenToWorld(double screenX, double screenY)
{
var screenCenterX = Width / 2.0;
var screenCenterY = Height / 2.0;
if (IsRotated)
{
var screen = new Point(screenX, screenY).Rotate(_rotation, screenCenterX, screenCenterY);
screenX = screen.X;
screenY = screen.Y;
}
var worldX = Center.X + (screenX - screenCenterX) * _resolution;
var worldY = Center.Y - (screenY - screenCenterY) * _resolution;
return new Point(worldX, worldY);
}
/// <inheritdoc />
public void Transform(Point positionScreen, Point previousPositionScreen, double deltaResolution = 1, double deltaRotation = 0)
{
var previous = ScreenToWorld(previousPositionScreen.X, previousPositionScreen.Y);
var current = ScreenToWorld(positionScreen.X, positionScreen.Y);
var newX = _center.X + previous.X - current.X;
var newY = _center.Y + previous.Y - current.Y;
if (deltaResolution != 1)
{
Resolution = Resolution / deltaResolution;
// Calculate current position again with adjusted resolution
// Zooming should be centered on the place where the map is touched.
// This is done with the scale correction.
var scaleCorrectionX = (1 - deltaResolution) * (current.X - Center.X);
var scaleCorrectionY = (1 - deltaResolution) * (current.Y - Center.Y);
newX -= scaleCorrectionX;
newY -= scaleCorrectionY;
}
SetCenter(newX, newY);
if (deltaRotation != 0)
{
current = ScreenToWorld(positionScreen.X, positionScreen.Y); // calculate current position again with adjusted resolution
Rotation += deltaRotation;
var postRotation = ScreenToWorld(positionScreen.X, positionScreen.Y); // calculate current position again with adjusted resolution
SetCenter(_center.X - (postRotation.X - current.X), _center.Y - (postRotation.Y - current.Y));
}
}
/// <summary>
/// Recalculates extents for viewport
/// </summary>
private void UpdateExtent()
{
// calculate the window extent which is not rotate
var halfSpanX = _width * _resolution * 0.5;
var halfSpanY = _height * _resolution * 0.5;
var left = Center.X - halfSpanX;
var bottom = Center.Y - halfSpanY;
var right = Center.X + halfSpanX;
var top = Center.Y + halfSpanY;
_windowExtent.BottomLeft = new Point(left, bottom);
_windowExtent.TopLeft = new Point(left, top);
_windowExtent.TopRight = new Point(right, top);
_windowExtent.BottomRight = new Point(right, bottom);
if (!IsRotated)
{
_extent.Min.X = left;
_extent.Min.Y = bottom;
_extent.Max.X = right;
_extent.Max.Y = top;
}
else
{
// Calculate the extent that will encompass a rotated viewport (slighly larger - used for tiles).
// Perform rotations on corner offsets and then add them to the Center point.
_windowExtent = _windowExtent.Rotate(-_rotation, Center.X, Center.Y);
var rotatedBoundingBox = _windowExtent.ToBoundingBox();
_extent.Min.X = rotatedBoundingBox.MinX;
_extent.Min.Y = rotatedBoundingBox.MinY;
_extent.Max.X = rotatedBoundingBox.MaxX;
_extent.Max.Y = rotatedBoundingBox.MaxY;
}
_modified = false;
}
public void SetSize(double width, double height)
{
_width = width;
_height = height;
OnViewportChanged();
}
public void SetCenter(double x, double y)
{
Center = new Point(x, y);
OnViewportChanged();
}
public void SetCenter(ReadOnlyPoint center)
{
Center = center;
OnViewportChanged();
}
public void SetResolution(double resolution)
{
Resolution = resolution;
OnViewportChanged();
}
public void SetRotation(double rotation)
{
Rotation = rotation;
OnViewportChanged();
}
/// <summary>
/// Property change event
/// </summary>
/// <param name="propertyName">Name of property that changed</param>
private void OnViewportChanged([CallerMemberName] string propertyName = null)
{
_modified = true;
ViewportChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}