-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTouchManager.cs
182 lines (160 loc) · 7.17 KB
/
TouchManager.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
using HarmonyLib;
using SlimDX.Direct3D9;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace Zbx1425.DXDynamicTexture {
public static class TouchManager {
internal static List<TouchTextureHandle> Handles = new List<TouchTextureHandle>();
private static int TouchPhase;
private static MouseButtons Button;
private static bool IsUp;
private static int mouseX, mouseY;
private static byte Nonse;
private static int potentialID = -1;
private static Random random = new Random();
private static HashSet<int> EventsToHandle = new HashSet<int>();
public enum EventType {
Down = 0, Up = 1
}
[DllImport("user32.dll")]
private static extern bool GetCursorPos(ref Point lpPoint);
internal static void Initialize() {
try {
Form mainForm = Application.OpenForms[0];
TextureManager.Harmony.Patch(
mainForm.GetType().GetMethod("OnPaint", BindingFlags.NonPublic | BindingFlags.Instance),
new HarmonyMethod(typeof(TouchManager), "OnPaintPrefix"),
new HarmonyMethod(typeof(TouchManager), "OnPaintPostfix")
);
mainForm.MouseDown += TouchManager_MouseDown;
mainForm.MouseUp += TouchManager_MouseUp;
} catch (Exception ex) {
MessageBox.Show(ex.ToString());
}
}
private static void TouchManager_MouseDown(object sender, MouseEventArgs e) {
if (EventsToHandle.Contains((int)e.Button)) {
TouchPhase = 1;
IsUp = false;
mouseX = e.X; mouseY = e.Y;
Button = e.Button;
}
}
private static void TouchManager_MouseUp(object sender, MouseEventArgs e) {
if (EventsToHandle.Contains((int)e.Button + 1)) {
TouchPhase = 1;
IsUp = true;
mouseX = e.X; mouseY = e.Y;
Button = e.Button;
}
}
public static void EnableEvent(MouseButtons button, EventType type) {
int eventID = (int)button + (int)type;
EventsToHandle.Add(eventID);
}
public static void DisableEvent(MouseButtons button, EventType type) {
int eventID = (int)button + (int)type;
EventsToHandle.Remove(eventID);
}
private static void OnPaintPrefix() {
if (TouchPhase == 0) return;
switch (TouchPhase) {
case 1:
Nonse = (byte)random.Next(0, 256);
for (int i = 0; i < Handles.Count; i++) {
Handles[i].BackupTexture();
Handles[i].FillColorPhase1(Nonse, (byte)i);
}
break;
case 2:
Handles[potentialID].FillColorPhase2((byte)potentialID);
break;
}
}
private static void OnPaintPostfix() {
if (TouchPhase == 0 || TextureManager.DXDevice == null) return;
var target = TextureManager.DXDevice.GetRenderTarget(0);
var destinationSurface = Surface.CreateOffscreenPlain(
TextureManager.DXDevice,
target.Description.Width, target.Description.Height,
Format.X8R8G8B8, Pool.SystemMemory
);
TextureManager.DXDevice.GetRenderTargetData(target, destinationSurface);
var rect = destinationSurface.LockRectangle(LockFlags.ReadOnly);
rect.Data.Seek((mouseY * target.Description.Width + mouseX) * 4, System.IO.SeekOrigin.Begin);
int b = rect.Data.ReadByte(), g = rect.Data.ReadByte(), r = rect.Data.ReadByte();
Color color = Color.FromArgb(r, g, b);
destinationSurface.UnlockRectangle();
destinationSurface.Dispose();
target.Dispose();
switch (TouchPhase) {
case 1:
if (color.R + color.B == 255 && color.G < Handles.Count) {
potentialID = color.G;
TouchPhase = 2;
} else {
potentialID = -1;
TouchPhase = 0;
}
for (int i = 0; i < Handles.Count; i++) {
if (i == potentialID) continue;
Handles[i].RestoreTexture();
}
break;
case 2:
Handles[potentialID].RestoreTexture();
TouchPhase = 0;
if (color.G == potentialID) {
Handles[potentialID].RaiseEvent(color.R, color.B, Button, IsUp, color);
}
break;
}
}
public static TouchTextureHandle Register(string fileNameEnding, int width, int height) {
if (fileNameEnding.Trim().Length == 0) throw new ArgumentException("Must not be empty.", "fileNameEnding");
if (!TextureManager.IsPowerOfTwo(width)) throw new ArgumentException("Must be a integral power of 2.", "width");
if (!TextureManager.IsPowerOfTwo(height)) throw new ArgumentException("Must be a integral power of 2.", "height");
fileNameEnding = fileNameEnding.ToLowerInvariant().Replace('\\', '/');
if (TextureManager.Handles.ContainsKey(fileNameEnding))
return (TouchTextureHandle)TextureManager.Handles[fileNameEnding];
var result = new TouchTextureHandle(width, height);
Handles.Add(result);
TextureManager.Handles.Add(fileNameEnding, result);
return result;
}
public static TouchTextureHandle Register(Texture texture) {
foreach (var item in TextureManager.Handles) {
if (item.Value?.DXTexture == texture) return (TouchTextureHandle)item.Value;
}
var result = new TouchTextureHandle(texture);
Handles.Add(result);
string guidString;
do {
guidString = Guid.NewGuid().ToString();
} while (TextureManager.Handles.ContainsKey(guidString));
TextureManager.Handles.Add(guidString, result);
return result;
}
private static Bitmap screenPixel = new Bitmap(1, 1, PixelFormat.Format32bppArgb);
private static Color GetColorAt(Point location) {
using (Graphics gdest = Graphics.FromImage(screenPixel)) {
using (Graphics gsrc = Graphics.FromHwnd(IntPtr.Zero)) {
IntPtr hSrcDC = gsrc.GetHdc();
IntPtr hDC = gdest.GetHdc();
GDIHelper.BitBlt(hDC, 0, 0, 1, 1, hSrcDC, location.X, location.Y, GDIHelper.SRCCOPY);
gdest.ReleaseHdc();
gsrc.ReleaseHdc();
}
}
return screenPixel.GetPixel(0, 0);
}
}
}