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:
1 parent
9a3851f802
commit
14516a878f
5 files changed
+90
-84
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 updateList(): void {
|
protected resizePool<T extends ex.Actor>(
|
||||||
this.clearLabelEntities();
|
pool: T[],
|
||||||
|
count: number,
|
||||||
let y = headingFont.size + spacingS;
|
create: () => T,
|
||||||
|
): void {
|
||||||
for (const currentClient of this._clients) {
|
while (pool.length < count) {
|
||||||
const isClient = getClient(currentClient).id === this._client?.id;
|
const entity = create();
|
||||||
const label = this.createLabel(currentClient, y, isClient);
|
this.addChild(entity);
|
||||||
|
pool.push(entity);
|
||||||
y += label.font.size + spacingS;
|
|
||||||
this.addChild(label);
|
|
||||||
this.entities.push(label);
|
|
||||||
}
|
}
|
||||||
|
while (pool.length > count) {
|
||||||
if (this._clients.length === 0) {
|
this.removeChild(pool.pop()!);
|
||||||
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(
|
protected updateList(): void {
|
||||||
client: Player | Spectator,
|
const clients = this._clients;
|
||||||
y: number,
|
const count = clients.length === 0 ? 1 : clients.length;
|
||||||
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);
|
|
||||||
|
|
||||||
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 {
|
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];
|
||||||
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,
|
|
||||||
});
|
|
||||||
label.pos.setTo(0, y);
|
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;
|
y += label.font.size + spacingS;
|
||||||
this.addChild(label);
|
|
||||||
this.entities.push(label);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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,14 +98,14 @@ 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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,
|
||||||
|
|||||||
Reference in new issue
Block a user