text components

beta.r3js.org
-=yb4f310 2018-01-15 20:16:47 +01:00
parent 40d66dd5db
commit a42947d7f5
9 changed files with 255 additions and 28 deletions

View File

@ -219,7 +219,7 @@ GameLib.Component.RENDER_TARGET = 0xc;
GameLib.Component.PASS = 0xd; GameLib.Component.PASS = 0xd;
GameLib.Component.SCENE = 0xe; GameLib.Component.SCENE = 0xe;
GameLib.Component.RAYCASTER = 0xf; GameLib.Component.RAYCASTER = 0xf;
//GameLib.Component.INPUT_EDITOR = 0x10; GameLib.Component.TEXT = 0x10;
//GameLib.Component.EDITOR = 0x11; //GameLib.Component.EDITOR = 0x11;
GameLib.Component.VIEWPORT = 0x12; GameLib.Component.VIEWPORT = 0x12;
GameLib.Component.SYSTEM = 0x13; GameLib.Component.SYSTEM = 0x13;
@ -399,7 +399,12 @@ GameLib.Component.GetComponentInfo = function(number) {
constructor : GameLib.D3.Raycaster, constructor : GameLib.D3.Raycaster,
apiConstructor : GameLib.D3.API.Raycaster apiConstructor : GameLib.D3.API.Raycaster
}; };
case 0x10 : return null; case 0x10 : return {
name : 'GameLib.D3.Text',
runtime : GameLib.Component.GRAPHICS_RUNTIME,
constructor : GameLib.D3.Text,
apiConstructor : GameLib.D3.API.Text
};
case 0x11 : return null; case 0x11 : return null;
case 0x12 : return { case 0x12 : return {
name : 'GameLib.D3.Viewport', name : 'GameLib.D3.Viewport',

View File

@ -4,6 +4,8 @@
* @param name * @param name
* @param width * @param width
* @param height * @param height
* @param texts
* @param parentTexture
* @param parentEntity * @param parentEntity
* @constructor * @constructor
*/ */
@ -12,6 +14,8 @@ GameLib.API.Canvas = function(
name, name,
width, width,
height, height,
texts,
parentTexture,
parentEntity parentEntity
) { ) {
if (GameLib.Utils.UndefinedOrNull(id)) { if (GameLib.Utils.UndefinedOrNull(id)) {
@ -34,6 +38,16 @@ GameLib.API.Canvas = function(
} }
this.height = height; this.height = height;
if (GameLib.Utils.UndefinedOrNull(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,
@ -43,18 +57,3 @@ GameLib.API.Canvas = function(
GameLib.API.Canvas.prototype = Object.create(GameLib.API.Component.prototype); GameLib.API.Canvas.prototype = Object.create(GameLib.API.Component.prototype);
GameLib.API.Canvas.prototype.constructor = GameLib.API.Canvas; GameLib.API.Canvas.prototype.constructor = GameLib.API.Canvas;
/**
* Returns an API light from an Object light
* @param objectCanvas
* @constructor
*/
GameLib.API.Canvas.FromObject = function(objectCanvas) {
return new GameLib.API.Canvas(
objectCanvas.id,
objectCanvas.name,
objectCanvas.width,
objectCanvas.height,
objectCanvas.parentEntity
);
};

View File

@ -17,10 +17,18 @@ GameLib.Canvas = function(
apiCanvas.name, apiCanvas.name,
apiCanvas.width, apiCanvas.width,
apiCanvas.height, apiCanvas.height,
apiCanvas.texts,
apiCanvas.parentTexture,
apiCanvas.parentEntity apiCanvas.parentEntity
); );
GameLib.Component.call(this); GameLib.Component.call(
this,
{
'parentTexture' : GameLib.D3.Texture,
'texts' : [GameLib.D3.Text]
}
);
}; };
GameLib.Canvas.prototype = Object.create(GameLib.Component.prototype); GameLib.Canvas.prototype = Object.create(GameLib.Component.prototype);
@ -44,6 +52,8 @@ GameLib.Canvas.prototype.createInstance = function() {
this.instance.width = this.width; this.instance.width = this.width;
this.instance.height = this.height; this.instance.height = this.height;
this.writeText();
GameLib.Component.prototype.createInstance.call(this); GameLib.Component.prototype.createInstance.call(this);
}; };
@ -69,6 +79,10 @@ GameLib.Canvas.prototype.updateInstance = function(property) {
this.height = Math.round(this.height); this.height = Math.round(this.height);
this.instance.height = this.height; this.instance.height = this.height;
} }
if (property === 'texts') {
this.writeText();
}
}; };
/** /**
@ -81,15 +95,35 @@ GameLib.Canvas.prototype.toApiObject = function() {
this.name, this.name,
this.width, this.width,
this.height, this.height,
this.texts.map(function(text){
return GameLib.Utils.IdOrNull(text)
}),
GameLib.Utils.IdOrNull(this.parentTexture),
GameLib.Utils.IdOrNull(this.parentEntity) GameLib.Utils.IdOrNull(this.parentEntity)
); );
}; };
/** GameLib.Canvas.prototype.writeText = function() {
* Returns a new GameLib.Canvas from a GameLib.API.Canvas
* @param objectCanvas GameLib.API.Canvas if (this.texts.length > 0) {
* @returns {GameLib.Canvas} var context = this.instance.getContext('2d');
*/ context.textBaseline = "middle";
GameLib.Canvas.FromObject = function(objectCanvas) { context.clearRect(0, 0, this.width, this.height);
return new GameLib.Canvas(objectCanvas); }
this.texts.map(
function(text){
text.parentCanvas = this;
context.fillStyle = text.fillStyle;
context.font = text.font;
context.fillText(text.value, text.offset.x, text.offset.y);
if (this.parentTexture && this.parentTexture.instance) {
this.parentTexture.instance.needsUpdate = true;
}
}.bind(this)
);
}; };

