From 640d8df5277af4ac4b545cc6d4cf2830509e61b9 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Fri, 4 May 2018 20:50:49 +0200 Subject: Add proper parsing of Text, add url --- include/StringView.hpp | 18 ++++++++++++++++++ include/Text.hpp | 38 ++++++++++++++++++++------------------ 2 files changed, 38 insertions(+), 18 deletions(-) (limited to 'include') diff --git a/include/StringView.hpp b/include/StringView.hpp index 45d0f5b..c4e7ce3 100644 --- a/include/StringView.hpp +++ b/include/StringView.hpp @@ -68,6 +68,24 @@ namespace dchat return data[index]; } + // Returns -1 if substr not found. + // TODO: Make this more efficient + usize find(const BasicStringView &substr, usize offset = 0) const + { + if(substr.size == 0) + return -1; + + if(offset + substr.size > size) + return -1; + + for(usize i = offset; i < size - (substr.size - 1); ++i) + { + if(memcmp(data + i, substr.data, substr.size) == 0) + return i; + } + return -1; + } + const CharType *data; usize size; }; diff --git a/include/Text.hpp b/include/Text.hpp index 016e852..daea7ca 100644 --- a/include/Text.hpp +++ b/include/Text.hpp @@ -10,6 +10,24 @@ namespace dchat { + struct TextElement + { + enum class Type + { + TEXT, + EMOJI, + URL + }; + + TextElement() {} + TextElement(const StringViewUtf32 &_text, Type _type) : text(_text), type(_type), ownLine(false) {} + + StringViewUtf32 text; + sf::Vector2f position; + Type type; + bool ownLine; // Currently only used for emoji, to make emoji bigger when it's the only thing on a line + }; + class Text { public: @@ -34,24 +52,7 @@ namespace dchat private: void stringSplitElements(sf::String &stringToSplit, usize startIndex); void updateGeometry(); - private: - struct TextElement - { - enum class Type - { - TEXT, - EMOJI - }; - - TextElement() {} - TextElement(const StringViewUtf32 &_text, Type _type) : text(_text), type(_type), ownLine(false) {} - - StringViewUtf32 text; - sf::Vector2f position; - Type type; - bool ownLine; // Currently only used for emoji, to make emoji bigger when it's the only thing on a line - }; - + private: sf::String str; const sf::Font *font; unsigned int characterSize; @@ -59,6 +60,7 @@ namespace dchat float maxWidth; sf::Vector2f position; sf::Color color; + sf::Color urlColor; bool dirty; bool plainText; float totalHeight; -- cgit v1.2.3