1 decade ago by riceje7
so i needed a way to dynamically display text within certain boundaries, and as there is no current built-in word-wrap functionality or much in the way of formatting text at all i decided to create a small plugin to auto-wrap text instead of having to manually insert line breaks into the text to be displayed.
my problem is that the string returned by the wrapper doesn&
it works by passing a user defined font and a &
I&
my problem is that the string returned by the wrapper doesn&
039;t execute line breaks in the displayed font (#font.draw()##). it does however display them correctly in the console when the string is logged. here is what i have for the plugin in so far: CODE:
ig.module('game.plugins.WordWrapper').requires('impact.font').defines(function() {
WordWrapper = ig.Class.extend({
font: null,
box: null,
init: function(f, b) {
this.font = f;
this.box = b;
},
wrapMessage: function(message) {
var words = message.split(" ");
var wordWidths = this.getWordWidths(words);
var width = this.box.w;
var newLine = "\n";
var space = " ";
var spaceWidth = this.font.widthForString(" ");
var widthCounter = 0;
var newMessage = "";
for (var i = 0; i < wordWidths.length; i++) {
if (widthCounter + wordWidths[i] + spaceWidth <= width) {
widthCounter += wordWidths[i] + spaceWidth;
newMessage += words[i] + space;
} else {
widthCounter = 0;
widthCounter += wordWidths[i] + spaceWidth;
newMessage += newLine + words[i] + space;
}
}
return newMessage;
},
getWordWidths: function(wordsArr) {
var arr = new Array();
for (var i = 0; i < wordsArr.length; i++) {
arr.push(this.font.widthForString(wordsArr[i]));
}
return arr;
}
});
});
it works by passing a user defined font and a &
039;box' object which has at least a value for the key #w ({w: 100}) for the width of the bounding box. and then in the ##wrapMessage(message)## function the message parameter is split into words delimited by spaces, then an array of the lengths of each word is created. then using ##font.widthForString(text)## and a counter words and spaces are added to a string to be returned in succession until the counter + word + space is greater than the bounding box&039;s width at which point the counter is reset, a #line break is added to the string followed by the word + space that would have made the counter greater than the box's width. and the process continues until the words array has been fully traversed.I&
039;m not sure what is being lost in translation, because like i said the returned string's line breaks display correctly in the console, just not when using ##font.draw(text, x, y)# and i know that line breaks are working in impact because if i hard code the line breaks into the string and don't process it through the WordWrapper then they display correctly. so i know it is something to do with my code just not sure what exactly i'm doing wrong. any help would be greatly appreciated. thanks 