Skip to content

Commit 2724cb0

Browse files
authored
Merge pull request #57 from Unarelith/chat-and-commands
2 parents 4b725b8 + bb0c8e8 commit 2724cb0

25 files changed

+592
-17
lines changed

client/include/gui/Text.hpp

+13
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include <string>
2727

28+
#include <gk/graphics/RectangleShape.hpp>
2829
#include <gk/graphics/Sprite.hpp>
2930

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

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

43+
void setBackgroundColor(const gk::Color &color) { m_background.setFillColor(color); }
44+
void setBackgroundSize(unsigned int width, unsigned int height) { m_background.setSize(width, height); }
45+
46+
void setPadding(int x, int y) { m_padding.x = x; m_padding.y = y; updateTextSprites(); }
47+
48+
void setMaxLineLength(unsigned int maxLineLength) { m_maxLineLength = maxLineLength; updateTextSprites(); }
49+
4250
private:
4351
void draw(gk::RenderTarget &target, gk::RenderStates states) const override;
4452

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

5664
gk::Vector2i m_size;
65+
gk::Vector2i m_padding{0, 0};
5766

5867
gk::Color m_color = gk::Color::White;
68+
69+
gk::RectangleShape m_background;
70+
71+
unsigned int m_maxLineLength = 0;
5972
};
6073

6174
#endif // TEXT_HPP_

client/include/gui/TextInput.hpp

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* =====================================================================================
3+
*
4+
* OpenMiner
5+
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <[email protected]>
6+
*
7+
* This program is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU Lesser General Public
9+
* License as published by the Free Software Foundation; either
10+
* version 2.1 of the License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public License
18+
* along with this program; if not, write to the Free Software Foundation, Inc.,
19+
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* =====================================================================================
22+
*/
23+
#ifndef TEXTINPUT_HPP_
24+
#define TEXTINPUT_HPP_
25+
26+
#include <gk/core/SDLHeaders.hpp>
27+
28+
#include "Text.hpp"
29+
30+
class TextInput : public gk::Drawable, public gk::Transformable {
31+
public:
32+
TextInput();
33+
34+
void onEvent(const SDL_Event &event);
35+
36+
const std::string &text() const { return m_content; }
37+
38+
void setBackgroundColor(const gk::Color &color) { m_text.setBackgroundColor(color); }
39+
void setBackgroundSize(unsigned int width, unsigned int height) { m_text.setBackgroundSize(width, height); }
40+
41+
void setPadding(int x, int y) { m_text.setPadding(x, y); }
42+
43+
private:
44+
void draw(gk::RenderTarget &target, gk::RenderStates states) const override;
45+
46+
Text m_text;
47+
std::string m_content;
48+
char m_cursor = '_';
49+
50+
u16 m_characterLimit = 0;
51+
};
52+
53+
#endif // TEXTINPUT_HPP_

client/include/hud/Chat.hpp

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* =====================================================================================
3+
*
4+
* OpenMiner
5+
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <[email protected]>
6+
*
7+
* This program is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU Lesser General Public
9+
* License as published by the Free Software Foundation; either
10+
* version 2.1 of the License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public License
18+
* along with this program; if not, write to the Free Software Foundation, Inc.,
19+
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* =====================================================================================
22+
*/
23+
#ifndef CHAT_HPP_
24+
#define CHAT_HPP_
25+
26+
#include <deque>
27+
28+
#include "ChatMessage.hpp"
29+
30+
class Client;
31+
32+
class Chat : public gk::Drawable, public gk::Transformable {
33+
public:
34+
Chat(Client &client);
35+
36+
void setMessageVisibility(bool areMessagesVisible);
37+
38+
private:
39+
void draw(gk::RenderTarget &target, gk::RenderStates states) const override;
40+
41+
std::deque<ChatMessage> m_chatMessages;
42+
43+
u32 m_posY = 0;
44+
};
45+
46+
#endif // CHAT_HPP_

client/include/hud/ChatMessage.hpp

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* =====================================================================================
3+
*
4+
* OpenMiner
5+
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <[email protected]>
6+
*
7+
* This program is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU Lesser General Public
9+
* License as published by the Free Software Foundation; either
10+
* version 2.1 of the License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public License
18+
* along with this program; if not, write to the Free Software Foundation, Inc.,
19+
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* =====================================================================================
22+
*/
23+
#ifndef CHATMESSAGE_HPP_
24+
#define CHATMESSAGE_HPP_
25+
26+
#include "Text.hpp"
27+
28+
class ChatMessage : public gk::Drawable, public gk::Transformable {
29+
public:
30+
ChatMessage(u16 clientID, const std::string &message, u32 posY);
31+
32+
void setVisible(bool isVisible) { m_isVisible = isVisible; }
33+
34+
const Text &text() const { return m_text; }
35+
36+
private:
37+
void draw(gk::RenderTarget &target, gk::RenderStates states) const override;
38+
39+
Text m_text;
40+
41+
gk::Timer m_timer;
42+
43+
bool m_isVisible = false;
44+
};
45+
46+
#endif // CHATMESSAGE_HPP_

