const SpriteText = require('three-spritetext').default;
/**
* Bitmap labels, this is used with {@link Glyph} to
* provide labels.
*
* @param {String} textIn - Text to be displayed dwith the label.
* @param {THREE.Color} colour - Colour to be set for the label.
*
* @class
* @author Alan Wu
* @return {Label}
*/
exports.Label = function (textIn, colourIn) {
let text = textIn;
let sprite = undefined;
const position = [0, 0, 0];
let colour = colourIn;
let size = 1.0;
let fontWeight = 500;
if (colourIn)
sprite = new SpriteText(text, 0.012, colourIn.getStyle());
else
sprite = new SpriteText(text, 0.012);
sprite.fontFace = "Asap";
sprite.fontWeight = fontWeight;
sprite.material.map.generateMipmaps = false;
sprite.material.sizeAttenuation = false;
sprite.center.x = -0.05;
sprite.center.y = 0;
const originalScale = [sprite.scale.x, sprite.scale.y];
/**
* Get the current position in an array containing the x, y and z
* coordinates.
*
* @return {Array}
*/
this.getPosition = () => {
if (sprite)
return [sprite.position.x, sprite.position.y, sprite.position.z];
return [0, 0, 0];
}
/**
* Set the position of the label in 3D coordinates.
*
* @param {Number} x - x coordinate to be set.
* @param {Number} y - y coordinate to be set.
* @param {Number} z - z coordinate to be set.
*/
this.setPosition = (x, y, z) => {
position[0] = x;
position[1] = y;
position[2] = z;
if (sprite) {
sprite.position.set(x, y, z);
}
}
/**
* Set the colour of the label
*
* @param {THREE.Color} colour - colour to be set
*/
this.setColour = colourIn => {
if (colourIn) {
sprite.color = colourIn.getStyle();
colour = colourIn;
}
}
/**
* Scale the label.
*
* @param {Number} scaling - Scale to be set.
*/
this.setScale = scaling => {
if (sprite && scaling > 0.0)
sprite.scale.set(scaling, scaling, 1.0);
}
/**
* Set depth test for sprite object.
*
* @param {Boolean} flag - Enable/disable depth test
*/
this.setDepthTest = flag => {
if (flag && flag !== sprite.material.depthTest) {
sprite.material.depthTest = flag;
}
}
/**
* Set a new text for the label.
*
* @param {Number} scaling - Scale to be set.
*/
this.setSize = sizeIn => {
if (sizeIn > 0.0) {
sprite.scale.x = originalScale[0] * sizeIn;
sprite.scale.y = originalScale[1] * sizeIn;
size = sizeIn;
}
}
/**
* Set a new text for the label.
*
* @param {Number} scaling - Scale to be set.
*/
this.setFontWeight = fontWeightIn => {
if (fontWeightIn && fontWeightIn !== fontWeight) {
sprite.fontWeight = fontWeightIn;
fontWeight = fontWeightIn;
}
}
/**
* Set a new text for the label.
*
* @param {Number} scaling - Scale to be set.
*/
this.setText = textIn => {
if (textIn && textIn !== sprite.text) {
sprite.text = textIn;
text = textIn;
}
}
/**
* Free up the memory
*/
this.dispose = () => {
//sprite.dispose();
}
/**
* Get the intrnal sprite.
*
* @return {THREE.Sprite}
*/
this.getSprite = () => {
return sprite;
}
/**
* Get the text.
*
* @return {String}
*/
this.getString = () => {
return text;
}
};