From 6819ba95c07aab2145422a1547545add4a7183bc Mon Sep 17 00:00:00 2001 From: -=yb4f310 Date: Tue, 20 Feb 2018 16:40:14 +0100 Subject: [PATCH] canvas size updates, mouse and raycaster to API --- src/game-lib-a-1-event.js | 6 +-- src/game-lib-api-canvas.js | 29 +++++++----- src/game-lib-api-custom-code.js | 16 ------- src/game-lib-api-mouse.js | 21 ++------- src/game-lib-canvas.js | 66 +++++++++++++++++++-------- src/game-lib-custom-code.js | 9 ++++ src/game-lib-d3-api-raycaster.js | 19 +------- src/game-lib-d3-raycaster.js | 73 +++++++++--------------------- src/game-lib-mouse.js | 8 ++-- src/game-lib-system-custom-code.js | 10 ++++ src/game-lib-system-input.js | 5 +- src/game-lib-system-render.js | 13 +++--- 12 files changed, 128 insertions(+), 147 deletions(-) diff --git a/src/game-lib-a-1-event.js b/src/game-lib-a-1-event.js index 8638fd5..c5f116e 100644 --- a/src/game-lib-a-1-event.js +++ b/src/game-lib-a-1-event.js @@ -61,8 +61,8 @@ GameLib.Event.RENDER = 0x2b; GameLib.Event.EVENT_LIST = 0x2c; GameLib.Event.COMPILE_SUCCESS = 0x2d; GameLib.Event.COMPILE_FAILED = 0x2e; -GameLib.Event.TEXTURE_INSTANCE_UPDATED = 0x2f; -//GameLib.Event.PARENT_ENTITY_CHANGED = 0x30; +GameLib.Event.TEXTURE_INSTANCE_UPDATED = 0x2f; +GameLib.Event.EVENT_ID_UPDATE = 0x30; GameLib.Event.MATERIAL_TEXTURES_UPDATED = 0x31; GameLib.Event.DELETE_COMPONENT_ERROR = 0x32; GameLib.Event.COMPONENT_DELETED = 0x33; @@ -189,7 +189,7 @@ GameLib.Event.GetEventName = function(number) { case 0x2d : return 'compile_success'; case 0x2e : return 'compile_failed'; case 0x2f : return 'texture_image_updated'; - case 0x30 : return 'unused'; + case 0x30 : return 'event_id_update'; case 0x31 : return 'material_textures_updated'; case 0x32 : return 'delete_component_error'; case 0x33 : return 'component_deleted'; diff --git a/src/game-lib-api-canvas.js b/src/game-lib-api-canvas.js index 5d44b96..96dc3b5 100644 --- a/src/game-lib-api-canvas.js +++ b/src/game-lib-api-canvas.js @@ -1,22 +1,24 @@ /** - * Raw Canvas API object - should always correspond with the Canvas Schema + * GameLib.API.Canvas * @param id * @param name + * @param parentEntity + * @param parentTexture + * @param autoUpdateSize * @param width * @param height * @param texts - * @param parentTexture - * @param parentEntity * @constructor */ GameLib.API.Canvas = function( id, name, + parentEntity, + parentTexture, + autoUpdateSize, width, height, - texts, - parentTexture, - parentEntity + texts ) { if (GameLib.Utils.UndefinedOrNull(id)) { id = GameLib.Utils.RandomId(); @@ -28,6 +30,16 @@ GameLib.API.Canvas = function( } 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)) { width = 512; } @@ -43,11 +55,6 @@ GameLib.API.Canvas = function( } this.texts = texts; - if (GameLib.Utils.UndefinedOrNull(parentTexture)) { - parentTexture = null; - } - this.parentTexture = parentTexture; - GameLib.API.Component.call( this, GameLib.Component.CANVAS, diff --git a/src/game-lib-api-custom-code.js b/src/game-lib-api-custom-code.js index e04818a..1f414b3 100644 --- a/src/game-lib-api-custom-code.js +++ b/src/game-lib-api-custom-code.js @@ -43,19 +43,3 @@ GameLib.API.CustomCode = function ( GameLib.API.CustomCode.prototype = Object.create(GameLib.API.Component.prototype); 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 - ); -}; diff --git a/src/game-lib-api-mouse.js b/src/game-lib-api-mouse.js index 77fb9b2..a986b48 100644 --- a/src/game-lib-api-mouse.js +++ b/src/game-lib-api-mouse.js @@ -2,17 +2,17 @@ * API Mouse * @param id * @param name + * @param parentEntity * @param x * @param y - * @param parentEntity * @constructor */ GameLib.API.Mouse = function( id, name, + parentEntity, x, - y, - parentEntity + y ) { if (GameLib.Utils.UndefinedOrNull(id)) { 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.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 - ) -}; diff --git a/src/game-lib-canvas.js b/src/game-lib-canvas.js index c9c2926..14e8f85 100644 --- a/src/game-lib-canvas.js +++ b/src/game-lib-canvas.js @@ -15,11 +15,12 @@ GameLib.Canvas = function( this, apiCanvas.id, apiCanvas.name, + apiCanvas.parentEntity, + apiCanvas.parentTexture, + apiCanvas.autoUpdateSize, apiCanvas.width, apiCanvas.height, - apiCanvas.texts, - apiCanvas.parentTexture, - apiCanvas.parentEntity + apiCanvas.texts ); GameLib.Component.call( @@ -46,11 +47,19 @@ GameLib.Canvas.prototype.createInstance = function() { this.instance.setAttribute('tabindex', '1'); - this.width = Math.round(this.width); - this.height = Math.round(this.height); - - this.instance.width = this.width; - this.instance.height = this.height; + if (this.autoUpdateSize) { + /** + * Update our size from the instance size + */ + 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(); @@ -70,14 +79,34 @@ GameLib.Canvas.prototype.updateInstance = function(property) { this.instance.setAttribute('id', this.id); } - if (property === 'width') { - this.width = Math.round(this.width); - this.instance.width = this.width; - } + if (property === 'autoUpdateSize' || + property === '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); - this.instance.height = this.height; + /** + * Update from our canvas size + */ + 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') { @@ -93,13 +122,14 @@ GameLib.Canvas.prototype.toApiObject = function() { return new GameLib.API.Canvas( this.id, this.name, + GameLib.Utils.IdOrNull(this.parentTexture), + GameLib.Utils.IdOrNull(this.parentEntity), + this.autoUpdateSize, this.width, this.height, this.texts.map(function(text){ return GameLib.Utils.IdOrNull(text) - }), - GameLib.Utils.IdOrNull(this.parentTexture), - GameLib.Utils.IdOrNull(this.parentEntity) + }) ); }; diff --git a/src/game-lib-custom-code.js b/src/game-lib-custom-code.js index d719922..c58b551 100644 --- a/src/game-lib-custom-code.js +++ b/src/game-lib-custom-code.js @@ -71,6 +71,15 @@ GameLib.CustomCode.prototype.updateInstance = function(property) { return; } + if (property === 'eventId') { + this.publish( + GameLib.Event.EVENT_ID_UPDATE, + { + component : this + } + ) + } + GameLib.Component.prototype.updateInstance.call(this, property); }; diff --git a/src/game-lib-d3-api-raycaster.js b/src/game-lib-d3-api-raycaster.js index ed61c2b..0a4c3e8 100644 --- a/src/game-lib-d3-api-raycaster.js +++ b/src/game-lib-d3-api-raycaster.js @@ -10,9 +10,9 @@ GameLib.D3.API.Raycaster = function( id, name, + parentEntity, position, - direction, - parentEntity + direction ) { 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.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 - ); -}; diff --git a/src/game-lib-d3-raycaster.js b/src/game-lib-d3-raycaster.js index 5c44b88..60197f8 100644 --- a/src/game-lib-d3-raycaster.js +++ b/src/game-lib-d3-raycaster.js @@ -20,6 +20,7 @@ GameLib.D3.Raycaster = function( this, apiRaycaster.id, apiRaycaster.name, + apiRaycaster.parentEntity, apiRaycaster.position, apiRaycaster.direction ); @@ -46,6 +47,7 @@ GameLib.D3.Raycaster.prototype.constructor = GameLib.D3.Raycaster; * Creates or updates a raycaster instance */ GameLib.D3.Raycaster.prototype.createInstance = function() { + this.instance = new THREE.Raycaster(); this.instance.set( this.position.instance, @@ -55,44 +57,37 @@ GameLib.D3.Raycaster.prototype.createInstance = function() { 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; - this.position.instance.y = this.position.y; - this.position.instance.z = this.position.z; + if (property === 'position') { + this.position.instance.x = this.position.x; + 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; - this.direction.instance.y = this.direction.y; - this.direction.instance.z = this.direction.z; + if (property === 'direction') { + this.direction.instance.x = this.direction.x; + this.direction.instance.y = this.direction.y; + this.direction.instance.z = this.direction.z; + this.instance.setDirection(this.direction.instance); + return; + } - this.instance.set( - this.position.instance, - this.direction.instance - ); + GameLib.Component.prototype.updateInstance.call(this, property); }; GameLib.D3.Raycaster.prototype.toApiObject = function() { return new GameLib.D3.API.Raycaster( this.id, this.name, + GameLib.Utils.IdOrNull(this.parentEntity), this.position.toApiObject(), - this.direction.toApiObject(), - GameLib.Utils.IdOrNull(this.parentEntity) + this.direction.toApiObject() ) }; -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 * @param position GameLib.Vector3 @@ -191,34 +186,8 @@ GameLib.D3.Raycaster.prototype.getIntersectedObjects = function(meshes) { }.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 * @param mesh GameLib.D3.Mesh @@ -274,4 +243,4 @@ GameLib.D3.Raycaster.prototype.getIntersectPoint = function(mesh) { } return point; -}; \ No newline at end of file +}; diff --git a/src/game-lib-mouse.js b/src/game-lib-mouse.js index a693dcb..7406df4 100644 --- a/src/game-lib-mouse.js +++ b/src/game-lib-mouse.js @@ -14,9 +14,9 @@ GameLib.Mouse = function (apiMouse) { this, apiMouse.id, apiMouse.name, + apiMouse.parentEntity, apiMouse.x, - apiMouse.y, - apiMouse.parentEntity + apiMouse.y ); GameLib.Component.call(this); @@ -60,8 +60,8 @@ GameLib.Mouse.prototype.toApiObject = function() { return new GameLib.API.Mouse( this.id, this.name, + GameLib.Utils.IdOrNull(this.parentEntity), this.x, - this.y, - this.parentEntity + this.y ); }; diff --git a/src/game-lib-system-custom-code.js b/src/game-lib-system-custom-code.js index 70c2273..c5187bc 100644 --- a/src/game-lib-system-custom-code.js +++ b/src/game-lib-system-custom-code.js @@ -16,6 +16,7 @@ GameLib.System.CustomCode = function( this.removeComponentSubscription = null; this.compileSuccessSubscription = null; this.compileFailedSubscription = null; + this.eventIdUpdateSubscription = null; this.subscriptions = {}; @@ -60,6 +61,10 @@ GameLib.System.CustomCode.prototype.start = function() { 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; } + if (this.eventIdUpdateSubscription) { + this.eventIdUpdateSubscription.remove(); + this.eventIdUpdateSubscription = null; + } + Object.keys(this.subscriptions).map( function(componentId) { if (this.subscriptions[componentId]) { diff --git a/src/game-lib-system-input.js b/src/game-lib-system-input.js index a9f9515..9dea43e 100644 --- a/src/game-lib-system-input.js +++ b/src/game-lib-system-input.js @@ -913,7 +913,7 @@ GameLib.System.Input.prototype.onKeyUp = function(event) { GameLib.Event.Emit( 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 */ GameLib.System.Input.prototype.onMouseMoveEdit = function(event) { + GameLib.EntityManager.Instance.queryComponents(GameLib.Component.MOUSE).map( function(mouse) { mouse.x = event.clientX; mouse.y = event.clientY; } - ) + ); this.editorControls.map( function(editorControl) { diff --git a/src/game-lib-system-render.js b/src/game-lib-system-render.js index 8506a2c..cc3b02d 100644 --- a/src/game-lib-system-render.js +++ b/src/game-lib-system-render.js @@ -235,22 +235,23 @@ GameLib.System.Render.prototype.windowResize = function(data) { } ); - var renderers = GameLib.EntityManager.Instance.queryComponents(GameLib.Component.RENDERER); - - renderers.map( + GameLib.EntityManager.Instance.queryComponents(GameLib.Component.RENDERER).map( function(renderer) { renderer.setSize(data.width, data.height); } ); - var effects = GameLib.EntityManager.Instance.queryComponentsByConstructor(GameLib.D3.Effect); - - effects.map( + GameLib.EntityManager.Instance.queryComponentsByConstructor(GameLib.D3.Effect).map( function(effect){ 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( function(camera){