client/include/hud/HUD.hpp

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#include "BlockCursor.hpp"
2929
#include "BlockInfoWidget.hpp"
30+
#include "Chat.hpp"
3031
#include "Crosshair.hpp"
3132
#include "DebugOverlay.hpp"
3233
#include "Hotbar.hpp"
@@ -41,6 +42,8 @@ class HUD : public gk::Transformable, public gk::Drawable {
4142

4243
void update();
4344

45+
Chat &chat() { return m_chat; }
46+
4447
private:
4548
void draw(gk::RenderTarget &target, gk::RenderStates states) const override;
4649

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

6063
Text m_fpsText;
64+
65+
Chat m_chat;
6166
};
6267

6368
#endif // HUD_HPP_

client/include/network/ClientCommandHandler.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,12 @@ class ClientCommandHandler {
4848
void sendBlockActivated(const glm::ivec4 &selectedBlock);
4949
void sendBlockInvUpdate(Inventory &inventory);
5050
void sendChunkRequest(s32 chunkX, s32 chunkY, s32 chunkZ);
51+
void sendChatMessage(const std::string &message);
5152

5253
void setupCallbacks();
5354

55+
Client &client() { return m_client; }
56+
5457
bool isRegistryInitialized() const { return m_isRegistryInitialized; }
5558

5659
private:

client/include/states/ChatState.hpp

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* =====================================================================================
3+
*
4+
* OpenMiner
5+
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <[email protected]>
6+
*
7+
* This program is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU Lesser General Public
9+
* License as published by the Free Software Foundation; either
10+
* version 2.1 of the License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public License
18+
* along with this program; if not, write to the Free Software Foundation, Inc.,
19+
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* =====================================================================================
22+
*/
23+
#ifndef CHATSTATE_HPP_
24+
#define CHATSTATE_HPP_
25+
26+
#include "InterfaceState.hpp"
27+
#include "TextInput.hpp"
28+
29+
class ClientCommandHandler;
30+
class Chat;
31+
32+
class ChatState : public InterfaceState {
33+
public:
34+
ChatState(ClientCommandHandler &clientCommandHandler, Chat &chat, gk::ApplicationState *parent = nullptr);
35+
36+
void updateTextInputGeometry();
37+
38+
void onEvent(const SDL_Event &event) override;
39+
40+
void update() override;
41+
42+
private:
43+
void draw(gk::RenderTarget &target, gk::RenderStates states) const override;
44+
45+
TextInput m_textInput;
46+
47+
ClientCommandHandler &m_clientCommandHandler;
48+
49+
Chat &m_chat;
50+
};
51+
52+
#endif // CHATSTATE_HPP_

client/include/states/InterfaceState.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class InterfaceState : public gk::ApplicationState {
3737

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

40+
bool m_drawBackground = true;
41+
4042
private:
4143
gk::Shader m_shader;
4244
// gk::View m_view;

client/include/world/ClientPlayer.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ class ClientPlayer : public Player {
6666
float y() const { return m_y; }
6767
float z() const { return m_z; }
6868

69+
void setPosition(float x, float y, float z);
70+
6971
const gk::Camera &camera() { return m_camera; }
7072

7173
private:

client/source/gui/Text.cpp

+17-6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#include "Text.hpp"
2828

2929
Text::Text() : m_texture(gk::ResourceHandler::getInstance().get<gk::Texture>("texture-font")) {
30+
m_background.setFillColor(gk::Color::Transparent);
31+
3032
updateCharWidth();
3133
}
3234

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

52+
target.draw(m_background, states);
53+
54+
states.transform.translate(m_padding.x, m_padding.y);
55+
5056
for(const gk::Sprite &sprite : m_textSprites) {
5157
target.draw(sprite, states);
5258
}
@@ -56,12 +62,12 @@ void Text::draw(gk::RenderTarget &target, gk::RenderStates states) const {
5662
void Text::updateTextSprites() {
5763
m_textSprites.clear();
5864

59-
int x = 0;
60-
int y = 0;
61-
int maxX = 0;
65+
unsigned int x = 0;
66+
unsigned int y = 0;
67+
unsigned int maxX = 0;
6268
gk::Color color = gk::Color{70, 70, 70, 255};
6369
for(char c : m_text) {
64-
if (c == '\n') {
70+
if (c == '\n' || (m_maxLineLength && x + m_charWidth[(u8)c] >= m_maxLineLength)) {
6571
y += 9;
6672
x = 0;
6773
continue;
@@ -78,7 +84,7 @@ void Text::updateTextSprites() {
7884
y = 0;
7985
color = m_color;
8086
for(char c : m_text) {
81-
if (c == '\n') {
87+
if (c == '\n' || (m_maxLineLength && x + m_charWidth[(u8)c] >= m_maxLineLength)) {
8288
maxX = std::max(x, maxX);
8389
y += 9;
8490
x = 0;
@@ -96,7 +102,12 @@ void Text::updateTextSprites() {
96102
}
97103

98104
m_size.x = std::max(x, maxX);
99-
m_size.y = 8 + y * 9;
105+
m_size.y = y + 9;
106+
107+
unsigned int backgroundX = std::max<int>(m_background.getSize().x, m_size.x + m_padding.x);
108+
unsigned int backgroundY = std::max<int>(m_background.getSize().y, m_size.y + m_padding.y);
109+
110+
m_background.setSize(backgroundX, backgroundY);
100111
}
101112

102113
// FIXME: Since I use the font from Minecraft assets, I needed to use

0 commit comments

Comments
 (0)