phaser/Phaser/Stage.ts

209 lines
6.3 KiB
TypeScript
Raw Normal View History

2013-04-18 14:16:18 +01:00
/// <reference path="Phaser.ts" />
2013-04-12 17:19:56 +01:00
/// <reference path="Game.ts" />
/// <reference path="system/StageScaleMode.ts" />
/// <reference path="system/screens/BootScreen.ts" />
/// <reference path="system/screens/PauseScreen.ts" />
2013-04-12 17:19:56 +01:00
2013-04-18 14:16:18 +01:00
/**
2013-04-18 16:49:08 +01: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 14:16:18 +01:00
*/
2013-04-12 17:19:56 +01:00
2013-04-18 14:16:18 +01:00
module Phaser {
2013-04-12 17:19:56 +01:00
2013-04-18 14:16:18 +01:00
export class Stage {
2013-04-12 17:19:56 +01:00
2013-04-18 14:16:18 +01:00
constructor(game: Game, parent: string, width: number, height: number) {
2013-04-12 17:19:56 +01:00
2013-04-18 14:16:18 +01:00
this._game = game;
2013-04-12 17:19:56 +01:00
2013-04-18 14:16:18 +01:00
this.canvas = <HTMLCanvasElement> document.createElement('canvas');
this.canvas.width = width;
this.canvas.height = height;
2013-04-12 17:19:56 +01:00
2013-04-18 14:16:18 +01: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 17:19:56 +01:00
// Consume default actions on the canvas
this.canvas.style.msTouchAction = 'none';
this.canvas.style['touch-action'] = 'none';
2013-04-18 14:16:18 +01:00
this.context = this.canvas.getContext('2d');
2013-04-12 17:19:56 +01:00
2013-04-18 14:16:18 +01: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);
this._bootScreen = new BootScreen(this._game);
this._pauseScreen = new PauseScreen(this._game, width, height);
document.addEventListener('visibilitychange', (event) => this.visibilityChange(event), false);
document.addEventListener('webkitvisibilitychange', (event) => this.visibilityChange(event), false);
2013-04-18 14:16:18 +01:00
window.onblur = (event) => this.visibilityChange(event);
window.onfocus = (event) => this.visibilityChange(event);
}
private _game: Game;
private _bgColor: string;
private _bootScreen;
private _pauseScreen;
2013-04-12 17:19:56 +01:00
2013-04-18 14:16:18 +01:00
public static ORIENTATION_LANDSCAPE: number = 0;
public static ORIENTATION_PORTRAIT: number = 1;
2013-04-18 14:16:18 +01:00
public bounds: Rectangle;
public aspectRatio: number;
public clear: bool = true;
public canvas: HTMLCanvasElement;
public context: CanvasRenderingContext2D;
2013-04-20 03:50:21 +01:00
public disablePauseScreen: bool = false;
public disableBootScreen: bool = false;
2013-04-18 14:16:18 +01:00
public offset: Point;
public scale: StageScaleMode;
public scaleMode: number;
2013-04-18 14:16:18 +01:00
public minScaleX: number = null;
public maxScaleX: number = null;
public minScaleY: number = null;
public maxScaleY: number = null;
2013-04-12 17:19:56 +01:00
2013-04-18 14:16:18 +01: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);
}
if (this._game.isRunning == false && this.disableBootScreen == false)
{
this._bootScreen.update();
this._bootScreen.render();
}
2013-04-12 17:19:56 +01:00
if (this._game.paused == true && this.disablePauseScreen == false)
{
this._pauseScreen.update();
this._pauseScreen.render();
}
2013-04-12 17:19:56 +01:00
2013-04-18 14:16:18 +01:00
}
2013-04-12 17:19:56 +01:00
2013-04-18 14:16:18 +01:00
private visibilityChange(event) {
2013-04-20 03:50:21 +01:00
if (this.disablePauseScreen)
{
return;
}
if (event.type === 'blur' || document['hidden'] === true || document['webkitHidden'] === true)
2013-04-18 14:16:18 +01:00
{
if (this._game.paused == false)
{
this._pauseScreen.onPaused();
this.saveCanvasValues();
this._game.paused = true;
}
2013-04-18 14:16:18 +01:00
}
else if (event.type == 'focus')
{
if (this._game.paused == true)
{
this._pauseScreen.onResume();
this._game.paused = false;
this.restoreCanvasValues();
}
2013-04-18 14:16:18 +01:00
}
2013-04-12 17:19:56 +01:00
}
2013-04-18 14:16:18 +01:00
private getOffset(element): Point {
2013-04-12 17:19:56 +01:00
2013-04-18 14:16:18 +01:00
var box = element.getBoundingClientRect();
2013-04-12 17:19:56 +01:00
2013-04-18 14:16:18 +01: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 17:19:56 +01:00
2013-04-18 14:16:18 +01:00
return new Point(box.left + scrollLeft - clientLeft, box.top + scrollTop - clientTop);
2013-04-12 17:19:56 +01:00
2013-04-18 14:16:18 +01:00
}
2013-04-12 17:19:56 +01:00
2013-04-18 14:16:18 +01:00
public strokeStyle: string;
public lineWidth: number;
public fillStyle: string;
2013-04-12 17:19:56 +01:00
2013-04-18 14:16:18 +01:00
public saveCanvasValues() {
2013-04-12 17:19:56 +01:00
2013-04-18 14:16:18 +01:00
this.strokeStyle = this.context.strokeStyle;
this.lineWidth = this.context.lineWidth;
this.fillStyle = this.context.fillStyle;
2013-04-12 17:19:56 +01:00
2013-04-18 14:16:18 +01:00
}
2013-04-12 17:19:56 +01:00
2013-04-18 14:16:18 +01:00
public restoreCanvasValues() {
2013-04-12 17:19:56 +01:00
2013-04-18 14:16:18 +01:00
this.context.strokeStyle = this.strokeStyle;
this.context.lineWidth = this.lineWidth;
this.context.fillStyle = this.fillStyle;
2013-04-12 17:19:56 +01:00
2013-04-18 14:16:18 +01:00
}
2013-04-12 17:19:56 +01:00
2013-04-18 14:16:18 +01:00
public set backgroundColor(color: string) {
this.canvas.style.backgroundColor = color;
}
2013-04-12 17:19:56 +01:00
2013-04-18 14:16:18 +01:00
public get backgroundColor(): string {
return this._bgColor;
}
2013-04-12 17:19:56 +01:00
2013-04-18 14:16:18 +01:00
public get x(): number {
return this.bounds.x;
}
2013-04-12 17:19:56 +01:00
2013-04-18 14:16:18 +01:00
public get y(): number {
return this.bounds.y;
}
2013-04-12 17:19:56 +01:00
2013-04-18 14:16:18 +01:00
public get width(): number {
return this.bounds.width;
}
2013-04-12 17:19:56 +01:00
2013-04-18 14:16:18 +01:00
public get height(): number {
return this.bounds.height;
}
2013-04-12 17:19:56 +01:00
2013-04-18 14:16:18 +01:00
public get centerX(): number {
return this.bounds.halfWidth;
}
2013-04-12 17:19:56 +01:00
2013-04-18 14:16:18 +01:00
public get centerY(): number {
return this.bounds.halfHeight;
}
2013-04-12 17:19:56 +01:00
2013-04-18 14:16:18 +01:00
public get randomX(): number {
return Math.round(Math.random() * this.bounds.width);
}
2013-04-12 17:19:56 +01:00
2013-04-18 14:16:18 +01:00
public get randomY(): number {
return Math.round(Math.random() * this.bounds.height);
}
2013-04-12 17:19:56 +01:00
2013-04-18 14:16:18 +01:00
}
2013-04-12 17:19:56 +01:00
}