1
0
Fork 0
spacetac/src/ui/common/UITextDialog.ts

50 lines
1.9 KiB
TypeScript
Raw Normal View History

2018-03-11 23:07:41 +00:00
module TK.SpaceTac.UI {
/**
* Dialog asking for a text input
*/
export class UITextDialog extends UIDialog {
private result: Promise<string | null>
private result_resolver?: (input: string | null) => void
constructor(view: BaseView, message: string, initial?: string) {
super(view);
2018-05-15 14:57:45 +00:00
this.content.text(message, this.width * 0.5, this.height * 0.3, { color: "#90FEE3", size: 32 });
2018-03-11 23:07:41 +00:00
2018-05-15 14:57:45 +00:00
let input = new UITextInput(this.content.styled({ size: 24 }), "menu-input", this.width / 2, this.height / 2, 12);
2018-03-11 23:07:41 +00:00
if (initial) {
input.setContent(initial);
}
this.result = new Promise((resolve, reject) => {
this.result_resolver = resolve;
2018-06-04 14:48:19 +00:00
this.content.button("common-button-cancel", this.width * 0.4, this.height * 0.7, () => resolve(null),
undefined, undefined, { center: true });
this.content.button("common-button-ok", this.width * 0.6, this.height * 0.7, () => resolve(input.getContent()),
undefined, undefined, { center: true });
2018-03-11 23:07:41 +00:00
});
}
/**
* Force the result (simulate filling the input and validation)
*/
async forceResult(input: string | null): Promise<void> {
if (this.result_resolver) {
this.result_resolver(input);
await this.result;
}
}
/**
* Convenient function to ask for an input, and have a promise of result
*/
static ask(view: BaseView, message: string, initial?: string): Promise<string | null> {
let dlg = new UITextDialog(view, message, initial);
let result = dlg.result;
return result.then(confirmed => {
dlg.close();
return confirmed;
});
}
}
}