-
Notifications
You must be signed in to change notification settings - Fork 4
/
ViewWindow.xaml.cs.notworking
228 lines (196 loc) · 9.24 KB
/
ViewWindow.xaml.cs.notworking
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.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using FontAwesome.WPF;
using static ZipImageViewer.ExtentionMethods;
namespace ZipImageViewer
{
public class CenterConterter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values == null || values.Length < 2)
throw new ArgumentException("Two double values need to be passed in this order -> totalWidth, width", nameof(values));
var totalWidth = (double)values[0];
var width = (double)values[1];
return (totalWidth - width) / 2;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public partial class ViewWindow : Window
{
public ImageInfo ImageInfo
{
get { return (ImageInfo)GetValue(ImageInfoProperty); }
set { SetValue(ImageInfoProperty, value); }
}
public static readonly DependencyProperty ImageInfoProperty =
Thumbnail.ImageInfoProperty.AddOwner(typeof(ViewWindow));
private double Scale = 1d;
private bool IsRealSize =>
IM.RealSize.Width.Equals(IM.ActualWidth * Scale) && IM.RealSize.Height.Equals(IM.ActualHeight * Scale);
public ViewWindow()
{
InitializeComponent();
}
private void ViewWindow_Loaded(object sender, RoutedEventArgs e) {
// var rect = NativeMethods.GetMonitorFromWindow(this);
// Top = rect.Top;
// Left = rect.Left;
// Width = rect.Width;
// Height = rect.Height;
// if (double.IsNaN(IM.Width)) IM.Width = IM.ActualWidth;
// if (double.IsNaN(IM.Height)) IM.Height = IM.ActualHeight;
SwitchRealSize();
}
private void ViewWin_MouseUp(object sender, MouseButtonEventArgs e) {
if (e.ChangedButton == MouseButton.Right) Close();
}
private void Transform(int ms, Size newSize, Point center, Matrix? fromMatrix = null) {
Transform(ms, newSize.Width / IM.RealSize.Width, center, fromMatrix);
}
private void Transform(int ms, double scale, Point center, Matrix? fromMatrix = null) {
var matrix = fromMatrix ?? IM_RT.Matrix;
matrix.ScaleAtPrepend(scale, scale, center.X, center.Y);
var anim = new MatrixAnimation(IM_RT.Value, matrix, new Duration(TimeSpan.FromMilliseconds(ms)));
IM_RT.BeginAnimation(MatrixTransform.MatrixProperty, anim);
Scale *= scale;
BM.Show($"{Scale:P0}");
}
private void SwitchRealSize(Point? mousePosition = null) {
if (IsRealSize) {
if (IM.ActualHeight * Scale <= CA.ActualHeight && IM.ActualWidth * Scale <= CA.ActualWidth) return;
var newSize = Helpers.UniformScaleUp(IM.ActualWidth, IM.ActualHeight, CA.ActualWidth, CA.ActualHeight);
Transform(400, newSize, new Point(0, 0));
}
else {
if (!mousePosition.HasValue)
mousePosition = new Point(IM.ActualWidth / 2d, IM.ActualHeight / 2d);
Transform(400, 1d, mousePosition.Value);
}
}
/*Point scrollMousePoint;
double hOff = 1;
double vOff = 1;
private void SV_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
scrollMousePoint = e.GetPosition(SV);
hOff = SV.HorizontalOffset;
vOff = SV.VerticalOffset;
SV.CaptureMouse();
}
private void SV_PreviewMouseMove(object sender, MouseEventArgs e)
{
if(SV.IsMouseCaptured)
{
SV.ScrollToHorizontalOffset(hOff + 3d * (scrollMousePoint.X - e.GetPosition(SV).X));
SV.ScrollToVerticalOffset(vOff + 3d * (scrollMousePoint.Y - e.GetPosition(SV).Y));
}
}
private void SV_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
SV.ReleaseMouseCapture();
}
private void SV_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e) {
if (IsActualSize) {
if (IM.ActualHeight <= SV.ViewportHeight && IM.ActualWidth <= SV.ViewportWidth) return;
var newSize = Helpers.UniformScaleUp(IM.ActualWidth, IM.ActualHeight, SV.ViewportWidth, SV.ViewportHeight);
IM.AnimateDoubleCubicEase(WidthProperty, newSize.Width, 400, EasingMode.EaseOut);
IM.AnimateDoubleCubicEase(HeightProperty, newSize.Height, 400, EasingMode.EaseOut);
IsActualSize = false;
}
else {
IM.AnimateDoubleCubicEase(WidthProperty, IM.RealSize.Width, 400, EasingMode.EaseOut);
IM.AnimateDoubleCubicEase(HeightProperty, IM.RealSize.Height, 400, EasingMode.EaseOut);
// SV.ScrollToHorizontalOffset((SV.ViewportWidth - SV.ExtentWidth) / 2);
// SV.ScrollToVerticalOffset((SV.ViewportHeight - SV.ExtentHeight) / 2);
// SV.UpdateLayout();
IsActualSize = true;
}
}
private void SV_PreviewMouseWheel(object sender, MouseWheelEventArgs e) {
IsActualSize = false;
var scale = e.Delta > 0 ? 1.2d : 0.8d;
//prevent zooming out smaller than viewport
var newSize = Helpers.UniformScaleDown(IM.ActualWidth * scale, IM.ActualHeight * scale,
SV.ViewportWidth, SV.ViewportHeight);
IM.AnimateDoubleCubicEase(WidthProperty, newSize.Width, 100, EasingMode.EaseOut);
IM.AnimateDoubleCubicEase(HeightProperty, newSize.Height, 100, EasingMode.EaseOut);
e.Handled = true;
// IM.Width *= scale;
// IM.Height *= scale;
//
// var mousePos = e.GetPosition(SV);
// var scale = e.Delta > 0 ? 0.1d : -0.1d;
//// Console.WriteLine(mousePos.X / SV.ViewportWidth);
//// SV.ScrollToHorizontalOffset(SV.ExtentWidth * mousePos.X / SV.ViewportWidth);
//// SV.ScrollToVerticalOffset(SV.ExtentHeight * mousePos.Y / SV.ViewportHeight);
// var st = (ScaleTransform)IM.LayoutTransform;
// st.CenterX = IM.ActualWidth * mousePos.X / SV.ViewportWidth;
// st.CenterY = IM.ActualHeight * mousePos.Y / SV.ViewportHeight;
// st.ScaleX += scale;
// st.ScaleY += scale;
// var mousePos = e.GetPosition(SV);
// var scale = e.Delta > 0 ? 0.1d : -0.1d;
// var st = (ScaleTransform)IM.LayoutTransform;
// st.ScaleX += scale;
// st.ScaleY += scale;
}*/
private Point mouseCapturePoint;
private Matrix existingTranslate;
private void CA_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
if (e.ClickCount == 1) {
mouseCapturePoint = e.GetPosition(CA);
existingTranslate = IM_RT.Matrix;
CA.CaptureMouse();
}
else if (e.ClickCount == 2) {
SwitchRealSize(e.GetPosition(IM));
}
}
private void CA_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (CA.IsMouseCaptured)
{
// Console.WriteLine(mouseCapturePoint.X - e.GetPosition(CA).X);
IM_RT.BeginAnimation(MatrixTransform.MatrixProperty, null);
IM_RT.Matrix.Translate(
existingTranslate.OffsetX + (e.GetPosition(CA).X - mouseCapturePoint.X) * Scale * 2d,
existingTranslate.OffsetY + (e.GetPosition(CA).Y - mouseCapturePoint.Y) * Scale * 2d);
}
}
private void CA_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
CA.ReleaseMouseCapture();
}
private void CA_PreviewMouseWheel(object sender, MouseWheelEventArgs e) {
var scale = e.Delta > 0 ? 1.2d : 1d / 1.2d;
//prevent zooming out smaller than viewport
var maxSize = new Size(Math.Min(IM.RealSize.Width, CA.ActualWidth), Math.Min(IM.RealSize.Height, CA.ActualHeight));
var newSize = Helpers.UniformScaleDown(IM.ActualWidth * scale, IM.ActualHeight * scale, maxSize.Width, maxSize.Height);
//resize and move according to mouse position
var position = e.GetPosition(IM);
var transform = IM.RenderTransform as MatrixTransform;
var matrix = transform.Matrix;
matrix.ScaleAtPrepend(scale, scale, position.X, position.Y);
transform.Matrix = matrix;
// Transform(100, newSize, new Point(e.GetPosition(CA).X * (scale - 1d), e.GetPosition(CA).Y * (scale - 1d)));
}
}
}