diff --git a/client/source/core/ClientApplication.cpp b/client/source/core/ClientApplication.cpp index 83aaa7ef9..e6a4e6af9 100644 --- a/client/source/core/ClientApplication.cpp +++ b/client/source/core/ClientApplication.cpp @@ -36,6 +36,7 @@ #include "TextureLoader.hpp" #include "GameState.hpp" +#include "ServerConnectState.hpp" #include "ServerLoadingState.hpp" #include "TitleScreenState.hpp" @@ -76,6 +77,7 @@ void ClientApplication::init() { Registry::setInstance(m_registry); // m_stateStack.push(); + // m_stateStack.push(); m_stateStack.push(m_host, m_port); diff --git a/client/source/gui/Text.hpp b/client/source/gui/Text.hpp index 04c52b050..c54f47c5b 100644 --- a/client/source/gui/Text.hpp +++ b/client/source/gui/Text.hpp @@ -46,9 +46,11 @@ class Text : public gk::Drawable, public gk::Transformable { void setColor(const gk::Color &color); const gk::Vector2i &getSize() const { return m_size; } + gk::Vector2f getBackgroundSize() const { return m_background.getSize(); } void setBackgroundColor(const gk::Color &color) { m_background.setFillColor(color); } void setBackgroundSize(unsigned int width, unsigned int height) { m_background.setSize(width, height); } + void setBackgroundOutline(int thickness, const gk::Color &color) { m_background.setOutlineThickness(thickness); m_background.setOutlineColor(color); } void setPadding(int x, int y); @@ -69,7 +71,7 @@ class Text : public gk::Drawable, public gk::Transformable { mutable u32 m_verticesCount = 0; mutable bool m_isUpdateNeeded = true; - mutable gk::Vector2i m_size; + mutable gk::Vector2i m_size{0, 0}; gk::Vector2i m_padding{0, 0}; gk::Color m_color = gk::Color::White; diff --git a/client/source/gui/TextInput.hpp b/client/source/gui/TextInput.hpp index 1b551709b..5c38789eb 100644 --- a/client/source/gui/TextInput.hpp +++ b/client/source/gui/TextInput.hpp @@ -40,10 +40,14 @@ class TextInput : public gk::Drawable, public gk::Transformable { const std::string &text() const { return m_content; } void setText(const std::string &text) { m_content = text; m_text.setText(m_content + m_cursor); } + gk::Vector2f getBackgroundSize() const { return m_text.getBackgroundSize(); } + void setBackgroundColor(const gk::Color &color) { m_text.setBackgroundColor(color); } void setBackgroundSize(unsigned int width, unsigned int height) { m_text.setBackgroundSize(width, height); } + void setBackgroundOutline(int thickness, const gk::Color &color) { m_text.setBackgroundOutline(thickness, color); } void setPadding(int x, int y) { m_text.setPadding(x, y); } + void setCharacterLimit(u16 characterLimit) { m_characterLimit = characterLimit; } private: void draw(gk::RenderTarget &target, gk::RenderStates states) const override; diff --git a/client/source/states/ServerConnectState.cpp b/client/source/states/ServerConnectState.cpp index beaa8aa3a..e3fb2aba2 100644 --- a/client/source/states/ServerConnectState.cpp +++ b/client/source/states/ServerConnectState.cpp @@ -31,23 +31,26 @@ #include "ServerLoadingState.hpp" ServerConnectState::ServerConnectState(gk::ApplicationState *parent) : InterfaceState(parent) { - m_textInput.setContent("localhost:4242"); + m_textInput.setText("localhost:4242"); m_textInput.setCharacterLimit(15 + 1 + 6); - m_textInput.setSize(400, 54); - m_textInput.setPosition(Config::screenWidth / 2.0f - m_textInput.getSize().x / 2, Config::screenHeight / 2.0f - m_textInput.getSize().y / 2); - m_textInput.setCursor("_"); + m_textInput.setBackgroundSize(150, 20); + m_textInput.setBackgroundOutline(1, gk::Color::White); + m_textInput.setPadding(5, 6); + m_textInput.setScale(Config::guiScale, Config::guiScale); + m_textInput.setPosition(Config::screenWidth / 2.0f - m_textInput.getBackgroundSize().x * Config::guiScale / 2.0f, + Config::screenHeight / 2.0f - m_textInput.getBackgroundSize().y * Config::guiScale / 2.0f); m_connectButton.setText("Connect"); m_connectButton.setPosition(Config::screenWidth / 2.0f - m_connectButton.getGlobalBounds().sizeX * Config::guiScale / 2.0f, Config::screenHeight - 340); - m_connectButton.setScale(Config::guiScale, Config::guiScale, 1); + m_connectButton.setScale(Config::guiScale, Config::guiScale); m_connectButton.setCallback([this](TextButton &) { - size_t sep = m_textInput.content().find_first_of(':'); + size_t sep = m_textInput.text().find_first_of(':'); - std::string host = m_textInput.content().substr(0, sep); + std::string host = m_textInput.text().substr(0, sep); int port = 0; try { - port = std::stoi(m_textInput.content().substr(sep + 1)); + port = std::stoi(m_textInput.text().substr(sep + 1)); } catch (const std::invalid_argument &e) { std::cerr << "Error: Invalid server address." << std::endl; @@ -61,7 +64,7 @@ ServerConnectState::ServerConnectState(gk::ApplicationState *parent) : Interface m_cancelButton.setText("Cancel"); m_cancelButton.setPosition(Config::screenWidth / 2.0f - m_cancelButton.getGlobalBounds().sizeX * Config::guiScale / 2.0f, Config::screenHeight - 261); - m_cancelButton.setScale(Config::guiScale, Config::guiScale, 1); + m_cancelButton.setScale(Config::guiScale, Config::guiScale); m_cancelButton.setCallback([this](TextButton &) { m_stateStack->pop(); }); @@ -71,7 +74,9 @@ void ServerConnectState::onEvent(const SDL_Event &event) { InterfaceState::onEvent(event); if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) { - m_textInput.setPosition(Config::screenWidth / 2.0f - m_textInput.getSize().x / 2, Config::screenHeight / 2.0f - m_textInput.getSize().y / 2); + m_textInput.setPosition(Config::screenWidth / 2.0f - m_textInput.getBackgroundSize().x * Config::guiScale / 2.0f, + Config::screenHeight / 2.0f - m_textInput.getBackgroundSize().y * Config::guiScale / 2.0f); + m_connectButton.setPosition(Config::screenWidth / 2.0f - m_connectButton.getGlobalBounds().sizeX / 2, Config::screenHeight - 340); m_cancelButton.setPosition(Config::screenWidth / 2.0f - m_cancelButton.getGlobalBounds().sizeX / 2, Config::screenHeight - 261); } diff --git a/client/source/states/ServerConnectState.hpp b/client/source/states/ServerConnectState.hpp index 8f0702f6d..cc893b244 100644 --- a/client/source/states/ServerConnectState.hpp +++ b/client/source/states/ServerConnectState.hpp @@ -27,10 +27,9 @@ #ifndef SERVERCONNECTSTATE_HPP_ #define SERVERCONNECTSTATE_HPP_ -#include - #include "InterfaceState.hpp" #include "TextButton.hpp" +#include "TextInput.hpp" class ServerConnectState : public InterfaceState { public: @@ -43,7 +42,7 @@ class ServerConnectState : public InterfaceState { private: void draw(gk::RenderTarget &target, gk::RenderStates states) const override; - gk::TextInput m_textInput; + TextInput m_textInput; TextButton m_connectButton; TextButton m_cancelButton;