Skip to content

Commit

Permalink
feat: Add maxWidth to label ctor
Browse files Browse the repository at this point in the history
  • Loading branch information
eonarheim committed Nov 29, 2024
1 parent fd327b7 commit 548b085
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion sandbox/src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ heart.graphics.add(group);
heart.pos = ex.vec(10, 10);
game.add(heart);

var label = new ex.Label({ text: 'Test Label', x: 200, y: 200 });
var label = new ex.Label({ text: 'Test Label', maxWidth: 20, x: 200, y: 200 });
game.add(label);

var testSpriteLabel = new ex.Label({ text: 'Test Sprite Label', x: 200, y: 100, spriteFont: spriteFont });
Expand Down
18 changes: 17 additions & 1 deletion src/engine/Label.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ export interface LabelOptions {
* Specify the label text
*/
text?: string;

/**
* Specify a max width for the text in pixels, if specified the text will wrap.
*
* **Not supported in SpriteFont**
*/
maxWidth?: number;
/**
* Specify the color of the text (does not apply to SpriteFonts)
*/
Expand All @@ -40,6 +47,14 @@ export class Label extends Actor {
private _font: Font = new Font();
private _text: Text = new Text({ text: '', font: this._font });

public set maxWidth(width: number | undefined) {
this._text.maxWidth = width;
}

public get maxWidth(): number | undefined {
return this._text.maxWidth;
}

public get font(): Font {
return this._font;
}
Expand Down Expand Up @@ -99,11 +114,12 @@ export class Label extends Actor {
*/
constructor(options?: LabelOptions & ActorArgs) {
super(options);
const { text, pos, x, y, spriteFont, font, color } = { text: '', ...options };
const { text, pos, x, y, spriteFont, font, color, maxWidth } = { text: '', ...options };

this.pos = pos ?? (x && y ? vec(x, y) : this.pos);
this.text = text ?? this.text;
this.font = font ?? this.font;
this.maxWidth = maxWidth ?? this.maxWidth;
this.spriteFont = spriteFont ?? this.spriteFont;
this._text.color = color ?? this.color;
const gfx = this.get(GraphicsComponent);
Expand Down

0 comments on commit 548b085

Please sign in to comment.