-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPositionSelector.xaml.cs
124 lines (110 loc) · 4.24 KB
/
PositionSelector.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
using System;
using System.CodeDom.Compiler;
using System.Security.Cryptography;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace BongoPawClicker
{
/// <summary>
/// PositionSelector.xaml
/// </summary>
public partial class PositionSelector : Window
{
//Screen Coords
private Point mouseDownPoint;
private Point mouseUpPoint;
private Point currentPoint;
private bool randomAreaSelection = false;
private bool isDragging = false;
//Selected Area Parameters
private int x = 0;
private int y = 0;
private int w = 0;
private int h = 0;
//The window coordinates are offset from the screen coordinates,
//so using the screen coordinates directly to draw the rectangle will result in an inaccurate rectangle,
//hence the window coordinates are used instead.
private Point previewRectangleStartPoint;
private Point previewRectangleEndPoint;
public PositionSelector(bool randomAreaEnable)
{
InitializeComponent();
randomAreaSelection = randomAreaEnable;
}
private void PositionSelectorCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
mouseDownPoint = PointToScreen(e.GetPosition(this));
x = (int)mouseDownPoint.X;
y = (int)mouseDownPoint.Y;
if (randomAreaSelection)
{
previewRectangleStartPoint = e.GetPosition(this);
isDragging = true;
SelectedAreaPreview.Visibility = Visibility.Visible;
}
}
private void PositionSelectorCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (randomAreaSelection)
{
mouseUpPoint = PointToScreen(e.GetPosition(this));
w = (int)mouseUpPoint.X - x;
h = (int)mouseUpPoint.Y - y;
//If the user's selection direction is not from top-left to bottom-right.
//Convert
if (w < 0)
{
x += w;
w *= -1;
}
if (h < 0)
{
y += h;
h *= -1;
}
isDragging = false;
SelectedAreaPreview.Visibility = Visibility.Hidden;
}
//Update TextBox in main window and close position selector
if (Owner is MainWindow mainWindow)
{
mainWindow.UpdateClickPositionTextBox(x, y, w, h);
//Make the main window visible again
mainWindow.Opacity = 1;
Close();
}
}
private void PositionSelectorCanvas_MouseMove(object sender, MouseEventArgs e)
{
//Update Realtime Mouse Coordinates Labels
currentPoint = PointToScreen(e.GetPosition(this));
XCoordsPreview.Content = (int)currentPoint.X;
YCoordsPreview.Content = (int)currentPoint.Y;
//Draw Preview Rectangle
previewRectangleEndPoint = e.GetPosition(this);
if (randomAreaSelection && isDragging)
{
SelectedAreaPreview.Margin = new Thickness(
Math.Min((int)previewRectangleStartPoint.X, (int)previewRectangleEndPoint.X),
Math.Min((int)previewRectangleStartPoint.Y, (int)previewRectangleEndPoint.Y),
0, 0);
SelectedAreaPreview.Width = Math.Abs((int)previewRectangleEndPoint.X - (int)previewRectangleStartPoint.X);
SelectedAreaPreview.Height = Math.Abs((int)previewRectangleEndPoint.Y - (int)previewRectangleStartPoint.Y);
}
}
//Press Esc to close the selector without any point clicked
private void PositionSelector_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape)
{
if (Owner is MainWindow mainWindow)
{
//Make the main window visible again
mainWindow.Opacity = 1;
}
Close();
}
}
}
}