Skip to content

Commit 2e7a336

Browse files
committed
Handling new style settings for fonts and padding.
1 parent 5e975ed commit 2e7a336

1 file changed

Lines changed: 92 additions & 19 deletions

File tree

  • v3/src/gameobjects/text/static

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

Lines changed: 92 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var Class = require('../../../utils/Class');
44
var Components = require('../../components');
55
var GameObject = require('../../GameObject');
66
var GetTextSize = require('../GetTextSize');
7+
var GetValue = require('../../../utils/object/GetValue');
78
var RemoveFromDOM = require('../../../dom/RemoveFromDOM');
89
var TextRender = require('./TextRender');
910
var TextStyle = require('../TextStyle');
@@ -68,10 +69,10 @@ var Text = new Class({
6869

6970
/**
7071
* Specify a padding value which is added to the line width and height when calculating the Text size.
71-
* Allows you to add extra spacing if Phaser is unable to accurately determine the true font dimensions.
72+
* Allows you to add extra spacing if the browser is unable to accurately determine the true font dimensions.
7273
* @property {Phaser.Point} padding
7374
*/
74-
this.padding = { x: 0, y: 0 };
75+
this.padding = { left: 0, right: 0, top: 0, bottom: 0 };
7576

7677
this.width = 1;
7778
this.height = 1;
@@ -81,6 +82,11 @@ var Text = new Class({
8182

8283
this.initRTL();
8384

85+
if (style && style.padding)
86+
{
87+
this.setPadding(style.padding);
88+
}
89+
8490
this.setText(text);
8591

8692
var _this = this;
@@ -145,6 +151,21 @@ var Text = new Class({
145151
return this.style.setFont(font);
146152
},
147153

154+
setFontFamily: function (family)
155+
{
156+
return this.style.setFontFamily(family);
157+
},
158+
159+
setFontSize: function (size)
160+
{
161+
return this.style.setFontSize(size);
162+
},
163+
164+
setFontStyle: function (style)
165+
{
166+
return this.style.setFontStyle(style);
167+
},
168+
148169
setFixedSize: function (width, height)
149170
{
150171
return this.style.setFixedSize(width, height);
@@ -155,9 +176,9 @@ var Text = new Class({
155176
return this.style.setBackgroundColor(color);
156177
},
157178

158-
setFill: function (color)
179+
setColor: function (color)
159180
{
160-
return this.style.setFill(color);
181+
return this.style.setColor(color);
161182
},
162183

163184
setStroke: function (color, thickness)
@@ -200,6 +221,59 @@ var Text = new Class({
200221
return this.style.setAlign(align);
201222
},
202223

224+
// 'left' can be an object
225+
// if only 'left' and 'top' are given they are treated as 'x' and 'y'
226+
setPadding: function (left, top, right, bottom)
227+
{
228+
if (typeof left === 'object')
229+
{
230+
var config = left;
231+
232+
// If they specify x and/or y this applies to all
233+
var x = GetValue(config, 'x', null);
234+
235+
if (x !== null)
236+
{
237+
left = x;
238+
right = x;
239+
}
240+
else
241+
{
242+
left = GetValue(config, 'left', 0);
243+
right = GetValue(config, 'right', left);
244+
}
245+
246+
var y = GetValue(config, 'y', null);
247+
248+
if (y !== null)
249+
{
250+
top = y;
251+
bottom = y;
252+
}
253+
else
254+
{
255+
top = GetValue(config, 'top', 0);
256+
bottom = GetValue(config, 'bottom', top);
257+
}
258+
}
259+
else
260+
{
261+
if (left === undefined) { left = 0; }
262+
if (top === undefined) { top = left; }
263+
if (right === undefined) { right = left; }
264+
if (bottom === undefined) { bottom = top; }
265+
}
266+
267+
this.padding.left = left;
268+
this.padding.top = top;
269+
this.padding.right = right;
270+
this.padding.bottom = bottom;
271+
272+
console.log(this.padding);
273+
274+
return this.updateText();
275+
},
276+
203277
setMaxLines: function (max)
204278
{
205279
return this.style.setMaxLines(max);
@@ -238,8 +312,8 @@ var Text = new Class({
238312

239313
var padding = this.padding;
240314

241-
var w = (textSize.width + (padding.x * 2)) * this.resolution;
242-
var h = (textSize.height + (padding.y * 2)) * this.resolution;
315+
var w = (textSize.width + padding.left + padding.right) * this.resolution;
316+
var h = (textSize.height + padding.top + padding.bottom) * this.resolution;
243317

244318
if (canvas.width !== w || canvas.height !== h)
245319
{
@@ -262,7 +336,7 @@ var Text = new Class({
262336
context.textBaseline = 'alphabetic';
263337

264338
// Apply padding
265-
context.translate(padding.x, padding.y);
339+
context.translate(padding.left, padding.top);
266340

267341
var linePositionX;
268342
var linePositionY;
@@ -282,16 +356,13 @@ var Text = new Class({
282356
{
283357
linePositionX = w - linePositionX;
284358
}
285-
else
359+
else if (style.align === 'right')
360+
{
361+
linePositionX += textSize.width - textSize.lineWidths[i];
362+
}
363+
else if (style.align === 'center')
286364
{
287-
if (style.align === 'right')
288-
{
289-
linePositionX += textSize.width - textSize.lineWidths[i];
290-
}
291-
else if (style.align === 'center')
292-
{
293-
linePositionX += (textSize.width - textSize.lineWidths[i]) / 2;
294-
}
365+
linePositionX += (textSize.width - textSize.lineWidths[i]) / 2;
295366
}
296367

297368
if (this.autoRound)
@@ -307,7 +378,7 @@ var Text = new Class({
307378
context.strokeText(lines[i], linePositionX, linePositionY);
308379
}
309380

310-
if (style.fill)
381+
if (style.color)
311382
{
312383
this.style.syncShadow(context, style.shadowFill);
313384

@@ -337,8 +408,10 @@ var Text = new Class({
337408
style: this.style.toJSON(),
338409
resolution: this.resolution,
339410
padding: {
340-
x: this.padding.x,
341-
y: this.padding.y
411+
left: this.padding.left,
412+
right: this.padding.right,
413+
top: this.padding.top,
414+
bottom: this.padding.bottom
342415
}
343416
};
344417

0 commit comments

Comments
 (0)