Repository navigation
Expand file tree
/
Copy pathMap.cs
More file actions
342 lines (307 loc) · 11.7 KB
/
Copy pathMap.cs
File metadata and controls
342 lines (307 loc) · 11.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
338
339
340
341
342
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Drawing2D;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MiniGIS.Properties;
using System.IO;
using System.Runtime.InteropServices;
using System.Reflection;
namespace MiniGIS
{
public partial class Map : UserControl
{
protected List<Layer> layers = new List<Layer>();
private List<MapObject> selectedObjects = new List<MapObject>();
public List<Layer> Layers => layers;
public string Name;
private int snap = 3;
public Color SelectoinColor { get; set; } = Color.Blue;
private Vertex mapCenter = new Vertex(0, 0);
private double mapScale = 1;
public double MapScale
{
get { return mapScale; }
set
{
if ((mapScale < 1000) || (value < mapScale))
mapScale = value;
Invalidate();
}
}
private bool IsMouseDown = false;
private System.Drawing.Point mouseDownPosition = new System.Drawing.Point();
public MapToolType activeTool;
public MapToolType ActiveTool
{
get { return activeTool; }
set
{
activeTool = value;
switch (activeTool)
{
case MapToolType.Select:
//Cursor = LoadCustomCursor(@"D:\Учеба\ГИС\MiniGIS\MiniGIS\Resources\arrow.cur");
Cursor = Cursors.Arrow;
break;
case MapToolType.Pan:
Cursor = LoadCustomCursor("hand.cur");
//Cursor = Cursors.Hand;
break;
case MapToolType.ZoomIn:
Cursor = LoadCustomCursor("zoom-in.cur");
//Cursor = Cursors.SizeAll;
break;
case MapToolType.ZoomOut:
Cursor = LoadCustomCursor("zoom-out.cur");
//Cursor = Cursors.SizeWE;
break;
}
}
}
public static Cursor LoadCustomCursor(string path)
{
IntPtr hCurs = LoadCursorFromFile(path);
if (hCurs == IntPtr.Zero) throw new Win32Exception();
var curs = new Cursor(hCurs);
// Note: force the cursor to own the handle so it gets released properly
var fi = typeof(Cursor).GetField("ownHandle", BindingFlags.NonPublic | BindingFlags.Instance);
fi.SetValue(curs, true);
return curs;
}
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern IntPtr LoadCursorFromFile(string path);
public Map()
{
InitializeComponent();
this.MouseWheel += new MouseEventHandler(Map_MouseWheel);
}
public Vertex Center
{
get { return mapCenter; }
set { mapCenter = value; }
}
public Bounds Bounds{ get { return CalcBounds(); } }//
public List<MapObject> SelectedObjects { get => selectedObjects;}
public Bounds CalcBounds()
{
Bounds bounds = new Bounds();
foreach (var layer in layers)
{
if (layer.Visible)
bounds += layer.Bounds;
}
return bounds;
}
public void AddLayer(Layer layer)
{
layers.Add(layer);
layer.map = this;
//Invalidate();
}
public void RemoveLayer(int index) => layers.RemoveAt(index);
public void RemoveLayer(Layer layer) => layers.Remove(layer);
public void RemoveAllLayer() => layers.Clear();
private void Map_Paint(object sender, PaintEventArgs e)
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
foreach (var layer in layers)
{
if (layer.Visible)
layer.Draw(e);
}
}
public System.Drawing.Point MapToScreen(Vertex mapPoint)
{
System.Drawing.Point screenPoint = new System.Drawing.Point();
screenPoint.X = (int)((mapPoint.X - Center.X) * MapScale + Width / 2 + 0.5);
screenPoint.Y = (int)(-(mapPoint.Y - Center.Y) * MapScale + Height / 2 + 0.5);
return screenPoint;
}
public Vertex ScreenToMap(System.Drawing.Point screenPoint)
{
Vertex mapPoint = new Vertex(0, 0);
mapPoint.X = (screenPoint.X - Width / 2) / MapScale + Center.X;
mapPoint.Y = -(screenPoint.Y - Height / 2) / MapScale + Center.Y;
return mapPoint;
}
private void Map_Resize(object sender, EventArgs e)
{
Invalidate();
}
private void Map_MouseDown(object sender, MouseEventArgs e)
{
IsMouseDown = true;
mouseDownPosition = e.Location;
//if (ActiveTool != MapToolType.Pan) return;
//Cursor = LoadCustomCursor(@"D:\Учеба\ГИС\MiniGIS\MiniGIS\Resources\dnd-move.cur");
switch (ActiveTool)
{
case MapToolType.Select:
break;
case MapToolType.Pan:
Cursor = LoadCustomCursor("dnd-move.cur");
break;
case MapToolType.ZoomIn:
break;
case MapToolType.ZoomOut:
break;
}
}
private void Map_MouseMove(object sender, MouseEventArgs e)
{
switch (ActiveTool)
{
case MapToolType.Select:
break;
case MapToolType.Pan:
if (!IsMouseDown) return;
var _dX = (e.X - mouseDownPosition.X) / MapScale;
var _dY = (e.Y - mouseDownPosition.Y) / MapScale;
Center.X -= _dX;
Center.Y += _dY;
Invalidate();
mouseDownPosition = e.Location;
break;
case MapToolType.ZoomIn:
if (!IsMouseDown) return;
var topLeft = new System.Drawing.Point
{
X = mouseDownPosition.X < e.X ? mouseDownPosition.X : e.X,
Y = mouseDownPosition.Y < e.Y ? mouseDownPosition.Y : e.Y
};
var bottomRight = new System.Drawing.Point
{
X = mouseDownPosition.X > e.X ? mouseDownPosition.X : e.X,
Y = mouseDownPosition.Y > e.Y ? mouseDownPosition.Y : e.Y
};
Graphics g = CreateGraphics();
g.DrawRectangle(new Pen(Color.Blue, 2), topLeft.X, topLeft.Y,
bottomRight.X - topLeft.X, bottomRight.Y - topLeft.Y);
Invalidate();
break;
case MapToolType.ZoomOut:
break;
}
}
private void Map_MouseUp(object sender, MouseEventArgs e)
{
IsMouseDown = false;
switch (ActiveTool)
{
case MapToolType.Select:
var dx = Math.Abs(mouseDownPosition.X - e.X);
var dy = Math.Abs(mouseDownPosition.Y - e.Y);
if (dx > snap && dy > snap) break;
var searchPoint = ScreenToMap(e.Location);
var d = snap / MapScale;
if (ModifierKeys != Keys.Control)
{
ClearSelection();
Invalidate();
}
var result = FindObject(searchPoint, d);
if (result == null) break;
result.Selected = true;
if(!selectedObjects.Contains(result))
selectedObjects.Add(result);
Invalidate();
break;
case MapToolType.Pan:
Cursor = LoadCustomCursor("hand.cur");
break;
case MapToolType.ZoomIn:
var _X = (mouseDownPosition.X + e.X) / 2;
var _Y = (mouseDownPosition.Y + e.Y) / 2;
Center = ScreenToMap(new System.Drawing.Point(_X, _Y));
var w = Math.Abs(mouseDownPosition.X - e.X);
var h = Math.Abs(mouseDownPosition.Y - e.Y);
if (w <= snap && h <= snap) MapScale *= 1.5;
if (w <= snap && h > snap) MapScale *= Height / h;
if (w > snap && h <= snap) MapScale *= Width / w;
if (w > snap && h > snap) MapScale *= Math.Min(Width / w, Height / h);
Invalidate();
break;
case MapToolType.ZoomOut:
MapScale /= 1.5;
break;
}
}
public MapObject FindObject(Vertex searchPoint, double d)
{
MapObject result = null;
for (int i = Layers.Count - 1; i >= 0; i--)
{
MapObject searchObj = Layers[i].FindObject(searchPoint, d);
if (searchObj != null)
{
result = searchObj;
break;
}
}
return result;
}
private void Map_MouseWheel(object sender, MouseEventArgs e)
{
if (e.Delta > 0)
MapScale *= 2;
else
MapScale /= 2;
}
public void ZoomAll()
{
if (!Bounds.Valid) return;
var w = (Bounds.Xmax - Bounds.Xmin) * MapScale;
var h = (Bounds.Ymax - Bounds.Ymin) * MapScale;
Center = new Vertex((Bounds.Xmin + Bounds.Xmax) / 2, (Bounds.Ymin + Bounds.Ymax) / 2);
if (!(w <= snap || h <= snap))
mapScale *= Math.Min(Width / w, Height / h);
Invalidate();
}
public void ZoomLayers(List<Layer> layers)
{
if (layers == null || layers.Count == 0) return;
Bounds bounds = new Bounds();
foreach (var layer in layers)
{
bounds += layer.Bounds;
}
if (!bounds.Valid) return;
var w = (bounds.Xmax - bounds.Xmin) * MapScale;
var h = (bounds.Ymax - bounds.Ymin) * MapScale;
Center = new Vertex((bounds.Xmin + bounds.Xmax) / 2, (bounds.Ymin + bounds.Ymax) / 2);
if (!(w == 0 || h == 0))
mapScale *= Math.Min(Width / w, Height / h);
Invalidate();
}
public void MoveLayerUp(Layer layer)
{
if (!layers.Contains(layer)) return;
var index = layers.IndexOf(layer);
if (index == layers.Count-1) return;
layers.Remove(layer);
layers.Insert(index + 1, layer);
Invalidate();
}
public void MoveLayerDown(Layer layer)
{
if (!layers.Contains(layer)) return;
var index = layers.IndexOf(layer);
if (index == 0) return;
layers.Remove(layer);
layers.Insert(index - 1, layer);
Invalidate();
}
public void ClearSelection()
{
foreach (var obj in selectedObjects)
obj.Selected = false;
selectedObjects.Clear();
}
}
}