2015-01-08 00:00:00 +00:00
|
|
|
module SpaceTac.View {
|
|
|
|
"use strict";
|
|
|
|
|
2015-02-22 00:00:00 +00:00
|
|
|
// Display mode for ValueBar
|
|
|
|
export enum ValueBarMode {
|
|
|
|
Scaled,
|
|
|
|
Truncated
|
|
|
|
}
|
|
|
|
|
2015-01-08 00:00:00 +00:00
|
|
|
// Bar to display a value (like a progress bar)
|
|
|
|
export class ValueBar extends Phaser.Sprite {
|
2015-02-22 00:00:00 +00:00
|
|
|
// Display mode, true to scale the internal bar, false to truncate it
|
|
|
|
display_mode: ValueBarMode;
|
|
|
|
|
2015-01-08 00:00:00 +00:00
|
|
|
// Current value
|
|
|
|
private current: number;
|
|
|
|
|
|
|
|
// Maximal value
|
|
|
|
private maximal: number;
|
|
|
|
|
|
|
|
// Proportional value
|
|
|
|
private proportional: number;
|
|
|
|
|
|
|
|
// Sprite of internal bar (inside the background sprite)
|
|
|
|
private bar_sprite: Phaser.Sprite;
|
2015-02-22 00:00:00 +00:00
|
|
|
private bar_sprite_rect: Phaser.Rectangle;
|
2015-01-08 00:00:00 +00:00
|
|
|
|
|
|
|
// Create a quick standard bar
|
|
|
|
static newStandard(game: Phaser.Game, x: number, y: number): ValueBar {
|
2015-01-23 00:00:00 +00:00
|
|
|
var result = new ValueBar(game, x, y, "common-standard-bar-background");
|
|
|
|
result.setBarImage("common-standard-bar-foreground", 5, 5);
|
2015-01-08 00:00:00 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-02-28 00:00:00 +00:00
|
|
|
// Create a quick styled bar
|
|
|
|
static newStyled(game: Phaser.Game, base_key: string, x: number, y: number): ValueBar {
|
|
|
|
var result = new ValueBar(game, x, y, base_key + "-empty");
|
|
|
|
result.setBarImage(base_key + "-full");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-01-08 00:00:00 +00:00
|
|
|
// Build an value bar sprite
|
|
|
|
// background is the key to the image to use as background
|
2015-02-22 00:00:00 +00:00
|
|
|
constructor(game: Phaser.Game, x: number, y: number, background: string,
|
|
|
|
display_mode: ValueBarMode = ValueBarMode.Truncated) {
|
2015-01-08 00:00:00 +00:00
|
|
|
super(game, x, y, background);
|
|
|
|
|
2015-02-22 00:00:00 +00:00
|
|
|
this.display_mode = display_mode;
|
|
|
|
|
2015-01-08 00:00:00 +00:00
|
|
|
this.setValue(0, 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set an image to use for the bar
|
2015-01-18 00:00:00 +00:00
|
|
|
setBarImage(key: string, offset_x: number = 0, offset_y: number = 0): void {
|
2015-01-08 00:00:00 +00:00
|
|
|
this.bar_sprite = new Phaser.Sprite(this.game, offset_x, offset_y, key);
|
2015-02-22 00:00:00 +00:00
|
|
|
this.bar_sprite_rect = new Phaser.Rectangle(0, 0, this.bar_sprite.width, this.bar_sprite.height);
|
2015-01-08 00:00:00 +00:00
|
|
|
this.addChild(this.bar_sprite);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update graphics representation
|
|
|
|
update() {
|
|
|
|
if (this.bar_sprite) {
|
2015-02-03 00:00:00 +00:00
|
|
|
var dest = this.proportional;
|
|
|
|
if (dest < 0.00001) {
|
|
|
|
dest = 0.00001;
|
|
|
|
}
|
|
|
|
|
2015-02-22 00:00:00 +00:00
|
|
|
if (this.display_mode === ValueBarMode.Scaled) {
|
|
|
|
this.game.tweens.create(this.bar_sprite.scale).to({x: dest}).start();
|
|
|
|
} else {
|
|
|
|
// TODO Animate
|
|
|
|
this.bar_sprite.crop(Phaser.Rectangle.clone(this.bar_sprite_rect).scale(dest, 1.0), false);
|
|
|
|
}
|
2015-01-08 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set current value
|
|
|
|
setValue(current: number, maximal: number = -1) {
|
|
|
|
this.current = current;
|
|
|
|
if (maximal >= 0) {
|
|
|
|
this.maximal = maximal;
|
|
|
|
}
|
|
|
|
if (this.maximal === 0) {
|
|
|
|
this.proportional = 0;
|
|
|
|
} else {
|
|
|
|
this.proportional = this.current / this.maximal;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.update();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the proportional (in 0.0-1.0 range) value
|
|
|
|
getProportionalValue(): number {
|
|
|
|
return this.proportional;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|