From 88ab104da4fd2db25f87c3a46eabd1cebaba13b2 Mon Sep 17 00:00:00 2001 From: photonstorm Date: Wed, 18 Feb 2015 22:36:12 +0000 Subject: [PATCH] BitmapText has a new `maxWidth` property that will attempt to wrap the text if it exceeds the width specified. --- README.md | 1 + src/pixi/text/BitmapText.js | 31 +++++++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 014276390..4b04a47b5 100644 --- a/README.md +++ b/README.md @@ -151,6 +151,7 @@ We've also removed functions and properties from Pixi classes that Phaser doesn' * InputHandler.enableDrag with a boundsSprite set now takes into account both the Sprites anchor and the boundsSprite anchor when limiting the drag. * Sound in Web Audio now uses AudioContext.onended to trigger when it will stop playing instead of using a time based value. This is only used if the sound doesn't loop and isn't an audio sprite, but will give a much more accurate `Sound.onStop` event. It also prevents short audio files from being cut off during playback (#1471) and accounts for time spent decoding. * If you load an image and provide a key that was already in-use in the Cache, then the old image is now destroyed (via `Cache.removeImage`) and the new image takes its place. +* BitmapText has a new `maxWidth` property that will attempt to wrap the text if it exceeds the width specified. ### Bug Fixes diff --git a/src/pixi/text/BitmapText.js b/src/pixi/text/BitmapText.js index a1d20af26..6e7f7d2e4 100644 --- a/src/pixi/text/BitmapText.js +++ b/src/pixi/text/BitmapText.js @@ -40,6 +40,15 @@ PIXI.BitmapText = function(text, style) */ this.textHeight = 0; + /** + * The max width of this bitmap text in pixels. If the text provided is longer than the value provided, line breaks will be + * automatically inserted in the last whitespace. Disable by setting value to 0. + * + * @property maxWidth + * @type Number + */ + this.maxWidth = 0; + /** * @property _pool * @type Array @@ -113,12 +122,14 @@ PIXI.BitmapText.prototype.updateText = function() var lineWidths = []; var line = 0; var scale = this.fontSize / data.size; + var lastSpace = 0; - for(var i = 0; i < this.text.length; i++) + for (var i = 0; i < this.text.length; i++) { var charCode = this.text.charCodeAt(i); + lastSpace = /(\s)/.test(this.text.charAt(i)) ? i : lastSpace; - if(/(?:\r\n|\r|\n)/.test(this.text.charAt(i))) + if (/(?:\r\n|\r|\n)/.test(this.text.charAt(i))) { lineWidths.push(pos.x); maxLineWidth = Math.max(maxLineWidth, pos.x); @@ -130,6 +141,22 @@ PIXI.BitmapText.prototype.updateText = function() continue; } + if (lastSpace !== -1 && this.maxWidth > 0 && pos.x * scale > this.maxWidth) + { + chars.splice(lastSpace, i - lastSpace); + i = lastSpace; + lastSpace = -1; + + lineWidths.push(lastLineWidth); + maxLineWidth = Math.max(maxLineWidth, lastLineWidth); + line++; + + pos.x = 0; + pos.y += data.lineHeight; + prevCharCode = null; + continue; + } + var charData = data.chars[charCode]; if(!charData) continue;