aboutsummaryrefslogtreecommitdiff
path: root/include/Text.hpp
blob: 53db93be24330726fd7d1944b2bbbfa2696b62c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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;
    };
}