Version 0.7 release. StageScaleMode support added and world input values exposed.

This commit is contained in:
Richard Davey
2013-04-14 02:31:00 +01:00
parent 0541e93db4
commit ebf83609ae
18 changed files with 9289 additions and 271 deletions
+8
View File
@@ -93,6 +93,10 @@ class Camera {
public visible: bool = true;
public alpha: number = 1;
// The x/y position of the current input event in world coordinates
public inputX: number = 0;
public inputY: number = 0;
/**
* The camera is filled with this color and returns to normal at the given duration.
*
@@ -350,6 +354,10 @@ class Camera {
this.worldView.x = this.scroll.x;
this.worldView.y = this.scroll.y;
// Input values
this.inputX = this.worldView.x + this._game.input.x;
this.inputY = this.worldView.y + this._game.input.y;
// Update the Flash effect
if (this._fxFlashAlpha > 0)
{
-125
View File
@@ -1,125 +0,0 @@
/// <reference path="../Game.ts" />
/*
* Based on code from Viewporter v2.0
* http://github.com/zynga/viewporter
*
* Copyright 2011, Zynga Inc.
* Licensed under the MIT License.
* https://raw.github.com/zynga/viewporter/master/MIT-LICENSE.txt
*/
class FullScreen {
constructor(game: Game) {
this._game = game;
this.orientation = window['orientation'];
window.addEventListener('orientationchange', (event) => this.checkOrientation(event), false);
this.width = window.innerWidth;
this.height = window.innerHeight;
}
private _game: Game;
private _startHeight: number;
private _iterations: number;
private _check;
public width: number;
public height: number;
public orientation;
public go() {
this.refresh();
}
public update() {
if (this._game.stage.scaleMode !== Stage.SCALE_FIXED && (window.innerWidth !== this.width || window.innerHeight !== this.height))
{
this.refresh();
}
}
public get isLandscape(): bool {
return window['orientation'] === 90 || window['orientation'] === -90;
}
private checkOrientation(event) {
if (window['orientation'] !== this.orientation)
{
this.refresh();
this.orientation = window['orientation'];
}
}
private refresh() {
// We can't do anything about the status bars in iPads, web apps or desktops
if (this._game.device.iPad == false && this._game.device.webApp == false && this._game.device.desktop == false)
{
document.documentElement.style.minHeight = '5000px';
this._startHeight = window.innerHeight;
if (this._game.device.android && this._game.device.chrome == false)
{
window.scrollTo(0, 1);
}
else
{
window.scrollTo(0, 0);
}
}
if (this._check == null)
{
this._iterations = 40;
this._check = window.setInterval(() => this.retryFullScreen(), 10);
}
}
private retryFullScreen() {
if (this._game.device.android && this._game.device.chrome == false)
{
window.scrollTo(0, 1);
}
else
{
window.scrollTo(0, 0);
}
this._iterations--;
if (window.innerHeight > this._startHeight || this._iterations < 0)
{
// set minimum height of content to new window height
document.documentElement.style.minHeight = window.innerHeight + 'px';
this._game.stage.canvas.style.width = window.innerWidth + 'px';
this._game.stage.canvas.style.height = window.innerHeight + 'px';
this.width = window.innerWidth;
this.height = window.innerHeight;
clearInterval(this._check);
this._check = null;
}
}
}
+166
View File
@@ -0,0 +1,166 @@
/// <reference path="../Game.ts" />
/*
* Based on code from Viewporter v2.0
* http://github.com/zynga/viewporter
*
* Copyright 2011, Zynga Inc.
* Licensed under the MIT License.
* https://raw.github.com/zynga/viewporter/master/MIT-LICENSE.txt
*/
class StageScaleMode {
constructor(game: Game) {
this._game = game;
this.orientation = window['orientation'];
window.addEventListener('orientationchange', (event) => this.checkOrientation(event), false);
}
private _game: Game;
private _startHeight: number = 0;
private _iterations: number;
private _check;
// Specifies that the game be visible in the specified area without trying to preserve the original aspect ratio.
public static EXACT_FIT:number = 0;
// Specifies that the size of the game be fixed, so that it remains unchanged even if the size of the window changes.
public static NO_SCALE:number = 1;
// Specifies that the entire game be visible in the specified area without distortion while maintaining the original aspect ratio.
public static SHOW_ALL:number = 2;
public width: number = 0;
public height: number = 0;
public orientation;
public update() {
if (this._game.stage.scaleMode !== StageScaleMode.NO_SCALE && (window.innerWidth !== this.width || window.innerHeight !== this.height))
{
this.refresh();
}
}
public get isLandscape(): bool {
return window['orientation'] === 90 || window['orientation'] === -90;
}
private checkOrientation(event) {
if (window['orientation'] !== this.orientation)
{
this.refresh();
this.orientation = window['orientation'];
}
}
private refresh() {
// We can't do anything about the status bars in iPads, web apps or desktops
if (this._game.device.iPad == false && this._game.device.webApp == false && this._game.device.desktop == false)
{
document.documentElement.style.minHeight = '5000px';
this._startHeight = window.innerHeight;
if (this._game.device.android && this._game.device.chrome == false)
{
window.scrollTo(0, 1);
}
else
{
window.scrollTo(0, 0);
}
}
if (this._check == null)
{
this._iterations = 40;
this._check = window.setInterval(() => this.setScreenSize(), 10);
}
}
private setScreenSize() {
if (this._game.device.iPad == false && this._game.device.webApp == false && this._game.device.desktop == false)
{
if (this._game.device.android && this._game.device.chrome == false)
{
window.scrollTo(0, 1);
}
else
{
window.scrollTo(0, 0);
}
}
this._iterations--;
if (window.innerHeight > this._startHeight || this._iterations < 0)
{
// Set minimum height of content to new window height
document.documentElement.style.minHeight = window.innerHeight + 'px';
if (this._game.stage.scaleMode == StageScaleMode.EXACT_FIT)
{
if (this._game.stage.maxScaleX && window.innerWidth > this._game.stage.maxScaleX)
{
this.width = this._game.stage.maxScaleX;
}
else
{
this.width = window.innerWidth;
}
if (this._game.stage.maxScaleY && window.innerHeight > this._game.stage.maxScaleY)
{
this.height = this._game.stage.maxScaleY;
}
else
{
this.height = window.innerHeight;
}
}
else if (this._game.stage.scaleMode == StageScaleMode.SHOW_ALL)
{
var multiplier = Math.min((window.innerHeight / this._game.stage.height), (window.innerWidth / this._game.stage.width));
this.width = Math.round(this._game.stage.width * multiplier);
this.height = Math.round(this._game.stage.height * multiplier);
if (this._game.stage.maxScaleX && this.width > this._game.stage.maxScaleX)
{
this.width = this._game.stage.maxScaleX;
}
if (this._game.stage.maxScaleY && this.height > this._game.stage.maxScaleY)
{
this.height = this._game.stage.maxScaleY;
}
}
this._game.stage.canvas.style.width = this.width + 'px';
this._game.stage.canvas.style.height = this.height + 'px';
this._game.input.scaleX = this._game.stage.width / this.width;
this._game.input.scaleY = this._game.stage.height / this.height;
clearInterval(this._check);
this._check = null;
}
}
}
+28 -6
View File
@@ -21,11 +21,23 @@ class Input {
public keyboard: Keyboard;
public touch: Touch;
public x: number;
public y: number;
public x: number = 0;
public y: number = 0;
public scaleX: number = 1;
public scaleY: number = 1;
public worldX: number = 0;
public worldY: number = 0;
public update() {
this.x = Math.round(this.x);
this.y = Math.round(this.y);
this.worldX = this._game.camera.worldView.x + this.x;
this.worldY = this._game.camera.worldView.y + this.y;
this.mouse.update();
this.touch.update();
@@ -39,15 +51,25 @@ class Input {
}
public getWorldX(camera: Camera): number {
public getWorldX(camera?: Camera = this._game.camera) {
return this.x;
return camera.worldView.x + this.x;
}
public getWorldY(camera: Camera): number {
public getWorldY(camera?: Camera = this._game.camera) {
return this.y;
return camera.worldView.y + this.y;
}
public renderDebugInfo(x: number, y: number, color?: string = 'rgb(255,255,255)') {
this._game.stage.context.fillStyle = color;
this._game.stage.context.fillText('Input', x, y);
this._game.stage.context.fillText('Screen X: ' + this.x + ' Screen Y: ' + this.y, x, y + 14);
this._game.stage.context.fillText('World X: ' + this.worldX + ' World Y: ' + this.worldY, x, y + 28);
this._game.stage.context.fillText('Scale X: ' + this.scaleX.toFixed(1) + ' Scale Y: ' + this.scaleY.toFixed(1), x, y + 42);
}
+4
View File
@@ -23,6 +23,8 @@ class Keyboard {
public onKeyDown(event: KeyboardEvent) {
event.preventDefault();
if (!this._keys[event.keyCode])
{
this._keys[event.keyCode] = { isDown: true, timeDown: this._game.time.now, timeUp: 0 };
@@ -37,6 +39,8 @@ class Keyboard {
public onKeyUp(event: KeyboardEvent) {
event.preventDefault();
if (!this._keys[event.keyCode])
{
this._keys[event.keyCode] = { isDown: false, timeDown: 0, timeUp: this._game.time.now };
+11 -2
View File
@@ -49,6 +49,9 @@ class Mouse {
this._x = event.clientX - this._game.stage.x;
this._y = event.clientY - this._game.stage.y;
this._game.input.x = this._x * this._game.input.scaleX;
this._game.input.y = this._y * this._game.input.scaleY;
this.isDown = true;
this.isUp = false;
this.timeDown = this._game.time.now;
@@ -57,8 +60,8 @@ class Mouse {
public update() {
this._game.input.x = this._x;
this._game.input.y = this._y;
//this._game.input.x = this._x * this._game.input.scaleX;
//this._game.input.y = this._y * this._game.input.scaleY;
if (this.isDown)
{
@@ -74,6 +77,9 @@ class Mouse {
this._x = event.clientX - this._game.stage.x;
this._y = event.clientY - this._game.stage.y;
this._game.input.x = this._x * this._game.input.scaleX;
this._game.input.y = this._y * this._game.input.scaleY;
}
public onMouseUp(event: MouseEvent) {
@@ -87,6 +93,9 @@ class Mouse {
this._x = event.clientX - this._game.stage.x;
this._y = event.clientY - this._game.stage.y;
this._game.input.x = this._x * this._game.input.scaleX;
this._game.input.y = this._y * this._game.input.scaleY;
}
}
+8 -4
View File
@@ -64,14 +64,14 @@ class Touch {
* @property x
* @type Number
**/
public x: number;
public x: number = 0;
/**
*
* @property y
* @type Number
**/
public y: number;
public y: number = 0;
/**
*
@@ -227,6 +227,8 @@ class Touch {
this._fingers[f].start(event.changedTouches[i]);
this.x = this._fingers[f].x;
this.y = this._fingers[f].y;
this._game.input.x = this.x * this._game.input.scaleX;
this._game.input.y = this.y * this._game.input.scaleY;
this.touchDown.dispatch(this._fingers[f].x, this._fingers[f].y, this._fingers[f].timeDown, this._fingers[f].timeUp, this._fingers[f].duration);
this.isDown = true;
this.isUp = false;
@@ -338,6 +340,8 @@ class Touch {
this._fingers[f].move(event.changedTouches[i]);
this.x = this._fingers[f].x;
this.y = this._fingers[f].y;
this._game.input.x = this.x * this._game.input.scaleX;
this._game.input.y = this.y * this._game.input.scaleY;
break;
}
}
@@ -367,6 +371,8 @@ class Touch {
this._fingers[f].stop(event.changedTouches[i]);
this.x = this._fingers[f].x;
this.y = this._fingers[f].y;
this._game.input.x = this.x * this._game.input.scaleX;
this._game.input.y = this.y * this._game.input.scaleY;
this.touchUp.dispatch(this._fingers[f].x, this._fingers[f].y, this._fingers[f].timeDown, this._fingers[f].timeUp, this._fingers[f].duration);
this.isDown = false;
this.isUp = true;
@@ -410,8 +416,6 @@ class Touch {
*/
public update() {
this._game.input.x = this.x;
this._game.input.y = this.y;
}