phaser/v3/src/gameobjects/bitmaptext/static/BitmapText.js

133 lines
2.7 KiB
JavaScript
Raw Normal View History

2017-03-08 22:36:53 +00:00
var Class = require('../../../utils/Class');
var Components = require('../../components');
var GameObject = require('../../GameObject');
2017-03-08 22:36:53 +00:00
var GetBitmapTextSize = require('../GetBitmapTextSize');
var ParseFromAtlas = require('../ParseFromAtlas');
var ParseRetroFont = require('../ParseRetroFont');
var Render = require('./BitmapTextRender');
var BitmapText = new Class({
2017-04-05 01:10:48 +00:00
Extends: GameObject,
Mixins: [
Components.Alpha,
Components.BlendMode,
Components.Origin,
Components.RenderTarget,
Components.ScaleMode,
Components.Texture,
Components.Transform,
Components.Visible,
2017-06-22 02:19:03 +00:00
Components.ScrollFactor,
Render
],
initialize:
function BitmapText (state, x, y, font, text, size)
{
if (text === undefined) { text = ''; }
GameObject.call(this, state, 'BitmapText');
this.font = font;
var entry = this.state.sys.cache.bitmapFont.get(font);
this.fontData = entry.data;
this.text = (Array.isArray(text)) ? text.join('\n') : text;
2017-03-01 17:11:51 +00:00
this.fontSize = size || this.fontData.size;
this.setTexture(entry.texture, entry.frame);
this.setPosition(x, y);
this.setOrigin(0, 0);
2017-07-12 16:32:45 +00:00
this._bounds = this.getTextBounds();
2017-03-01 17:11:51 +00:00
},
setFontSize: function (size)
{
this.fontSize = size;
return this;
},
2017-03-01 17:11:51 +00:00
setText: function (text)
{
this.text = text;
2017-03-01 17:11:51 +00:00
return this;
},
// {
// local: {
// x,
// y,
// width,
// height
// },
// global: {
// x,
// y,
// width,
// height
// }
// }
2017-07-12 16:32:45 +00:00
getTextBounds: function (round)
{
// local = the BitmapText based on fontSize and 0x0 coords
// global = the BitmapText, taking into account scale and world position
2017-07-12 16:32:45 +00:00
this._bounds = GetBitmapTextSize(this, round);
return this._bounds;
},
width: {
get: function ()
{
this.getTextBounds(false);
return this._bounds.global.width;
}
},
height: {
get: function ()
{
this.getTextBounds(false);
return this._bounds.global.height;
}
},
toJSON: function ()
{
var out = Components.ToJSON(this);
// Extra data is added here
var data = {
font: this.font,
text: this.text,
fontSize: this.fontSize
};
out.data = data;
return out;
}
});
BitmapText.ParseRetroFont = ParseRetroFont;
BitmapText.ParseFromAtlas = ParseFromAtlas;
module.exports = BitmapText;