-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControl_AI.pas
More file actions
188 lines (160 loc) · 4.02 KB
/
Copy pathControl_AI.pas
File metadata and controls
188 lines (160 loc) · 4.02 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
unit Control_AI;
interface
uses GraphABC;
type
TButtonState = record
Held: boolean;
Pressed: boolean;
Released: boolean;
end;
TInputState = record
Keys: array[0..255] of TButtonState;
MouseX, MouseY: integer;
MouseButtons: array[1..3] of TButtonState;
end;
var
Input: TInputState;
procedure InitInput;
procedure UpdateInput;
function IsKeyHeld(key: integer): boolean;
function IsKeyPressed(key: integer): boolean;
function IsKeyReleased(key: integer): boolean;
function IsMouseButtonHeld(button: integer): boolean;
function IsMouseButtonPressed(button: integer): boolean;
function IsMouseButtonReleased(button: integer): boolean;
// Сбросить флаг Pressed для указанной кнопки мыши (после обработки события)
procedure ClearMouseButtonPressed(button: integer);
function GetMousePos: Point;
implementation
procedure KeyDown(key: integer);
begin
if (key >= 0) and (key <= 255) then
begin
Input.Keys[key].Held := true;
Input.Keys[key].Pressed := true;
end;
end;
procedure KeyUp(key: integer);
begin
if (key >= 0) and (key <= 255) then
begin
Input.Keys[key].Held := false;
Input.Keys[key].Released := true;
end;
end;
procedure MouseDown(x, y, mb: integer);
begin
Input.MouseX := x;
Input.MouseY := y;
if (mb >= 1) and (mb <= 3) then
begin
Input.MouseButtons[mb].Held := true;
Input.MouseButtons[mb].Pressed := true;
end;
end;
procedure MouseUp(x, y, mb: integer);
begin
Input.MouseX := x;
Input.MouseY := y;
if (mb >= 1) and (mb <= 3) then
begin
Input.MouseButtons[mb].Held := false;
Input.MouseButtons[mb].Released := true;
end;
end;
procedure MouseMove(x, y, mb: integer);
begin
Input.MouseX := x;
Input.MouseY := y;
end;
procedure InitInput;
var
i: integer;
begin
for i := 0 to 255 do
begin
Input.Keys[i].Held := false;
Input.Keys[i].Pressed := false;
Input.Keys[i].Released := false;
end;
for i := 1 to 3 do
begin
Input.MouseButtons[i].Held := false;
Input.MouseButtons[i].Pressed := false;
Input.MouseButtons[i].Released := false;
end;
Input.MouseX := 0;
Input.MouseY := 0;
OnKeyDown := KeyDown;
OnKeyUp := KeyUp;
OnMouseDown := MouseDown;
OnMouseUp := MouseUp;
OnMouseMove := MouseMove;
end;
procedure UpdateInput;
var
i: integer;
begin
// Сбрасываем Pressed и Released для клавиш (для клавиш оставляем как есть)
for i := 0 to 255 do
begin
Input.Keys[i].Pressed := false;
Input.Keys[i].Released := false;
end;
// Для мыши сбрасываем только Released, Pressed сбрасываем вручную через ClearMouseButtonPressed
for i := 1 to 3 do
Input.MouseButtons[i].Released := false;
end;
procedure ClearMouseButtonPressed(button: integer);
begin
if (button >= 1) and (button <= 3) then
Input.MouseButtons[button].Pressed := false;
end;
function IsKeyHeld(key: integer): boolean;
begin
if (key >= 0) and (key <= 255) then
Result := Input.Keys[key].Held
else
Result := false;
end;
function IsKeyPressed(key: integer): boolean;
begin
if (key >= 0) and (key <= 255) then
Result := Input.Keys[key].Pressed
else
Result := false;
end;
function IsKeyReleased(key: integer): boolean;
begin
if (key >= 0) and (key <= 255) then
Result := Input.Keys[key].Released
else
Result := false;
end;
function IsMouseButtonHeld(button: integer): boolean;
begin
if (button >= 1) and (button <= 3) then
Result := Input.MouseButtons[button].Held
else
Result := false;
end;
function IsMouseButtonPressed(button: integer): boolean;
begin
if (button >= 1) and (button <= 3) then
Result := Input.MouseButtons[button].Pressed
else
Result := false;
end;
function IsMouseButtonReleased(button: integer): boolean;
begin
if (button >= 1) and (button <= 3) then
Result := Input.MouseButtons[button].Released
else
Result := false;
end;
function GetMousePos: Point;
begin
Result.X := Input.MouseX;
Result.Y := Input.MouseY;
end;
end.