View File

@ -0,0 +1,66 @@
/**
* Raw Text API object - should always correspond with the Text Schema
* @param id
* @param name
* @param offset
* @param font
* @param fillStyle
* @param value
* @param parentCanvas
* @param parentEntity
* @constructor
*/
GameLib.D3.API.Text = function(
id,
name,
offset,
font,
fillStyle,
value,
parentCanvas,
parentEntity
) {
if (GameLib.Utils.UndefinedOrNull(id)) {
id = GameLib.Utils.RandomId();
}
this.id = id;
if (GameLib.Utils.UndefinedOrNull(name)) {
name = 'Text (' + id + ')';
}
this.name = name;
if (GameLib.Utils.UndefinedOrNull(offset)) {
offset = new GameLib.API.Vector2(0,0);
}
this.offset = offset;
if (GameLib.Utils.UndefinedOrNull(font)) {
font = '10pt Arial';
}
this.font = font;
if (GameLib.Utils.UndefinedOrNull(fillStyle)) {
fillStyle = '#ffffff';
}
this.fillStyle = fillStyle;
if (GameLib.Utils.UndefinedOrNull(value)) {
value = 'Hello';
}
this.value = value;
if (GameLib.Utils.UndefinedOrNull(parentCanvas)) {
parentCanvas = null;
}
this.parentCanvas = parentCanvas;
GameLib.API.Component.call(
this,
GameLib.Component.TEXT,
parentEntity
);
};
GameLib.D3.API.Text.prototype = Object.create(GameLib.API.Component.prototype);
GameLib.D3.API.Text.prototype.constructor = GameLib.D3.API.Text;

View File

@ -248,11 +248,12 @@ GameLib.D3.Mesh.Plane.prototype.generateDotMap = function() {
} }
var dot = new THREE.Mesh(geometry, material); var dot = new THREE.Mesh(geometry, material);
dot.name = 'dot ' + this.dots.length;
dot.position.x = x; dot.position.x = x;
dot.position.y = y; dot.position.y = y;
dot.position.z = data[(y * width) + x]; dot.position.z = data[(y * width) + x];
var scale = data[(y * width) + x] / 100; var scale = data[(y * width) + x] / 100 + 0.00001;
dot.scale.x = scale; dot.scale.x = scale;
dot.scale.y = scale; dot.scale.y = scale;

94
src/game-lib-d3-text.js Normal file
View File

