From 14516a878fc1f662278d95084c912119852c8c38 Mon Sep 17 00:00:00 2001 From: Lucas Habersaat Date: Mon, 4 May 2026 21:07:48 +0200 Subject: [PATCH] Frontend - fix flickers (#44) * fix flicker for list labels by not recreating * only show rejoin button when connected * fix initial flicker on text --- .../ScreenElements/ClientListScreenElement.ts | 89 +++++++++---------- .../ScreenElements/PlayerListScreenElement.ts | 67 +++++++------- src/frontend/src/ScreenElements/Text.ts | 6 ++ .../src/UIElements/PlayerNameInput.ts | 10 +-- src/frontend/src/UIElements/Title.ts | 2 - 5 files changed, 90 insertions(+), 84 deletions(-) diff --git a/src/frontend/src/ScreenElements/ClientListScreenElement.ts b/src/frontend/src/ScreenElements/ClientListScreenElement.ts index 4dd755b..be48fcb 100644 --- a/src/frontend/src/ScreenElements/ClientListScreenElement.ts +++ b/src/frontend/src/ScreenElements/ClientListScreenElement.ts @@ -1,13 +1,13 @@ -import * as ex from "excalibur"; +import { Color, ScreenElement } from "excalibur"; import { spacingS } from "../utils/formattingUtils"; import { headingFont, paragraphFont } from "../utils/fontUtil"; import { Text } from "./Text"; import { Client, getClient, Player, Spectator } from "../jsonTypes"; -const clientColor = ex.Color.fromHex("40E0D0"); +const clientColor = Color.fromHex("40E0D0"); -export class ClientListScreenElement extends ex.ScreenElement { +export class ClientListScreenElement extends ScreenElement { protected _clients: Player[] | Spectator[] = []; protected _client: Client | null = null; @@ -45,48 +45,50 @@ export class ClientListScreenElement extends ex.ScreenElement { } } - protected updateList(): void { - this.clearLabelEntities(); - - let y = headingFont.size + spacingS; - - for (const currentClient of this._clients) { - const isClient = getClient(currentClient).id === this._client?.id; - const label = this.createLabel(currentClient, y, isClient); - - y += label.font.size + spacingS; - this.addChild(label); - this.entities.push(label); + protected resizePool( + pool: T[], + count: number, + create: () => T, + ): void { + while (pool.length < count) { + const entity = create(); + this.addChild(entity); + pool.push(entity); } - - if (this._clients.length === 0) { - const label = new Text({ - text: "No one cares.", - font: paragraphFont, - }); - label.pos = ex.vec(0, y); - label.anchor = ex.vec(0.5, 0); - this.addChild(label); - this.entities.push(label); + while (pool.length > count) { + this.removeChild(pool.pop()!); } } - protected createLabel( - client: Player | Spectator, - y: number, - isClient: boolean, - ): Text { - const color = isClient ? clientColor : undefined; - const { name } = getClient(client); - const label = new Text({ - text: name, - font: paragraphFont, - color, - }); - label.pos.setTo(0, y); - label.anchor.setTo(0.5, 0); + protected updateList(): void { + const clients = this._clients; + const count = clients.length === 0 ? 1 : clients.length; - return label; + this.resizePool(this.entities as Text[], count, () => { + const label = new Text({ font: paragraphFont }); + label.anchor.setTo(0.5, 0); + return label; + }); + + let y = headingFont.size + spacingS; + + if (clients.length === 0) { + const label = this.entities[0] as Text; + label.text = "No one cares."; + label.color = Color.White; + label.pos.setTo(0, y); + } else { + for (let i = 0; i < clients.length; i++) { + const currentClient = clients[i]; + const isClient = getClient(currentClient).id === this._client?.id; + const label = this.entities[i] as Text; + const { name } = getClient(currentClient); + label.text = name; + label.color = isClient ? clientColor : Color.White; + label.pos.setTo(0, y); + y += paragraphFont.size + spacingS; + } + } } protected computeChildrenHeight(numLabels: number): number { @@ -96,11 +98,4 @@ export class ClientListScreenElement extends ex.ScreenElement { Math.max(1, numLabels) * (spacingS + paragraphFont.size) ); } - - protected clearLabelEntities(): void { - for (const labelEntity of this.entities) { - this.removeChild(labelEntity); - } - this.entities = []; - } } diff --git a/src/frontend/src/ScreenElements/PlayerListScreenElement.ts b/src/frontend/src/ScreenElements/PlayerListScreenElement.ts index 85cb010..766c9f2 100644 --- a/src/frontend/src/ScreenElements/PlayerListScreenElement.ts +++ b/src/frontend/src/ScreenElements/PlayerListScreenElement.ts @@ -1,6 +1,6 @@ import * as ex from "excalibur"; import { spacingS } from "../utils/formattingUtils"; -import { disabledFont } from "../utils/fontUtil"; +import { disabledFont, paragraphFont } from "../utils/fontUtil"; import { Text } from "./Text"; import { Checkbox } from "./Checkbox"; import { Player } from "../jsonTypes"; @@ -8,6 +8,9 @@ import { ClientListScreenElement } from "./ClientListScreenElement"; export class PlayerListScreenElement extends ClientListScreenElement { protected override _clients: Player[] = []; + private _labelPool: Text[] = []; + private _checkboxPool: Checkbox[] = []; + constructor( headingText: string, private _maxPlayers: number, @@ -16,40 +19,44 @@ export class PlayerListScreenElement extends ClientListScreenElement { } protected override updateList(): void { - this.clearLabelEntities(); + const clients = this._clients.slice(0, this._maxPlayers); + + this.resizePool(this._labelPool, this._maxPlayers, () => { + const label = new Text({ font: paragraphFont }); + label.anchor.setTo(0.5, 0); + return label; + }); + + this.resizePool( + this._checkboxPool, + clients.length, + () => new Checkbox({ checked: false }), + ); let y = this.heading.font.size + spacingS; - const clients = this._clients.slice(0, this._maxPlayers); - - for (const currentClient of clients) { - const { name, id } = currentClient.client; - const isClient = id === this._client?.id; - const label = this.createLabel(currentClient, y, isClient); - - this.addChild(label); - this.entities.push(label); - - const ready = new Checkbox({ checked: currentClient.ready }); - const textWidth = label.font.measureTextWithoutCache(name).width; - ready.pos = ex.vec(textWidth / 2 + spacingS, y); - this.addChild(ready); - - this.entities.push(ready); - - y += label.font.size + spacingS; - } - - for (let i = clients.length; i < this._maxPlayers; i++) { - const label = new Text({ - text: "Empty", - font: disabledFont, - }); + for (let i = 0; i < this._maxPlayers; i++) { + const label = this._labelPool[i]; label.pos.setTo(0, y); - label.anchor.setTo(0.5, 0); + + if (i < clients.length) { + const { name, id } = clients[i].client; + const isClient = id === this._client?.id; + label.font = paragraphFont; + label.text = name; + label.color = isClient ? ex.Color.fromHex("40E0D0") : ex.Color.White; + + const cb = this._checkboxPool[i]; + const textWidth = label.font.measureTextWithoutCache(name).width; + cb.pos.setTo(textWidth / 2 + spacingS, y); + cb.checked = clients[i].ready; + } else { + label.font = disabledFont; + label.text = "Empty"; + label.color = ex.Color.White; + } + y += label.font.size + spacingS; - this.addChild(label); - this.entities.push(label); } } diff --git a/src/frontend/src/ScreenElements/Text.ts b/src/frontend/src/ScreenElements/Text.ts index 9897c00..49f39ec 100644 --- a/src/frontend/src/ScreenElements/Text.ts +++ b/src/frontend/src/ScreenElements/Text.ts @@ -5,5 +5,11 @@ type ConstructProperties = ex.LabelOptions & ex.ActorArgs; export class Text extends ex.Label { constructor(options?: ConstructProperties) { super(options); + // ex.Label sets this.text before this.font in its constructor, so + // _calculateDimension() runs with the default Font instead of options.font. + // Re-setting triggers recalculation with the now-correct font. + + // eslint-disable-next-line no-self-assign + this.text = this.text; } } diff --git a/src/frontend/src/UIElements/PlayerNameInput.ts b/src/frontend/src/UIElements/PlayerNameInput.ts index 64ef49d..1c4c897 100644 --- a/src/frontend/src/UIElements/PlayerNameInput.ts +++ b/src/frontend/src/UIElements/PlayerNameInput.ts @@ -98,14 +98,14 @@ export class PlayerNameInput extends ex.ScreenElement { } private _update(): void { - const joinDisabled = - !this._connection.connected || this._nameInput.text === ""; + const { connected } = this._connection; + const joinDisabled = !connected || this._nameInput.text === ""; this._joinButton.disabled = joinDisabled; if (this._rejoinButton != null) { - const rejoinVisible = this._connection.connected; - if (rejoinVisible) { - this.addChild(this._rejoinButton); + if (connected) { + if (this.children.includes(this._rejoinButton)) + this.addChild(this._rejoinButton); } else { this.removeChild(this._rejoinButton); } diff --git a/src/frontend/src/UIElements/Title.ts b/src/frontend/src/UIElements/Title.ts index d522de9..d7d018c 100644 --- a/src/frontend/src/UIElements/Title.ts +++ b/src/frontend/src/UIElements/Title.ts @@ -9,7 +9,6 @@ export class Title extends ScreenElement { // eslint-disable-next-line @typescript-eslint/no-unused-vars onInitialize(_engine: Engine): void { - // Title this._title = new Text({ text: "Auto Beer Pong", font: titleFont, @@ -17,7 +16,6 @@ export class Title extends ScreenElement { this._title.anchor = vec(0.5, 0); this.addChild(this._title); - // Sub title this._subTitle = new Text({ text: "An auto battler with stupid people.", font: subTitleFont,