From 548b0853baff3e748fccfd35d3e46b32e87bee2e Mon Sep 17 00:00:00 2001 From: Erik Onarheim Date: Thu, 28 Nov 2024 19:44:21 -0600 Subject: [PATCH] feat: Add maxWidth to label ctor --- sandbox/src/game.ts | 2 +- src/engine/Label.ts | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/sandbox/src/game.ts b/sandbox/src/game.ts index 61d05531..49e66bea 100644 --- a/sandbox/src/game.ts +++ b/sandbox/src/game.ts @@ -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 }); diff --git a/src/engine/Label.ts b/src/engine/Label.ts index 80705ea2..fae988df 100644 --- a/src/engine/Label.ts +++ b/src/engine/Label.ts @@ -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) */ @@ -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; } @@ -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); -- 2.51.2