Files
reallife-gamemode/ReallifeGamemode.Client/inputhelper/index.ts
2020-07-30 22:09:28 +02:00

109 lines
2.8 KiB
TypeScript

import game, { GlobalData } from "..";
export default class InputHelper {
private title: string;
private value: string;
private created: boolean;
private browser: BrowserMp;
private data: IGlobalData;
static active: boolean = false;
static timerStarted: boolean = false;
constructor(title: string, globalData?: IGlobalData) {
this.title = title;
this.data = globalData || GlobalData;
this.cefTitleCall = this.cefTitleCall.bind(this);
mp.events.add('cef_request_title', this.cefTitleCall);
this.cefCallback = this.cefCallback.bind(this);
mp.events.add('cef_inputhelper_sendvalue', this.cefCallback);
this.finish = this.finish.bind(this);
this.show = this.show.bind(this);
this.valueGetter = this.valueGetter.bind(this);
this.getValue = this.getValue.bind(this);
this.value = undefined;
}
startDisableTimer(): void {
mp.gui.chat.push("RENDER REGISTRIEREN");
mp.events.add("render", () => {
game.ui.sendChatMessage("render in inputhelper");
if (InputHelper.active) {
mp.game.controls.disableAllControlActions(0);
mp.game.controls.disableAllControlActions(1);
mp.game.controls.disableAllControlActions(2);
mp.game.controls.disableAllControlActions(27);
}
})
}
private disableControls() {
//for (var x = 0; x < 358; x++) {
// mp.game.controls.disableControlAction(1, x, true);
//}
}
show() {
if (this.created) return;
this.data.InInput = true;
this.created = true;
this.browser = mp.browsers.new('package://assets/html/inputhelper/index.html');
InputHelper.active = true;
if (!InputHelper.timerStarted) {
InputHelper.timerStarted = true;
this.startDisableTimer();
}
}
private finish() {
if (this.browser) {
mp.events.remove('cef_inputhelper_sendvalue');
mp.events.remove('cef_request_title');
InputHelper.active = false;
this.browser.destroy();
this.data.InInput = false;
this.created = false;
this.browser = null;
}
}
private cefTitleCall() {
this.browser.execute(`setTitle('${this.title}')`);
}
private cefCallback(val) {
this.value = val;
this.finish();
}
private valueGetter(): Promise<string> {
return new Promise(resolve => {
while (this.value === undefined) {
mp.game.wait(1);
}
resolve(this.value);
});
}
async getValue(callback: Function) {
var getVal = await this.valueGetter();
callback(getVal);
}
}
exports = InputHelper;