Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chat and commands (Fixes #55) #57

Merged
merged 9 commits into from
Feb 21, 2020
13 changes: 13 additions & 0 deletions client/include/gui/Text.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include <string>

#include <gk/graphics/RectangleShape.hpp>
#include <gk/graphics/Sprite.hpp>

class Text : public gk::Drawable, public gk::Transformable {
Expand All @@ -39,6 +40,13 @@ class Text : public gk::Drawable, public gk::Transformable {

const gk::Vector2i &getSize() const { return m_size; }

void setBackgroundColor(const gk::Color &color) { m_background.setFillColor(color); }
void setBackgroundSize(unsigned int width, unsigned int height) { m_background.setSize(width, height); }

void setPadding(int x, int y) { m_padding.x = x; m_padding.y = y; updateTextSprites(); }

void setMaxLineLength(unsigned int maxLineLength) { m_maxLineLength = maxLineLength; updateTextSprites(); }

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

Expand All @@ -54,8 +62,13 @@ class Text : public gk::Drawable, public gk::Transformable {
gk::VertexBuffer m_vbo;

gk::Vector2i m_size;
gk::Vector2i m_padding{0, 0};

gk::Color m_color = gk::Color::White;

gk::RectangleShape m_background;

unsigned int m_maxLineLength = 0;
};

#endif // TEXT_HPP_
53 changes: 53 additions & 0 deletions client/include/gui/TextInput.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* =====================================================================================
*
* OpenMiner
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* =====================================================================================
*/
#ifndef TEXTINPUT_HPP_
#define TEXTINPUT_HPP_

#include <gk/core/SDLHeaders.hpp>

#include "Text.hpp"

class TextInput : public gk::Drawable, public gk::Transformable {
public:
TextInput();

void onEvent(const SDL_Event &event);

const std::string &text() const { return m_content; }

void setBackgroundColor(const gk::Color &color) { m_text.setBackgroundColor(color); }
void setBackgroundSize(unsigned int width, unsigned int height) { m_text.setBackgroundSize(width, height); }

void setPadding(int x, int y) { m_text.setPadding(x, y); }

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

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

u16 m_characterLimit = 0;
};

#endif // TEXTINPUT_HPP_
46 changes: 46 additions & 0 deletions client/include/hud/Chat.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* =====================================================================================
*
* OpenMiner
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* =====================================================================================
*/
#ifndef CHAT_HPP_
#define CHAT_HPP_

#include <deque>

#include "ChatMessage.hpp"

class Client;

class Chat : public gk::Drawable, public gk::Transformable {
public:
Chat(Client &client);

void setMessageVisibility(bool areMessagesVisible);

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

std::deque<ChatMessage> m_chatMessages;

u32 m_posY = 0;
};

#endif // CHAT_HPP_
46 changes: 46 additions & 0 deletions client/include/hud/ChatMessage.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* =====================================================================================
*
* OpenMiner
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* =====================================================================================
*/
#ifndef CHATMESSAGE_HPP_
#define CHATMESSAGE_HPP_

#include "Text.hpp"

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

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

const Text &text() const { return m_text; }

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

Text m_text;

gk::Timer m_timer;

bool m_isVisible = false;
};

#endif // CHATMESSAGE_HPP_
5 changes: 5 additions & 0 deletions client/include/hud/HUD.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include "BlockCursor.hpp"
#include "BlockInfoWidget.hpp"
#include "Chat.hpp"
#include "Crosshair.hpp"
#include "DebugOverlay.hpp"
#include "Hotbar.hpp"
Expand All @@ -41,6 +42,8 @@ class HUD : public gk::Transformable, public gk::Drawable {

void update();

Chat &chat() { return m_chat; }

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

Expand All @@ -58,6 +61,8 @@ class HUD : public gk::Transformable, public gk::Drawable {
BlockInfoWidget m_blockInfoWidget;

Text m_fpsText;

Chat m_chat;
};

#endif // HUD_HPP_
3 changes: 3 additions & 0 deletions client/include/network/ClientCommandHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@ class ClientCommandHandler {
void sendBlockActivated(const glm::ivec4 &selectedBlock);
void sendBlockInvUpdate(Inventory &inventory);
void sendChunkRequest(s32 chunkX, s32 chunkY, s32 chunkZ);
void sendChatMessage(const std::string &message);

void setupCallbacks();

Client &client() { return m_client; }

bool isRegistryInitialized() const { return m_isRegistryInitialized; }

private:
Expand Down
52 changes: 52 additions & 0 deletions client/include/states/ChatState.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* =====================================================================================
*
* OpenMiner
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* =====================================================================================
*/
#ifndef CHATSTATE_HPP_
#define CHATSTATE_HPP_

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

class ClientCommandHandler;
class Chat;

class ChatState : public InterfaceState {
public:
ChatState(ClientCommandHandler &clientCommandHandler, Chat &chat, gk::ApplicationState *parent = nullptr);

void updateTextInputGeometry();

void onEvent(const SDL_Event &event) override;

void update() override;

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

TextInput m_textInput;

ClientCommandHandler &m_clientCommandHandler;

Chat &m_chat;
};

#endif // CHATSTATE_HPP_
2 changes: 2 additions & 0 deletions client/include/states/InterfaceState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class InterfaceState : public gk::ApplicationState {

void prepareDraw(gk::RenderTarget &target, gk::RenderStates &states) const;

bool m_drawBackground = true;

private:
gk::Shader m_shader;
// gk::View m_view;
Expand Down
2 changes: 2 additions & 0 deletions client/include/world/ClientPlayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class ClientPlayer : public Player {
float y() const { return m_y; }
float z() const { return m_z; }

void setPosition(float x, float y, float z);

const gk::Camera &camera() { return m_camera; }

private:
Expand Down
23 changes: 17 additions & 6 deletions client/source/gui/Text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include "Text.hpp"

Text::Text() : m_texture(gk::ResourceHandler::getInstance().get<gk::Texture>("texture-font")) {
m_background.setFillColor(gk::Color::Transparent);

updateCharWidth();
}

Expand All @@ -47,6 +49,10 @@ void Text::setColor(const gk::Color &color) {
void Text::draw(gk::RenderTarget &target, gk::RenderStates states) const {
states.transform *= getTransform();

target.draw(m_background, states);

states.transform.translate(m_padding.x, m_padding.y);

for(const gk::Sprite &sprite : m_textSprites) {
target.draw(sprite, states);
}
Expand All @@ -56,12 +62,12 @@ void Text::draw(gk::RenderTarget &target, gk::RenderStates states) const {
void Text::updateTextSprites() {
m_textSprites.clear();

int x = 0;
int y = 0;
int maxX = 0;
unsigned int x = 0;
unsigned int y = 0;
unsigned int maxX = 0;
gk::Color color = gk::Color{70, 70, 70, 255};
for(char c : m_text) {
if (c == '\n') {
if (c == '\n' || (m_maxLineLength && x + m_charWidth[(u8)c] >= m_maxLineLength)) {
y += 9;
x = 0;
continue;
Expand All @@ -78,7 +84,7 @@ void Text::updateTextSprites() {
y = 0;
color = m_color;
for(char c : m_text) {
if (c == '\n') {
if (c == '\n' || (m_maxLineLength && x + m_charWidth[(u8)c] >= m_maxLineLength)) {
maxX = std::max(x, maxX);
y += 9;
x = 0;
Expand All @@ -96,7 +102,12 @@ void Text::updateTextSprites() {
}

m_size.x = std::max(x, maxX);
m_size.y = 8 + y * 9;
m_size.y = y + 9;

unsigned int backgroundX = std::max<int>(m_background.getSize().x, m_size.x + m_padding.x);
unsigned int backgroundY = std::max<int>(m_background.getSize().y, m_size.y + m_padding.y);

m_background.setSize(backgroundX, backgroundY);
}

// FIXME: Since I use the font from Minecraft assets, I needed to use
Expand Down
Loading