-
Notifications
You must be signed in to change notification settings - Fork 1
/
ZoomAndPanControl_Mouse.cs
228 lines (199 loc) · 7.98 KB
/
ZoomAndPanControl_Mouse.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
using System;
using System.Windows;
using System.Windows.Input;
namespace ZoomAndPan
{
/// <summary>
/// A class that wraps up zooming and panning of it's content.
/// </summary>
public partial class ZoomAndPanControl
{
public delegate double DZoomScale(ZoomAndPanControl control, double scale);
public event DZoomScale ZoomScale;
public event DZoomScale ZoomInScale;
public event DZoomScale ZoomOutScale;
/// <summary>
/// Specifies the current state of the mouse handling logic.
/// </summary>
private MouseHandlingMode mouseHandlingMode = MouseHandlingMode.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;
protected override void OnMouseDown(System.Windows.Input.MouseButtonEventArgs e)
{
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 = MouseHandlingMode.Zooming;
}
else if (mouseButtonDown == MouseButton.Left)
{
// Just a plain old left-down initiates panning mode.
mouseHandlingMode = MouseHandlingMode.Panning;
}
if (mouseHandlingMode != MouseHandlingMode.None)
{
// Capture the mouse so that we eventually receive the mouse up event.
this.CaptureMouse();
e.Handled = true;
}
base.OnMouseDown(e);
}
protected override void OnMouseUp(MouseButtonEventArgs e)
{
if (mouseHandlingMode != MouseHandlingMode.None)
{
if (mouseHandlingMode == MouseHandlingMode.Zooming)
{
if (mouseButtonDown == MouseButton.Left)
{
// Shift + left-click zooms in on the content.
ZoomIn(e);
}
else if (mouseButtonDown == MouseButton.Right)
{
// Shift + left-click zooms out from the content.
ZoomOut(e);
}
}
this.ReleaseMouseCapture();
mouseHandlingMode = MouseHandlingMode.None;
e.Handled = true;
}
base.OnMouseUp(e);
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (mouseHandlingMode == MouseHandlingMode.Panning)
{
//
// The user is left-dragging the mouse.
// Pan the viewport by the appropriate amount.
//
Point curContentMousePoint = e.GetPosition(content);
Vector dragOffset = curContentMousePoint - origContentMouseDownPoint;
this.ContentOffsetX -= dragOffset.X;
this.ContentOffsetY -= dragOffset.Y;
e.Handled = true;
}
base.OnMouseMove(e);
}
protected override void OnMouseWheel(MouseWheelEventArgs e)
{
e.Handled = true;
if (e.Delta > 0)
{
ZoomIn(e);
}
else if (e.Delta < 0)
{
ZoomOut(e);
}
base.OnMouseWheel(e);
}
const double ZoomModifiersAdjust = 5.0;
/// <summary>
/// Zoom the viewport out, centering on the specified point (in content coordinates).
/// </summary>
protected virtual void ZoomOut(Point contentZoomCenter)
{
double scale = AdjustScale(ContentScale);
if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
scale /= ZoomModifiersAdjust;
if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift)
scale *= ZoomModifiersAdjust / 3.0;
if (null != ZoomOutScale)
{
var Event = ZoomOutScale;
scale = Event(this, scale);
}
else if (null != ZoomScale)
{
var Event = ZoomScale;
scale = Event(this, scale);
}
if (scale <= 0)
throw new ArgumentOutOfRangeException("Scale cannot be zero or less for zooming");
//ZoomAboutPoint(this.ContentScale - scale, contentZoomCenter);
AnimatedZoomAboutPoint(this.ContentScale - scale, contentZoomCenter);
}
/// <summary>
/// Zoom the viewport in, centering on the specified point (in content coordinates).
/// </summary>
protected virtual void ZoomIn(Point contentZoomCenter)
{
double scale = AdjustScale(ContentScale);
if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
scale /= ZoomModifiersAdjust;
if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift)
scale *= ZoomModifiersAdjust;
if (null != ZoomInScale)
{
var Event = ZoomInScale;
scale = Event(this, scale);
}
else if (null != ZoomScale)
{
var Event = ZoomScale;
scale = Event(this, scale);
}
if (scale <= 0)
throw new ArgumentOutOfRangeException("Scale cannot be zero or less for zooming");
//ZoomAboutPoint(this.ContentScale + scale, contentZoomCenter);
AnimatedZoomAboutPoint(this.ContentScale + scale, contentZoomCenter);
}
/// <summary>
/// Zoom the viewport out, centering on the mouse location (in content coordinates).
/// </summary>
protected virtual void ZoomOut(MouseEventArgs e)
{
ZoomOut(e.GetPosition(content));
}
/// <summary>
/// Zoom the viewport in, centering on the mouse location (in content coordinates).
/// </summary>
protected virtual void ZoomIn(MouseEventArgs e)
{
ZoomIn(e.GetPosition(content));
}
/// <summary>
/// Make changes less noticeable than the great leaps it otherwise would be
/// </summary>
/// <param name="scale"></param>
/// <returns></returns>
private double AdjustScale(double scale)
{
return scale / 2.0;
/* old scaler when input was a constant, not based on size of image
* and this would adjust for image size, and it wasn't that great
double adjust = 1.0;
while (ContentScaleNormal < adjust)
{
scale /= 5;
adjust /= 10;
}
if (ContentScale < ContentScaleNormal)
scale /= 5;
else if (ContentScale < ContentScaleNormal * 1.25)
scale /= 2;
return scale;
*/
}
}
}