impr(name-input): join on enter (#53)
This commit is contained in:
1 parent
51a28417ad
commit
710d3ec041
3 files changed
+44
-7
No files matched your search
@@ -87,12 +87,17 @@ export class Connection extends Evented<typeof ConnectionEvents> {
|
||||
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<typeof ConnectionEvents> {
|
||||
storeClient(this._client);
|
||||
|
||||
this._waitForJoin = false;
|
||||
this._joined = true;
|
||||
this.emit("joined", new JoinedEvent());
|
||||
return;
|
||||
}
|
||||
@@ -158,6 +164,8 @@ export class Connection extends Evented<typeof ConnectionEvents> {
|
||||
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) {
|
||||
|
||||
@@ -12,9 +12,16 @@ type ConstructProperties = ex.ActorArgs & TextInputProperties;
|
||||
|
||||
type TextInputEvents = {
|
||||
textChanged: TextChangedEvent;
|
||||
submitText: TextSubmitEvent;
|
||||
};
|
||||
|
||||
export class TextChangedEvent extends ex.GameEvent<string> {
|
||||
export class TextChangedEvent extends ex.GameEvent<TextInput> {
|
||||
constructor(public text: string) {
|
||||
super();
|
||||
}
|
||||
}
|
||||
|
||||
export class TextSubmitEvent extends ex.GameEvent<TextInput> {
|
||||
constructor(public text: string) {
|
||||
super();
|
||||
}
|
||||
@@ -22,6 +29,7 @@ export class TextChangedEvent extends ex.GameEvent<string> {
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in new issue
Block a user