awayjs-display
Version:
AwayJS displaylist classes
492 lines • 17.7 kB
JavaScript
"use strict";
var getTimer_1 = require("awayjs-core/lib/utils/getTimer");
var TouchPoint_1 = require("./base/TouchPoint");
var Scene_1 = require("./display/Scene");
var RaycastPicker_1 = require("./pick/RaycastPicker");
var Camera_1 = require("./display/Camera");
var CameraEvent_1 = require("./events/CameraEvent");
var DisplayObjectEvent_1 = require("./events/DisplayObjectEvent");
var RendererEvent_1 = require("./events/RendererEvent");
var MouseManager_1 = require("./managers/MouseManager");
var View = (function () {
/*
***********************************************************************
* Disabled / Not yet implemented
***********************************************************************
*
* private _background:away.textures.Texture2DBase;
*
* public _pTouch3DManager:away.managers.Touch3DManager;
*
*/
function View(renderer, scene, camera) {
var _this = this;
if (scene === void 0) { scene = null; }
if (camera === void 0) { camera = null; }
this._width = 0;
this._height = 0;
this._time = 0;
this._deltaTime = 0;
this._backgroundColor = 0x000000;
this._backgroundAlpha = 1;
this._viewportDirty = true;
this._scissorDirty = true;
this._mousePicker = new RaycastPicker_1.default();
this._pTouchPoints = new Array();
this._onPartitionChangedDelegate = function (event) { return _this._onPartitionChanged(event); };
this._onProjectionChangedDelegate = function (event) { return _this._onProjectionChanged(event); };
this._onViewportUpdatedDelegate = function (event) { return _this._onViewportUpdated(event); };
this._onScissorUpdatedDelegate = function (event) { return _this._onScissorUpdated(event); };
this.scene = scene || new Scene_1.default();
this.camera = camera || new Camera_1.default();
this.renderer = renderer;
//make sure document border is zero
if (document) {
document.body.style.margin = "0px";
this._htmlElement = document.createElement("div");
this._htmlElement.style.position = "absolute";
document.body.appendChild(this._htmlElement);
}
this._mouseManager = MouseManager_1.default.getInstance();
this._mouseManager.registerView(this);
// if (this._shareContext)
// this._mouse3DManager.addViewLayer(this);
}
Object.defineProperty(View.prototype, "mouseX", {
get: function () {
return this._pMouseX;
},
enumerable: true,
configurable: true
});
Object.defineProperty(View.prototype, "mouseY", {
get: function () {
return this._pMouseY;
},
enumerable: true,
configurable: true
});
Object.defineProperty(View.prototype, "touchPoints", {
get: function () {
return this._pTouchPoints;
},
enumerable: true,
configurable: true
});
View.prototype.getLocalMouseX = function (displayObject) {
return displayObject.inverseSceneTransform.transformVector(this.unproject(this._pMouseX, this._pMouseY, 1000)).x;
};
View.prototype.getLocalMouseY = function (displayObject) {
return displayObject.inverseSceneTransform.transformVector(this.unproject(this._pMouseX, this._pMouseY, 1000)).y;
};
View.prototype.getLocalTouchPoints = function (displayObject) {
var localPosition;
var localTouchPoints = new Array();
var len = this._pTouchPoints.length;
for (var i = 0; i < len; i++) {
localPosition = displayObject.inverseSceneTransform.transformVector(this.unproject(this._pTouchPoints[i].x, this._pTouchPoints[i].y, 1000));
localTouchPoints.push(new TouchPoint_1.default(localPosition.x, localPosition.y, this._pTouchPoints[i].id));
}
return localTouchPoints;
};
Object.defineProperty(View.prototype, "htmlElement", {
/**
*
*/
get: function () {
return this._htmlElement;
},
enumerable: true,
configurable: true
});
Object.defineProperty(View.prototype, "renderer", {
/**
*
*/
get: function () {
return this._pRenderer;
},
set: function (value) {
if (this._pRenderer == value)
return;
if (this._pRenderer) {
this._pRenderer.dispose();
this._pRenderer.removeEventListener(RendererEvent_1.default.VIEWPORT_UPDATED, this._onViewportUpdatedDelegate);
this._pRenderer.removeEventListener(RendererEvent_1.default.SCISSOR_UPDATED, this._onScissorUpdatedDelegate);
}
this._pRenderer = value;
this._pRenderer.addEventListener(RendererEvent_1.default.VIEWPORT_UPDATED, this._onViewportUpdatedDelegate);
this._pRenderer.addEventListener(RendererEvent_1.default.SCISSOR_UPDATED, this._onScissorUpdatedDelegate);
//reset back buffer
this._pRenderer._iBackgroundR = ((this._backgroundColor >> 16) & 0xff) / 0xff;
this._pRenderer._iBackgroundG = ((this._backgroundColor >> 8) & 0xff) / 0xff;
this._pRenderer._iBackgroundB = (this._backgroundColor & 0xff) / 0xff;
this._pRenderer._iBackgroundAlpha = this._backgroundAlpha;
this._pRenderer.width = this._width;
this._pRenderer.height = this._height;
this._pRenderer.shareContext = this._shareContext;
},
enumerable: true,
configurable: true
});
Object.defineProperty(View.prototype, "shareContext", {
/**
*
*/
get: function () {
return this._shareContext;
},
set: function (value) {
if (this._shareContext == value)
return;
this._shareContext = value;
if (this._pRenderer)
this._pRenderer.shareContext = this._shareContext;
},
enumerable: true,
configurable: true
});
Object.defineProperty(View.prototype, "backgroundColor", {
/**
*
*/
get: function () {
return this._backgroundColor;
},
set: function (value) {
if (this._backgroundColor == value)
return;
this._backgroundColor = value;
this._pRenderer._iBackgroundR = ((value >> 16) & 0xff) / 0xff;
this._pRenderer._iBackgroundG = ((value >> 8) & 0xff) / 0xff;
this._pRenderer._iBackgroundB = (value & 0xff) / 0xff;
},
enumerable: true,
configurable: true
});
Object.defineProperty(View.prototype, "backgroundAlpha", {
/**
*
* @returns {number}
*/
get: function () {
return this._backgroundAlpha;
},
/**
*
* @param value
*/
set: function (value) {
if (value > 1)
value = 1;
else if (value < 0)
value = 0;
if (this._backgroundAlpha == value)
return;
this._pRenderer._iBackgroundAlpha = this._backgroundAlpha = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(View.prototype, "camera", {
/**
*
* @returns {Camera3D}
*/
get: function () {
return this._pCamera;
},
/**
* Set camera that's used to render the scene for this viewport
*/
set: function (value) {
if (this._pCamera == value)
return;
if (this._pCamera)
this._pCamera.removeEventListener(CameraEvent_1.default.PROJECTION_CHANGED, this._onProjectionChangedDelegate);
this._pCamera = value;
if (this._pScene)
this._pScene.partition._iRegisterEntity(this._pCamera);
this._pCamera.addEventListener(CameraEvent_1.default.PROJECTION_CHANGED, this._onProjectionChangedDelegate);
this._scissorDirty = true;
this._viewportDirty = true;
},
enumerable: true,
configurable: true
});
Object.defineProperty(View.prototype, "scene", {
/**
*
* @returns {away.containers.Scene3D}
*/
get: function () {
return this._pScene;
},
/**
* Set the scene that's used to render for this viewport
*/
set: function (value) {
if (this._pScene == value)
return;
if (this._pScene)
this._pScene.removeEventListener(DisplayObjectEvent_1.default.PARTITION_CHANGED, this._onPartitionChangedDelegate);
this._pScene = value;
this._pScene.addEventListener(DisplayObjectEvent_1.default.PARTITION_CHANGED, this._onPartitionChangedDelegate);
if (this._pCamera)
this._pScene.partition._iRegisterEntity(this._pCamera);
},
enumerable: true,
configurable: true
});
Object.defineProperty(View.prototype, "deltaTime", {
/**
*
* @returns {number}
*/
get: function () {
return this._deltaTime;
},
enumerable: true,
configurable: true
});
Object.defineProperty(View.prototype, "width", {
/**
*
*/
get: function () {
return this._width;
},
set: function (value) {
if (this._width == value)
return;
this._width = value;
this._aspectRatio = this._width / this._height;
this._pCamera.projection._iAspectRatio = this._aspectRatio;
this._pRenderer.width = value;
if (this._htmlElement) {
this._htmlElement.style.width = value + "px";
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(View.prototype, "height", {
/**
*
*/
get: function () {
return this._height;
},
set: function (value) {
if (this._height == value)
return;
this._height = value;
this._aspectRatio = this._width / this._height;
this._pCamera.projection._iAspectRatio = this._aspectRatio;
this._pRenderer.height = value;
if (this._htmlElement) {
this._htmlElement.style.height = value + "px";
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(View.prototype, "mousePicker", {
/**
*
*/
get: function () {
return this._mousePicker;
},
set: function (value) {
if (this._mousePicker == value)
return;
if (value == null)
this._mousePicker = new RaycastPicker_1.default();
else
this._mousePicker = value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(View.prototype, "x", {
/**
*
*/
get: function () {
return this._pRenderer.x;
},
set: function (value) {
if (this._pRenderer.x == value)
return;
this._pRenderer.x == value;
if (this._htmlElement) {
this._htmlElement.style.left = value + "px";
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(View.prototype, "y", {
/**
*
*/
get: function () {
return this._pRenderer.y;
},
set: function (value) {
if (this._pRenderer.y == value)
return;
this._pRenderer.y == value;
if (this._htmlElement) {
this._htmlElement.style.top = value + "px";
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(View.prototype, "visible", {
/**
*
*/
get: function () {
return (this._htmlElement && this._htmlElement.style.visibility == "visible");
},
set: function (value) {
if (this._htmlElement) {
this._htmlElement.style.visibility = value ? "visible" : "hidden";
}
//TODO transfer visible property to associated context (if one exists)
},
enumerable: true,
configurable: true
});
Object.defineProperty(View.prototype, "renderedFacesCount", {
/**
*
* @returns {number}
*/
get: function () {
return 0; //TODO
//return this._pEntityCollector._pNumTriangles;//numTriangles;
},
enumerable: true,
configurable: true
});
/**
* Renders the view.
*/
View.prototype.render = function () {
this.pUpdateTime();
//update view and size data
this._pCamera.projection._iAspectRatio = this._aspectRatio;
if (this._scissorDirty) {
this._scissorDirty = false;
this._pCamera.projection._iUpdateScissorRect(this._pRenderer.scissorRect.x, this._pRenderer.scissorRect.y, this._pRenderer.scissorRect.width, this._pRenderer.scissorRect.height);
}
if (this._viewportDirty) {
this._viewportDirty = false;
this._pCamera.projection._iUpdateViewport(this._pRenderer.viewPort.x, this._pRenderer.viewPort.y, this._pRenderer.viewPort.width, this._pRenderer.viewPort.height);
}
// update picking
if (!this._shareContext) {
if (this.forceMouseMove && this._htmlElement == this._mouseManager._iActiveDiv && !this._mouseManager._iUpdateDirty)
this._mouseManager._iCollision = this.mousePicker.getViewCollision(this._pMouseX, this._pMouseY, this);
this._mouseManager.fireMouseEvents(this.forceMouseMove);
}
//_touch3DManager.updateCollider();
//render the contents of the scene
this._pRenderer.render(this._pCamera, this._pScene);
};
/**
*
*/
View.prototype.pUpdateTime = function () {
var time = getTimer_1.default();
if (this._time == 0)
this._time = time;
this._deltaTime = time - this._time;
this._time = time;
};
/**
*
*/
View.prototype.dispose = function () {
this._pRenderer.dispose();
// TODO: imeplement mouseManager / touch3DManager
this._mouseManager.unregisterView(this);
//this._touch3DManager.disableTouchListeners(this);
//this._touch3DManager.dispose();
this._mouseManager = null;
//this._touch3DManager = null;
this._pRenderer = null;
};
/**
*
* @param e
*/
View.prototype._onPartitionChanged = function (event) {
if (this._pCamera)
this._pScene.partition._iRegisterEntity(this._pCamera);
};
/**
*
*/
View.prototype._onProjectionChanged = function (event) {
this._scissorDirty = true;
this._viewportDirty = true;
};
/**
*
*/
View.prototype._onViewportUpdated = function (event) {
this._viewportDirty = true;
};
/**
*
*/
View.prototype._onScissorUpdated = function (event) {
this._scissorDirty = true;
};
View.prototype.project = function (point3d) {
var v = this._pCamera.project(point3d);
v.x = v.x * this._pRenderer.viewPort.width / 2 + this._width * this._pCamera.projection.originX;
v.y = v.y * this._pRenderer.viewPort.height / 2 + this._height * this._pCamera.projection.originY;
return v;
};
View.prototype.unproject = function (sX, sY, sZ) {
return this._pCamera.unproject(2 * (sX - this._width * this._pCamera.projection.originX) / this._pRenderer.viewPort.width, 2 * (sY - this._height * this._pCamera.projection.originY) / this._pRenderer.viewPort.height, sZ);
};
View.prototype.getRay = function (sX, sY, sZ) {
return this._pCamera.getRay((sX * 2 - this._width) / this._width, (sY * 2 - this._height) / this._height, sZ);
};
/*TODO: implement Background
public get background():away.textures.Texture2DBase
{
return this._background;
}
*/
/*TODO: implement Background
public set background( value:away.textures.Texture2DBase )
{
this._background = value;
this._renderer.background = _background;
}
*/
// TODO: required dependency stageGL
View.prototype.updateCollider = function () {
if (!this._shareContext) {
if (this._htmlElement == this._mouseManager._iActiveDiv)
this._mouseManager._iCollision = this.mousePicker.getViewCollision(this._pMouseX, this._pMouseY, this);
}
else {
var collidingObject = this.mousePicker.getViewCollision(this._pMouseX, this._pMouseY, this);
if (this.layeredView || this._mouseManager._iCollision == null || collidingObject.rayEntryDistance < this._mouseManager._iCollision.rayEntryDistance)
this._mouseManager._iCollision = collidingObject;
}
};
return View;
}());
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = View;
//# sourceMappingURL=View.js.map