canvas size updates, mouse and raycaster to API

beta.r3js.org
-=yb4f310 2018-02-20 16:40:14 +01:00
parent 92f124e623
commit 6819ba95c0
12 changed files with 128 additions and 147 deletions

View File

@ -61,8 +61,8 @@ GameLib.Event.RENDER = 0x2b;
GameLib.Event.EVENT_LIST = 0x2c; GameLib.Event.EVENT_LIST = 0x2c;
GameLib.Event.COMPILE_SUCCESS = 0x2d; GameLib.Event.COMPILE_SUCCESS = 0x2d;
GameLib.Event.COMPILE_FAILED = 0x2e; GameLib.Event.COMPILE_FAILED = 0x2e;
GameLib.Event.TEXTURE_INSTANCE_UPDATED = 0x2f; GameLib.Event.TEXTURE_INSTANCE_UPDATED = 0x2f;
//GameLib.Event.PARENT_ENTITY_CHANGED = 0x30; GameLib.Event.EVENT_ID_UPDATE = 0x30;
GameLib.Event.MATERIAL_TEXTURES_UPDATED = 0x31; GameLib.Event.MATERIAL_TEXTURES_UPDATED = 0x31;
GameLib.Event.DELETE_COMPONENT_ERROR = 0x32; GameLib.Event.DELETE_COMPONENT_ERROR = 0x32;
GameLib.Event.COMPONENT_DELETED = 0x33; GameLib.Event.COMPONENT_DELETED = 0x33;
@ -189,7 +189,7 @@ GameLib.Event.GetEventName = function(number) {
case 0x2d : return 'compile_success'; case 0x2d : return 'compile_success';
case 0x2e : return 'compile_failed'; case 0x2e : return 'compile_failed';
case 0x2f : return 'texture_image_updated'; case 0x2f : return 'texture_image_updated';
case 0x30 : return 'unused'; case 0x30 : return 'event_id_update';
case 0x31 : return 'material_textures_updated'; case 0x31 : return 'material_textures_updated';
case 0x32 : return 'delete_component_error'; case 0x32 : return 'delete_component_error';
case 0x33 : return 'component_deleted'; case 0x33 : return 'component_deleted';

View File

@ -1,22 +1,24 @@
/** /**
* Raw Canvas API object - should always correspond with the Canvas Schema * GameLib.API.Canvas
* @param id * @param id
* @param name * @param name
* @param parentEntity
* @param parentTexture
* @param autoUpdateSize
* @param width * @param width
* @param height * @param height
* @param texts * @param texts
* @param parentTexture
* @param parentEntity
* @constructor * @constructor
*/ */
GameLib.API.Canvas = function( GameLib.API.Canvas = function(
id, id,
name, name,
parentEntity,
parentTexture,
autoUpdateSize,
width, width,
height, height,
texts, texts
parentTexture,
parentEntity
) { ) {
if (GameLib.Utils.UndefinedOrNull(id)) { if (GameLib.Utils.UndefinedOrNull(id)) {
id = GameLib.Utils.RandomId(); id = GameLib.Utils.RandomId();
@ -28,6 +30,16 @@ GameLib.API.Canvas = function(
} }
this.name = name; this.name = name;
if (GameLib.Utils.UndefinedOrNull(parentTexture)) {
parentTexture = null;
}
this.parentTexture = parentTexture;
if (GameLib.Utils.UndefinedOrNull(autoUpdateSize)) {
autoUpdateSize = true;
}
this.autoUpdateSize = autoUpdateSize;
if (GameLib.Utils.UndefinedOrNull(width)) { if (GameLib.Utils.UndefinedOrNull(width)) {
width = 512; width = 512;
} }
@ -43,11 +55,6 @@ GameLib.API.Canvas = function(
} }
this.texts = texts; this.texts = texts;
if (GameLib.Utils.UndefinedOrNull(parentTexture)) {
parentTexture = null;
}
this.parentTexture = parentTexture;
GameLib.API.Component.call( GameLib.API.Component.call(
this, this,
GameLib.Component.CANVAS, GameLib.Component.CANVAS,

View File

@ -43,19 +43,3 @@ GameLib.API.CustomCode = function (
GameLib.API.CustomCode.prototype = Object.create(GameLib.API.Component.prototype); GameLib.API.CustomCode.prototype = Object.create(GameLib.API.Component.prototype);
GameLib.API.CustomCode.prototype.constructor = GameLib.API.CustomCode; GameLib.API.CustomCode.prototype.constructor = GameLib.API.CustomCode;
/**
* Object to GameLib.API.CustomCode
* @param objectComponent
* @returns {GameLib.API.CustomCode}
* @constructor
*/
GameLib.API.CustomCode.FromObject = function(objectComponent) {
return new GameLib.API.CustomCode(
objectComponent.id,
objectComponent.name,
objectComponent.eventId,
objectComponent.code,
objectComponent.parentEntity
);
};

View File

@ -2,17 +2,17 @@
* API Mouse * API Mouse
* @param id * @param id
* @param name * @param name
* @param parentEntity
* @param x * @param x
* @param y * @param y
* @param parentEntity
* @constructor * @constructor
*/ */
GameLib.API.Mouse = function( GameLib.API.Mouse = function(
id, id,
name, name,
parentEntity,
x, x,
y, y
parentEntity
) { ) {
if (GameLib.Utils.UndefinedOrNull(id)) { if (GameLib.Utils.UndefinedOrNull(id)) {
id = GameLib.Utils.RandomId(); id = GameLib.Utils.RandomId();
@ -43,18 +43,3 @@ GameLib.API.Mouse = function(
GameLib.API.Mouse.prototype = Object.create(GameLib.API.Component.prototype); GameLib.API.Mouse.prototype = Object.create(GameLib.API.Component.prototype);
GameLib.API.Mouse.prototype.constructor = GameLib.API.Mouse; GameLib.API.Mouse.prototype.constructor = GameLib.API.Mouse;
/**
* Returns an API mouse from an Object mouse
* @param objectMouse
* @constructor
*/
GameLib.API.Mouse.FromObject = function (objectMouse) {
return new GameLib.API.Mouse(
objectMouse.id,
objectMouse.name,
objectMouse.x,
objectMouse.y,
objectMouse.parentEntity
)
};

View File

@ -15,11 +15,12 @@ GameLib.Canvas = function(
this, this,
apiCanvas.id, apiCanvas.id,
apiCanvas.name, apiCanvas.name,
apiCanvas.parentEntity,
apiCanvas.parentTexture,
apiCanvas.autoUpdateSize,
apiCanvas.width, apiCanvas.width,
apiCanvas.height, apiCanvas.height,
apiCanvas.texts, apiCanvas.texts
apiCanvas.parentTexture,
apiCanvas.parentEntity
); );
GameLib.Component.call( GameLib.Component.call(
@ -46,11 +47,19 @@ GameLib.Canvas.prototype.createInstance = function() {
this.instance.setAttribute('tabindex', '1'); this.instance.setAttribute('tabindex', '1');
this.width = Math.round(this.width); if (this.autoUpdateSize) {
this.height = Math.round(this.height); /**
* Update our size from the instance size
this.instance.width = this.width; */
this.instance.height = this.height; this.width = this.instance.width;
this.height = this.instance.height;
} else {
/**
* Update our instance with our size
*/
this.instance.width = this.width;
this.instance.height = this.height;
}
this.writeText(); this.writeText();
@ -70,14 +79,34 @@ GameLib.Canvas.prototype.updateInstance = function(property) {
this.instance.setAttribute('id', this.id); this.instance.setAttribute('id', this.id);
} }
if (property === 'width') { if (property === 'autoUpdateSize' ||
this.width = Math.round(this.width); property === 'width' ||
this.instance.width = this.width; property === 'height'
} ) {
/**
* We cannot control everything about the canvas - this is dependent on where the canvas lives and its
* dimensions can also be controlled via CSS -
*
* This means - autoUpdateSize works a little different for this component - instead of getting our size and
* applying it, it gets our canvas size and applies it, or applies our size to the canvas - of course
* the user settings override this.
*/
if (this.autoUpdateSize) {
if (property === 'height') { /**
this.height = Math.round(this.height); * Update from our canvas size
this.instance.height = this.height; */
this.width = this.instance.width;
this.height = this.instance.height;
} else {
/**
* Command our canvas to take a size - this is not guaranteed however - CSS wins
*/
this.instance.width = this.width;
this.instance.height = this.height;
}
} }
if (property === 'texts') { if (property === 'texts') {
@ -93,13 +122,14 @@ GameLib.Canvas.prototype.toApiObject = function() {
return new GameLib.API.Canvas( return new GameLib.API.Canvas(
this.id, this.id,
this.name, this.name,
GameLib.Utils.IdOrNull(this.parentTexture),
GameLib.Utils.IdOrNull(this.parentEntity),
this.autoUpdateSize,
this.width, this.width,
this.height, this.height,
this.texts.map(function(text){ this.texts.map(function(text){
return GameLib.Utils.IdOrNull(text) return GameLib.Utils.IdOrNull(text)
}), })
GameLib.Utils.IdOrNull(this.parentTexture),
GameLib.Utils.IdOrNull(this.parentEntity)
); );
}; };

View File

@ -71,6 +71,15 @@ GameLib.CustomCode.prototype.updateInstance = function(property) {
return; return;
} }
if (property === 'eventId') {
this.publish(
GameLib.Event.EVENT_ID_UPDATE,
{
component : this
}
)
}
GameLib.Component.prototype.updateInstance.call(this, property); GameLib.Component.prototype.updateInstance.call(this, property);
}; };

View File

@ -10,9 +10,9 @@
GameLib.D3.API.Raycaster = function( GameLib.D3.API.Raycaster = function(
id, id,
name, name,
parentEntity,
position, position,
direction, direction
parentEntity
) { ) {
if (GameLib.Utils.UndefinedOrNull(id)) { if (GameLib.Utils.UndefinedOrNull(id)) {
@ -44,18 +44,3 @@ GameLib.D3.API.Raycaster = function(
GameLib.D3.API.Raycaster.prototype = Object.create(GameLib.API.Component.prototype); GameLib.D3.API.Raycaster.prototype = Object.create(GameLib.API.Component.prototype);
GameLib.D3.API.Raycaster.prototype.constructor = GameLib.D3.API.Raycaster; GameLib.D3.API.Raycaster.prototype.constructor = GameLib.D3.API.Raycaster;
/**
* Returns an API raycaster from an Object raycaster
* @param objectRaycaster
* @constructor
*/
GameLib.D3.API.Raycaster.FromObject = function(objectRaycaster) {
return new GameLib.D3.API.Raycaster(
objectRaycaster.id,
objectRaycaster.name,
GameLib.API.Vector3.FromObject(objectRaycaster.position),
GameLib.API.Vector3.FromObject(objectRaycaster.direction),
objectRaycaster.parentEntity
);
};

View File

@ -20,6 +20,7 @@ GameLib.D3.Raycaster = function(
this, this,
apiRaycaster.id, apiRaycaster.id,
apiRaycaster.name, apiRaycaster.name,
apiRaycaster.parentEntity,
apiRaycaster.position, apiRaycaster.position,
apiRaycaster.direction apiRaycaster.direction
); );
@ -46,6 +47,7 @@ GameLib.D3.Raycaster.prototype.constructor = GameLib.D3.Raycaster;
* Creates or updates a raycaster instance * Creates or updates a raycaster instance
*/ */
GameLib.D3.Raycaster.prototype.createInstance = function() { GameLib.D3.Raycaster.prototype.createInstance = function() {
this.instance = new THREE.Raycaster(); this.instance = new THREE.Raycaster();
this.instance.set( this.instance.set(
this.position.instance, this.position.instance,
@ -55,44 +57,37 @@ GameLib.D3.Raycaster.prototype.createInstance = function() {
GameLib.Component.prototype.createInstance.call(this); GameLib.Component.prototype.createInstance.call(this);
}; };
GameLib.D3.Raycaster.prototype.updateInstance = function() { GameLib.D3.Raycaster.prototype.updateInstance = function(property) {
this.position.instance.x = this.position.x; if (property === 'position') {
this.position.instance.y = this.position.y; this.position.instance.x = this.position.x;
this.position.instance.z = this.position.z; this.position.instance.y = this.position.y;
this.position.instance.z = this.position.z;
this.instance.setPosition(this.position.instance);
return;
}
this.direction.instance.x = this.direction.x; if (property === 'direction') {
this.direction.instance.y = this.direction.y; this.direction.instance.x = this.direction.x;
this.direction.instance.z = this.direction.z; this.direction.instance.y = this.direction.y;
this.direction.instance.z = this.direction.z;
this.instance.setDirection(this.direction.instance);
return;
}
this.instance.set( GameLib.Component.prototype.updateInstance.call(this, property);
this.position.instance,
this.direction.instance
);
}; };
GameLib.D3.Raycaster.prototype.toApiObject = function() { GameLib.D3.Raycaster.prototype.toApiObject = function() {
return new GameLib.D3.API.Raycaster( return new GameLib.D3.API.Raycaster(
this.id, this.id,
this.name, this.name,
GameLib.Utils.IdOrNull(this.parentEntity),
this.position.toApiObject(), this.position.toApiObject(),
this.direction.toApiObject(), this.direction.toApiObject()
GameLib.Utils.IdOrNull(this.parentEntity)
) )
}; };
GameLib.D3.Raycaster.FromObject = function(graphics, objectRaycaster) {
var apiRaycaster = GameLib.D3.API.Raycaster.FromObject(objectRaycaster);
var raycaster = new GameLib.D3.Raycaster(
graphics,
apiRaycaster
);
return raycaster;
};
/** /**
* Sets the direction and position of this raycaster * Sets the direction and position of this raycaster
* @param position GameLib.Vector3 * @param position GameLib.Vector3
@ -191,34 +186,8 @@ GameLib.D3.Raycaster.prototype.getIntersectedObjects = function(meshes) {
}.bind(this), }.bind(this),
[] []
); );
// var intersects = this.instance.intersectObjects(meshInstances);
//
// return intersects.reduce(
//
// function (result, intersect) {
//
// meshes.map(
// function(mesh){
// if (mesh.instance === intersect.object){
// result.push(
// {
// mesh : mesh,
// distance : intersect.distance
// }
// );
// }
// }
// );
//
// return result;
// },
// []
// );
}; };
/** /**
* Returns the face normal (if any) of an intersection between current ray position, direction and a provided mesh * Returns the face normal (if any) of an intersection between current ray position, direction and a provided mesh
* @param mesh GameLib.D3.Mesh * @param mesh GameLib.D3.Mesh
@ -274,4 +243,4 @@ GameLib.D3.Raycaster.prototype.getIntersectPoint = function(mesh) {
} }
return point; return point;
}; };

View File

@ -14,9 +14,9 @@ GameLib.Mouse = function (apiMouse) {
this, this,
apiMouse.id, apiMouse.id,
apiMouse.name, apiMouse.name,
apiMouse.parentEntity,
apiMouse.x, apiMouse.x,
apiMouse.y, apiMouse.y
apiMouse.parentEntity
); );
GameLib.Component.call(this); GameLib.Component.call(this);
@ -60,8 +60,8 @@ GameLib.Mouse.prototype.toApiObject = function() {
return new GameLib.API.Mouse( return new GameLib.API.Mouse(
this.id, this.id,
this.name, this.name,
GameLib.Utils.IdOrNull(this.parentEntity),
this.x, this.x,
this.y, this.y
this.parentEntity
); );
}; };

View File

@ -16,6 +16,7 @@ GameLib.System.CustomCode = function(
this.removeComponentSubscription = null; this.removeComponentSubscription = null;
this.compileSuccessSubscription = null; this.compileSuccessSubscription = null;
this.compileFailedSubscription = null; this.compileFailedSubscription = null;
this.eventIdUpdateSubscription = null;
this.subscriptions = {}; this.subscriptions = {};
@ -60,6 +61,10 @@ GameLib.System.CustomCode.prototype.start = function() {
this.compileFailed.bind(this) this.compileFailed.bind(this)
); );
this.eventIdUpdateSubscription = this.subscribe(
GameLib.Event.EVENT_ID_UPDATE,
this.compileSuccess
);
}; };
@ -134,6 +139,11 @@ GameLib.System.CustomCode.prototype.stop = function() {
this.compileFailedSubscription = null; this.compileFailedSubscription = null;
} }
if (this.eventIdUpdateSubscription) {
this.eventIdUpdateSubscription.remove();
this.eventIdUpdateSubscription = null;
}
Object.keys(this.subscriptions).map( Object.keys(this.subscriptions).map(
function(componentId) { function(componentId) {
if (this.subscriptions[componentId]) { if (this.subscriptions[componentId]) {

View File

@ -913,7 +913,7 @@ GameLib.System.Input.prototype.onKeyUp = function(event) {
GameLib.Event.Emit( GameLib.Event.Emit(
GameLib.Event.KEY_UP, GameLib.Event.KEY_UP,
{ {
code : event.code code : event.code || event.key
} }
); );
@ -1076,12 +1076,13 @@ GameLib.System.Input.prototype.onMouseDownEdit = function(event) {
* @param event * @param event
*/ */
GameLib.System.Input.prototype.onMouseMoveEdit = function(event) { GameLib.System.Input.prototype.onMouseMoveEdit = function(event) {
GameLib.EntityManager.Instance.queryComponents(GameLib.Component.MOUSE).map( GameLib.EntityManager.Instance.queryComponents(GameLib.Component.MOUSE).map(
function(mouse) { function(mouse) {
mouse.x = event.clientX; mouse.x = event.clientX;
mouse.y = event.clientY; mouse.y = event.clientY;
} }
) );
this.editorControls.map( this.editorControls.map(
function(editorControl) { function(editorControl) {

View File

@ -235,22 +235,23 @@ GameLib.System.Render.prototype.windowResize = function(data) {
} }
); );
var renderers = GameLib.EntityManager.Instance.queryComponents(GameLib.Component.RENDERER); GameLib.EntityManager.Instance.queryComponents(GameLib.Component.RENDERER).map(
renderers.map(
function(renderer) { function(renderer) {
renderer.setSize(data.width, data.height); renderer.setSize(data.width, data.height);
} }
); );
var effects = GameLib.EntityManager.Instance.queryComponentsByConstructor(GameLib.D3.Effect); GameLib.EntityManager.Instance.queryComponentsByConstructor(GameLib.D3.Effect).map(
effects.map(
function(effect){ function(effect){
effect.setSize(data.width, data.height); effect.setSize(data.width, data.height);
} }
); );
GameLib.EntityManager.Instance.queryComponentsByConstructor(GameLib.Canvas).map(
function(canvas) {
canvas.updateInstance('autoUpdateSize');
}
);
GameLib.EntityManager.Instance.queryComponentsByConstructor(GameLib.D3.Camera).map( GameLib.EntityManager.Instance.queryComponentsByConstructor(GameLib.D3.Camera).map(
function(camera){ function(camera){