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

63 lines
1.9 KiB
TypeScript
Raw Normal View History

2017-09-24 22:23:22 +00:00
module TK.SpaceTac.UI {
2018-05-15 14:57:45 +00:00
/**
* A single displayed message
*/
class Message extends UIContainer {
2017-05-22 20:41:34 +00:00
view: BaseView
2018-05-15 14:57:45 +00:00
background: UIBackground
text: UIText
2017-02-16 22:59:41 +00:00
constructor(parent: Messages, text: string, duration: number) {
2018-05-15 14:57:45 +00:00
super(parent.view);
2017-05-22 20:41:34 +00:00
this.view = parent.view;
2018-04-16 18:12:26 +00:00
let builder = new UIBuilder(this.view).in(this);
2017-05-22 20:41:34 +00:00
2018-05-15 14:57:45 +00:00
this.background = new UIBackground(this.view, this);
2018-04-16 18:12:26 +00:00
this.text = builder.text(text, 0, 0, { color: "#DBEFF9", shadow: true, size: 16, center: false, vcenter: false });
2018-05-15 14:57:45 +00:00
UITools.drawBackground(this.text, this.background, 6);
2017-05-22 20:41:34 +00:00
2018-05-15 14:57:45 +00:00
let bounds = UITools.getBounds(this);
this.setPosition(parent.view.getX(1) - bounds.width - 25, 25);
2017-05-22 20:41:34 +00:00
parent.view.timer.schedule(duration, () => this.hide());
}
2018-05-15 14:57:45 +00:00
/**
* Hide the message
*/
hide() {
2018-05-15 14:57:45 +00:00
this.view.animations.addAnimation<UIContainer>(this, { y: this.y + 50, alpha: 0 }, 400, "Circ.easeIn").then(() => this.destroy());
2017-05-22 20:41:34 +00:00
}
}
2018-05-15 14:57:45 +00:00
/**
* Visual notifications of game-related messages (eg. "Game saved"...)
*/
export class Messages {
// Link to parent view
2018-05-15 14:57:45 +00:00
view: BaseView
// Main group to hold the visual messages
2018-05-15 14:57:45 +00:00
container: UIContainer
2018-05-15 14:57:45 +00:00
constructor(view: BaseView) {
this.view = view;
this.container = new UIBuilder(view, view.messages_layer).container("messages");
}
2018-05-15 14:57:45 +00:00
/**
* Add a new message to the notifications
*/
2017-05-22 20:41:34 +00:00
addMessage(text: string, duration: number = 3000): void {
2018-05-15 14:57:45 +00:00
let message = new Message(this, text, duration);
let bounds = UITools.getBounds(message);
cfilter(this.container.list, Message).forEach(child => {
child.y += bounds.height + 15;
2018-05-15 14:57:45 +00:00
});
this.container.add(message);
}
}
}