Skip to content

Commit

Permalink
[Text] Added the possibility to cut a line in the middle if it's too …
Browse files Browse the repository at this point in the history
…long.
  • Loading branch information
Unarelith committed Feb 21, 2020
1 parent ec15fec commit 3c4fae0
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 13 deletions.
6 changes: 5 additions & 1 deletion client/include/gui/Text.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ class Text : public gk::Drawable, public gk::Transformable {
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; }
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 @@ -65,6 +67,8 @@ class Text : public gk::Drawable, public gk::Transformable {
gk::Color m_color = gk::Color::White;

gk::RectangleShape m_background;

unsigned int m_maxLineLength = 0;
};

#endif // TEXT_HPP_
2 changes: 2 additions & 0 deletions client/include/hud/Chat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class Chat : public gk::Drawable, public gk::Transformable {
void draw(gk::RenderTarget &target, gk::RenderStates states) const override;

std::deque<ChatMessage> m_chatMessages;

u32 m_posY = 0;
};

#endif // CHAT_HPP_
4 changes: 3 additions & 1 deletion client/include/hud/ChatMessage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@

class ChatMessage : public gk::Drawable, public gk::Transformable {
public:
ChatMessage(u16 clientID, const std::string &message, u32 messageCount);
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;

Expand Down
17 changes: 11 additions & 6 deletions client/source/gui/Text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,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 @@ -84,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 @@ -102,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
6 changes: 4 additions & 2 deletions client/source/hud/Chat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ Chat::Chat(Client &client) {
std::string message;
packet >> clientID >> message;

m_chatMessages.emplace_back(clientID, message, m_chatMessages.size());
m_chatMessages.emplace_back(clientID, message, m_posY);

move(0, -10);
m_posY += m_chatMessages.back().text().getSize().y + 1;

move(0, -m_chatMessages.back().text().getSize().y - 1);
});
}

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

ChatMessage::ChatMessage(u16 clientID, const std::string &message, u32 messageCount) {
ChatMessage::ChatMessage(u16 clientID, const std::string &message, u32 posY) {
m_text.setText("<Client " + std::to_string(clientID) + "> " + message);
m_text.setPosition(0, 10 * messageCount);
m_text.setPosition(0, posY);
m_text.setBackgroundColor(gk::Color{0, 0, 0, 127});
m_text.setBackgroundSize(300, 10);
m_text.setMaxLineLength(300);
m_text.setPadding(1, 1);

m_timer.reset();
m_timer.start();
}

void ChatMessage::draw(gk::RenderTarget &target, gk::RenderStates states) const {
if (m_timer.time() <= 5000 || m_isVisible)
if (m_timer.time() <= 10000 || m_isVisible)
target.draw(m_text, states);
}

0 comments on commit 3c4fae0

Please sign in to comment.