/
page.ts
333 lines (283 loc) · 8.82 KB
/
page.ts
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import * as alt from 'alt-client';
import * as native from 'natives';
import * as AthenaClient from '@AthenaClient/api';
import { BaseKeyInfo } from '@AthenaClient/interface/hotkeys';
type AnyCallback = ((...args: any[]) => void) | ((...args: any[]) => Promise<void>) | Function;
export interface IPage {
/**
* The full name of the Vue file you are trying to load.
*
* @type {string}
*
*/
name: string;
/**
* Events to call when the page is opened.
*
*
*/
callbacks: {
/**
* Function to call when the View is loaded.
* Usually used to pass data to the WebView after it's ready.
*/
onReady: AnyCallback;
/**
* Function to call when the View is closed.
*/
onClose: AnyCallback;
};
options?: {
/**
* Disable the escape key auto-close bind.
*
* @type {boolean}
*/
disableEscapeKey?: boolean;
onOpen?: {
/**
* Focus the WebView when this page is opened.
*
* @type {boolean}
*/
focus?: boolean;
/**
* Show the cursor when the WebView is focused?
*
* @type {boolean}
*/
showCursor?: boolean;
/**
* Hide the HUD when the WebView is opened.
*
* @type {boolean}
*/
hideHud?: boolean;
/**
* Hide overlays when the WebView is opened.
*
* @type {boolean}
*/
hideOverlays?: boolean;
/**
* Disable game controls on open?
*
* @type {boolean}
*/
disableControls?: 'all' | 'camera' | 'none';
/**
* Disable pause menu while this page is open?
*
* @type {boolean}
*/
disablePauseMenu?: boolean;
/**
* Blur the game.
*
* @type {boolean}
*/
blurBackground?: boolean;
/**
* Sets alt.Player.local.isMenuOpen to false if false.
*
* @type {boolean}
*/
setIsMenuOpenToTrue?: boolean;
/**
* Force the WebView open. Disregard all rules.
*
* @type {boolean}
*/
forceOpen?: boolean;
};
onClose?: {
/**
* Unfocus the WebView when it is closed?
*
* @type {boolean}
*/
unfocus?: boolean;
/**
* Hide the cursor when the WebView is closed?
*
* @type {boolean}
*/
hideCursor?: boolean;
/**
* Show the HUD when the WebView is closed?
*
* @type {boolean}
*/
showHud?: boolean;
/**
* Show overlays when the WebView is closed?
*
* @type {boolean}
*/
showOverlays?: boolean;
/**
* Enable game controls on close.
*
* @type {boolean}
*/
enableControls?: boolean;
/**
* Enable the pause menu on close?
*
* @type {boolean}
*/
enablePauseMenu?: boolean;
/**
* Unblur the game.
*
* @type {boolean}
*/
unblurBackground?: boolean;
/**
* Sets alt.Player.local.isMenuOpen to false if true.
*
* @type {boolean}
*/
setIsMenuOpenToFalse?: boolean;
};
};
/**
* An optional hotkey to open / close the page.
* Set `useSameKeyToClose` to true to force the same key to close the interface.
*
* @type {(BaseKeyInfo & { useSameKeyToClose?: boolean })}
*
*/
keybind?: BaseKeyInfo & { useSameKeyToClose?: boolean };
}
export class Page {
private info: IPage;
/**
* Creates a WebView Page Controller
* @param {IPage} page
*
*/
constructor(page: IPage) {
this.info = page;
if (this.info.keybind) {
if (this.info.keybind.delayedKeyDown && this.info.keybind.delayedKeyDown.msToTrigger) {
AthenaClient.systems.hotkeys.add({
...this.info.keybind,
delayedKeyDown: {
msToTrigger: this.info.keybind.delayedKeyDown.msToTrigger,
callback: this.open.bind(this),
},
});
} else {
AthenaClient.systems.hotkeys.add({
...this.info.keybind,
keyDown: this.open.bind(this),
delayedKeyDown: undefined,
});
}
}
if (page.options && page.options.disableEscapeKey) {
AthenaClient.webview.disableEscapeKeyForPage(this.info.name);
}
AthenaClient.webview.ready(page.name, page.callbacks.onReady);
}
/**
* Open this WebView Page
*
* @return {Promise<boolean>}
*
*/
async open(): Promise<boolean> {
if (this.info.keybind && this.info.keybind.useSameKeyToClose) {
if (AthenaClient.webview.isPageOpen(this.info.name)) {
this.close(true);
return false;
}
}
if (AthenaClient.webview.isAnyMenuOpen() && !this.info.options.onOpen.forceOpen) {
return false;
}
if (alt.isConsoleOpen() && !this.info.options.onOpen.forceOpen) {
return false;
}
if (alt.isMenuOpen() && !this.info.options.onOpen.forceOpen) {
return false;
}
if (this.info.options.onOpen.focus) {
AthenaClient.webview.focus();
}
const hideOverlays = this.info.options.onOpen.hideOverlays === false ? false : true;
if (this.info.options.onOpen.hideHud) {
AthenaClient.webview.setOverlaysVisible(false);
}
if (this.info.options.onOpen.showCursor) {
AthenaClient.webview.showCursor(true);
}
switch (this.info.options.onOpen.disableControls) {
case 'all':
alt.toggleGameControls(false);
break;
case 'camera':
AthenaClient.camera.gameplay.disable();
break;
default:
break;
}
if (this.info.options.onOpen.disablePauseMenu) {
AthenaClient.utility.pauseMenu.disable();
}
if (this.info.options.onOpen.hideHud) {
native.displayRadar(false);
native.displayHud(false);
}
if (this.info.options.onOpen.blurBackground) {
native.triggerScreenblurFadeIn(250);
}
if (this.info.options.onOpen.setIsMenuOpenToTrue) {
alt.Player.local.isMenuOpen = true;
}
await AthenaClient.webview.openPages([this.info.name], hideOverlays, this.close.bind(this));
return true;
}
/**
* If `isManuallyTriggered` is set to true.
* This means that the close event is not coming from the 'Escape' key bind.
* Useful for when you want your own exit functionality for your WebView.
*
* @param {boolean} [isManuallyTriggered=false]
*
*/
close(isManuallyTriggered = false) {
const showOverlays = this.info.options.onClose.showOverlays === false ? false : true;
if (isManuallyTriggered) {
AthenaClient.webview.closePages([this.info.name], showOverlays);
}
if (!isManuallyTriggered && showOverlays) {
AthenaClient.webview.setOverlaysVisible(true, false);
}
if (this.info.options.onClose.hideCursor) {
AthenaClient.webview.showCursor(false);
}
if (this.info.options.onClose.unfocus) {
AthenaClient.webview.unfocus();
}
if (this.info.options.onClose.unblurBackground) {
native.triggerScreenblurFadeOut(250);
}
if (this.info.options.onClose.setIsMenuOpenToFalse) {
alt.Player.local.isMenuOpen = false;
}
if (this.info.options.onClose.showHud) {
native.displayRadar(true);
native.displayHud(true);
}
if (this.info.options.onClose.enablePauseMenu) {
AthenaClient.utility.pauseMenu.enable();
}
if (this.info.options.onClose.enableControls) {
alt.toggleGameControls(true);
AthenaClient.camera.gameplay.enable();
}
this.info.callbacks.onClose();
}
}