r3-legacy/src/r3-d3-text.js

116 lines
2.5 KiB
JavaScript

/**
* Text object
* @param graphics
* @param apiText
* @returns {R3.D3.Text}
* @constructor
*/
R3.D3.Text = function(
graphics,
apiText
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
if (R3.Utils.UndefinedOrNull(apiText)) {
apiText = {};
}
R3.D3.API.Text.call(
this,
apiText.id,
apiText.name,
apiText.offset,
apiText.font,
apiText.fillStyle,
apiText.value,
apiText.parentCanvas,
apiText.parentEntity
);
this.offset = new R3.Vector2(
this.graphics,
this.offset,
this
);
R3.Component.call(this);
};
R3.D3.Text.prototype = Object.create(R3.Component.prototype);
R3.D3.Text.prototype.constructor = R3.D3.Text;
/**
* Creates a light instance
* @returns {*}
*/
R3.D3.Text.prototype.createInstance = function() {
this.instance = true;
R3.Component.prototype.createInstance.call(this);
};
/**
* Updates the instance with the current state
*/
R3.D3.Text.prototype.updateInstance = function(property) {
if (R3.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');
}
if (property === 'parentCanvas') {
R3.EntityManager.Instance.queryComponents(R3.Component.CANVAS).map(
function(canvas) {
var index = canvas.texts.indexOf(this);
if (index !== -1) {
canvas.texts.splice(index, 1);
canvas.texts.updateInstance('texts');
}
}.bind(this)
);
if (this.parentCanvas) {
R3.Utils.PushUnique(this.parentCanvas.texts, this);
this.parentCanvas.updateInstance('texts');
}
}
R3.Component.prototype.updateInstance.call(this, property);
};
/**
* Converts a R3.D3.Text to a R3.D3.API.Text
* @returns {R3.D3.API.Text}
*/
R3.D3.Text.prototype.toApiObject = function() {
return new R3.D3.API.Text(
this.id,
this.name,
this.offset.toApiObject(),
this.font,
this.fillStyle,
this.value,
R3.Utils.IdOrNull(this.parentCanvas),
R3.Utils.IdOrNull(this.parentEntity)
);
};