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

159 lines
4.8 KiB
TypeScript
Raw Normal View History

2017-03-15 21:40:19 +00:00
module TS.SpaceTac.UI {
class TooltipContainer extends Phaser.Group {
view: BaseView
background: Phaser.Graphics
content: Phaser.Group
anchorpoint: [number, number] = [0, 0]
constructor(view: BaseView) {
super(view.game);
this.view = view;
this.visible = false;
this.background = new Phaser.Graphics(this.game);
this.add(this.background);
this.content = new Phaser.Group(this.game);
this.add(this.content);
2017-05-14 23:00:36 +00:00
this.view.tooltip_layer.add(this);
}
show(x: number, y: number) {
this.anchorpoint = [x, y];
this.visible = true;
}
update() {
if (this.visible) {
let bounds = this.content.getBounds();
let width = bounds.width + 20;
let height = bounds.height + 20;
if (this.background.width != width || this.background.height != height) {
this.background.clear();
this.background.beginFill(0x202225, 0.9);
this.background.drawRect(-10, -10, width, height);
this.background.endFill();
}
let [x, y] = UITools.positionInside({ x: this.anchorpoint[0], y: this.anchorpoint[1], width: width, height: height }, { x: 0, y: 0, width: this.view.getWidth(), height: this.view.getHeight() });
if (x != this.x || y != this.y) {
this.position.set(x, y);
}
}
}
hide() {
this.content.removeAll();
this.visible = false;
}
}
2017-05-14 23:00:36 +00:00
/**
* Functions used to fill a tooltip content
*/
export class TooltipFiller {
private container: TooltipContainer;
constructor(container: TooltipContainer) {
this.container = container;
}
/**
* Add an image to the content
*/
addImage(x: number, y: number, key: string, scale = 1) {
let image = new Phaser.Image(this.container.game, x, y, key);
image.scale.set(scale);
this.container.content.add(image);
}
/**
* Add a text to the content
*/
addText(x: number, y: number, content: string, color = "#ffffff", size = 16, center = false, bold = false) {
let style = { font: `${bold ? "bold " : ""}${size}pt Arial`, fill: color, align: center ? "center" : "left" };
let text = new Phaser.Text(this.container.game, x, y, content, style);
this.container.content.add(text);
}
}
2017-03-15 21:40:19 +00:00
/**
* Tooltip system, to display information on hover
*/
export class Tooltip {
2017-05-14 23:00:36 +00:00
protected view: BaseView;
protected container: TooltipContainer;
2017-03-15 21:40:19 +00:00
constructor(view: BaseView) {
this.view = view;
this.container = new TooltipContainer(view);
2017-03-15 21:40:19 +00:00
}
2017-05-14 23:00:36 +00:00
get ui(): MainUI {
return this.view.gameui;
}
/**
* Get a tooltip filler
*/
getFiller(): TooltipFiller {
return new TooltipFiller(this.container);
}
2017-03-15 21:40:19 +00:00
/**
* Bind to an UI component
*
* When the component is hovered, the function is called to allow filling the tooltip container
*/
2017-05-14 23:00:36 +00:00
bind(obj: Phaser.Button, func: (filler: TooltipFiller) => boolean): void {
UITools.setHoverClick(obj,
2017-03-15 21:40:19 +00:00
// enter
() => {
2017-04-11 17:01:05 +00:00
this.hide();
2017-05-14 23:00:36 +00:00
if (func(this.getFiller())) {
2017-03-15 21:40:19 +00:00
let bounds = obj.getBounds();
this.container.show(bounds.x + bounds.width + 4, bounds.y + bounds.height + 4);
2017-03-15 21:40:19 +00:00
}
},
// leave
2017-04-11 17:01:05 +00:00
() => this.hide(),
2017-03-15 21:40:19 +00:00
// click
2017-04-11 17:01:05 +00:00
() => this.hide()
2017-03-15 21:40:19 +00:00
);
2017-04-11 17:01:05 +00:00
obj.onInputDown.add(() => this.hide());
2017-03-15 21:40:19 +00:00
}
/**
* Bind to an UI component to display a dynamic text
*/
bindDynamicText(obj: Phaser.Button, text_getter: () => string): void {
2017-05-14 23:00:36 +00:00
this.bind(obj, filler => {
let content = text_getter();
if (content) {
2017-05-14 23:00:36 +00:00
filler.addText(0, 0, content, "#cccccc", 20, false, true);
return true;
} else {
return false;
}
2017-03-15 21:40:19 +00:00
});
}
/**
* Bind to an UI component to display a simple text
*/
bindStaticText(obj: Phaser.Button, text: string): void {
this.bindDynamicText(obj, () => text);
}
2017-04-11 17:01:05 +00:00
/**
* Hide the current tooltip
*/
hide(): void {
this.container.hide();
2017-04-11 17:01:05 +00:00
}
2017-03-15 21:40:19 +00:00
}
}