From 0c7adf7cc2190e769ea09a9d0300d7e0e46e292b Mon Sep 17 00:00:00 2001 From: Tyler Lentz Date: Thu, 2 May 2024 15:32:01 -0700 Subject: [PATCH] add keyboard input for player name --- config.json | 6 +++--- include/client/gui/gui.hpp | 1 + src/client/client.cpp | 7 ++++++- src/client/gui/gui.cpp | 28 ++++++++++++++++------------ src/client/gui/widget/textinput.cpp | 4 ++++ 5 files changed, 30 insertions(+), 16 deletions(-) diff --git a/config.json b/config.json index b3e98048..656af412 100644 --- a/config.json +++ b/config.json @@ -7,12 +7,12 @@ "server_port": 2355 }, "server": { - "lobby_name": "Tyler's Lobby", + "lobby_name": "Unnamed Lobby", "lobby_broadcast": true, - "max_players": 1 + "max_players": 2 }, "client": { - "default_name": "Tyler", + "default_name": "John Doe", "lobby_discovery": true } } \ No newline at end of file diff --git a/include/client/gui/gui.hpp b/include/client/gui/gui.hpp index 5095e5d0..4e0da16a 100644 --- a/include/client/gui/gui.hpp +++ b/include/client/gui/gui.hpp @@ -52,6 +52,7 @@ class GUI { void setCaptureKeystrokes(bool should_capture); void captureKeystroke(char c); void captureBackspace(); + void clearCapturedKeyboardInput(); std::string getCapturedKeyboardInput() const; template diff --git a/src/client/client.cpp b/src/client/client.cpp index d6ce8223..2201825e 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -76,8 +76,13 @@ void Client::connectAndListen(std::string ip_addr) { this->session->connectTo(this->endpoints); + auto name = this->gui.getCapturedKeyboardInput(); + if (name == "") { + name = config.client.default_name; + } + auto packet = PackagedPacket::make_shared(PacketType::ClientDeclareInfo, - ClientDeclareInfoPacket { .player_name = config.client.default_name }); + ClientDeclareInfoPacket { .player_name = name }); this->session->sendPacketAsync(packet); diff --git a/src/client/gui/gui.cpp b/src/client/gui/gui.cpp index 4d260f89..c7799f47 100644 --- a/src/client/gui/gui.cpp +++ b/src/client/gui/gui.cpp @@ -103,6 +103,10 @@ void GUI::setCaptureKeystrokes(bool should_capture) { this->capture_keystrokes = should_capture; } +void GUI::clearCapturedKeyboardInput() { + this->keyboard_input = ""; +} + // TODO: reduce copied code between these two functions void GUI::handleClick(float x, float y) { @@ -240,18 +244,18 @@ void GUI::_layoutLobbyBrowser() { this->addWidget(std::move(lobbies_flex)); - // this->addWidget(widget::TextInput::make( - // glm::vec2(300.0f, 300.0f), - // "Enter a name", - // this, - // fonts, - // widget::DynText::Options { - // .font = font::Font::TEXT, - // .font_size = font::FontSizePx::SMALL, - // .color = font::getRGB(font::FontColor::BLACK), - // .scale = 1.0f - // } - // )); + this->addWidget(widget::TextInput::make( + glm::vec2(FRAC_WINDOW_WIDTH(2, 5), FRAC_WINDOW_HEIGHT(1, 6)), + "Enter a name", + this, + fonts, + widget::DynText::Options { + .font = font::Font::TEXT, + .font_size = font::FontSizePx::SMALL, + .color = font::getRGB(font::FontColor::BLACK), + .scale = 1.0f + } + )); } void GUI::_layoutLobby() { diff --git a/src/client/gui/widget/textinput.cpp b/src/client/gui/widget/textinput.cpp index aab574e1..94ececec 100644 --- a/src/client/gui/widget/textinput.cpp +++ b/src/client/gui/widget/textinput.cpp @@ -39,6 +39,10 @@ TextInput::TextInput(glm::vec2 origin, this->dyntext->addOnClick([gui](widget::Handle handle) { gui->setCaptureKeystrokes(true); }); + + auto [width, height] = this->dyntext->getSize(); + this->width = width; + this->height = height; } TextInput::TextInput(glm::vec2 origin,