Skip to content

Commit

Permalink
[ServerConnectState] Text input added to select an username.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Jun 25, 2020
1 parent ac70901 commit 0eaa133
Show file tree
Hide file tree
Showing 22 changed files with 195 additions and 76 deletions.
3 changes: 2 additions & 1 deletion docs/lua-api-cpp.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,15 @@

## Player

- `Inventory *inventory()`
- `string name()`
- `double x()`
- `double y()`
- `double z()`
- `void set_position(double x, double y, double z)`
- `u16 dimension()`
- `void set_dimension(u16 dimension)`
- `u16 client_id()`
- `Inventory *inventory()`

## Recipe

Expand Down
6 changes: 5 additions & 1 deletion docs/network-protocol.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ _This packet has no field._

Packet sent from a client attempting to connect.

_This packet has no field._
| Field name | Field type | Notes |
| ------------- | ----------- | ---------------------------------------------------- |
| Username | std::string | Name of the player attempting to connect |

#### ClientDisconnect

Expand Down Expand Up @@ -87,6 +89,8 @@ _This packet has no field._
| Player X | double | Player X coordinate |
| Player Y | double | Player Y coordinate |
| Player Z | double | Player Z coordinate |
| Dimension | u16 | Dimension ID |
| Username | std::string | Name of the player |

#### PlayerChangeDimension

Expand Down
4 changes: 2 additions & 2 deletions source/client/gui/Text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ void Text::draw(gk::RenderTarget &target, gk::RenderStates states) const {
m_isUpdateNeeded = false;
}

if (m_verticesCount == 0) return;

states.transform *= getTransform();

target.draw(m_background, states);

if (m_verticesCount == 0) return;

states.transform.translate(m_padding.x, m_padding.y);
states.texture = &m_font.texture();
states.vertexAttributes = VertexAttribute::Basic;
Expand Down
43 changes: 32 additions & 11 deletions source/client/gui/TextInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,50 @@
#include "TextInput.hpp"

TextInput::TextInput() {
m_text.setString(std::string{m_cursor});
m_cursor.setString("_");

m_placeholder.setColor(gk::Color(150, 150, 150));
}

void TextInput::onEvent(const sf::Event &event) {
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Backspace && !m_content.empty()) {
m_content.erase(m_content.begin() + m_content.length() - 1);

m_text.setString(m_content + m_cursor);
}
if (m_hasFocus) {
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Backspace && !m_content.empty()) {
m_content.erase(m_content.begin() + m_content.length() - 1);

if (event.type == sf::Event::TextEntered) {
u32 c = event.text.unicode;
if (isprint(c) && (!m_characterLimit || m_content.size() < m_characterLimit)) {
m_content += c;
m_text.setString(m_content);
m_text.updateVertexBuffer();
m_cursor.setPosition(m_text.getSize().x, 0);
}

m_text.setString(m_content + m_cursor);
if (event.type == sf::Event::TextEntered) {
u32 c = event.text.unicode;
if (isprint(c) && (!m_characterLimit || m_content.size() < m_characterLimit)) {
m_content += c;
}

m_text.setString(m_content);
m_text.updateVertexBuffer();
m_cursor.setPosition(m_text.getSize().x, 0);
}
}
}

void TextInput::setString(const std::string &string) {
m_content = string;
m_text.setString(m_content);
m_text.updateVertexBuffer();
m_cursor.setPosition(m_text.getSize().x, 0);
}

void TextInput::draw(gk::RenderTarget &target, gk::RenderStates states) const {
states.transform *= getTransform();

target.draw(m_text, states);

if (m_hasFocus)
target.draw(m_cursor, states);

if (m_content.empty() && !m_hasFocus)
target.draw(m_placeholder, states);
}

15 changes: 12 additions & 3 deletions source/client/gui/TextInput.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,34 @@ class TextInput : public gk::Drawable, public gk::Transformable {
void onEvent(const sf::Event &event);

const std::string &string() const { return m_content; }
void setString(const std::string &string) { m_content = string; m_text.setString(m_content + m_cursor); }
void setString(const std::string &string);

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 setPadding(int x, int y) { m_text.setPadding(x, y); m_cursor.setPadding(x, y); m_placeholder.setPadding(x, y); }
void setCharacterLimit(u16 characterLimit) { m_characterLimit = characterLimit; }

void setPlaceholder(const std::string &placeholder) { m_placeholder.setString(placeholder); }

void setFocus(bool hasFocus) { m_hasFocus = hasFocus; }

private:
void draw(gk::RenderTarget &target, gk::RenderStates states) const override;

Text m_text;
std::string m_content;
char m_cursor = '_';

Text m_cursor;

u16 m_characterLimit = 0;

Text m_placeholder;

bool m_hasFocus = true;
};

#endif // TEXTINPUT_HPP_
7 changes: 5 additions & 2 deletions source/client/hud/Chat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@
#include "Config.hpp"

