Skip to content

Commit e15bebd

Browse files
committed
Text.lineSpacing allows you to control the spacing between each line that is rendered.
Text.inputEnabled allows you to enable all input events over Text objects: dragging, clicking, etc - anything that works on a Sprite works on Text now too.
1 parent 9ee5cda commit e15bebd

4 files changed

Lines changed: 79 additions & 13 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ New features:
8585
* Text now works happily with font names with spaces in them.
8686
* Text.setShadow applies a drop shadow to the Text being rendered. Control the x, y, color and blur.
8787
* Text.lineSpacing allows you to control the spacing between each line that is rendered.
88+
* Text.inputEnabled allows you to enable all input events over Text objects: dragging, clicking, etc - anything that works on a Sprite works on Text now too.
8889

8990

9091
New Examples:

examples/wip/text.js

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,52 @@ var text;
1212

1313
function create() {
1414

15-
game.stage.backgroundColor = '#c48844';
15+
game.stage.backgroundColor = '#2d2d2d';
1616

1717
text = game.add.text(game.world.centerX, game.world.centerY, "- phaser -\nwith a sprinkle of\npixi dust");
1818

1919
text.anchor.setTo(0.5);
2020

2121
text.font = 'Art of Fighting 2';
2222
// text.font = 'Arial';
23-
text.fontSize = 40;
23+
text.fontSize = 30;
2424
// text.fontWeight = 'bold italic';
2525

2626
// x0, y0 - x1, y1
27-
var grd = text.context.createLinearGradient(0, 0, 0, text.canvas.height);
27+
// var grd = text.context.createLinearGradient(0, 0, 0, text.canvas.height);
28+
// grd.addColorStop(0, '#8ED6FF');
29+
// grd.addColorStop(1, '#004CB3');
30+
// text.fill = grd;
2831

29-
grd.addColorStop(0, '#8ED6FF');
30-
grd.addColorStop(1, '#004CB3');
31-
32-
// text.fill = '#ff0044';
33-
// text.lineSpacing = 16;
34-
text.fill = grd;
32+
text.fill = '#ff0044';
33+
text.lineSpacing = 16;
3534
text.align = 'center';
36-
text.stroke = '#ff00ff';
35+
text.stroke = '#000000';
3736
text.strokeThickness = 2;
3837

39-
text.setShadow(5, 5, 'rgba(0,0,0,0.5)', 5);
38+
// text.setShadow(5, 5, 'rgba(0,0,0,0.5)', 5);
39+
// text.wordWrap = true;
40+
// test.wordWrapWidth = 50;
41+
42+
// game.input.onDown.add(change, this);
43+
44+
text.inputEnabled = true;
45+
text.input.enableDrag();
46+
47+
text.events.onInputOver.add(over, this);
48+
text.events.onInputOut.add(out, this);
49+
50+
}
51+
52+
function out() {
53+
54+
text.fill = '#ff0044';
55+
56+
}
57+
58+
function over() {
4059

41-
game.input.onDown.add(change, this);
60+
text.fill = '#ff00ff';
4261

4362
}
4463

src/gameobjects/Sprite.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@ Object.defineProperty(Phaser.Sprite.prototype, "renderOrderID", {
784784
});
785785

786786
/**
787-
* By default an Image won't process any input events at all. By setting inputEnabled to true the Phaser.InputHandler is
787+
* By default a Sprite won't process any input events at all. By setting inputEnabled to true the Phaser.InputHandler is
788788
* activated for this object and it will then start to process click/touch events and more.
789789
*
790790
* @name Phaser.Sprite#inputEnabled

src/gameobjects/Text.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@ Phaser.Text = function (game, x, y, text, style) {
8686
*/
8787
this._lineSpacing = 0;
8888

89+
/**
90+
* @property {Phaser.Events} events - The Events you can subscribe to that are dispatched when certain things happen on this Sprite or its components.
91+
*/
92+
this.events = new Phaser.Events(this);
93+
94+
/**
95+
* @property {Phaser.InputHandler|null} input - The Input Handler for this object. Needs to be enabled with image.inputEnabled = true before you can use it.
96+
*/
97+
this.input = null;
98+
8999
PIXI.Text.call(this, text, style);
90100

91101
this.position.set(x, y);
@@ -689,3 +699,39 @@ Object.defineProperty(Phaser.Text.prototype, 'shadowBlur', {
689699
}
690700

691701
});
702+
703+
/**
704+
* By default a Text object won't process any input events at all. By setting inputEnabled to true the Phaser.InputHandler is
705+
* activated for this object and it will then start to process click/touch events and more.
706+
*
707+
* @name Phaser.Text#inputEnabled
708+
* @property {boolean} inputEnabled - Set to true to allow this object to receive input events.
709+
*/
710+
Object.defineProperty(Phaser.Text.prototype, "inputEnabled", {
711+
712+
get: function () {
713+
714+
return (this.input && this.input.enabled);
715+
716+
},
717+
718+
set: function (value) {
719+
720+
if (value)
721+
{
722+
if (this.input === null)
723+
{
724+
this.input = new Phaser.InputHandler(this);
725+
this.input.start();
726+
}
727+
}
728+
else
729+
{
730+
if (this.input && this.input.enabled)
731+
{
732+
this.input.stop();
733+
}
734+
}
735+
}
736+
737+
});

0 commit comments

Comments
 (0)