Skip to content

Commit

Permalink
[ServerConnectState] Now uses TextInput instead of gk::TextInput.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Mar 4, 2020
1 parent 10d5154 commit d804aac
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 14 deletions.
2 changes: 2 additions & 0 deletions client/source/core/ClientApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "TextureLoader.hpp"

#include "GameState.hpp"
#include "ServerConnectState.hpp"
#include "ServerLoadingState.hpp"
#include "TitleScreenState.hpp"

Expand Down Expand Up @@ -76,6 +77,7 @@ void ClientApplication::init() {
Registry::setInstance(m_registry);

// m_stateStack.push<TitleScreenState>();
// m_stateStack.push<ServerConnectState>();

m_stateStack.push<GameState>(m_host, m_port);

Expand Down
4 changes: 3 additions & 1 deletion client/source/gui/Text.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions client/source/gui/TextInput.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
25 changes: 15 additions & 10 deletions client/source/states/ServerConnectState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
});
Expand All @@ -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);
}
Expand Down
5 changes: 2 additions & 3 deletions client/source/states/ServerConnectState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@
#ifndef SERVERCONNECTSTATE_HPP_
#define SERVERCONNECTSTATE_HPP_

#include <gk/graphics/TextInput.hpp>

#include "InterfaceState.hpp"
#include "TextButton.hpp"
#include "TextInput.hpp"

class ServerConnectState : public InterfaceState {
public:
Expand All @@ -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;
Expand Down

0 comments on commit d804aac

Please sign in to comment.