Add TextInput and add name input to lobby (#4)
This commit is contained in:
1 parent
77dcad1015
commit
3bc69e670a
5 files changed
+315
-28
No files matched your search
@@ -0,0 +1,53 @@
|
|||||||
|
import * as ex from 'excalibur';
|
||||||
|
import { TextInput } from '../ScreenElements/TextInput';
|
||||||
|
import { Button } from '../ScreenElements/Button';
|
||||||
|
import { paragraphFont } from '../utils/fontUtil';
|
||||||
|
import { Text } from '../ScreenElements/Text';
|
||||||
|
import { spacingM } from '../utils/formattingUtils';
|
||||||
|
|
||||||
|
export class PlayerNameInput extends ex.ScreenElement {
|
||||||
|
private _nameLabel: Text = null!;
|
||||||
|
private _nameInput: TextInput = null!;
|
||||||
|
private _joinButton: Button = null!;
|
||||||
|
|
||||||
|
private _joinCallback: (name: string) => void;
|
||||||
|
|
||||||
|
constructor(joinCallback: (name: string) => void) {
|
||||||
|
super();
|
||||||
|
this._joinCallback = joinCallback;
|
||||||
|
}
|
||||||
|
|
||||||
|
onInitialize(_engine: ex.Engine): void {
|
||||||
|
this._nameLabel = new Text({
|
||||||
|
text: "What are you called?",
|
||||||
|
font: paragraphFont,
|
||||||
|
})
|
||||||
|
this._nameLabel.anchor = ex.vec(0.5, 0);
|
||||||
|
this.addChild(this._nameLabel);
|
||||||
|
|
||||||
|
this._nameInput = new TextInput({ text: "", width: 300 });
|
||||||
|
this._nameInput.anchor = ex.vec(0.5, 0);
|
||||||
|
this.addChild(this._nameInput);
|
||||||
|
|
||||||
|
this._joinButton = new Button({
|
||||||
|
text: "Join",
|
||||||
|
anchor: ex.vec(0.5, 0),
|
||||||
|
onClick: () => this._joinCallback(this._nameInput.text),
|
||||||
|
});
|
||||||
|
this.addChild(this._joinButton);
|
||||||
|
|
||||||
|
let y = 0;
|
||||||
|
this._nameLabel.pos = ex.vec(0, y);
|
||||||
|
y += this._nameLabel.font.size + spacingM;
|
||||||
|
|
||||||
|
this._nameInput.pos = ex.vec(0, y);
|
||||||
|
y += this._nameInput.height + spacingM;
|
||||||
|
|
||||||
|
this._joinButton.pos = ex.vec(0, y);
|
||||||
|
y += this._joinButton.height + spacingM;
|
||||||
|
}
|
||||||
|
|
||||||
|
get playerName(): string {
|
||||||
|
return this._nameInput.text;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import * as ex from 'excalibur';
|
import * as ex from 'excalibur';
|
||||||
import { spacingL, spacingM, spacingS } from '../utils/formattingUtils';
|
import { spacingL, spacingM } from '../utils/formattingUtils';
|
||||||
import { subTitleFont, titleFont } from '../utils/fontUtil';
|
import { subTitleFont, titleFont } from '../utils/fontUtil';
|
||||||
import { Cups } from '../Cups';
|
import { Cups } from '../Cups';
|
||||||
import { GameState } from '../GameState';
|
import { GameState } from '../GameState';
|
||||||
@@ -8,10 +8,13 @@ import { PlayerListScreenElement } from '../ScreenElements/PlayerListScreenEleme
|
|||||||
import { TitledListScreenElement } from '../ScreenElements/TitledListScreenElement';
|
import { TitledListScreenElement } from '../ScreenElements/TitledListScreenElement';
|
||||||
import { Text } from '../ScreenElements/Text';
|
import { Text } from '../ScreenElements/Text';
|
||||||
import { Button } from '../ScreenElements/Button';
|
import { Button } from '../ScreenElements/Button';
|
||||||
|
import { PlayerNameInput } from '../GameElements/PlayerNameInput';
|
||||||
|
|
||||||
const tableWidth = 400;
|
const tableWidth = 400;
|
||||||
const maxPlayers = 2;
|
const maxPlayers = 2;
|
||||||
|
|
||||||
|
export type Screens = "join" | "lobby";
|
||||||
|
|
||||||
export class LobbyScene extends GameStateScene {
|
export class LobbyScene extends GameStateScene {
|
||||||
private _table: ex.ScreenElement = null!;
|
private _table: ex.ScreenElement = null!;
|
||||||
private _cups: ex.ScreenElement = null!;
|
private _cups: ex.ScreenElement = null!;
|
||||||
@@ -29,6 +32,10 @@ export class LobbyScene extends GameStateScene {
|
|||||||
private _joinSpectatorButton: Button = null!;
|
private _joinSpectatorButton: Button = null!;
|
||||||
private _joinPlayerButton: Button = null!;
|
private _joinPlayerButton: Button = null!;
|
||||||
|
|
||||||
|
private _playerNameInput: PlayerNameInput = null!;
|
||||||
|
|
||||||
|
private _screen: "join" | "lobby" = "join";
|
||||||
|
|
||||||
onInitialize(engine: ex.Engine): void {
|
onInitialize(engine: ex.Engine): void {
|
||||||
// Table background
|
// Table background
|
||||||
this._table = new ex.ScreenElement({
|
this._table = new ex.ScreenElement({
|
||||||
@@ -77,7 +84,6 @@ export class LobbyScene extends GameStateScene {
|
|||||||
text: "An auto battler with stupid people.",
|
text: "An auto battler with stupid people.",
|
||||||
font: subTitleFont,
|
font: subTitleFont,
|
||||||
});
|
});
|
||||||
|
|
||||||
this._subTitle.anchor.setTo(0.5, 0);
|
this._subTitle.anchor.setTo(0.5, 0);
|
||||||
this.add(this._subTitle);
|
this.add(this._subTitle);
|
||||||
|
|
||||||
@@ -85,30 +91,29 @@ export class LobbyScene extends GameStateScene {
|
|||||||
this._playerList = new PlayerListScreenElement("Players", maxPlayers);
|
this._playerList = new PlayerListScreenElement("Players", maxPlayers);
|
||||||
this._playerList.labels = ["Dubbel"];
|
this._playerList.labels = ["Dubbel"];
|
||||||
this._playerList.anchor.setTo(0.5, 0);
|
this._playerList.anchor.setTo(0.5, 0);
|
||||||
this.add(this._playerList);
|
|
||||||
|
|
||||||
this._joinPlayerButton = new Button({
|
this._joinPlayerButton = new Button({
|
||||||
text: "Join as player",
|
text: "Join as player",
|
||||||
anchor: ex.vec(0.5, 0),
|
anchor: ex.vec(0.5, 0),
|
||||||
onClick: () => this._joinAsPlayer(),
|
onClick: () => this._joinAsPlayer(),
|
||||||
});
|
});
|
||||||
this.add(this._joinPlayerButton);
|
|
||||||
|
|
||||||
|
|
||||||
this._spectatorsList = new TitledListScreenElement("Spectators");
|
this._spectatorsList = new TitledListScreenElement("Spectators");
|
||||||
this._spectatorsList.labels = ["Dubbel", "Cocksucker"];
|
this._spectatorsList.labels = ["Dubbel", "Cocksucker"];
|
||||||
this._spectatorsList.anchor.setTo(0.5, 0);
|
this._spectatorsList.anchor.setTo(0.5, 0);
|
||||||
this.add(this._spectatorsList);
|
|
||||||
|
|
||||||
|
|
||||||
this._joinSpectatorButton = new Button({
|
this._joinSpectatorButton = new Button({
|
||||||
text: "Join as spectator",
|
text: "Join as spectator",
|
||||||
anchor: ex.vec(0.5, 0),
|
anchor: ex.vec(0.5, 0),
|
||||||
onClick: () => this._joinAsSpectator(),
|
onClick: () => this._joinAsSpectator(),
|
||||||
});
|
});
|
||||||
this.add(this._joinSpectatorButton);
|
|
||||||
|
|
||||||
this._updatePositions(engine.screen);
|
// Name input
|
||||||
|
this._playerNameInput = new PlayerNameInput((_name: string) => this._join());
|
||||||
|
this._playerNameInput.anchor = ex.vec(0.5, 0);
|
||||||
|
|
||||||
|
this._updateVisibility();
|
||||||
|
this._updatePositions();
|
||||||
}
|
}
|
||||||
|
|
||||||
private _createBar(): ex.ScreenElement {
|
private _createBar(): ex.ScreenElement {
|
||||||
@@ -124,9 +129,27 @@ export class LobbyScene extends GameStateScene {
|
|||||||
return bar;
|
return bar;
|
||||||
}
|
}
|
||||||
|
|
||||||
private _updatePositions(screen: ex.Screen): void {
|
private _updateVisibility(): void {
|
||||||
|
const isJoinScreen = this._screen === "join";
|
||||||
|
const setVisible = (g: ex.Actor, visible: boolean): void => {
|
||||||
|
if (visible) {
|
||||||
|
this.add(g);
|
||||||
|
} else {
|
||||||
|
this.remove(g);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setVisible(this._playerNameInput, isJoinScreen);
|
||||||
|
setVisible(this._playerList, !isJoinScreen);
|
||||||
|
setVisible(this._joinPlayerButton, !isJoinScreen);
|
||||||
|
setVisible(this._spectatorsList, !isJoinScreen);
|
||||||
|
setVisible(this._bar3, !isJoinScreen);
|
||||||
|
setVisible(this._joinSpectatorButton, !isJoinScreen);
|
||||||
|
}
|
||||||
|
|
||||||
|
private _updatePositions(): void {
|
||||||
let y = spacingM;
|
let y = spacingM;
|
||||||
const halfWidth = screen.width / 2;
|
const halfWidth = this.engine.screen.width / 2;
|
||||||
|
|
||||||
this._table.pos = ex.vec(halfWidth, y);
|
this._table.pos = ex.vec(halfWidth, y);
|
||||||
y += spacingM;
|
y += spacingM;
|
||||||
@@ -147,28 +170,41 @@ export class LobbyScene extends GameStateScene {
|
|||||||
this._bar2.pos = ex.vec(halfWidth, y);
|
this._bar2.pos = ex.vec(halfWidth, y);
|
||||||
y += this._bar2.height + spacingL;
|
y += this._bar2.height + spacingL;
|
||||||
|
|
||||||
this._playerList.pos = ex.vec(halfWidth, y);
|
if (this._screen === "join") {
|
||||||
y += this._playerList.childrenHeight + spacingL;
|
this._playerNameInput.pos = ex.vec(halfWidth, y);
|
||||||
|
} else {
|
||||||
|
this._playerList.pos = ex.vec(halfWidth, y);
|
||||||
|
y += this._playerList.childrenHeight + spacingM;
|
||||||
|
|
||||||
this._joinPlayerButton.pos = ex.vec(halfWidth, y);
|
this._joinPlayerButton.pos = ex.vec(halfWidth, y);
|
||||||
y += this._joinPlayerButton.height + spacingL;
|
y += this._joinPlayerButton.height + spacingL;
|
||||||
|
|
||||||
this._bar3.pos = ex.vec(halfWidth, y);
|
this._bar3.pos = ex.vec(halfWidth, y);
|
||||||
y += this._bar3.height + spacingL;
|
y += this._bar3.height + spacingL;
|
||||||
|
|
||||||
|
this._spectatorsList.pos = ex.vec(halfWidth, y);
|
||||||
|
y += this._spectatorsList.childrenHeight + spacingM;
|
||||||
|
|
||||||
this._spectatorsList.pos = ex.vec(halfWidth, y);
|
this._joinSpectatorButton.pos = ex.vec(halfWidth, y);
|
||||||
y += this._spectatorsList.childrenHeight + spacingL;
|
y += this._spectatorsList.childrenHeight + spacingL;
|
||||||
|
}
|
||||||
this._joinSpectatorButton.pos = ex.vec(halfWidth, y);
|
|
||||||
y += this._spectatorsList.childrenHeight + spacingL;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public updateFromGameState(engine: ex.Engine, gameState: GameState): void {
|
public updateFromGameState(engine: ex.Engine, gameState: GameState): void {
|
||||||
this._playerList.labels = gameState.players;
|
this._playerList.labels = gameState.players;
|
||||||
this._spectatorsList.labels = gameState.spectators;
|
this._spectatorsList.labels = gameState.spectators;
|
||||||
this._updatePositions(engine.screen);
|
this._updatePositions();
|
||||||
|
}
|
||||||
|
|
||||||
|
public setScreen(screen: Screens): void {
|
||||||
|
this._screen = screen;
|
||||||
|
this._updateVisibility();
|
||||||
|
this._updatePositions();
|
||||||
|
}
|
||||||
|
|
||||||
|
private _join(): void {
|
||||||
|
this.setScreen("lobby");
|
||||||
|
// Send request
|
||||||
}
|
}
|
||||||
|
|
||||||
private _joinAsPlayer(): void {
|
private _joinAsPlayer(): void {
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ interface ButtonProperties {
|
|||||||
disabled?: boolean;
|
disabled?: boolean;
|
||||||
onClick?: (event: ex.PointerEvent) => void;
|
onClick?: (event: ex.PointerEvent) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
type ConstructProperties = ex.ActorArgs & ButtonProperties;
|
type ConstructProperties = ex.ActorArgs & ButtonProperties;
|
||||||
|
|
||||||
export class Button extends ex.ScreenElement {
|
export class Button extends ex.ScreenElement {
|
||||||
@@ -26,9 +27,8 @@ export class Button extends ex.ScreenElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
onInitialize(_engine: ex.Engine): void {
|
onInitialize(_engine: ex.Engine): void {
|
||||||
const margin = spacingS * 2;
|
const width = paragraphFont.measureTextWithoutCache(this.text).width + this.padding;
|
||||||
const width = paragraphFont.measureTextWithoutCache(this.text).width + margin;
|
const height = this.height;
|
||||||
const height = paragraphFont.size + margin;
|
|
||||||
|
|
||||||
this._textElement = new Text({
|
this._textElement = new Text({
|
||||||
text: this._text,
|
text: this._text,
|
||||||
@@ -76,6 +76,14 @@ export class Button extends ex.ScreenElement {
|
|||||||
this.graphics.use(value ? 'disabled' : 'idle')
|
this.graphics.use(value ? 'disabled' : 'idle')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get height(): number {
|
||||||
|
return paragraphFont.size + this.padding;
|
||||||
|
}
|
||||||
|
|
||||||
|
get padding(): number {
|
||||||
|
return 2 * spacingS;
|
||||||
|
}
|
||||||
|
|
||||||
onPointerUp(event: ex.PointerEvent): void {
|
onPointerUp(event: ex.PointerEvent): void {
|
||||||
if (this._disabled) {
|
if (this._disabled) {
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -0,0 +1,190 @@
|
|||||||
|
import * as ex from 'excalibur';
|
||||||
|
import { paragraphFont } from '../utils/fontUtil';
|
||||||
|
import { Text } from './Text';
|
||||||
|
import { spacingS } from '../utils/formattingUtils';
|
||||||
|
|
||||||
|
interface TextInputProperties {
|
||||||
|
text?: string;
|
||||||
|
width: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
type ConstructProperties = ex.ActorArgs & TextInputProperties;
|
||||||
|
|
||||||
|
export class TextInput extends ex.ScreenElement {
|
||||||
|
private _width: number;
|
||||||
|
private _text?: string;
|
||||||
|
private _textElement: Text = null!;
|
||||||
|
|
||||||
|
private _focused = false;
|
||||||
|
|
||||||
|
private _cursor: ex.ScreenElement = null!;
|
||||||
|
private _cursorPosition = 0;
|
||||||
|
private _cursorTimer: ex.Timer = null!
|
||||||
|
|
||||||
|
constructor(config: ConstructProperties) {
|
||||||
|
super(config);
|
||||||
|
this._width = config?.width;
|
||||||
|
this._text = config?.text;
|
||||||
|
}
|
||||||
|
|
||||||
|
onInitialize(engine: ex.Engine): void {
|
||||||
|
const width = this._width;
|
||||||
|
const height = this.height;
|
||||||
|
|
||||||
|
this._textElement = new Text({
|
||||||
|
text: this.text,
|
||||||
|
font: paragraphFont,
|
||||||
|
});
|
||||||
|
this._textElement.anchor = ex.vec(0, 0.5);
|
||||||
|
this._textElement.pos = ex.vec(-width / 2 + this.padding, height / 2);
|
||||||
|
this.addChild(this._textElement);
|
||||||
|
|
||||||
|
this.graphics.add(new ex.Rectangle({
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
color: ex.Color.fromHex("D3D3D3"),
|
||||||
|
}));
|
||||||
|
|
||||||
|
this._cursor = new ex.ScreenElement();
|
||||||
|
this._cursor.graphics.add(new ex.Line({
|
||||||
|
start: ex.vec(0, 0),
|
||||||
|
end: ex.vec(0, this.textHeight),
|
||||||
|
thickness: 2,
|
||||||
|
color: ex.Color.Black,
|
||||||
|
}));
|
||||||
|
this._cursor.graphics.isVisible = false;
|
||||||
|
this._cursor.pos = ex.vec(-width / 2 + this.padding, this.padding)
|
||||||
|
|
||||||
|
this._cursorTimer = new ex.Timer({
|
||||||
|
fcn: () => {
|
||||||
|
this._cursor.graphics.isVisible = !this._cursor.graphics.isVisible;
|
||||||
|
},
|
||||||
|
repeats: true,
|
||||||
|
interval: 500,
|
||||||
|
});
|
||||||
|
engine.currentScene.add(this._cursorTimer);
|
||||||
|
this.addChild(this._cursor);
|
||||||
|
|
||||||
|
this.on("pointerup", () => this._focus());
|
||||||
|
engine.input.keyboard.on("press", (e) => this._onKeyPress(e));
|
||||||
|
}
|
||||||
|
|
||||||
|
get height(): number {
|
||||||
|
return this.textHeight + 2 * spacingS;
|
||||||
|
}
|
||||||
|
|
||||||
|
get textHeight(): number {
|
||||||
|
return paragraphFont.size;
|
||||||
|
}
|
||||||
|
|
||||||
|
get padding(): number {
|
||||||
|
return spacingS;
|
||||||
|
}
|
||||||
|
|
||||||
|
get text(): string {
|
||||||
|
return this._text ?? "";
|
||||||
|
}
|
||||||
|
|
||||||
|
set text(value: string) {
|
||||||
|
this._text = value;
|
||||||
|
this._textElement.text = value;
|
||||||
|
|
||||||
|
this._updateCursor();
|
||||||
|
}
|
||||||
|
|
||||||
|
get cursorPosition(): number {
|
||||||
|
return this._cursorPosition;
|
||||||
|
}
|
||||||
|
|
||||||
|
set cursorPosition(value: number) {
|
||||||
|
this._cursorPosition = Math.min(Math.max(0, value), this.text.length);
|
||||||
|
this._updateCursor();
|
||||||
|
}
|
||||||
|
|
||||||
|
private _focus(): void {
|
||||||
|
this._focused = true;
|
||||||
|
this._cursorTimer.start();
|
||||||
|
this._cursor.graphics.isVisible = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private _unfocus(): void {
|
||||||
|
this._focused = false;
|
||||||
|
this._cursorTimer.stop();
|
||||||
|
this._cursor.graphics.isVisible = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private _updateCursor(): void {
|
||||||
|
const textBeforeCursor = this.text.slice(0, this._cursorPosition);
|
||||||
|
const textWidth = this._textElement.font.measureTextWithoutCache(textBeforeCursor).width;
|
||||||
|
const x = -this._width / 2 + this.padding + textWidth - 2;
|
||||||
|
this._cursor.pos = ex.vec(x, this.padding);
|
||||||
|
this._cursor.graphics.isVisible = true;
|
||||||
|
this._cursorTimer.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
private _onKeyPress(event: ex.KeyEvent): void {
|
||||||
|
if (!this._focused) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { key } = event;
|
||||||
|
|
||||||
|
if (key === ex.Keys.Esc) {
|
||||||
|
this._unfocus();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (key === ex.Keys.Enter) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( key === ex.Keys.Space) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const invalidKeys = [ex.Keys.Alt, ex.Keys.ShiftLeft, ex.Keys.ShiftRight, ex.Keys.AltLeft, ex.Keys.AltRight, ex.Keys.ControlLeft, ex.Keys.ControlRight,
|
||||||
|
ex.Keys.Home, ex.Keys.End, ex.Keys.PageDown, ex.Keys.PageUp, ex.Keys.Insert, ex.Keys.PrintScreen];
|
||||||
|
if (invalidKeys.includes(key)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (key === ex.Keys.Backspace) {
|
||||||
|
const firstPart = this.text.slice(0, this._cursorPosition - 1);
|
||||||
|
const secondPart = this.text.slice(this._cursorPosition);
|
||||||
|
this.text = firstPart.concat(secondPart);
|
||||||
|
this.cursorPosition--;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (key === ex.Keys.Delete) {
|
||||||
|
const firstPart = this.text.slice(0, this._cursorPosition);
|
||||||
|
const secondPart = this.text.slice(this._cursorPosition + 1);
|
||||||
|
this.text = firstPart.concat(secondPart);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (key === ex.Keys.ArrowLeft) {
|
||||||
|
this.cursorPosition--;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (key === ex.Keys.ArrowRight) {
|
||||||
|
this.cursorPosition++;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (key === ex.Keys.ArrowDown) {
|
||||||
|
this.cursorPosition = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (key === ex.Keys.ArrowUp) {
|
||||||
|
this.cursorPosition = this.text.length;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.text = this.text + event.value;
|
||||||
|
this.cursorPosition++;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -66,7 +66,7 @@ export class TitledListScreenElement extends ex.ScreenElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected computeChildrenHeight(numLabels: number): number {
|
protected computeChildrenHeight(numLabels: number): number {
|
||||||
return headingFont.size + spacingS + numLabels * (spacingS + paragraphFont.size);
|
return headingFont.size + spacingS + Math.max(1, numLabels) * (spacingS + paragraphFont.size);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected clearLabelEntities(): void {
|
protected clearLabelEntities(): void {
|
||||||
|
|||||||
Reference in new issue
Block a user