-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTouchTextureHandle.cs
96 lines (81 loc) · 3.7 KB
/
TouchTextureHandle.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
using SlimDX.Direct3D9;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace Zbx1425.DXDynamicTexture {
public class TouchTextureHandle : TextureHandle {
private byte[] tempBufferA, tempBufferB;
private Rectangle clickableArea;
public event EventHandler<TouchEventArgs> MouseDown;
public event EventHandler<TouchEventArgs> MouseUp;
internal TouchTextureHandle(int width, int height) : base(width, height) {
tempBufferA = new byte[width * height * PIXEL_LEN];
tempBufferB = new byte[width * height * PIXEL_LEN];
clickableArea = new Rectangle(0, 0, width, height);
}
internal TouchTextureHandle(Texture texture) : base(texture) {
tempBufferA = new byte[Width * Height * PIXEL_LEN];
tempBufferB = new byte[Width * Height * PIXEL_LEN];
clickableArea = new Rectangle(0, 0, Width, Height);
}
public void SetClickableArea(int x0, int y0, int width, int height) {
clickableArea = new Rectangle(x0, y0, width, height);
Array.Clear(tempBufferB, 0, tempBufferB.Length);
}
internal void BackupTexture() {
if (DXTexture == null) return;
var rect = DXTexture.LockRectangle(0, LockFlags.ReadOnly);
rect.Data.Read(tempBufferA, 0, tempBufferA.Length);
DXTexture.UnlockRectangle(0);
}
internal void RestoreTexture() {
Update(tempBufferA);
}
private const int OFFSET_R = 2, OFFSET_G = 1, OFFSET_B = 0, OFFSET_A = 3, PIXEL_LEN = 4;
internal void FillColorPhase1(byte nonse, byte id) {
unsafe {
for (int y = 0; y < clickableArea.Height; y++) {
for (int x = 0; x < clickableArea.Width; x++) {
int offset = ((y + clickableArea.Y) * Description.Width + (x + clickableArea.X)) * PIXEL_LEN;
tempBufferB[offset + OFFSET_R] = nonse;
tempBufferB[offset + OFFSET_G] = id;
tempBufferB[offset + OFFSET_B] = (byte)(0xFF - nonse);
tempBufferB[offset + OFFSET_A] = 0xFF;
}
}
}
Update(tempBufferB);
}
internal void FillColorPhase2(byte id) {
unsafe {
for (int y = 0; y < clickableArea.Height; y++) {
for (int x = 0; x < clickableArea.Width; x++) {
int offset = ((y + clickableArea.Y) * Description.Width + (x + clickableArea.X)) * PIXEL_LEN;
tempBufferB[offset + OFFSET_R] = (byte)((double)x / clickableArea.Width * 255);
// tempBufferB[offset + OFFSET_G] = id;
tempBufferB[offset + OFFSET_B] = (byte)((double)y / clickableArea.Height * 255);
// tempBufferB[offset + OFFSET_A] = 0xFF;
}
}
}
Update(tempBufferB);
}
internal bool RaiseEvent(int rx, int ry, MouseButtons button, bool isUp, Color color) {
int x = (int)((double)rx / 255 * clickableArea.Width);
int y = (int)((double)ry / 255 * clickableArea.Height);
var eventArgs = new TouchEventArgs(x, y, button);
eventArgs.Color = color;
if (isUp) {
MouseUp?.Invoke(this, eventArgs);
} else {
MouseDown?.Invoke(this, eventArgs);
}
return eventArgs.PreventDefault;
}
}
}