-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZoomAndPanControl_EventHandlers.cs
414 lines (363 loc) · 16.3 KB
/
ZoomAndPanControl_EventHandlers.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
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace ZoomAndPan
{
public partial class ZoomAndPanControl
{
private void ZoomAndPanControl_EventHandlers_OnApplyTemplate()
{
_partDragZoomBorder = this.Template.FindName("PART_DragZoomBorder", this) as Border;
_partDragZoomCanvas = this.Template.FindName("PART_DragZoomCanvas", this) as Canvas;
}
public void ZoomExtend()
{
SaveZoom();
AnimatedZoomTo(FitZoomValue);
RaiseCanExecuteChanged();
}
/// <summary>
/// The control for creating a zoom border
/// </summary>
private Border _partDragZoomBorder;
/// <summary>
/// The control for containing a zoom border
/// </summary>
private Canvas _partDragZoomCanvas;
/// <summary>
/// Specifies the current state of the mouse handling logic.
/// </summary>
private MouseHandlingModeEnum _mouseHandlingMode = MouseHandlingModeEnum.None;
/// <summary>
/// The point that was clicked relative to the ZoomAndPanControl.
/// </summary>
private Point _origZoomAndPanControlMouseDownPoint;
/// <summary>
/// The point that was clicked relative to the content that is contained within the ZoomAndPanControl.
/// </summary>
private Point _origContentMouseDownPoint;
/// <summary>
/// Records which mouse button clicked during mouse dragging.
/// </summary>
private MouseButton _mouseButtonDown;
/// <summary>
/// Event raised on mouse down in the ZoomAndPanControl.
/// </summary>
protected override void OnMouseDown(MouseButtonEventArgs e)
{
base.OnMouseDown(e);
SaveZoom();
_content.Focus();
Keyboard.Focus(_content);
_mouseButtonDown = e.ChangedButton;
_origZoomAndPanControlMouseDownPoint = e.GetPosition(this);
_origContentMouseDownPoint = e.GetPosition(_content);
if ((Keyboard.Modifiers & ModifierKeys.Shift) != 0 &&
(e.ChangedButton == MouseButton.Left ||
e.ChangedButton == MouseButton.Right))
{
// Shift + left- or right-down initiates zooming mode.
_mouseHandlingMode = MouseHandlingModeEnum.Zooming;
}
else if (_mouseButtonDown == MouseButton.Left)
{
// Just a plain old left-down initiates panning mode.
_mouseHandlingMode = MouseHandlingModeEnum.Panning;
}
if (_mouseHandlingMode != MouseHandlingModeEnum.None)
{
// Capture the mouse so that we eventually receive the mouse up event.
this.CaptureMouse();
}
}
/// <summary>
/// Event raised on mouse up in the ZoomAndPanControl.
/// </summary>
protected override void OnMouseUp(MouseButtonEventArgs e)
{
base.OnMouseUp(e);
if (_mouseHandlingMode != MouseHandlingModeEnum.None)
{
if (_mouseHandlingMode == MouseHandlingModeEnum.Zooming)
{
if (_mouseButtonDown == MouseButton.Left)
{
// Shift + left-click zooms in on the content.
ZoomIn(_origContentMouseDownPoint);
}
else if (_mouseButtonDown == MouseButton.Right)
{
// Shift + left-click zooms out from the content.
ZoomOut(_origContentMouseDownPoint);
}
}
else if (_mouseHandlingMode == MouseHandlingModeEnum.DragZooming)
{
var finalContentMousePoint = e.GetPosition(_content);
// When drag-zooming has finished we zoom in on the rectangle that was highlighted by the user.
ApplyDragZoomRect(finalContentMousePoint);
}
this.ReleaseMouseCapture();
_mouseHandlingMode = MouseHandlingModeEnum.None;
}
}
/// <summary>
/// Event raised on mouse move in the ZoomAndPanControl.
/// </summary>
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
var oldContentMousePoint = MousePosition;
var curContentMousePoint = e.GetPosition(_content);
MousePosition = curContentMousePoint.FilterClamp(_content.ActualWidth - 1, _content.ActualHeight - 1);
OnPropertyChanged(new DependencyPropertyChangedEventArgs(MousePositionProperty, oldContentMousePoint,
curContentMousePoint));
if (_mouseHandlingMode == MouseHandlingModeEnum.Panning)
{
//
// The user is left-dragging the mouse.
// Pan the viewport by the appropriate amount.
//
var dragOffset = curContentMousePoint - _origContentMouseDownPoint;
this.ContentOffsetX -= dragOffset.X;
this.ContentOffsetY -= dragOffset.Y;
e.Handled = true;
}
else if (_mouseHandlingMode == MouseHandlingModeEnum.Zooming)
{
var curZoomAndPanControlMousePoint = e.GetPosition(this);
var dragOffset = curZoomAndPanControlMousePoint - _origZoomAndPanControlMouseDownPoint;
double dragThreshold = 10;
if (_mouseButtonDown == MouseButton.Left &&
(Math.Abs(dragOffset.X) > dragThreshold ||
Math.Abs(dragOffset.Y) > dragThreshold))
{
//
// When Shift + left-down zooming mode and the user drags beyond the drag threshold,
// initiate drag zooming mode where the user can drag out a rectangle to select the area
// to zoom in on.
//
_mouseHandlingMode = MouseHandlingModeEnum.DragZooming;
InitDragZoomRect(_origContentMouseDownPoint, curContentMousePoint);
}
}
else if (_mouseHandlingMode == MouseHandlingModeEnum.DragZooming)
{
//
// When in drag zooming mode continously update the position of the rectangle
// that the user is dragging out.
//
curContentMousePoint = e.GetPosition(this);
SetDragZoomRect(_origZoomAndPanControlMouseDownPoint, curContentMousePoint);
}
}
/// <summary>
/// Event raised on mouse wheel moved in the ZoomAndPanControl.
/// </summary>
protected override void OnMouseWheel(MouseWheelEventArgs e)
{
base.OnMouseWheel(e);
DelayedSaveZoom750Miliseconds();
e.Handled = true;
if (e.Delta > 0)
ZoomIn(e.GetPosition(_content));
else if (e.Delta < 0)
ZoomOut(e.GetPosition(_content));
}
/// <summary>
/// Event raised with the double click command
/// </summary>
protected override void OnMouseDoubleClick(MouseButtonEventArgs e)
{
base.OnMouseDoubleClick(e);
if ((Keyboard.Modifiers & ModifierKeys.Shift) == 0)
{
SaveZoom();
this.AnimatedSnapTo(e.GetPosition(_content));
}
}
#region private Zoom methods
/// <summary>
/// Zoom the viewport out, centering on the specified point (in content coordinates).
/// </summary>
private void ZoomOut(Point contentZoomCenter)
{
this.ZoomAboutPoint(this.InternalViewportZoom * 0.90909090909, contentZoomCenter);
}
/// <summary>
/// Zoom the viewport in, centering on the specified point (in content coordinates).
/// </summary>
private void ZoomIn(Point contentZoomCenter)
{
this.ZoomAboutPoint(this.InternalViewportZoom * 1.1, contentZoomCenter);
}
/// <summary>
/// Initialise the rectangle that the use is dragging out.
/// </summary>
private void InitDragZoomRect(Point pt1, Point pt2)
{
_partDragZoomCanvas.Visibility = Visibility.Visible;
_partDragZoomBorder.Opacity = 1;
SetDragZoomRect(pt1, pt2);
}
/// <summary>
/// Update the position and size of the rectangle that user is dragging out.
/// </summary>
private void SetDragZoomRect(Point pt1, Point pt2)
{
//
// Update the coordinates of the rectangle that is being dragged out by the user.
// The we offset and rescale to convert from content coordinates.
//
var rect = ViewportHelpers.Clip(pt1, pt2, new Point(0, 0),
new Point(_partDragZoomCanvas.ActualWidth, _partDragZoomCanvas.ActualHeight));
ViewportHelpers.PositionBorderOnCanvas(_partDragZoomBorder, rect);
}
/// <summary>
/// When the user has finished dragging out the rectangle the zoom operation is applied.
/// </summary>
private void ApplyDragZoomRect(Point finalContentMousePoint)
{
var rect = ViewportHelpers.Clip(finalContentMousePoint, _origContentMouseDownPoint, new Point(0, 0),
new Point(_partDragZoomCanvas.ActualWidth, _partDragZoomCanvas.ActualHeight));
this.AnimatedZoomTo(rect);
// new Rect(contentX, contentY, contentWidth, contentHeight));
FadeOutDragZoomRect();
}
//
// Fade out the drag zoom rectangle.
//
private void FadeOutDragZoomRect()
{
AnimationHelper.StartAnimation(_partDragZoomBorder, OpacityProperty, 0.0, 0.1,
delegate { _partDragZoomCanvas.Visibility = Visibility.Collapsed; }, UseAnimations);
}
#endregion
#region Commands
/// <summary>
/// Command to implement the zoom to fill
/// </summary>
public ICommand FillCommand => _fillCommand ?? (_fillCommand = new RelayCommand(() =>
{
SaveZoom();
AnimatedZoomToCentered(FillZoomValue);
RaiseCanExecuteChanged();
}, () => !InternalViewportZoom.IsWithinOnePercent(FillZoomValue) && FillZoomValue >= MinimumZoomClamped));
private RelayCommand _fillCommand;
/// <summary>
/// Command to implement the zoom to fit
/// </summary>
public ICommand FitCommand => _fitCommand ?? (_fitCommand = new RelayCommand(() =>
{
SaveZoom();
AnimatedZoomTo(FitZoomValue);
RaiseCanExecuteChanged();
}, () => !InternalViewportZoom.IsWithinOnePercent(FitZoomValue) && FitZoomValue >= MinimumZoomClamped));
private RelayCommand _fitCommand;
/// <summary>
/// Command to implement the zoom to a percentage where 100 (100%) is the default and
/// shows the image at a zoom where 1 pixel is 1 pixel. Other percentages specified
/// with the command parameter. 50 (i.e. 50%) would display 4 times as much of the image
/// </summary>
public ICommand ZoomPercentCommand
=> _zoomPercentCommand ?? (_zoomPercentCommand = new RelayCommand<double>(value =>
{
SaveZoom();
var adjustedValue = value == 0 ? 1 : value / 100;
AnimatedZoomTo(adjustedValue);
RaiseCanExecuteChanged();
}, value =>
{
var adjustedValue = value == 0 ? 1 : value / 100;
return !InternalViewportZoom.IsWithinOnePercent(adjustedValue) && adjustedValue >= MinimumZoomClamped;
}));
// Math.Abs(InternalViewportZoom - ((value == 0) ? 1.0 : value / 100)) > .01 * InternalViewportZoom
private RelayCommand<double> _zoomPercentCommand;
/// <summary>
/// Command to implement the zoom ratio where 1 is is the the specified minimum. 2 make the image twices the size,
/// and is the default. Other values are specified with the CommandParameter.
/// </summary>
public ICommand ZoomRatioFromMinimumCommand
=> _zoomRatioFromMinimumCommand ?? (_zoomRatioFromMinimumCommand = new RelayCommand<double>(value =>
{
SaveZoom();
var adjustedValue = (value == 0 ? 2 : value) * MinimumZoomClamped;
AnimatedZoomTo(adjustedValue);
RaiseCanExecuteChanged();
}, value =>
{
var adjustedValue = (value == 0 ? 2 : value) * MinimumZoomClamped;
return !InternalViewportZoom.IsWithinOnePercent(adjustedValue) && adjustedValue >= MinimumZoomClamped;
}));
private RelayCommand<double> _zoomRatioFromMinimumCommand;
/// <summary>
/// Command to implement the zoom out by 110%
/// </summary>
public ICommand ZoomOutCommand => _zoomOutCommand ?? (_zoomOutCommand = new RelayCommand(() =>
{
DelayedSaveZoom1500Miliseconds();
ZoomOut(new Point(ContentZoomFocusX, ContentZoomFocusY));
}, () => InternalViewportZoom > MinimumZoomClamped));
private RelayCommand _zoomOutCommand;
/// <summary>
/// Command to implement the zoom in by 91%
/// </summary>
public ICommand ZoomInCommand => _zoomInCommand ?? (_zoomInCommand = new RelayCommand(() =>
{
DelayedSaveZoom1500Miliseconds();
ZoomIn(new Point(ContentZoomFocusX, ContentZoomFocusY));
}, () => InternalViewportZoom < MaximumZoom));
private RelayCommand _zoomInCommand;
private void RaiseCanExecuteChanged()
{
_zoomPercentCommand?.RaiseCanExecuteChanged();
_zoomOutCommand?.RaiseCanExecuteChanged();
_zoomInCommand?.RaiseCanExecuteChanged();
_fitCommand?.RaiseCanExecuteChanged();
_fillCommand?.RaiseCanExecuteChanged();
}
#endregion
/// <summary>
/// When content is renewed, set event to set the initial position as specified
/// </summary>
/// <param name="oldContent"></param>
/// <param name="newContent"></param>
protected override void OnContentChanged(object oldContent, object newContent)
{
base.OnContentChanged(oldContent, newContent);
if (oldContent != null)
((FrameworkElement)oldContent).SizeChanged -= SetZoomAndPanInitialPosition;
((FrameworkElement)newContent).SizeChanged += SetZoomAndPanInitialPosition;
}
/// <summary>
/// When content is renewed, set the initial position as specified
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SetZoomAndPanInitialPosition(object sender, SizeChangedEventArgs e)
{
switch (ZoomAndPanInitialPosition)
{
case ZoomAndPanInitialPositionEnum.Default:
break;
case ZoomAndPanInitialPositionEnum.FitScreen:
InternalViewportZoom = FitZoomValue;
break;
case ZoomAndPanInitialPositionEnum.FillScreen:
InternalViewportZoom = FillZoomValue;
ContentOffsetX = (_content.ActualWidth - ViewportWidth / InternalViewportZoom) / 2;
ContentOffsetY = (_content.ActualHeight - ViewportHeight / InternalViewportZoom) / 2;
break;
case ZoomAndPanInitialPositionEnum.OneHundredPercentCentered:
InternalViewportZoom = 1.0;
ContentOffsetX = (_content.ActualWidth - ViewportWidth) / 2;
ContentOffsetY = (_content.ActualHeight - ViewportHeight) / 2;
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
}