Skip to content

Commit 58b00a1

Browse files
committed
Make word wrap functions more pure so they are easier to share with other GOs later
1 parent d003945 commit 58b00a1

1 file changed

Lines changed: 9 additions & 10 deletions

File tree

  • v3/src/gameobjects/text/static

v3/src/gameobjects/text/static/Text.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ var Text = new Class({
129129
* bounds.
130130
*
131131
* @param {string} text - The text to perform word wrap detection against.
132+
* @return {string} The text after wrapping has been applied.
132133
*/
133134
runWordWrap: function (text)
134135
{
@@ -146,11 +147,11 @@ var Text = new Class({
146147
{
147148
if (style.wordWrapUseAdvanced)
148149
{
149-
return this.advancedWordWrap(text);
150+
return this.advancedWordWrap(text, this.context, this.style.wordWrapWidth);
150151
}
151152
else
152153
{
153-
return this.basicWordWrap(text);
154+
return this.basicWordWrap(text, this.context, this.style.wordWrapWidth);
154155
}
155156
}
156157
else
@@ -167,10 +168,8 @@ var Text = new Class({
167168
*
168169
* @param {string} text - The text to perform word wrap detection against.
169170
*/
170-
advancedWordWrap: function (text)
171+
advancedWordWrap: function (text, context, wordWrapWidth)
171172
{
172-
var context = this.context;
173-
var wordWrapWidth = this.style.wordWrapWidth;
174173
var output = '';
175174

176175
// condense consecutive spaces and split into lines
@@ -282,20 +281,20 @@ var Text = new Class({
282281
*
283282
* @param {string} text - The text to perform word wrap detection against.
284283
*/
285-
basicWordWrap: function (text)
284+
basicWordWrap: function (text, context, wordWrapWidth)
286285
{
287286
var result = '';
288287
var lines = text.split('\n');
289288

290289
for (var i = 0; i < lines.length; i++)
291290
{
292-
var spaceLeft = this.style.wordWrapWidth;
291+
var spaceLeft = wordWrapWidth;
293292
var words = lines[i].split(' ');
294293

295294
for (var j = 0; j < words.length; j++)
296295
{
297-
var wordWidth = this.context.measureText(words[j]).width;
298-
var wordWidthWithSpace = wordWidth + this.context.measureText(' ').width;
296+
var wordWidth = context.measureText(words[j]).width;
297+
var wordWidthWithSpace = wordWidth + context.measureText(' ').width;
299298

300299
if (wordWidthWithSpace > spaceLeft)
301300
{
@@ -306,7 +305,7 @@ var Text = new Class({
306305
result += '\n';
307306
}
308307
result += words[j] + ' ';
309-
spaceLeft = this.style.wordWrapWidth - wordWidth;
308+
spaceLeft = wordWrapWidth - wordWidth;
310309
}
311310
else
312311
{

0 commit comments

Comments
 (0)