Frontend - fix flickers (#44)

* fix flicker for list labels by not recreating
* only show rejoin button when connected
* fix initial flicker on text
This commit is contained in:
lucas authored and GitHub committed 2026-05-04 21:07:48 +02:00
1 parent 9a3851f802
commit 14516a878f
5 files changed
+84 -78

No files matched your search

@@ -1,13 +1,13 @@
import * as ex from "excalibur"; import { Color, ScreenElement } from "excalibur";
import { spacingS } from "../utils/formattingUtils"; import { spacingS } from "../utils/formattingUtils";
import { headingFont, paragraphFont } from "../utils/fontUtil"; import { headingFont, paragraphFont } from "../utils/fontUtil";
import { Text } from "./Text"; import { Text } from "./Text";
import { Client, getClient, Player, Spectator } from "../jsonTypes"; 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 _clients: Player[] | Spectator[] = [];
protected _client: Client | null = null; protected _client: Client | null = null;
@@ -45,48 +45,50 @@ export class ClientListScreenElement extends ex.ScreenElement {
} }
} }
protected resizePool<T extends ex.Actor>(
pool: T[],
count: number,
create: () => T,
): void {
while (pool.length < count) {
const entity = create();
this.addChild(entity);
pool.push(entity);
}
while (pool.length > count) {
this.removeChild(pool.pop()!);
}
}
protected updateList(): void { protected updateList(): void {
this.clearLabelEntities(); const clients = this._clients;
const count = clients.length === 0 ? 1 : clients.length;
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; let y = headingFont.size + spacingS;
for (const currentClient of this._clients) { if (clients.length === 0) {
const isClient = getClient(currentClient).id === this._client?.id; const label = this.entities[0] as Text;
const label = this.createLabel(currentClient, y, isClient); label.text = "No one cares.";
label.color = Color.White;
y += label.font.size + spacingS;
this.addChild(label);
this.entities.push(label);
}
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);
}
}
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.pos.setTo(0, y);
label.anchor.setTo(0.5, 0); } else {
for (let i = 0; i < clients.length; i++) {
return label; 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 { protected computeChildrenHeight(numLabels: number): number {
@@ -96,11 +98,4 @@ export class ClientListScreenElement extends ex.ScreenElement {
Math.max(1, numLabels) * (spacingS + paragraphFont.size) Math.max(1, numLabels) * (spacingS + paragraphFont.size)
); );
} }
protected clearLabelEntities(): void {
for (const labelEntity of this.entities) {
this.removeChild(labelEntity);
}
this.entities = [];
}
} }
@@ -1,6 +1,6 @@
import * as ex from "excalibur"; import * as ex from "excalibur";
import { spacingS } from "../utils/formattingUtils"; import { spacingS } from "../utils/formattingUtils";
import { disabledFont } from "../utils/fontUtil"; import { disabledFont, paragraphFont } from "../utils/fontUtil";
import { Text } from "./Text"; import { Text } from "./Text";
import { Checkbox } from "./Checkbox"; import { Checkbox } from "./Checkbox";
import { Player } from "../jsonTypes"; import { Player } from "../jsonTypes";
@@ -8,6 +8,9 @@ import { ClientListScreenElement } from "./ClientListScreenElement";
export class PlayerListScreenElement extends ClientListScreenElement { export class PlayerListScreenElement extends ClientListScreenElement {
protected override _clients: Player[] = []; protected override _clients: Player[] = [];
private _labelPool: Text[] = [];
private _checkboxPool: Checkbox[] = [];
constructor( constructor(
headingText: string, headingText: string,
private _maxPlayers: number, private _maxPlayers: number,
@@ -16,40 +19,44 @@ export class PlayerListScreenElement extends ClientListScreenElement {
} }
protected override updateList(): void { 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; let y = this.heading.font.size + spacingS;
const clients = this._clients.slice(0, this._maxPlayers); for (let i = 0; i < this._maxPlayers; i++) {
const label = this._labelPool[i];
label.pos.setTo(0, y);
for (const currentClient of clients) { if (i < clients.length) {
const { name, id } = currentClient.client; const { name, id } = clients[i].client;
const isClient = id === this._client?.id; const isClient = id === this._client?.id;
const label = this.createLabel(currentClient, y, isClient); label.font = paragraphFont;
label.text = name;
label.color = isClient ? ex.Color.fromHex("40E0D0") : ex.Color.White;
this.addChild(label); const cb = this._checkboxPool[i];
this.entities.push(label);
const ready = new Checkbox({ checked: currentClient.ready });
const textWidth = label.font.measureTextWithoutCache(name).width; const textWidth = label.font.measureTextWithoutCache(name).width;
ready.pos = ex.vec(textWidth / 2 + spacingS, y); cb.pos.setTo(textWidth / 2 + spacingS, y);
this.addChild(ready); cb.checked = clients[i].ready;
} else {
this.entities.push(ready); label.font = disabledFont;
label.text = "Empty";
y += label.font.size + spacingS; label.color = ex.Color.White;
} }
for (let i = clients.length; i < this._maxPlayers; i++) {
const label = new Text({
text: "Empty",
font: disabledFont,
});
label.pos.setTo(0, y);
label.anchor.setTo(0.5, 0);
y += label.font.size + spacingS; y += label.font.size + spacingS;
this.addChild(label);
this.entities.push(label);
} }
} }
+6
View File
@@ -5,5 +5,11 @@ type ConstructProperties = ex.LabelOptions & ex.ActorArgs;
export class Text extends ex.Label { export class Text extends ex.Label {
constructor(options?: ConstructProperties) { constructor(options?: ConstructProperties) {
super(options); 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;
} }
} }
@@ -98,13 +98,13 @@ export class PlayerNameInput extends ex.ScreenElement {
} }
private _update(): void { private _update(): void {
const joinDisabled = const { connected } = this._connection;
!this._connection.connected || this._nameInput.text === ""; const joinDisabled = !connected || this._nameInput.text === "";
this._joinButton.disabled = joinDisabled; this._joinButton.disabled = joinDisabled;
if (this._rejoinButton != null) { if (this._rejoinButton != null) {
const rejoinVisible = this._connection.connected; if (connected) {
if (rejoinVisible) { if (this.children.includes(this._rejoinButton))
this.addChild(this._rejoinButton); this.addChild(this._rejoinButton);
} else { } else {
this.removeChild(this._rejoinButton); this.removeChild(this._rejoinButton);
-2
View File
@@ -9,7 +9,6 @@ export class Title extends ScreenElement {
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
onInitialize(_engine: Engine): void { onInitialize(_engine: Engine): void {
// Title
this._title = new Text({ this._title = new Text({
text: "Auto Beer Pong", text: "Auto Beer Pong",
font: titleFont, font: titleFont,
@@ -17,7 +16,6 @@ export class Title extends ScreenElement {
this._title.anchor = vec(0.5, 0); this._title.anchor = vec(0.5, 0);
this.addChild(this._title); this.addChild(this._title);
// Sub title
this._subTitle = new Text({ this._subTitle = new Text({
text: "An auto battler with stupid people.", text: "An auto battler with stupid people.",
font: subTitleFont, font: subTitleFont,