-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathLayoutControl.cs
More file actions
553 lines (477 loc) · 21.6 KB
/
Copy pathLayoutControl.cs
File metadata and controls
553 lines (477 loc) · 21.6 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
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
// -------------------------------------------------------------------------------------------
// <copyright file="MouseAwareLayoutControl.cs" company="MapWindow OSS Team - www.mapwindow.org">
// MapWindow OSS Team - 2015
// </copyright>
// -------------------------------------------------------------------------------------------
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Windows.Forms;
using MW5.Api.Map;
using MW5.Plugins.Printing.Enums;
using MW5.Plugins.Printing.Helpers;
using MW5.Plugins.Printing.Model;
using MW5.Plugins.Printing.Model.Elements;
using MW5.Plugins.Printing.Properties;
using MW5.Plugins.Printing.Services;
using MW5.Shared;
namespace MW5.Plugins.Printing.Controls.Layout
{
[ToolboxItem(false)]
public class LayoutControl : ContentAwareLayoutControl
{
private LayoutElement _elementToAddWithMouse;
private PointF _lastMousePoint;
private LayoutPage _lastPage;
private PointF _mouseStartPoint;
private Edge _resizeSelectedEdge;
private IPrintableMap _map;
private TileLoadingService _loadingService;
public LayoutControl()
{
MouseDoubleClick += LayoutControlMouseDoubleClick;
MouseDown += LayoutControlMouseDown;
MouseMove += LayoutControlMouseMove;
MouseUp += LayoutControlMouseUp;
KeyUp += LayoutControlKeyUp;
if (DesignMode)
{
ZoomFitToScreen();
}
}
public TileLoadingService TileLoader
{
get { return _loadingService; }
}
public void Initialize(IPrintableMap map, TileLoadingService loadingService)
{
// TODO: assign page settings explicitly
if (map == null) throw new ArgumentNullException("map");
if (loadingService == null) throw new ArgumentNullException("loadingService");
_map = map;
_loadingService = loadingService;
_loadingService.Initialize(_map, this);
_initialized = true;
ZoomFitToScreen();
UpdateScrollBars();
}
protected override void Dispose(bool disposing)
{
_loadingService.Dispose();
base.Dispose(disposing);
}
/// <summary>
/// Occurs when an element is double clicked.
/// </summary>
public event EventHandler<LayoutElementEventArgs> ElementDoubleClicked;
/// <summary>
/// Gets or sets the map pan mode
/// </summary>
[Browsable(false)]
public bool PanMode
{
get { return _mouseMode == MouseMode.PanMap || _mouseMode == MouseMode.StartPanMap; }
set { _mouseMode = value ? MouseMode.StartPanMap : MouseMode.Default; }
}
public override bool ShowPageNumbers
{
get { return base.ShowPageNumbers; }
set
{
_lastPage = null;
base.ShowPageNumbers = value;
}
}
/// <summary>
/// Allows the user to click on the layout and drag a rectangle where they want to insert an element
/// </summary>
public void AddElementWithMouse(LayoutElement le)
{
_elementToAddWithMouse = le;
ClearSelection();
_mouseMode = MouseMode.StartInsertNewElement;
Cursor = Cursors.Cross;
}
private void LayoutControlKeyUp(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Delete:
DeleteSelected();
break;
case Keys.F5:
RefreshElements();
break;
}
}
private void LayoutControlMouseDoubleClick(object sender, MouseEventArgs e)
{
if (DesignMode) return;
var pnt = ScreenToPaper(e.Location);
foreach (var el in LayoutElements)
{
if (el.ClickWithin((int)pnt.X, (int)pnt.Y))
{
var args = new LayoutElementEventArgs(el) { Cancel = false };
DelegateHelper.FireEvent(this, ElementDoubleClicked, args);
if (!args.Cancel)
{
DoInvalidate();
}
break;
}
}
}
private void LayoutControlMouseDown(object sender, MouseEventArgs e)
{
if (DesignMode) return;
//When the user clicks down we start tracking the mouses location
_mouseStartPoint = new PointF(e.X, e.Y);
_lastMousePoint = new PointF(e.X, e.Y);
var mousePointPaper = ScreenToPaper(_mouseStartPoint);
if (e.Button == MouseButtons.Left)
{
if (_showPageNumbers)
{
var page = PageByClick(e.X, e.Y);
if (page != null)
{
if (ModifierKeys == Keys.Shift && _lastPage != null)
{
int startX = Math.Min(_lastPage.X, page.X);
int endX = Math.Max(_lastPage.X, page.X);
int startY = Math.Min(_lastPage.Y, page.Y);
int endY = Math.Max(_lastPage.Y, page.Y);
for (int x = startX; x <= endX; x++)
{
for (int y = startY; y <= endY; y++)
{
var p = _pages.GetPage(x, y);
if (p != null)
{
p.Selected = _lastPage.Selected;
}
}
}
_lastPage = null;
}
else
{
page.Selected = !page.Selected;
_lastPage = page; // save the page for group selection
}
FirePageSelectionChanged();
DoInvalidate();
}
}
else
{
switch (_mouseMode)
{
case MouseMode.Default:
//Handles resizing stuff
if (_resizeSelectedEdge != Edge.None)
{
_mouseMode = MouseMode.ResizeSelected;
if (_selectedLayoutElements.Count > 0)
{
var el = _selectedLayoutElements[0];
el.Resizing = true;
if (el.ResizeStyle != ResizeStyle.HandledInternally)
{
var selecteScreenRect = PaperToScreen(el.Rectangle);
_resizeTempBitmap = new Bitmap(Convert.ToInt32(selecteScreenRect.Width),
Convert.ToInt32(selecteScreenRect.Height), PixelFormat.Format32bppArgb);
using (var graph = Graphics.FromImage(_resizeTempBitmap))
{
graph.SmoothingMode = DrawingQuality;
graph.ScaleTransform(_zoom, _zoom);
el.DrawElement(graph, false, false);
}
}
}
return;
}
//Starts moving selected elements
if (ModifierKeys != Keys.Control)
{
if (_selectedLayoutElements.Any(le => le.IntersectsWith(mousePointPaper)))
{
_mouseMode = MouseMode.MoveSelection;
Cursor = Cursors.SizeAll;
return;
}
}
//Starts the selection code.
_mouseMode = MouseMode.CreateSelection;
_mouseBox = new RectangleF(e.X, e.Y, 0F, 0F);
break;
//Start drag rectangle insert new element
case MouseMode.StartInsertNewElement:
_mouseMode = MouseMode.InsertNewElement;
_mouseBox = new RectangleF(e.X, e.Y, 0F, 0F);
break;
//Starts the pan mode for the map
case MouseMode.StartPanMap:
_mouseMode = MouseMode.PanMap;
_mouseBox = new RectangleF(e.X, e.Y, 0F, 0F);
break;
}
}
}
//Deals with right button clicks
if (e.Button == MouseButtons.Right)
{
switch (_mouseMode)
{
//If the user was in insert mode we cancel it
case (MouseMode.StartInsertNewElement):
_mouseMode = MouseMode.Default;
_elementToAddWithMouse = null;
Cursor = Cursors.Default;
break;
}
}
}
private void LayoutControlMouseMove(object sender, MouseEventArgs e)
{
if (DesignMode) return;
// the amount the mouse moved since the last time
float deltaX = _lastMousePoint.X - e.X;
float deltaY = _lastMousePoint.Y - e.Y;
_lastMousePoint = e.Location;
//Handles various different mouse modes
switch (_mouseMode)
{
case MouseMode.InsertNewElement:
case MouseMode.CreateSelection:
DoInvalidate(new Region(_mouseBox));
_mouseBox.Width = Math.Abs(_mouseStartPoint.X - e.X);
_mouseBox.Height = Math.Abs(_mouseStartPoint.Y - e.Y);
_mouseBox.X = Math.Min(_mouseStartPoint.X, e.X);
_mouseBox.Y = Math.Min(_mouseStartPoint.Y, e.Y);
DoInvalidate(new Region(_mouseBox));
break;
//Deals with moving the selection
case MouseMode.MoveSelection:
_suppressElementInvalidation = true;
foreach (var le in _selectedLayoutElements)
{
var elementLocScreen = PaperToScreen(le.LocationF);
le.LocationF = ScreenToPaper(elementLocScreen.X - deltaX, elementLocScreen.Y - deltaY);
DoInvalidate();
Update();
}
_suppressElementInvalidation = false;
break;
//This handle mouse movement when in resize mode
case MouseMode.ResizeSelected:
ResizeSelected(deltaX, deltaY);
break;
case MouseMode.StartPanMap:
if (_selectedLayoutElements.Count == 1 && _selectedLayoutElements[0] is LayoutMap)
{
bool mouseWithinMap =
_selectedLayoutElements[0].IntersectsWith(ScreenToPaper(e.X * 1F, e.Y * 1F));
Cursor = mouseWithinMap ? new Cursor(Resources.ico_pan16.Handle) : Cursors.Default;
}
break;
case MouseMode.PanMap:
_mouseBox.Width = e.X - _mouseStartPoint.X;
_mouseBox.Height = e.Y - _mouseStartPoint.Y;
DoInvalidate(new Region(PaperToScreen(_selectedLayoutElements[0].Rectangle)));
break;
case MouseMode.Default:
//If theres only one element selected and we're on its edge change the cursor to the resize cursor
if (_selectedLayoutElements.Count == 1)
{
var edge = LayoutHelper.IntersectElementEdge(
PaperToScreen(_selectedLayoutElements[0].Rectangle), new PointF(e.X, e.Y), 3F);
if ((edge == Edge.Bottom || edge == Edge.Top) &&
(_selectedLayoutElements[0] is LayoutTable || _selectedLayoutElements[0] is LayoutLegend)) // don't allow to change vertical size
return;
_resizeSelectedEdge = edge;
switch (_resizeSelectedEdge)
{
case Edge.TopLeft:
case Edge.BottomRight:
Cursor = Cursors.SizeNWSE;
break;
case Edge.Top:
case Edge.Bottom:
Cursor = Cursors.SizeNS;
break;
case Edge.TopRight:
case Edge.BottomLeft:
Cursor = Cursors.SizeNESW;
break;
case Edge.Left:
case Edge.Right:
Cursor = Cursors.SizeWE;
break;
case Edge.None:
Cursor = Cursors.Default;
break;
}
}
break;
}
_mousePosition = e.Location;
DoInvalidate(LayoutInvalidateType.Rulers);
}
private void LayoutControlMouseUp(object sender, MouseEventArgs e)
{
if (DesignMode) return;
if (e.Button == MouseButtons.Left)
{
//Handles various different mouse modes
switch (_mouseMode)
{
//If we are dealing with a selection we look here
case MouseMode.CreateSelection:
var selectBoxTL = ScreenToPaper(_mouseBox.Location);
var selectBoxBR = ScreenToPaper(_mouseBox.Location.X + _mouseBox.Width,
_mouseBox.Location.Y + _mouseBox.Height);
var selectBoxPaper = new RectangleF(selectBoxTL.X, selectBoxTL.Y, selectBoxBR.X - selectBoxTL.X,
selectBoxBR.Y - selectBoxTL.Y);
var elements = _layoutElements.Where(el => el.Visible);
if (ModifierKeys == Keys.Control)
{
foreach (var le in elements)
{
if (le.IntersectsWith(selectBoxPaper))
{
if (_selectedLayoutElements.Contains(le)) _selectedLayoutElements.Remove(le);
else _selectedLayoutElements.Add(le);
//If the box is just a point only select the top most
if (_mouseBox.Width <= 1 && _mouseBox.Height <= 1) break;
}
}
}
else
{
_selectedLayoutElements.Clear();
foreach (var le in elements)
{
if (le.IntersectsWith(selectBoxPaper))
{
_selectedLayoutElements.Add(le);
//If the box is just a point only select the top most
if (_mouseBox.Width <= 1 && _mouseBox.Height <= 1) break;
}
}
}
FireSelectionChanged();
_mouseMode = MouseMode.Default;
DoInvalidate();
break;
//Stops moving the selection
case MouseMode.MoveSelection:
_mouseMode = MouseMode.Default;
Cursor = Cursors.Default;
break;
//Turns of resize
case MouseMode.ResizeSelected:
RecycleResizeBitmap();
_mouseMode = MouseMode.Default;
Cursor = Cursors.Default;
if (_selectedLayoutElements.Count > 0)
{
_selectedLayoutElements[0].Resizing = false;
_selectedLayoutElements[0].SizeF = _selectedLayoutElements[0].SizeF;
DoInvalidate(new Region(PaperToScreen(_selectedLayoutElements[0].Rectangle)));
}
break;
case MouseMode.InsertNewElement:
if (_mouseBox.Width <= 0) _mouseBox.Width = 200;
if (_mouseBox.Height <= 0) _mouseBox.Height = 100;
if (_mouseBox.Width < 0)
{
_mouseBox.X = _mouseBox.X + _mouseBox.Width;
_mouseBox.Width = -_mouseBox.Width;
}
if (_mouseBox.Height < 0)
{
_mouseBox.Y = _mouseBox.Y + _mouseBox.Height;
_mouseBox.Height = -_mouseBox.Height;
}
_elementToAddWithMouse.Rectangle = ScreenToPaper(_mouseBox);
AddToLayout(_elementToAddWithMouse);
_elementToAddWithMouse = null;
_mouseMode = MouseMode.Default;
DoInvalidate();
break;
case MouseMode.PanMap:
_mouseMode = MouseMode.StartPanMap;
Cursor = Cursors.Default;
PanMap(_selectedLayoutElements[0] as LayoutMap, -_mouseBox.Width, -_mouseBox.Height);
break;
case MouseMode.Default:
break;
}
}
}
private void RecycleResizeBitmap()
{
if (_resizeTempBitmap != null)
{
_resizeTempBitmap.Dispose();
_resizeTempBitmap = null;
}
}
private void ResizeSelected(float deltaX, float deltaY)
{
if (NumericHelper.Equal(deltaX, 0f) && NumericHelper.Equal(deltaY, 0f)) return;
_suppressElementInvalidation = true;
var el = _selectedLayoutElements[0];
var oldScreenRect = PaperToScreen(el.Rectangle);
AdjustRectangle(ref oldScreenRect, deltaX, deltaY);
el.Rectangle = ScreenToPaper(oldScreenRect);
DoInvalidate();
Update();
_suppressElementInvalidation = false;
}
private void AdjustRectangle(ref RectangleF oldScreenRect, float deltaX, float deltaY)
{
switch (_resizeSelectedEdge)
{
case Edge.TopLeft:
oldScreenRect.X = oldScreenRect.X - deltaX;
oldScreenRect.Y = oldScreenRect.Y - deltaY;
oldScreenRect.Width = oldScreenRect.Width + deltaX;
oldScreenRect.Height = oldScreenRect.Height + deltaY;
break;
case Edge.Top:
oldScreenRect.Y = oldScreenRect.Y - deltaY;
oldScreenRect.Height = oldScreenRect.Height + deltaY;
break;
case Edge.TopRight:
oldScreenRect.Y = oldScreenRect.Y - deltaY;
oldScreenRect.Height = oldScreenRect.Height + deltaY;
oldScreenRect.Width = oldScreenRect.Width - deltaX;
break;
case Edge.Right:
oldScreenRect.Width = oldScreenRect.Width - deltaX;
break;
case Edge.BottomRight:
oldScreenRect.Width = oldScreenRect.Width - deltaX;
oldScreenRect.Height = oldScreenRect.Height - deltaY;
break;
case Edge.Bottom:
oldScreenRect.Height = oldScreenRect.Height - deltaY;
break;
case Edge.BottomLeft:
oldScreenRect.X = oldScreenRect.X - deltaX;
oldScreenRect.Width = oldScreenRect.Width + deltaX;
oldScreenRect.Height = oldScreenRect.Height - deltaY;
break;
case Edge.Left:
oldScreenRect.X = oldScreenRect.X - deltaX;
oldScreenRect.Width = oldScreenRect.Width + deltaX;
break;
}
}
}
}