66 lines
1.7 KiB
JavaScript
66 lines
1.7 KiB
JavaScript
class InputHelper {
|
|
constructor(title) {
|
|
this.title = title;
|
|
|
|
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;
|
|
|
|
mp.events.add('render', this.disableControls);
|
|
}
|
|
|
|
disableControls() {
|
|
for (var i = 0; i <= 33; i++) {
|
|
mp.game.controls.disableAllControlActions(i);
|
|
}
|
|
}
|
|
|
|
show() {
|
|
if (this.created) return;
|
|
this.created = true;
|
|
this.browser = mp.browsers.new('package://assets/html/inputhelper/index.html');
|
|
}
|
|
|
|
finish() {
|
|
if (this.browser) {
|
|
mp.events.remove('cef_inputhelper_sendvalue');
|
|
mp.events.remove('cef_request_title');
|
|
mp.events.remove('render', this.disableControls);
|
|
this.browser.destroy();
|
|
this.created = false;
|
|
}
|
|
}
|
|
|
|
cefTitleCall() {
|
|
this.browser.execute(`setTitle('${this.title}')`);
|
|
}
|
|
|
|
cefCallback(val) {
|
|
this.value = val;
|
|
this.finish();
|
|
}
|
|
|
|
valueGetter() {
|
|
return new Promise(resolve => {
|
|
setInterval(() => {
|
|
if (this.value !== undefined) resolve(this.value);
|
|
}, 50);
|
|
});
|
|
}
|
|
|
|
async getValue(callback) {
|
|
var getVal = await this.valueGetter();
|
|
callback(getVal);
|
|
}
|
|
}
|
|
|
|
exports = InputHelper; |