[Text] Added the possibility to cut a line in the middle if it's too long.

This commit is contained in:
Quentin Bazin 2020-02-22 01:00:42 +09:00
parent ec15fec087
commit 3c4fae07f5
6 changed files with 29 additions and 13 deletions

View File

@ -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;
@ -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_

View File

@ -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_

View File

@ -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;

View File

@ -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;
@ -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;
@ -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

View File

@ -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);
});
}

View File

@ -22,11 +22,12 @@
*/
#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();
@ -34,7 +35,7 @@ ChatMessage::ChatMessage(u16 clientID, const std::string &message, u32 messageCo
}
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);
}