-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMainWindow.xaml.cs
316 lines (289 loc) · 10.9 KB
/
MainWindow.xaml.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
using System;
using System.IO;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace GPUImageViewer
{
/// <summary>
/// Логика взаимодействия для MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
timer.Interval = new TimeSpan(100000);
timer.Tick += new EventHandler(handle_timer);
timer.Start();
string[] args = Environment.GetCommandLineArgs();
if (args.Length > 1)
open_filename(args[1]);
grid.Background = brushes[brush_index];
RenderOptions.SetBitmapScalingMode(image, BitmapScalingMode.HighQuality);
}
private string file_directory = null;
private List<string> filenames = null;
private int cursor = 0;
enum PICSTARTMODE
{
FIT,
ORIGINALTOP,
};
PICSTARTMODE startmode = PICSTARTMODE.FIT;
Brush[] brushes = new Brush[] {
new SolidColorBrush(Colors.Gray),
new SolidColorBrush(Colors.Black),
new SolidColorBrush(Colors.White)};
int brush_index = 0;
public static ImageSource BitmapFromUri(Uri source)
{
var bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = source;
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.EndInit();
return bitmap;
}
private void Window_Drop(object sender, DragEventArgs e)
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
open_filename(files[0]);
}
private void open_filename(string name)
{
image.Source = BitmapFromUri(new Uri(name));
init_transform();
file_directory = System.IO.Path.GetDirectoryName(name);
var all_files = new List<string>(Directory.GetFiles(file_directory).OrderBy(f => f));
var regex = new Regex(@".+\.(jpg|png|jpeg|bmp)", RegexOptions.IgnoreCase);
filenames = all_files.Where(f => regex.IsMatch(f)).ToList();
cursor = filenames.IndexOf(name);
}
private void image_MouseWheel(object sender, MouseWheelEventArgs e)
{
Point p = e.MouseDevice.GetPosition(image);
double zoom = e.Delta > 0 ? .2 : -.2;
perform_zoom(p, zoom);
}
private void perform_zoom(Point p, double zoom)
{
var m = image.RenderTransform.Value;
m.ScaleAtPrepend(1.0 + zoom, 1.0 + zoom, p.X, p.Y);
image.RenderTransform = new MatrixTransform(m);
}
Point old_pos = new Point();
bool dragging = false;
private void image_MouseMove(object sender, MouseEventArgs e)
{
if (dragging)
{
Point new_pos = e.GetPosition(this);
double deltax = new_pos.X - old_pos.X;
double deltay = new_pos.Y - old_pos.Y;
var m = image.RenderTransform.Value;
m.Translate(deltax, deltay);
image.RenderTransform = new MatrixTransform(m);
old_pos = new_pos;
}
}
private void image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (!dragging)
{
dragging = true;
old_pos = e.GetPosition(this);
}
}
private void image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
dragging = false;
}
private void Window_KeyDown(object sender, KeyEventArgs e)
{
switch (e.Key)
{
case Key.F:
if (this.WindowState == WindowState.Maximized)
{
if (this.WindowStyle == WindowStyle.None)
{
this.WindowStyle = WindowStyle.SingleBorderWindow;
this.ResizeMode = ResizeMode.CanResize;
this.WindowState = WindowState.Normal;
this.WindowState = WindowState.Maximized;
Mouse.OverrideCursor = null;
}
else
{
this.WindowStyle = WindowStyle.None;
this.ResizeMode = ResizeMode.NoResize;
this.WindowState = WindowState.Normal;
this.WindowState = WindowState.Maximized;
Mouse.OverrideCursor = Cursors.None;
}
}
else
{
this.WindowStyle = WindowStyle.None;
this.ResizeMode = ResizeMode.NoResize;
this.WindowState = WindowState.Maximized;
Mouse.OverrideCursor = Cursors.None;
}
break;
case Key.Escape:
init_transform();
break;
case Key.X:
next_picture();
break;
case Key.Z:
prev_picture();
break;
case Key.Right:
if (Keyboard.IsKeyDown(Key.LeftShift) ||
Keyboard.IsKeyDown(Key.RightShift))
{
next_picture();
}
break;
case Key.Left:
if (Keyboard.IsKeyDown(Key.LeftShift) ||
Keyboard.IsKeyDown(Key.RightShift))
{
prev_picture();
}
break;
case Key.V:
if (startmode == PICSTARTMODE.FIT)
startmode = PICSTARTMODE.ORIGINALTOP;
else if (startmode == PICSTARTMODE.ORIGINALTOP)
startmode = PICSTARTMODE.FIT;
break;
case Key.B:
brush_index = (brush_index + 1 == brushes.Length) ? 0 : brush_index + 1;
grid.Background = brushes[brush_index];
break;
}
}
private void next_picture()
{
if (filenames.Count > 0)
{
cursor = (cursor + 1) % filenames.Count;
update_from_cursor();
}
}
private void prev_picture()
{
if (filenames.Count > 0)
{
cursor--;
if (cursor < 0)
cursor = filenames.Count - 1;
update_from_cursor();
}
}
private Point get_image_center_global()
{
var m = image.RenderTransform.Value;
var im = m;
im.Invert();
Point p = im.Transform(new Point(image.ActualWidth / 2.0, image.ActualHeight / 2.0));
return p;
}
DispatcherTimer timer = new DispatcherTimer();
const double KEYBOARD_SPEED = 12.5;
const double KEYBOARD_ZOOM_SPEED = 0.03;
private void handle_timer(object sender, EventArgs args)
{
if (image.Source == null)
return;
if (!this.IsKeyboardFocusWithin)
return;
if (!(Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift)))
{
if (Keyboard.IsKeyDown(Key.Left) || Keyboard.IsKeyDown(Key.A))
{
var m = image.RenderTransform.Value;
m.Translate(KEYBOARD_SPEED, 0.0);
image.RenderTransform = new MatrixTransform(m);
}
if (Keyboard.IsKeyDown(Key.Right) || Keyboard.IsKeyDown(Key.D))
{
var m = image.RenderTransform.Value;
m.Translate(-KEYBOARD_SPEED, 0.0);
image.RenderTransform = new MatrixTransform(m);
}
if (Keyboard.IsKeyDown(Key.Up) || Keyboard.IsKeyDown(Key.W))
{
var m = image.RenderTransform.Value;
m.Translate(0.0, KEYBOARD_SPEED);
image.RenderTransform = new MatrixTransform(m);
}
if (Keyboard.IsKeyDown(Key.Down) || Keyboard.IsKeyDown(Key.S))
{
var m = image.RenderTransform.Value;
m.Translate(0.0, -KEYBOARD_SPEED);
image.RenderTransform = new MatrixTransform(m);
}
if (Keyboard.IsKeyDown(Key.Q))
{
Point p = get_image_center_global();
perform_zoom(p, -KEYBOARD_ZOOM_SPEED);
}
if (Keyboard.IsKeyDown(Key.E))
{
Point p = get_image_center_global();
perform_zoom(p, KEYBOARD_ZOOM_SPEED);
}
}
}
private void update_from_cursor()
{
if (filenames.Count > 1)
{
string img_name = filenames[cursor];
image.Source = BitmapFromUri(new Uri(img_name));
init_transform();
}
}
private void init_transform()
{
if (image.Source == null)
return;
switch (startmode)
{
case PICSTARTMODE.FIT:
image.RenderTransform = new MatrixTransform();
break;
case PICSTARTMODE.ORIGINALTOP:
image.RenderTransform = new MatrixTransform();
double xscale = grid.ActualWidth / image.Source.Width;
double yscale = grid.ActualHeight / image.Source.Height;
double current_scale = Math.Min(xscale, yscale);
double k = 1.0;
if (xscale < 1.0)
k = xscale;
var trans = new MatrixTransform();
var m = trans.Value;
m.ScaleAtPrepend(k / current_scale, k / current_scale,
current_scale * image.Source.Width / 2.0, 0.0);
image.RenderTransform = new MatrixTransform(m);
break;
}
}
}
}