From ddff0f1b7ea84f6a1321b8eb8a4d47317873d955 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 23 Apr 2018 09:53:31 +0200 Subject: Add word wrap for message board & TODO TODO: Message board is now redrawn every frame. Text should be modified to render on static & dynamic texture -> text & static images on static texture, gif & video on dynamic texture --- include/Gif.hpp | 4 ++-- include/Message.hpp | 14 +++-------- include/MessageBoard.hpp | 1 + include/MessagePart.hpp | 55 ------------------------------------------ include/StringView.hpp | 26 +++++++++++--------- include/Text.hpp | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 83 insertions(+), 79 deletions(-) delete mode 100644 include/MessagePart.hpp create mode 100644 include/Text.hpp (limited to 'include') diff --git a/include/Gif.hpp b/include/Gif.hpp index 1a69a52..87e6956 100644 --- a/include/Gif.hpp +++ b/include/Gif.hpp @@ -1,7 +1,7 @@ #pragma once #include "StringView.hpp" -#include +#include #include #include #include @@ -30,7 +30,7 @@ namespace dchat void setPosition(const sf::Vector2f &position); void setSize(const sf::Vector2f &size); - void draw(sf::RenderWindow &window); + void draw(sf::RenderTarget &target); static bool isDataGif(const StringView &data); private: diff --git a/include/Message.hpp b/include/Message.hpp index 7cd7fdf..a6edddf 100644 --- a/include/Message.hpp +++ b/include/Message.hpp @@ -1,7 +1,7 @@ #pragma once -#include "MessagePart.hpp" #include "User.hpp" +#include "Text.hpp" #include #include @@ -10,17 +10,9 @@ namespace dchat class Message { public: - Message(User *user); - virtual ~Message(); - - void addText(const std::string &text, bool newLine = true); - void addEmoji(const std::string &url, bool newLine = true); - std::vector& getParts(); - - static Message* buildFromString(User *user, const std::string &str); + Message(User *user, const std::string &text); const User *user; - private: - std::vector messageParts; + Text text; }; } diff --git a/include/MessageBoard.hpp b/include/MessageBoard.hpp index c510164..06a7cd1 100644 --- a/include/MessageBoard.hpp +++ b/include/MessageBoard.hpp @@ -1,6 +1,7 @@ #pragma once #include "Message.hpp" +#include "types.hpp" #include "../include/Cache.hpp" #include #include diff --git a/include/MessagePart.hpp b/include/MessagePart.hpp deleted file mode 100644 index 00544fb..0000000 --- a/include/MessagePart.hpp +++ /dev/null @@ -1,55 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -namespace dchat -{ - class MessagePart - { - public: - enum class Type - { - TEXT, - EMOJI - }; - - MessagePart(Type _type, bool _newLine) : type(_type), newLine(_newLine) {} - virtual ~MessagePart(){} - - static float getSizeScaled(); - virtual sf::Vector2f getPosition() const = 0; - virtual sf::Vector2f getSize() const = 0; - - const Type type; - bool newLine; - }; - - class MessagePartText : public MessagePart - { - public: - MessagePartText(const std::string &text, bool newLine); - - static float getFontSizeScaled(); - virtual sf::Vector2f getPosition() const override; - virtual sf::Vector2f getSize() const override; - - sf::Text text; - }; - - class MessagePartEmoji : public MessagePart - { - public: - MessagePartEmoji(const std::string &url, bool newLine); - - static float getHeightScaled(); - virtual sf::Vector2f getPosition() const override; - virtual sf::Vector2f getSize() const override; - - sf::Sprite sprite; - std::string url; - bool dirty; - }; -} diff --git a/include/StringView.hpp b/include/StringView.hpp index 3293358..4e9066b 100644 --- a/include/StringView.hpp +++ b/include/StringView.hpp @@ -6,36 +6,37 @@ namespace dchat { - class StringView + template + class BasicStringView { public: - StringView() : data(nullptr), size(0) + BasicStringView() : data(nullptr), size(0) { } - StringView(const StringView &other) : data(other.data), size(other.size) + BasicStringView(const BasicStringView &other) : data(other.data), size(other.size) { } - StringView(const char *_data) : data(_data), size(strlen(_data)) + BasicStringView(const CharType *_data) : data(_data), size(strlen(_data)) { } - StringView(const char *_data, usize _size) : data(_data), size(_size) + BasicStringView(const CharType *_data, usize _size) : data(_data), size(_size) { } - StringView operator = (const StringView &other) + BasicStringView operator = (const BasicStringView &other) { - StringView result(other.data, other.size); + BasicStringView result(other.data, other.size); return result; } - StringView(StringView &&other) + BasicStringView( BasicStringView &&other) { data = other.data; size = other.size; @@ -44,19 +45,22 @@ namespace dchat other.size = 0; } - bool equals(const StringView &other) const + bool equals(const BasicStringView &other) const { if(size != other.size) return false; return memcmp(data, other.data, size) == 0; } - char operator [] (usize index) const + CharType operator [] (usize index) const { assert(index < size); return data[index]; } - const char *data; + const CharType *data; usize size; }; + + using StringView = BasicStringView; + using StringViewUtf32 = BasicStringView; } diff --git a/include/Text.hpp b/include/Text.hpp new file mode 100644 index 0000000..53db93b --- /dev/null +++ b/include/Text.hpp @@ -0,0 +1,62 @@ +#pragma once + +#include "StringView.hpp" +#include "Cache.hpp" +#include +#include +#include +#include +#include + +namespace dchat +{ + class Text + { + public: + Text(const sf::Font &font); + Text(const sf::String &str, const sf::Font &font, unsigned int characterSize, float maxWidth, bool plainText = true); + + void setString(const sf::String &str); + void setPosition(float x, float y); + void setPosition(const sf::Vector2f &position); + void setMaxWidth(float maxWidth); + void setCharacterSize(unsigned int characterSize); + void setFillColor(sf::Color color); + + // Warning: won't update until @draw is called + float getHeight() const; + + void draw(sf::RenderTarget &target, Cache &cache); + private: + void stringSplitElements(); + void updateGeometry(); + private: + struct TextElement + { + enum class Type + { + TEXT, + EMOJI + }; + + TextElement() {} + TextElement(const StringViewUtf32 &_text, Type _type) : text(_text), type(_type) {} + + StringViewUtf32 text; + sf::Vector2f position; + Type type; + }; + + sf::String str; + sf::Font font; + unsigned int characterSize; + sf::VertexArray vertices; + float maxWidth; + sf::Vector2f position; + sf::Color color; + bool dirty; + bool plainText; + float totalHeight; + std::vector textElements; + }; +} -- cgit v1.2.3