forked from CommunityToolkit/WindowsCommunityToolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageCropper.Events.cs
178 lines (157 loc) · 6.48 KB
/
ImageCropper.Events.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using Windows.Foundation;
using Windows.System;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Input;
namespace Microsoft.Toolkit.Uwp.UI.Controls
{
/// <summary>
/// The <see cref="ImageCropper"/> control allows user to crop image freely.
/// </summary>
public partial class ImageCropper
{
private void ImageCropperThumb_KeyDown(object sender, KeyRoutedEventArgs e)
{
var changed = false;
var diffPos = default(Point);
if (e.Key == VirtualKey.Left)
{
diffPos.X--;
var upKeyState = Window.Current.CoreWindow.GetAsyncKeyState(VirtualKey.Up);
var downKeyState = Window.Current.CoreWindow.GetAsyncKeyState(VirtualKey.Down);
if (upKeyState == CoreVirtualKeyStates.Down)
{
diffPos.Y--;
}
if (downKeyState == CoreVirtualKeyStates.Down)
{
diffPos.Y++;
}
changed = true;
}
else if (e.Key == VirtualKey.Right)
{
diffPos.X++;
var upKeyState = Window.Current.CoreWindow.GetAsyncKeyState(VirtualKey.Up);
var downKeyState = Window.Current.CoreWindow.GetAsyncKeyState(VirtualKey.Down);
if (upKeyState == CoreVirtualKeyStates.Down)
{
diffPos.Y--;
}
if (downKeyState == CoreVirtualKeyStates.Down)
{
diffPos.Y++;
}
changed = true;
}
else if (e.Key == VirtualKey.Up)
{
diffPos.Y--;
var leftKeyState = Window.Current.CoreWindow.GetAsyncKeyState(VirtualKey.Left);
var rightKeyState = Window.Current.CoreWindow.GetAsyncKeyState(VirtualKey.Right);
if (leftKeyState == CoreVirtualKeyStates.Down)
{
diffPos.X--;
}
if (rightKeyState == CoreVirtualKeyStates.Down)
{
diffPos.X++;
}
changed = true;
}
else if (e.Key == VirtualKey.Down)
{
diffPos.Y++;
var leftKeyState = Window.Current.CoreWindow.GetAsyncKeyState(VirtualKey.Left);
var rightKeyState = Window.Current.CoreWindow.GetAsyncKeyState(VirtualKey.Right);
if (leftKeyState == CoreVirtualKeyStates.Down)
{
diffPos.X--;
}
if (rightKeyState == CoreVirtualKeyStates.Down)
{
diffPos.X++;
}
changed = true;
}
if (changed)
{
var imageCropperThumb = (ImageCropperThumb)sender;
UpdateCroppedRect(imageCropperThumb.Position, diffPos);
}
}
private void ImageCropperThumb_KeyUp(object sender, KeyRoutedEventArgs e)
{
var selectedRect = new Rect(new Point(_startX, _startY), new Point(_endX, _endY));
var croppedRect = _inverseImageTransform.TransformBounds(selectedRect);
if (croppedRect.Width > MinCropSize.Width && croppedRect.Height > MinCropSize.Height)
{
croppedRect.Intersect(_restrictedCropRect);
CroppedRegion = croppedRect;
}
UpdateImageLayout(true);
}
private void ImageCropperThumb_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
{
var selectedRect = new Rect(new Point(_startX, _startY), new Point(_endX, _endY));
var croppedRect = _inverseImageTransform.TransformBounds(selectedRect);
if (croppedRect.Width > MinCropSize.Width && croppedRect.Height > MinCropSize.Height)
{
croppedRect.Intersect(_restrictedCropRect);
CroppedRegion = croppedRect;
}
UpdateImageLayout(true);
}
private void ImageCropperThumb_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
var imageCropperThumb = (ImageCropperThumb)sender;
var currentPointerPosition = new Point(
imageCropperThumb.X + e.Position.X + e.Delta.Translation.X - (imageCropperThumb.ActualWidth / 2),
imageCropperThumb.Y + e.Position.Y + e.Delta.Translation.Y - (imageCropperThumb.ActualHeight / 2));
var safePosition = GetSafePoint(_restrictedSelectRect, currentPointerPosition);
var safeDiffPoint = new Point(safePosition.X - imageCropperThumb.X, safePosition.Y - imageCropperThumb.Y);
UpdateCroppedRect(imageCropperThumb.Position, safeDiffPoint);
}
private void SourceImage_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
var offsetX = -e.Delta.Translation.X;
var offsetY = -e.Delta.Translation.Y;
if (offsetX > 0)
{
offsetX = Math.Min(offsetX, _restrictedSelectRect.X + _restrictedSelectRect.Width - _endX);
}
else
{
offsetX = Math.Max(offsetX, _restrictedSelectRect.X - _startX);
}
if (offsetY > 0)
{
offsetY = Math.Min(offsetY, _restrictedSelectRect.Y + _restrictedSelectRect.Height - _endY);
}
else
{
offsetY = Math.Max(offsetY, _restrictedSelectRect.Y - _startY);
}
var selectedRect = new Rect(new Point(_startX, _startY), new Point(_endX, _endY));
selectedRect.X += offsetX;
selectedRect.Y += offsetY;
var croppedRect = _inverseImageTransform.TransformBounds(selectedRect);
croppedRect.Intersect(_restrictedCropRect);
CroppedRegion = croppedRect;
UpdateImageLayout();
}
private void ImageCanvas_SizeChanged(object sender, SizeChangedEventArgs e)
{
if (Source == null)
{
return;
}
UpdateImageLayout();
UpdateMaskArea();
}
}
}