@ -0,0 +1,94 @@
/**
* Text object
* @param graphics
* @param apiText
* @returns {GameLib.D3.Text}
* @constructor
*/
GameLib.D3.Text = function(
graphics,
apiText
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
if (GameLib.Utils.UndefinedOrNull(apiText)) {
apiText = {};
}
GameLib.D3.API.Text.call(
this,
apiText.id,
apiText.name,
apiText.offset,
apiText.font,
apiText.fillStyle,
apiText.value,
apiText.parentCanvas,
apiText.parentEntity
);
this.offset = new GameLib.Vector2(
this.graphics,
this.offset,
this
);
GameLib.Component.call(this);
};
GameLib.D3.Text.prototype = Object.create(GameLib.Component.prototype);
GameLib.D3.Text.prototype.constructor = GameLib.D3.Text;
/**
* Creates a light instance
* @returns {*}
*/
GameLib.D3.Text.prototype.createInstance = function() {
this.instance = true;
GameLib.Component.prototype.createInstance.call(this);
};
/**
* Updates the instance with the current state
*/
GameLib.D3.Text.prototype.updateInstance = function(property) {
if (GameLib.Utils.UndefinedOrNull(property)) {
console.warn('unknown property update for Text: ' + property);
}
if (
property === 'offset' ||
property === 'font' ||
property === 'fillStyle' ||
property === 'value'
) {
if (!this.parentCanvas) {
console.warn('no parent canvas set');
return;
}
this.parentCanvas.updateInstance('texts');
}
};
/**
* Converts a GameLib.D3.Text to a GameLib.D3.API.Text
* @returns {GameLib.D3.API.Text}
*/
GameLib.D3.Text.prototype.toApiObject = function() {
return new GameLib.D3.API.Text(
this.id,
this.name,
this.offset.toApiObject(),
this.font,
this.fillStyle,
this.value,
GameLib.Utils.IdOrNull(this.parentCanvas),
GameLib.Utils.IdOrNull(this.parentEntity)
);
};

View File

@ -161,6 +161,8 @@ GameLib.D3.Texture.prototype.createInstance = function() {
throw new Error('no canvas instance'); throw new Error('no canvas instance');
} }
this.canvas.parentTexture = this;
this.instance = new THREE.Texture( this.instance = new THREE.Texture(
this.canvas.instance this.canvas.instance
); );
@ -212,7 +214,8 @@ GameLib.D3.Texture.prototype.updateInstance = function(property) {
} }
if ( if (
property === 'textureType' || property === 'textureType' ||
property === 'canvas' ||
property === 'image' property === 'image'
) { ) {
this.createInstance(); this.createInstance();

View File

@ -616,6 +616,8 @@ GameLib.System.GUI.prototype.buildArrayManagerControl = function(
} }
); );
component.updateInstance(property);
// addArrayItem(activeSelection.component, component[property].length - 1); // addArrayItem(activeSelection.component, component[property].length - 1);
} }
}); });
@ -1422,7 +1424,7 @@ GameLib.System.GUI.prototype.buildControl = function(folder, componentTemplate,
property === 'width' || property === 'width' ||
property === 'height' property === 'height'
) { ) {
controllers.push(folder.add(object, property, 0, 1000, 0.001)); controllers.push(folder.add(object, property, 0, 4096, 0.001));
} else if ( } else if (
property === 'depth' || property === 'depth' ||
property === 'radius' property === 'radius'

View File

@ -145,6 +145,29 @@ GameLib.System.Render.prototype.instanceCreated = function(data) {
if (data.component instanceof GameLib.Stats) { if (data.component instanceof GameLib.Stats) {
this.statistics.push(data.component); this.statistics.push(data.component);
} }
/**
* Link Parent Textures to Canvas Objects
*/
if (data.component instanceof GameLib.D3.Texture) {
GameLib.EntityManager.Instance.queryComponents(GameLib.Component.CANVAS).map(
function(canvas){
if (canvas.parentTexture === data.component.id) {
canvas.parentTexture = data.component;
}
}
)
}
if (data.component instanceof GameLib.Canvas) {
GameLib.EntityManager.Instance.queryComponents(GameLib.Component.TEXTURE).map(
function(texture){
if (data.component.parentTexture === texture.id) {
data.component.parentTexture = texture;
}
}
)
}
}; };
/** /**