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

69 lines
2.1 KiB
TypeScript
Raw Normal View History

2017-09-24 22:23:22 +00:00
module TK.SpaceTac.UI {
2017-06-08 17:32:57 +00:00
/**
* Base class for modal dialogs
*
* When a modal dialog opens, an overlay is displayed behind it to prevent clicking through it
*/
2018-05-15 14:57:45 +00:00
export class UIDialog {
readonly base: UIContainer
readonly content: UIBuilder
readonly width: number
readonly height: number
2017-06-08 17:32:57 +00:00
2018-05-15 14:57:45 +00:00
constructor(readonly view: BaseView, background_key = "common-dialog") {
if (view.dialogs_opened.length == 0) {
this.addOverlay(view.dialogs_layer);
2017-06-08 17:32:57 +00:00
}
2018-05-15 14:57:45 +00:00
let builder = new UIBuilder(view, view.dialogs_layer);
this.base = builder.container("dialog-base");
builder = builder.in(this.base);
let background = builder.image(background_key);
this.width = background.width;
this.height = background.height;
this.base.setPosition((this.view.getWidth() - this.width) / 2, (this.view.getHeight() - this.height) / 2);
this.content = builder.in(builder.container("content"));
add(view.dialogs_opened, this);
2017-07-19 23:22:18 +00:00
2018-05-15 14:57:45 +00:00
view.audio.playOnce("ui-dialog-open");
2017-06-08 17:32:57 +00:00
}
/**
2018-05-15 14:57:45 +00:00
* Add an input-capturing overlay
2017-06-08 17:32:57 +00:00
*/
2018-05-15 14:57:45 +00:00
addOverlay(layer: UIContainer): void {
2018-06-06 21:00:22 +00:00
new UIBuilder(this.view, layer).overlay({
color: 0x888888,
alpha: 0.3
});
2017-06-08 17:32:57 +00:00
}
/**
* Add a close button
*/
2018-04-12 22:03:29 +00:00
addCloseButton(key = "common-dialog-close", x = 1290, y = 90): void {
2018-05-15 14:57:45 +00:00
let builder = new UIBuilder(this.view, this.base);
builder.button(key, x, y, () => this.close(), "Close this dialog");
2017-06-08 17:32:57 +00:00
}
/**
* Close the dialog, removing the overlay if needed
*/
close() {
2018-05-15 14:57:45 +00:00
this.base.destroy();
2017-06-08 17:32:57 +00:00
2017-07-19 23:22:18 +00:00
this.view.audio.playOnce("ui-dialog-close");
remove(this.view.dialogs_opened, this);
if (this.view.dialogs_opened.length == 0) {
// Remove overlay
2017-06-08 17:32:57 +00:00
this.view.dialogs_layer.removeAll(true);
}
}
}
}