aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
Diffstat (limited to 'include')
-rw-r--r--include/Gif.hpp4
-rw-r--r--include/Message.hpp14
-rw-r--r--include/MessageBoard.hpp1
-rw-r--r--include/MessagePart.hpp55
-rw-r--r--include/StringView.hpp26
-rw-r--r--include/Text.hpp62
6 files changed, 83 insertions, 79 deletions
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 <SFML/Graphics/RenderWindow.hpp>
+#include <SFML/Graphics/RenderTarget.hpp>
#include <SFML/Graphics/Texture.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/System/Clock.hpp>
@@ -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 <string>
#include <vector>
@@ -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<MessagePart*>& getParts();
-
- static Message* buildFromString(User *user, const std::string &str);
+ Message(User *user, const std::string &text);
const User *user;
- private:
- std::vector<MessagePart*> 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 <SFML/Graphics/RenderTexture.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
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 <SFML/Graphics/Text.hpp>
-#include <SFML/Graphics/Sprite.hpp>
-#include <SFML/System/Vector2.hpp>
-#include <string>
-
-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 <typename CharType>
+ 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<CharType> &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<CharType> operator = (const BasicStringView<CharType> &other)
{
- StringView result(other.data, other.size);
+ BasicStringView<CharType> result(other.data, other.size);
return result;
}
- StringView(StringView &&other)
+ BasicStringView( BasicStringView<CharType> &&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<CharType> &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<char>;
+ using StringViewUtf32 = BasicStringView<u32>;
}
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 <SFML/Graphics/VertexArray.hpp>
+#include <SFML/Graphics/Font.hpp>
+#include <SFML/Graphics/RenderTarget.hpp>
+#include <SFML/System/String.hpp>
+#include <vector>
+
+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<TextElement> textElements;
+ };
+}