2013-04-18 13:16:18 +00:00
|
|
|
/// <reference path="Phaser.ts" />
|
2013-04-12 16:19:56 +00:00
|
|
|
/// <reference path="Game.ts" />
|
2013-04-14 01:31:00 +00:00
|
|
|
/// <reference path="system/StageScaleMode.ts" />
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
/**
|
2013-04-18 15:49:08 +00:00
|
|
|
* Phaser - Stage
|
|
|
|
*
|
|
|
|
* The Stage is the canvas on which everything is displayed. This class handles display within the web browser, focus handling,
|
|
|
|
* resizing, scaling and pause/boot screens.
|
2013-04-18 13:16:18 +00:00
|
|
|
*/
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
module Phaser {
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
export class Stage {
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
constructor(game: Game, parent: string, width: number, height: number) {
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this._game = game;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this.canvas = <HTMLCanvasElement> document.createElement('canvas');
|
|
|
|
this.canvas.width = width;
|
|
|
|
this.canvas.height = height;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
if (document.getElementById(parent))
|
|
|
|
{
|
|
|
|
document.getElementById(parent).appendChild(this.canvas);
|
|
|
|
document.getElementById(parent).style.overflow = 'hidden';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
document.body.appendChild(this.canvas);
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-19 08:23:36 +00:00
|
|
|
// Consume default actions on the canvas
|
|
|
|
this.canvas.style.msTouchAction = 'none';
|
|
|
|
this.canvas.style['touch-action'] = 'none';
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this.context = this.canvas.getContext('2d');
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this.offset = this.getOffset(this.canvas);
|
|
|
|
this.bounds = new Rectangle(this.offset.x, this.offset.y, width, height);
|
|
|
|
this.aspectRatio = width / height;
|
|
|
|
this.scaleMode = StageScaleMode.NO_SCALE;
|
|
|
|
this.scale = new StageScaleMode(this._game);
|
|
|
|
|
2013-04-25 19:05:56 +00:00
|
|
|
document.addEventListener('visibilitychange', (event) => this.visibilityChange(event), false);
|
|
|
|
document.addEventListener('webkitvisibilitychange', (event) => this.visibilityChange(event), false);
|
2013-04-18 13:16:18 +00:00
|
|
|
window.onblur = (event) => this.visibilityChange(event);
|
|
|
|
window.onfocus = (event) => this.visibilityChange(event);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private _game: Game;
|
|
|
|
private _bgColor: string;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
public static ORIENTATION_LANDSCAPE: number = 0;
|
|
|
|
public static ORIENTATION_PORTRAIT: number = 1;
|
2013-04-13 19:17:51 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
public bounds: Rectangle;
|
|
|
|
public aspectRatio: number;
|
|
|
|
public clear: bool = true;
|
|
|
|
public canvas: HTMLCanvasElement;
|
|
|
|
public context: CanvasRenderingContext2D;
|
2013-04-20 02:50:21 +00:00
|
|
|
public disablePauseScreen: bool = false;
|
2013-04-18 13:16:18 +00:00
|
|
|
public offset: Point;
|
|
|
|
public scale: StageScaleMode;
|
|
|
|
public scaleMode: number;
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
public minScaleX: number = null;
|
|
|
|
public maxScaleX: number = null;
|
|
|
|
public minScaleY: number = null;
|
|
|
|
public maxScaleY: number = null;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
public update() {
|
|
|
|
|
|
|
|
this.scale.update();
|
|
|
|
|
|
|
|
if (this.clear)
|
|
|
|
{
|
|
|
|
// implement dirty rect? could take up more cpu time than it saves. needs benching.
|
|
|
|
this.context.clearRect(0, 0, this.width, this.height);
|
|
|
|
}
|
2013-04-13 03:25:03 +00:00
|
|
|
|
2013-04-12 16:19:56 +00:00
|
|
|
}
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
public renderDebugInfo() {
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this.context.fillStyle = 'rgb(255,255,255)';
|
|
|
|
this.context.fillText(Phaser.VERSION, 10, 20);
|
|
|
|
this.context.fillText('Game Size: ' + this.width + ' x ' + this.height, 10, 40);
|
|
|
|
this.context.fillText('x: ' + this.x + ' y: ' + this.y, 10, 60);
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-20 02:50:21 +00:00
|
|
|
//if (document['hidden'] === true || document['webkitHidden'] === true)
|
2013-04-18 13:16:18 +00:00
|
|
|
private visibilityChange(event) {
|
|
|
|
|
2013-04-25 19:05:56 +00:00
|
|
|
//console.log(event);
|
|
|
|
|
2013-04-20 02:50:21 +00:00
|
|
|
if (this.disablePauseScreen)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
if (event.type == 'blur' && this._game.paused == false && this._game.isBooted == true)
|
|
|
|
{
|
|
|
|
this._game.paused = true;
|
|
|
|
this.drawPauseScreen();
|
|
|
|
}
|
|
|
|
else if (event.type == 'focus')
|
|
|
|
{
|
|
|
|
this._game.paused = false;
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
|
|
|
}
|
2013-04-18 13:16:18 +00:00
|
|
|
|
|
|
|
public drawInitScreen() {
|
|
|
|
|
|
|
|
this.context.fillStyle = 'rgb(40, 40, 40)';
|
|
|
|
this.context.fillRect(0, 0, this.width, this.height);
|
|
|
|
|
|
|
|
this.context.fillStyle = 'rgb(255,255,255)';
|
|
|
|
this.context.font = 'bold 18px Arial';
|
|
|
|
this.context.textBaseline = 'top';
|
|
|
|
this.context.fillText(Phaser.VERSION, 54, 32);
|
|
|
|
this.context.fillText('Game Size: ' + this.width + ' x ' + this.height, 32, 64);
|
|
|
|
this.context.fillText('www.photonstorm.com', 32, 96);
|
|
|
|
this.context.font = '16px Arial';
|
|
|
|
this.context.fillText('You are seeing this screen because you didn\'t specify any default', 32, 160);
|
|
|
|
this.context.fillText('functions in the Game constructor, or use Game.loadState()', 32, 184);
|
|
|
|
|
|
|
|
var image = new Image();
|
|
|
|
var that = this;
|
|
|
|
|
|
|
|
image.onload = function () {
|
|
|
|
that.context.drawImage(image, 32, 32);
|
|
|
|
};
|
|
|
|
|
|
|
|
image.src = this._logo;
|
|
|
|
|
2013-04-12 16:19:56 +00:00
|
|
|
}
|
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
private drawPauseScreen() {
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this.saveCanvasValues();
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this.context.fillStyle = 'rgba(0, 0, 0, 0.4)';
|
|
|
|
this.context.fillRect(0, 0, this.width, this.height);
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
// Draw a 'play' arrow
|
|
|
|
var arrowWidth = Math.round(this.width / 2);
|
|
|
|
var arrowHeight = Math.round(this.height / 2);
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
var sx = this.centerX - arrowWidth / 2;
|
|
|
|
var sy = this.centerY - arrowHeight / 2;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this.context.beginPath();
|
|
|
|
this.context.moveTo(sx, sy);
|
|
|
|
this.context.lineTo(sx, sy + arrowHeight);
|
|
|
|
this.context.lineTo(sx + arrowWidth, this.centerY);
|
|
|
|
this.context.fillStyle = 'rgba(255, 255, 255, 0.8)';
|
|
|
|
this.context.fill();
|
|
|
|
this.context.closePath();
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this.restoreCanvasValues();
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
private getOffset(element): Point {
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
var box = element.getBoundingClientRect();
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
var clientTop = element.clientTop || document.body.clientTop || 0;
|
|
|
|
var clientLeft = element.clientLeft || document.body.clientLeft || 0;
|
|
|
|
var scrollTop = window.pageYOffset || element.scrollTop || document.body.scrollTop;
|
|
|
|
var scrollLeft = window.pageXOffset || element.scrollLeft || document.body.scrollLeft;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
return new Point(box.left + scrollLeft - clientLeft, box.top + scrollTop - clientTop);
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
public strokeStyle: string;
|
|
|
|
public lineWidth: number;
|
|
|
|
public fillStyle: string;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
public saveCanvasValues() {
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this.strokeStyle = this.context.strokeStyle;
|
|
|
|
this.lineWidth = this.context.lineWidth;
|
|
|
|
this.fillStyle = this.context.fillStyle;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
public restoreCanvasValues() {
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
this.context.strokeStyle = this.strokeStyle;
|
|
|
|
this.context.lineWidth = this.lineWidth;
|
|
|
|
this.context.fillStyle = this.fillStyle;
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
public set backgroundColor(color: string) {
|
|
|
|
this.canvas.style.backgroundColor = color;
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
public get backgroundColor(): string {
|
|
|
|
return this._bgColor;
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
public get x(): number {
|
|
|
|
return this.bounds.x;
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
public get y(): number {
|
|
|
|
return this.bounds.y;
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
public get width(): number {
|
|
|
|
return this.bounds.width;
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
public get height(): number {
|
|
|
|
return this.bounds.height;
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
public get centerX(): number {
|
|
|
|
return this.bounds.halfWidth;
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
public get centerY(): number {
|
|
|
|
return this.bounds.halfHeight;
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
public get randomX(): number {
|
|
|
|
return Math.round(Math.random() * this.bounds.width);
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
public get randomY(): number {
|
|
|
|
return Math.round(Math.random() * this.bounds.height);
|
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
private _logo: string = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAO1JREFUeNpi/P//PwM6YGRkxBQEAqBaRnQxFmwa10d6MAjrMqMofHv5L1we2SBGmAtAktg0ogOQQYHLd8ANYYFpPtTmzUAMAFmwnsEDrAdkCAvMZlIAsiFMMAEYsKvaSrQhIMCELkGsV2AAbIC8gCQYgwKIUABiNYBf9yoYH7n7n6CzN274g2IYEyFbsNmKLIaSkHpP7WSwUfbA0ASzFQRslBlxp0RcAF0TRhggA3zhAJIDpUKU5A9KyshpHDkjFZu5g2nJMFcwXVJSgqIGnBKx5bKenh4w/XzVbgbPtlIUcVgSxuoCUgHIIIAAAwArtXwJBABO6QAAAABJRU5ErkJggg==";
|
2013-04-12 16:19:56 +00:00
|
|
|
|
2013-04-18 13:16:18 +00:00
|
|
|
}
|
2013-04-12 16:19:56 +00:00
|
|
|
|
|
|
|
}
|