-
Notifications
You must be signed in to change notification settings - Fork 12
/
Scaleform.ts
341 lines (320 loc) · 7.56 KB
/
Scaleform.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
334
335
336
337
338
339
340
341
import { PointF, Vector3 } from '../utils';
/**
* Scaleforms will automatically load when calling any of the render functions.
*
* Example:
*
* ```typescript
* import { Scaleform } from '@nativewrappers/client/ui';
*
* const scaleform = new Cfx.Scaleform("MIDSIZED_MESSAGE");
*
* scaleform.callFunction("SHOW_MIDSIZED_MESSAGE", ["Title", "Message"]);
*
* setTick(() => {
* await scaleform.render2D();
* });
* ```
*/
export class Scaleform {
public static render2DMasked(scaleform1: Scaleform, scaleform2: Scaleform): Promise<void> {
return new Promise(async resolve => {
if (scaleform1.IsLoaded && scaleform2.IsLoaded) {
DrawScaleformMovieFullscreenMasked(
scaleform1.Handle,
scaleform2.Handle,
255,
255,
255,
255,
);
} else {
await scaleform1.load();
await scaleform2.load();
}
resolve();
});
}
protected handle: number;
protected name: string;
protected loaded = false;
constructor(name: string) {
this.name = name;
this.handle = RequestScaleformMovie(this.name);
}
/**
* Get the handle of the scaleform.
*/
public get Handle(): number {
return this.handle;
}
/**
* Get whether the handle is a valid handle.
*/
public get IsValid(): boolean {
return this.handle !== 0;
}
/**
* Get whether the scaleform is loaded.
*/
public get IsLoaded(): boolean {
if (!this.loaded) {
this.loaded = HasScaleformMovieLoaded(this.handle);
}
return this.loaded;
}
/**
* Dispose the scaleform allowing the GTA engine to free memory when wanted.
*/
public dispose(): void {
if (this.IsLoaded) {
SetScaleformMovieAsNoLongerNeeded(this.handle);
this.loaded = false;
}
}
/**
* Call a function on the scaleform.
*
* @param name Name of the function
* @param args Additional arguments
*/
public callFunction(name: string, ...args: unknown[]): void {
BeginScaleformMovieMethod(this.handle, name);
args.forEach(arg => {
switch (typeof arg) {
case 'number':
PushScaleformMovieFunctionParameterInt(arg);
break;
case 'string':
PushScaleformMovieFunctionParameterString(arg);
break;
case 'boolean':
PushScaleformMovieFunctionParameterBool(arg);
break;
default:
throw new Error(
`Unknown argument type [${typeof arg}] passed to scaleform with handle [${
this.handle
}], had value of ${arg}`,
);
}
});
EndScaleformMovieMethod();
}
/**
* Calls a void method on the scaleform.
*
* @param name Name of the function
*/
public callVoidMethod(name: string): void {
CallScaleformMovieMethod(this.handle, name);
}
/**
* Calls a string method on the scaleform.
*
* @param name Name of the function
* @param param1
* @param param2
* @param param3
* @param param4
* @param param5
*/
public callStringMethod(
name: string,
param1 = '',
param2 = '',
param3 = '',
param4 = '',
param5 = '',
): void {
CallScaleformMovieMethodWithString(this.handle, name, param1, param2, param3, param4, param5);
}
/**
* Calls a number method on the scaleform.
*
* @param name Name of the function
* @param param1
* @param param2
* @param param3
* @param param4
* @param param5
*/
public callNumberMethod(
name: string,
param1 = -1.0,
param2 = -1.0,
param3 = -1.0,
param4 = -1.0,
param5 = -1.0,
): void {
CallScaleformMovieMethodWithNumber(this.handle, name, param1, param2, param3, param4, param5);
}
/**
* Calls a number & string method on the scaleform.
*
* @param name Name of the function
* @param fParam1
* @param fParam2
* @param fParam3
* @param fParam4
* @param fParam5
* @param sParam1
* @param sParam2
* @param sParam3
* @param sParam4
* @param sParam5
*/
public callSharedMethod(
name: string,
fParam1 = -1.0,
fParam2 = -1.0,
fParam3 = -1.0,
fParam4 = -1.0,
fParam5 = -1.0,
sParam1 = '',
sParam2 = '',
sParam3 = '',
sParam4 = '',
sParam5 = '',
): void {
CallScaleformMovieMethodWithNumberAndString(
this.handle,
name,
fParam1,
fParam2,
fParam3,
fParam4,
fParam5,
sParam1,
sParam2,
sParam3,
sParam4,
sParam5,
);
}
/**
* Sets a duration the scaleform should be shown.
* Useful for showing a scaleform for a known amount of time, such as messages.
*
* This only works for any scaleform using {@linkcode render2D};
*
* @param duration Duration in milliseconds
*/
public setDuration(duration: number): void {
if (duration <= 0) {
return;
}
const start = GetGameTimer();
const interval = setInterval(async () => {
if (GetGameTimer() - start < duration) {
await this.render2D();
} else {
clearInterval(interval);
}
}, 0);
}
public render2D(): Promise<void> {
return new Promise(async resolve => {
if (this.IsLoaded) {
DrawScaleformMovieFullscreen(this.handle, 255, 255, 255, 255, 0);
} else {
await this.load();
}
resolve();
});
}
public render2DScreenSpace(location: PointF, size: PointF): Promise<void> {
return new Promise(async resolve => {
if (this.IsLoaded) {
const x = location.x; /* UI.Screen.Width*/
const y = location.y; /* UI.Screen.Height*/
const width = size.x; /* UI.Screen.Width*/
const height = size.y; /* UI.Screen.Height*/
DrawScaleformMovie(
this.handle,
x + width / 2,
y + height / 2,
width,
height,
255,
255,
255,
255,
0,
);
} else {
await this.load();
}
resolve();
});
}
public render3D(position: Vector3, rotation: Vector3, scale: Vector3): Promise<void> {
return new Promise(async resolve => {
if (this.IsLoaded) {
DrawScaleformMovie_3dNonAdditive(
this.handle,
position.x,
position.y,
position.z,
rotation.x,
rotation.y,
rotation.z,
2,
2,
1,
scale.x,
scale.y,
scale.z,
2,
);
} else {
await this.load();
}
resolve();
});
}
public render3DAdditive(position: Vector3, rotation: Vector3, scale: Vector3): Promise<void> {
return new Promise(async resolve => {
if (this.IsLoaded) {
DrawScaleformMovie_3d(
this.handle,
position.x,
position.y,
position.z,
rotation.x,
rotation.y,
rotation.z,
2,
2,
1,
scale.x,
scale.y,
scale.z,
2,
);
} else {
await this.load();
}
resolve();
});
}
public load(): Promise<boolean> {
return new Promise(resolve => {
if (this.IsLoaded) {
resolve(true);
} else {
const start = GetGameTimer();
const interval = setInterval(() => {
if (this.IsLoaded) {
clearInterval(interval);
resolve(true);
} else if (GetGameTimer() - start > 5000) {
clearInterval(interval);
console.log(`^1[fivemjs/client] Could not load scaleform ${this.name}!^7`);
resolve(false);
}
}, 0);
}
});
}
}