diff --git a/src/frontend/src/Connection.ts b/src/frontend/src/Connection.ts index 7ea6cae..7d74d0e 100644 --- a/src/frontend/src/Connection.ts +++ b/src/frontend/src/Connection.ts @@ -87,12 +87,17 @@ export class Connection extends Evented { private _wasConnected = false; private _waitForJoin = false; + private _joined = false; private _client: Client | null = null; get connected(): boolean { return this._socket?.connected ?? false; } + get joined(): boolean { + return this._joined; + } + connect(mock?: BaseSocket): void { this._socket?.offAll(); this._socket = mock ?? new Socket(); @@ -119,6 +124,7 @@ export class Connection extends Evented { storeClient(this._client); this._waitForJoin = false; + this._joined = true; this.emit("joined", new JoinedEvent()); return; } @@ -158,6 +164,8 @@ export class Connection extends Evented { private _onDisconnect(): void { log("Connection", "disconnected"); + this._joined = false; + // The socket close event is also invoked when a connection attempt fails. // Only emit the disconnect event when we actually had a connection. if (this._wasConnected) { diff --git a/src/frontend/src/ScreenElements/TextInput.ts b/src/frontend/src/ScreenElements/TextInput.ts index 870b7db..dacb042 100644 --- a/src/frontend/src/ScreenElements/TextInput.ts +++ b/src/frontend/src/ScreenElements/TextInput.ts @@ -12,9 +12,16 @@ type ConstructProperties = ex.ActorArgs & TextInputProperties; type TextInputEvents = { textChanged: TextChangedEvent; + submitText: TextSubmitEvent; }; -export class TextChangedEvent extends ex.GameEvent { +export class TextChangedEvent extends ex.GameEvent { + constructor(public text: string) { + super(); + } +} + +export class TextSubmitEvent extends ex.GameEvent { constructor(public text: string) { super(); } @@ -22,6 +29,7 @@ export class TextChangedEvent extends ex.GameEvent { export const TextInputEvents = { Textchanged: "textchanged", + TextSubmit: "textsubmit", } as const; export class TextInput extends ex.ScreenElement { @@ -85,8 +93,10 @@ export class TextInput extends ex.ScreenElement { engine.currentScene.add(this._cursorTimer); this.addChild(this._cursor); - this.on("pointerup", () => this._focus()); + this.on("pointerup", () => this.focus()); engine.input.keyboard.on("press", (e) => this._onKeyPress(e)); + + this.focus(); } get height(): number { @@ -121,13 +131,13 @@ export class TextInput extends ex.ScreenElement { this._updateCursor(); } - private _focus(): void { + focus(): void { this._focused = true; this._cursorTimer.start(); this._cursor.graphics.isVisible = true; } - private _unfocus(): void { + unfocus(): void { this._focused = false; this._cursorTimer.stop(); this._cursor.graphics.isVisible = false; @@ -151,11 +161,12 @@ export class TextInput extends ex.ScreenElement { const { key } = event; if (key === ex.Keys.Esc) { - this._unfocus(); + this.unfocus(); return; } if (key === ex.Keys.Enter) { + this.emit(TextInputEvents.TextSubmit, this.text); return; } diff --git a/src/frontend/src/UIElements/PlayerNameInput.ts b/src/frontend/src/UIElements/PlayerNameInput.ts index 461a3c4..f5a8f3f 100644 --- a/src/frontend/src/UIElements/PlayerNameInput.ts +++ b/src/frontend/src/UIElements/PlayerNameInput.ts @@ -39,7 +39,7 @@ export class PlayerNameInput extends ex.ScreenElement { this._joinButton = new Button({ text: "Join", anchor: ex.vec(0.5, 0), - onClick: () => this._joinCallback(this._nameInput.text), + onClick: () => this._join(), disabled: true, }); this.addChild(this._joinButton); @@ -64,6 +64,9 @@ export class PlayerNameInput extends ex.ScreenElement { this._nameInput.on(TextInputEvents.Textchanged, () => this._updateVisibility(), ); + this._nameInput.on(TextInputEvents.TextSubmit, () => { + this._join(); + }); this._updatePositions(); @@ -85,9 +88,24 @@ export class PlayerNameInput extends ex.ScreenElement { this._updateVisibility(); } + private get _nameValid(): boolean { + const name = this._nameInput.text; + if (name === "") { + return false; + } + + return true; + } + + private _join(): void { + if (this._nameValid && !this._connection.joined) { + this._joinCallback(this._nameInput.text); + } + } + private _updateVisibility(): void { const { connected } = this._connection; - const joinDisabled = !connected || this._nameInput.text === ""; + const joinDisabled = !connected || !this._nameValid; this._joinButton.disabled = joinDisabled; if (this._rejoinButton != null) {