Skip to content

Commit 166d802

Browse files
committed
make Label multiline
1 parent 742df13 commit 166d802

3 files changed

Lines changed: 35 additions & 15 deletions

File tree

src/JSTools.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ class Label extends Control {
319319
textHeight: number;
320320
fontFamily: string;
321321
xAlign: "start" | "center";
322-
yAlign: CanvasTextBaseline;
322+
yAlign: "middle" | "top";
323323
constructor(x: number, y: number, text: string, xAlign: "start" | "center") {
324324
super(x, y); //Inherit from Control
325325
this.text = text;
@@ -330,6 +330,9 @@ class Label extends Control {
330330
this.xAlign = xAlign; //Horizontal Aligning
331331
this.yAlign = "middle"; //Vertical Aligning
332332
}
333+
/**
334+
* Call before Lable.draw
335+
*/
333336
updateContext () {
334337
const ctx = (window as any).globals.canvas.getContext('2d') as CanvasRenderingContext2D;
335338

@@ -342,20 +345,36 @@ class Label extends Control {
342345
ctx.font = "bold " + this.textHeight + "px " + this.fontFamily;
343346
}
344347
}
348+
/**
349+
* Call after Label.updateContext
350+
*/
345351
draw () {
346352
const ctx = (window as any).globals.canvas.getContext('2d') as CanvasRenderingContext2D;
347353

348354
if (this.visible) {
349-
/*
350-
var something;
351-
if (this.xAlign) {
352-
something = this.x - c.measureText(this.text).width / 2;
355+
// support multi-line text
356+
const lines = this.text.split(/\r?\n/);
357+
console.log("🚀 ~ lines:", lines);
358+
const lineHeight = this.textHeight * 1.2;
359+
360+
// Use 'middle' baseline for consistent vertical centering per line
361+
if (this.yAlign === "middle") ctx.textBaseline = "middle"
362+
else if (this.yAlign === "top") ctx.textBaseline = "top";
363+
364+
// Compute starting Y so multi-line block is centered when yAlign === "middle"
365+
let startY: number;
366+
if (this.yAlign === "middle") {
367+
startY = this.y - ((lines.length - 1) / 2) * lineHeight;
353368
} else {
354-
something = this.x;
355-
}*/ //Substituido por c.textAlign
356-
357-
ctx.strokeText(this.text, this.x, this.y);
358-
ctx.fillText(this.text, this.x, this.y);
369+
// fallback: align first line at this.y
370+
startY = this.y;
371+
}
372+
373+
for (let i = 0; i < lines.length; i++) {
374+
const lineY = startY + i * lineHeight;
375+
ctx.strokeText(lines[i], this.x, lineY);
376+
ctx.fillText(lines[i], this.x, lineY);
377+
}
359378
}
360379
};
361380
updateLanguage () {

src/Monopoly/Main.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,36 @@
11
import { canvasPadding } from './Initialization';
22
import { Board } from './Classes';
3-
import { AnimationLOOP, GameState, JSWindow, TextButton } from '../JSTools';
3+
import { AnimationLOOP, GameState, JSWindow, Label, TextButton } from '../JSTools';
44

55
const canvas = (window as any).globals.canvas;
66

77
const game = new GameState();
88
game.x = canvasPadding;
99
game.y = canvasPadding;
10-
//Space 980x980
1110
game.width = canvas.width - canvasPadding * 2;
1211
game.height = canvas.height - canvasPadding * 2;
1312
game.color = "#B0B3B7";
1413
game.outlinePosition = "outer";
1514
game.showIndex = 1;
1615
game.visible = true;
1716

18-
const startWindow = new JSWindow(game.width-40,game.height-40);
17+
const startWindow = new JSWindow(1300, 900);
1918
game.container.startWindow = startWindow;
2019
startWindow.color = "#263599";
2120
startWindow.showIndex = 100;
2221
startWindow.show();
2322

2423
(window as any).globals.startWindow = startWindow;
2524

26-
startWindow.container.closeButton = new TextButton(game.width-60, 20, "X", "center", 20, 5);
25+
startWindow.container.closeButton = new TextButton(startWindow.width-20, 20, "X", "center", 20, 5);
2726
startWindow.container.closeButton.updateLanguage = function() {this.text="X"};
2827
startWindow.container.closeButton.onclick = () => {
2928
startWindow.hide();
3029
}
3130

31+
startWindow.container.welcomeText = new Label(650, 450, "Welcome! 😀", "center");
32+
startWindow.container.welcomeText.updateLanguage = function(){this.text="Bem vindo(a)\nao Monopólio!"};
33+
3234

3335
game.container.board = new Board(0, 0);
3436
AnimationLOOP(function(){

src/TheBallGame/Main.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ var debugTimer: number;
7474
// }
7575
// mainMenu.container.w.container.welcomemsg = new Label(350, 250, "Welcome! 😀", "center");
7676
// mainMenu.container.w.container.welcomemsg.updateLanguage = function(){this.text="Welcome! 😀"};
77-
// mainMenu.container.w.visible = true;
7877
// mainMenu.container.w.show();
7978

8079
// (window as any).globals.mainMenu = mainMenu;

0 commit comments

Comments
 (0)