From 55d0b2b361d9df0fd2b765bf916a5b8db95adf57 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sat, 3 Dec 2016 23:26:24 +1100 Subject: [PATCH] Added ability to change username, handle & avatar in lobby --- client/base_scene.cpp | 4 ++ client/base_scene.hpp | 3 +- client/client_application.cpp | 10 ++++ client/scenes/lobby_menu.cpp | 88 ++++++++++++++++++++++++++++++++--- client/scenes/lobby_menu.hpp | 6 +++ 5 files changed, 104 insertions(+), 7 deletions(-) diff --git a/client/base_scene.cpp b/client/base_scene.cpp index 03d7f4c..85dda51 100644 --- a/client/base_scene.cpp +++ b/client/base_scene.cpp @@ -103,3 +103,7 @@ void BaseScene::KeyDown(SDL_KeyboardEvent const& event) { void BaseScene::KeyUp(SDL_KeyboardEvent const& event) { //EMPTY } + +void BaseScene::TextInput(SDL_TextInputEvent const& event) { + //EMPTY +} diff --git a/client/base_scene.hpp b/client/base_scene.hpp index 877af3a..f15f32d 100644 --- a/client/base_scene.hpp +++ b/client/base_scene.hpp @@ -47,8 +47,9 @@ public: virtual void MouseWheel(SDL_MouseWheelEvent const& event); virtual void KeyDown(SDL_KeyboardEvent const& event); virtual void KeyUp(SDL_KeyboardEvent const& event); + virtual void TextInput(SDL_TextInputEvent const& event); - //TODO: (9) joystick and controller events + //TODO: joystick and controller events protected: //control diff --git a/client/client_application.cpp b/client/client_application.cpp index 7009125..bcf2441 100644 --- a/client/client_application.cpp +++ b/client/client_application.cpp @@ -188,6 +188,12 @@ void ClientApplication::Init(int argc, char* argv[]) { DEBUG_INTERNAL_VAR(MAX_PACKET_SIZE); DEBUG_INTERNAL_VAR(static_cast(SerialPacketType::LAST)); + //------------------------- + //BUGFIX + //------------------------- + + SDL_StopTextInput(); + //------------------------- //finalize the startup //------------------------- @@ -298,6 +304,10 @@ void ClientApplication::ProcessEvents() { activeScene->KeyUp(event.key); break; + case SDL_TEXTINPUT: + activeScene->TextInput(event.text); + break; + //TODO: (9) joystick and controller events //window events are handled internally diff --git a/client/scenes/lobby_menu.cpp b/client/scenes/lobby_menu.cpp index 7c8e385..ac4d9ef 100644 --- a/client/scenes/lobby_menu.cpp +++ b/client/scenes/lobby_menu.cpp @@ -68,6 +68,26 @@ LobbyMenu::LobbyMenu(int* const argClientIndex, int* const argAccountIndex): backButton.SetX(50); backButton.SetY(90); + //setup the text fields + username.SetText(GetRenderer(), font, WHITE, config["client.username"]); + password.SetText(GetRenderer(), font, WHITE, config["client.password"]); + handle.SetText(GetRenderer(), font, WHITE, config["client.handle"]); + avatar.SetText(GetRenderer(), font, WHITE, config["client.avatar"]); + + username.SetBounds(BoundingBox{0, 0, 300, 20}); + password.SetBounds(BoundingBox{0, 0, 300, 20}); + handle.SetBounds(BoundingBox{0, 0, 300, 20}); + avatar.SetBounds(BoundingBox{0, 0, 300, 20}); + + username.SetX(50); + username.SetY(110); + password.SetX(50); + password.SetY(130); + handle.SetX(50); + handle.SetY(150); + avatar.SetX(50); + avatar.SetY(170); + //pseudo-list selection //TODO: move this into the UI library? boundingBox = {300, 50, 200, 12}; @@ -115,6 +135,11 @@ void LobbyMenu::RenderFrame(SDL_Renderer* renderer) { joinButton.DrawTo(renderer); backButton.DrawTo(renderer); + username.DrawTo(renderer); + password.DrawTo(renderer); + handle.DrawTo(renderer); + avatar.DrawTo(renderer); + //TODO: (3) draw headers for the server list //TODO: (3) ping/delay displayed in the server list for (int i = 0; i < serverVector.size(); i++) { @@ -148,6 +173,19 @@ void LobbyMenu::MouseButtonDown(SDL_MouseButtonEvent const& event) { searchButton.MouseButtonDown(event); joinButton.MouseButtonDown(event); backButton.MouseButtonDown(event); + + if (username.MouseButtonDown(event)) { + SDL_StartTextInput(); + } + if (password.MouseButtonDown(event)) { + SDL_StartTextInput(); + } + if (handle.MouseButtonDown(event)) { + SDL_StartTextInput(); + } + if (avatar.MouseButtonDown(event)) { + SDL_StartTextInput(); + } } void LobbyMenu::MouseButtonUp(SDL_MouseButtonEvent const& event) { @@ -182,6 +220,22 @@ void LobbyMenu::KeyDown(SDL_KeyboardEvent const& event) { case SDLK_ESCAPE: SetSceneSignal(SceneSignal::MAINMENU); break; + + case SDLK_BACKSPACE: + //easier than mucking about with SDL_TextEditEvent + if (username.GetFocus()) { + username.PopChars(GetRenderer(), font, WHITE, 1); + } + if (password.GetFocus()) { + password.PopChars(GetRenderer(), font, WHITE, 1); + } + if (handle.GetFocus()) { + handle.PopChars(GetRenderer(), font, WHITE, 1); + } + if (avatar.GetFocus()) { + avatar.PopChars(GetRenderer(), font, WHITE, 1); + } + break; } } @@ -189,6 +243,21 @@ void LobbyMenu::KeyUp(SDL_KeyboardEvent const& event) { // } +void LobbyMenu::TextInput(SDL_TextInputEvent const& event) { + if (username.GetFocus()) { + username.PushText(GetRenderer(), font, WHITE, std::string(event.text)); + } + if (password.GetFocus()) { + password.PushText(GetRenderer(), font, WHITE, std::string(event.text)); + } + if (handle.GetFocus()) { + handle.PushText(GetRenderer(), font, WHITE, std::string(event.text)); + } + if (avatar.GetFocus()) { + avatar.PushText(GetRenderer(), font, WHITE, std::string(event.text)); + } +} + //------------------------- //Network handlers //------------------------- @@ -303,13 +372,14 @@ void LobbyMenu::SendBroadcastRequest() { } void LobbyMenu::SendJoinRequest() { + //BUG: 101 received in LobbyMenu on failed join //pack the packet - ClientPacket packet; - packet.type = SerialPacketType::JOIN_REQUEST; + ClientPacket packet; + packet.type = SerialPacketType::JOIN_REQUEST; - //join the selected server - network.SendTo(selection->address, &packet); - selection = nullptr; + //join the selected server + network.SendTo(selection->address, &packet); + selection = nullptr; } void LobbyMenu::SendLoginRequest() { @@ -318,7 +388,13 @@ void LobbyMenu::SendLoginRequest() { ClientPacket packet; packet.type = SerialPacketType::LOGIN_REQUEST; packet.clientIndex = clientIndex; - strncpy(packet.username, config["client.username"].c_str(), PACKET_STRING_SIZE+1); + strncpy(packet.username, username.GetText().c_str(), PACKET_STRING_SIZE+1); network.SendTo(Channels::SERVER, &packet); + + //TODO: remove + config["client.username"] = username.GetText(); + config["client.password"] = password.GetText(); + config["client.handle"] = handle.GetText(); + config["client.avatar"] = avatar.GetText(); } diff --git a/client/scenes/lobby_menu.hpp b/client/scenes/lobby_menu.hpp index a13b2da..4cb020d 100644 --- a/client/scenes/lobby_menu.hpp +++ b/client/scenes/lobby_menu.hpp @@ -25,6 +25,7 @@ #include "image.hpp" #include "button.hpp" #include "bounding_box.hpp" +#include "text_field.hpp" #include "text_line.hpp" #include "SDL2/SDL_ttf.h" @@ -61,6 +62,7 @@ protected: void MouseWheel(SDL_MouseWheelEvent const& event) override; void KeyDown(SDL_KeyboardEvent const& event) override; void KeyUp(SDL_KeyboardEvent const& event) override; + void TextInput(SDL_TextInputEvent const& event) override; //Network handlers void HandlePacket(SerialPacket* const); @@ -102,6 +104,10 @@ protected: Button searchButton; Button joinButton; Button backButton; + TextField username; + TextField password; + TextField handle; + TextField avatar; std::vector serverVector; ServerInfo* selection = nullptr;