Chat::Chat(Client &client) {
client.setCommandCallback(Network::Command::ChatMessage, [this](sf::Packet &packet) {
client.setCommandCallback(Network::Command::ChatMessage, [&](sf::Packet &packet) {
u16 clientID;
std::string message;
packet >> clientID >> message;

m_chatMessages.emplace_back(clientID, message, m_posY);
if (clientID != 0)
m_chatMessages.emplace_back(message, m_posY, &client.getPlayer(clientID));
else
m_chatMessages.emplace_back(message, m_posY);

m_posY += m_chatMessages.back().text().getSize().y + 1;

Expand Down
7 changes: 4 additions & 3 deletions source/client/hud/ChatMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@
* =====================================================================================
*/
#include "ChatMessage.hpp"
#include "Player.hpp"

ChatMessage::ChatMessage(u16 clientID, const std::string &message, u32 posY) {
if (clientID > 0)
m_text.setString("<Client " + std::to_string(clientID) + "> " + message);
ChatMessage::ChatMessage(const std::string &message, u32 posY, const Player *player) {
if (player)
m_text.setString("<" + player->name() + "> " + message);
else
m_text.setString(message);

Expand Down
4 changes: 3 additions & 1 deletion source/client/hud/ChatMessage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@

#include "Text.hpp"

class Player;

class ChatMessage : public gk::Drawable, public gk::Transformable {
public:
ChatMessage(u16 clientID, const std::string &message, u32 posY);
ChatMessage(const std::string &message, u32 posY, const Player *player = nullptr);

void setVisible(bool isVisible) { m_isVisible = isVisible; }

Expand Down
4 changes: 2 additions & 2 deletions source/client/network/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

#include "Client.hpp"

void Client::connect(sf::IpAddress serverAddress, u16 serverPort) {
void Client::connect(sf::IpAddress serverAddress, u16 serverPort, Player &player) {
m_serverAddress = serverAddress;
m_serverPort = serverPort;

Expand All @@ -40,7 +40,7 @@ void Client::connect(sf::IpAddress serverAddress, u16 serverPort) {
throw ClientConnectException("Network error: Unable to connect to server " + serverAddress.toString() + ":" + std::to_string(serverPort));

Network::Packet packet;
packet << Network::Command::ClientConnect;
packet << Network::Command::ClientConnect << player.name();
m_tcpSocket->send(packet);

Network::Packet answer;
Expand Down
9 changes: 8 additions & 1 deletion source/client/network/Client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <gk/core/Timer.hpp>

#include "Network.hpp"
#include "Player.hpp"

class ClientConnectException {
public:
Expand All @@ -55,7 +56,7 @@ class Client {
using CommandCallback = std::function<void(Network::Packet &packet)>;

public:
void connect(sf::IpAddress serverAddress, u16 serverPort);
void connect(sf::IpAddress serverAddress, u16 serverPort, Player &player);
void disconnect();

void send(Network::Packet &packet);
Expand Down Expand Up @@ -83,6 +84,10 @@ class Client {
const std::string &worldName() const { return m_worldName; }
void setWorldName(const std::string &worldName) { m_worldName = worldName; }

void addPlayer(Player &player) { m_players.emplace(player.clientID(), player); }
void removePlayer(u16 clientID) { m_players.erase(clientID); }
Player &getPlayer(u16 clientID) { return m_players.at(clientID); }

private:
bool m_isConnected = false;
bool m_isSingleplayer = false;
Expand All @@ -100,6 +105,8 @@ class Client {

std::string m_texturePack;
std::string m_worldName;

std::unordered_map<u16, Player &> m_players;
};

#endif // CLIENT_HPP_
16 changes: 12 additions & 4 deletions source/client/network/ClientCommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,10 @@ void ClientCommandHandler::setupCallbacks() {
packet >> clientID;

auto it = m_playerBoxes.find(clientID);
if (it != m_playerBoxes.end())
if (it != m_playerBoxes.end()) {
m_client.addPlayer(m_playerBoxes.at(clientID));
m_playerBoxes.erase(it);
}
});

m_client.setCommandCallback(Network::Command::ServerClosed, [this](Network::Packet &packet) {
Expand Down Expand Up @@ -227,12 +229,18 @@ void ClientCommandHandler::setupCallbacks() {
m_client.setCommandCallback(Network::Command::PlayerSpawn, [this](Network::Packet &packet) {
u16 clientId;
gk::Vector3d pos;
packet >> clientId >> pos.x >> pos.y >> pos.z;
u16 dimension;
std::string username;
packet >> clientId >> pos.x >> pos.y >> pos.z >> dimension >> username;

if (clientId != m_client.id()) {
m_playerBoxes.emplace(clientId, PlayerBox{m_player.camera()});
m_playerBoxes.at(clientId).setPosition(pos.x, pos.y, pos.z);
m_playerBoxes.at(clientId).setClientID(clientId);
Player &player = m_playerBoxes.at(clientId);
player.setPosition(pos.x, pos.y, pos.z);
player.setDimension(dimension);
player.setClientID(clientId);
player.setName(username);
m_client.addPlayer(player);
}
else {
m_player.setPosition(pos.x, pos.y, pos.z);
Expand Down
6 changes: 4 additions & 2 deletions source/client/states/GameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,11 @@ void GameState::init() {
m_eventHandler->addListener<GuiScaleChangedEvent>(&GameState::onGuiScaleChanged, this);
}

void GameState::connect(const std::string &host, int port) {
m_client.connect(host, port);
void GameState::connect(const std::string &host, int port, const std::string &username) {
m_client.connect(host, port, m_player);
m_player.setClientID(m_client.id());
m_player.setName(username);
m_client.addPlayer(m_player);

gk::Mouse::setCursorVisible(false);
gk::Mouse::setCursorGrabbed(true);
Expand Down
2 changes: 1 addition & 1 deletion source/client/states/GameState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class GameState : public gk::ApplicationState {

void init() override;

void connect(const std::string &host, int port);
void connect(const std::string &host, int port, const std::string &username);

void onEvent(const sf::Event &event) override;

Expand Down
Loading

0 comments on commit 0eaa133

Please sign in to comment.