-
Notifications
You must be signed in to change notification settings - Fork 2
/
MapLayer.cs
473 lines (436 loc) · 15 KB
/
MapLayer.cs
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace NaviMapNet
{
/// <summary>
/// Êëàññ ñëîÿ êàðòû
/// </summary>
public class MapLayer
{
/// <summary>
/// Ñëîè
/// </summary>
internal MapLayers _Layers = null;
/// <summary>
/// Èìÿ ñëîÿ
/// </summary>
private string _name;
/// <summary>
/// Èìÿ ñëîÿ
/// </summary>
public string Name { set { _name = value; } get { return _name; } }
/// <summary>
/// Ñîçäàíèå ñëîÿ
/// </summary>
/// <param name="Name">Èìÿ ñëîÿ</param>
public MapLayer(string Name)
{
_name = Name;
}
/// <summary>
/// Îáúåêòû ñëîÿ
/// </summary>
private List<MapObject> _objects = new List<MapObject>();
/// <summary>
/// Îáúåêò ñëîÿ ïî èíäåêñó
/// </summary>
/// <param name="index">Èíäåêñ îáúåêòà</param>
/// <returns>Îáúåêò</returns>
public MapObject this[int index]
{
set
{
_objects[index] = value;
}
get
{
return _objects[index];
}
}
/// <summary>
/// Îáúåêò ñëîÿ ïî èìåíè
/// </summary>
/// <param name="Name">Èìÿ îáúåêòà</param>
/// <returns>Îáúåêò</returns>
public MapObject this[string Name]
{
set
{
for (int i = 0; i < _objects.Count; i++)
if (_objects[i].Name == Name)
{
_objects[i] = value;
return;
};
_objects.Add(value);
value._layer = this;
if (value._id == String.Empty) value._id = "MOID_" + System.DateTime.UtcNow.Ticks.ToString() + "_" + this._Layers._ttl_obj_counter.ToString();
if (value.Name == String.Empty) value.Name = value._id;
this._Layers._ttl_obj_counter++;
}
get
{
foreach (MapObject obj in _objects)
if (obj.Name == Name) return obj;
return null;
}
}
/// <summary>
/// Èíäåêñ îáúåêòà
/// </summary>
/// <param name="obj">Îáúåêò</param>
/// <returns>Èíäåêñ</returns>
public int this[MapObject obj]
{
get
{
return _objects.IndexOf(obj);
}
}
/// <summary>
/// Âèäèìîñòü ñëîÿ
/// </summary>
private bool _visible = true;
/// <summary>
/// Âèäèìîñòü ñëîÿ
/// </summary>
public bool Visible { set { _visible = value; } get { return _visible; } }
/// <summary>
/// Âèäèìûå îáúåêòû ñëîÿ
/// </summary>
public MapObject[] VisibleObjects
{
get
{
ArrayList objs = new ArrayList();
foreach (MapObject obj in _objects)
if (obj.Visible) objs.Add(objs);
return (MapObject[])objs.ToArray();
}
}
/// <summary>
/// ×èñëî âèäèìûõ îáúåêòîâ êàðòû
/// </summary>
public int VisibleObjectsCount
{
get
{
int objs = 0;
foreach (MapObject obj in _objects)
if (obj.Visible) objs++;
return objs;
}
}
/// <summary>
/// Íåâèäèìûå îáúåêòû ñëîÿ
/// </summary>
public MapObject[] HiddenObjects
{
get
{
ArrayList objs = new ArrayList();
foreach (MapObject obj in _objects)
if (!obj.Visible) objs.Add(objs);
return (MapObject[])objs.ToArray();
}
}
/// <summary>
/// ×èñëî íåâèäèìûõ îáúåêòîâ êàðòû
/// </summary>
public int HiddenObjectsCount
{
get
{
int objs = 0;
foreach (MapObject obj in _objects)
if (!obj.Visible) objs++;
return objs;
}
}
/// <summary>
/// ×èñëî îáúåêòîâ ñëîÿ
/// </summary>
public int ObjectsCount
{
get { return _objects.Count; }
}
/// <summary>
/// Ãðàíèöû âñåõ îáúåêòîâ ñëîÿ
/// </summary>
public RectangleF Bounds
{
get
{
float xl = 0, yt = 0, xr = 0, yb = 0;
if (_objects.Count > 0)
{
xl = _objects[0].Bounds.Left;
yt = _objects[0].Bounds.Bottom; // Remember Reverse Load
xr = _objects[0].Bounds.Right;
yb = _objects[0].Bounds.Top; // Remember Reverse Load
};
foreach (MapObject obj in _objects)
{
if (obj.Bounds.Left < xl) xl = obj.Bounds.Left;
if (obj.Bounds.Bottom > yt) yt = obj.Bounds.Bottom; // Remember Reverse Load
if (obj.Bounds.Right > xr) xr = obj.Bounds.Right;
if (obj.Bounds.Top < yb) yb = obj.Bounds.Top; // Remember Reverse Load
};
return new RectangleF(xl, yb, xr - xl, yt - yb); // Remember Reverse Save
}
}
/// <summary>
/// Ãðàíèöû âèäèìûõ îáúåêòîâ ñëîÿ
/// </summary>
public RectangleF BoundsVisible
{
get
{
float xl = 0, yt = 0, xr = 0, yb = 0;
int s_o = -1;
for (int i = 0; i < _objects.Count; i++) if (_objects[i].Visible) { s_o = i; break; };
if (_objects.Count > 0 && s_o > -1)
{
xl = _objects[s_o].Bounds.Left;
yt = _objects[s_o].Bounds.Bottom; // Remember Reverse Load
xr = _objects[s_o].Bounds.Right;
yb = _objects[s_o].Bounds.Top; // Remember Reverse Load
};
foreach (MapObject obj in _objects)
if (obj.Visible)
{
if (obj.Bounds.Left < xl) xl = obj.Bounds.Left;
if (obj.Bounds.Bottom > yt) yt = obj.Bounds.Bottom; // Remember Reverse Load
if (obj.Bounds.Right > xr) xr = obj.Bounds.Right;
if (obj.Bounds.Top < yb) yb = obj.Bounds.Top; // Remember Reverse Load
};
return new RectangleF(xl, yb, xr - xl, yt - yb); // Remember Reverse Save
}
}
/// <summary>
/// Èíäåêñ ñëîÿ
/// </summary>
public int Index
{
get
{
if (_Layers == null) return -1;
return _Layers[this];
}
}
/// <summary>
/// Äîáàâëåíèå îáúåêòà
/// </summary>
/// <param name="obj"></param>
public void Add(MapObject obj)
{
_objects.Add(obj);
obj._layer = this;
if (obj._id == String.Empty) obj._id = "MOID_" + System.DateTime.UtcNow.Ticks.ToString() + "_" + this._Layers._ttl_obj_counter.ToString();
if (obj.Name == String.Empty) obj.Name = obj._id;
this._Layers._ttl_obj_counter++;
}
/// <summary>
/// Âñòàâêà îáúåêòà
/// </summary>
/// <param name="obj">Îáúåêò</param>
/// <param name="index">Èíäåêñ</param>
public void Add(MapObject obj, int index)
{
_objects.Insert(index, obj);
obj._layer = this;
if (obj._id == String.Empty) obj._id = "MOID_" + System.DateTime.UtcNow.Ticks.ToString() + "_" + this._Layers._ttl_obj_counter.ToString();
if (obj.Name == String.Empty) obj.Name = obj._id;
this._Layers._ttl_obj_counter++;
}
/// <summary>
/// Óäàëåíèå îáúåêòà
/// </summary>
/// <param name="obj">Ñëîé</param>
public void Remove(MapObject obj)
{
_objects.Remove(obj);
obj._layer = null;
}
/// <summary>
/// Óäàëåíèå îáúåêòà
/// </summary>
/// <param name="index">Èíäåêñ îáúåêòà</param>
public void Remove(int index)
{
_objects[index]._layer = null;
_objects.RemoveAt(index);
}
/// <summary>
/// Ñóùåñòâîâàíèå îáúåêòà
/// </summary>
/// <param name="obj">Îáúåêò</param>
/// <returns>Ñóùåñòâóåò</returns>
public bool Contains(MapObject obj)
{
return _objects.Contains(obj);
}
/// <summary>
/// Ñóùåñòâîâàíèå îáúåêòà
/// </summary>
/// <param name="Name">Èìÿ îáúåêòà</param>
/// <returns>Ñóùåñòâóåò</returns>
public bool Contains(string Name)
{
foreach (MapObject obj in _objects)
if (obj.Name == Name) return true;
return false;
}
/// <summary>
/// Óäàëåíèå îáúåêòîâ ñëîÿ
/// </summary>
public void Clear()
{
_objects.Clear();
}
/// <summary>
/// Èçìåíåíèå î÷åðåäíîñòè îáúåêòîâ
/// </summary>
/// <param name="obj">Îáúåêò</param>
/// <param name="newIndex">Íîâûé èíäåêñ</param>
public void Move(MapObject obj, int newIndex)
{
_objects.Remove(obj);
_objects.Insert(newIndex, obj);
}
/// <summary>
/// Èçìåíåíèå î÷åðåäíîñòè îáúåêòîâ
/// </summary>
/// <param name="index">Òåêóùèé èíäåêñ</param>
/// <param name="newIndex">Íîâûé èíäåêñ</param>
public void Move(int index, int newIndex)
{
MapObject obj = this[index];
_objects.Remove(obj);
_objects.Insert(newIndex, obj);
}
/// <summary>
/// Ïåðåñòàíîâêà îáúåêòîâ
/// </summary>
/// <param name="objA">îáúåêò A</param>
/// <param name="objB">îáúåêò B</param>
public void Change(MapObject objA, MapObject objB)
{
int _a = this[objA];
int _b = this[objB];
_objects.Remove(objA);
_objects.Remove(objB);
if (_a < _b)
{
_objects.Insert(_a, objA);
_objects.Insert(_b, objB);
}
else
{
_objects.Insert(_b, objB);
_objects.Insert(_a, objA);
};
}
/// <summary>
/// Ïåðåñòàíîâêà îáúåêòîâ
/// </summary>
/// <param name="indexA">Èíäåêñ îáúåêòà A äëÿ ïåðåñòàíîâêè</param>
/// <param name="indexB">Èíäåêñ îáúåêòà B äëÿ ïåðåñòàíîâêè</param>
public void Change(int indexA, int indexB)
{
MapObject layA = this[indexA];
MapObject layB = this[indexB];
_objects.Remove(layA);
_objects.Remove(layB);
if (indexA < indexB)
{
_objects.Insert(indexA, layA);
_objects.Insert(indexB, layB);
}
else
{
_objects.Insert(indexB, layB);
_objects.Insert(indexA, layA);
};
}
/// <summary>
/// Îáúåêòû â óêàçàííîé çîíå
/// </summary>
/// <param name="bounds">Çîíà â ìåòðàõ</param>
/// <returns>Îáúåêòû</returns>
public MapObject[] Select(RectangleF bounds)
{
List<MapObject> objs = new List<MapObject>();
foreach (MapObject mo in _objects)
{
if (InRectangle(mo.Bounds, bounds)) objs.Add(mo);
};
return objs.ToArray();
}
/// <summary>
/// Îáúåêòû â óêàçàííîé çîíå
/// </summary>
/// <param name="bounds">Çîíà â ìåòðàõ</param>
/// <param name="objTypes">Êàêèå îáúåêòû âûáèðàòü</param>
/// <returns>Îáúåêòû</returns>
public MapObject[] Select(PointF[] _bounds, MapObjectType objTypes)
{
RectangleF bounds = new RectangleF((float)(_bounds[0].X), (float)(_bounds[1].Y), (float)(_bounds[1].X - _bounds[0].X), (float)(_bounds[0].Y - _bounds[1].Y));
List<MapObject> objs = new List<MapObject>();
foreach (MapObject mo in _objects)
{
if ((objTypes & mo.ObjectType) != 0)
if (InRectangle(mo.Bounds, bounds))
objs.Add(mo);
};
return objs.ToArray();
}
/// <summary>
/// Îáúåêòû â óêàçàííîé çîíå
/// </summary>
/// <param name="objType">Êàêèå îáúåêòû âûáèðàòü</param>
/// <returns></returns>
public MapObject[] Select(MapObjectType objTypes)
{
List<MapObject> objs = new List<MapObject>();
foreach (MapObject mo in _objects)
{
if ((objTypes & mo.ObjectType) != 0)
objs.Add(mo);
};
return objs.ToArray();
}
/// <summary>
/// Îáúåêòû â óêàçàííîé çîíå
/// </summary>
/// <param name="bounds">Çîíà â ìåòðàõ</param>
/// <param name="objTypes">Êàêèå îáúåêòû âûáèðàòü</param>
/// <param name="visible">Âèäèìûå îáúåêòû</param>
/// <param name="hidden">Íåâèäèìûå îáúåêòû</param>
/// <returns></returns>
public MapObject[] Select(RectangleF bounds, MapObjectType objTypes, bool visible, bool hidden)
{
List<MapObject> objs = new List<MapObject>();
foreach (MapObject mo in _objects)
{
if ((visible && mo.Visible) || (hidden && (!mo.Visible)))
if ((objTypes & mo.ObjectType) != 0)
if (InRectangle(mo.Bounds, bounds)) objs.Add(mo);
};
return objs.ToArray();
}
/// <summary>
/// Ïðîâåðêà íà ïåðåñå÷åíèå ïðÿìîóãîëüíèêîâ
/// </summary>
/// <param name="r1">Ïðÿìîóãîëüíèê</param>
/// <param name="r2">Ïðÿìîóãîëüíèê</param>
/// <returns></returns>
private bool InRectangle(RectangleF r1, RectangleF r2)
{
return (r1.Left < r2.Right) && (r2.Left < r1.Right) && (r1.Top < r2.Bottom) && (r2.Top < r1.Bottom);
}
}
}