-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTriggerColor.cs
More file actions
170 lines (138 loc) · 5.12 KB
/
TriggerColor.cs
File metadata and controls
170 lines (138 loc) · 5.12 KB
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
using System.Runtime.InteropServices;
using Emgu.CV;
using Emgu.CV.CvEnum;
class TriggerColor(CancellationToken token, Arduino arduino, string enemyColor)
{
[DllImport("user32.dll")]
private static extern short GetAsyncKeyState(int vKey);
[DllImport("gdi32.dll")]
private static extern bool BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
[DllImport("user32.dll")]
private static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
private static extern IntPtr GetWindowDC(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
private const int VK_XBUTTON2 = 0x06; // Side btn front
public void InitTriggerColor()
{
Mat screen = null;
try
{
while (!token.IsCancellationRequested)
{
var isPressed = IsSideFrontMouseButtonPressed();
if (isPressed)
{
screen = CaptureRegionFov();
if (screen != null)
{
Point? target = FindTarget(screen);
if (target.HasValue)
{
SendFireToArduino();
Thread.Sleep(50);
}
}
}
}
}
finally
{
screen?.Dispose();
}
}
private Mat CaptureRegionFov()
{
int screenWidth = Screen.PrimaryScreen.Bounds.Width;
int screenHeight = Screen.PrimaryScreen.Bounds.Height;
int fov = 4;
int diameter = fov * 2;
int x = (screenWidth - diameter) / 2;
int y = (screenHeight - diameter) / 2;
if (x < 0 || y < 0 || x + diameter > screenWidth || y + diameter > screenHeight || diameter <= 0)
{
Console.WriteLine("Região de captura fora dos limites da tela ou inválida.");
return null;
}
Rectangle region = new Rectangle(x, y, diameter, diameter);
Bitmap bitmap = new Bitmap(region.Width, region.Height);
try
{
using (Graphics g = Graphics.FromImage(bitmap))
{
IntPtr desktopHandle = GetDesktopWindow();
IntPtr desktopDC = GetWindowDC(desktopHandle);
IntPtr graphicsDC = g.GetHdc();
if (!BitBlt(graphicsDC, 0, 0, region.Width, region.Height, desktopDC, region.X, region.Y, 0x00CC0020))
{
Console.WriteLine("Erro ao copiar a região usando BitBlt.");
}
g.ReleaseHdc(graphicsDC);
ReleaseDC(desktopHandle, desktopDC);
}
Mat mat = new Mat();
BitmapToMat(bitmap, mat);
return mat;
}
catch (Exception ex)
{
Console.WriteLine($"Erro ao capturar a tela: {ex.Message}");
return null;
}
}
private void BitmapToMat(Bitmap bitmap, Mat mat)
{
if (bitmap.PixelFormat != System.Drawing.Imaging.PixelFormat.Format24bppRgb &&
bitmap.PixelFormat != System.Drawing.Imaging.PixelFormat.Format32bppArgb)
{
throw new NotSupportedException("Somente bitmaps em Format24bppRgb ou Format32bppArgb são suportados.");
}
System.Drawing.Imaging.BitmapData bitmapData = bitmap.LockBits(
new Rectangle(0, 0, bitmap.Width, bitmap.Height),
System.Drawing.Imaging.ImageLockMode.ReadOnly,
bitmap.PixelFormat
);
try
{
int channels = (bitmap.PixelFormat == System.Drawing.Imaging.PixelFormat.Format24bppRgb) ? 3 : 4;
mat.Create(bitmap.Height, bitmap.Width, Emgu.CV.CvEnum.DepthType.Cv8U, channels);
using (Mat tempMat = new(bitmap.Height, bitmap.Width, Emgu.CV.CvEnum.DepthType.Cv8U, channels, bitmapData.Scan0, bitmapData.Stride))
{
tempMat.CopyTo(mat);
}
}
finally
{
bitmap.UnlockBits(bitmapData);
}
}
private Point? FindTarget(Mat screen)
{
var lowerBound = Colors.GetColorBounds(enemyColor).Item1;
var upperBound = Colors.GetColorBounds(enemyColor).Item2;
using (Mat hsvImage = new())
using (Mat mask = new())
{
CvInvoke.CvtColor(screen, hsvImage, ColorConversion.Bgr2Hsv);
CvInvoke.InRange(hsvImage, new ScalarArray(lowerBound), new ScalarArray(upperBound), mask);
var moments = CvInvoke.Moments(mask, false);
if (moments.M00 > 0)
{
int x = (int)(moments.M10 / moments.M00);
int y = (int)(moments.M01 / moments.M00);
return new Point(x, y);
}
}
return null;
}
private bool IsSideFrontMouseButtonPressed()
{
return (GetAsyncKeyState(VK_XBUTTON2) & 0x8000) != 0;
}
private void SendFireToArduino()
{
string message = "FIRE\n";
arduino.Write(message);
}
}