Skip to content

Commit

Permalink
add keyboard input for player name
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler-Lentz committed May 2, 2024
1 parent 53ccd2d commit 0c7adf7
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 16 deletions.
6 changes: 3 additions & 3 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
1 change: 1 addition & 0 deletions include/client/gui/gui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class GUI {
void setCaptureKeystrokes(bool should_capture);
void captureKeystroke(char c);
void captureBackspace();
void clearCapturedKeyboardInput();
std::string getCapturedKeyboardInput() const;

template <typename W>
Expand Down
7 changes: 6 additions & 1 deletion src/client/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
28 changes: 16 additions & 12 deletions src/client/gui/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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() {
Expand Down
4 changes: 4 additions & 0 deletions src/client/gui/widget/textinput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 0c7adf7

Please sign in to comment.