aboutsummaryrefslogtreecommitdiff
path: root/src/graphics/Text.cpp
blob: e138a286300adda9ccc02512d55e8bcad09b3283 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "../../include/mglpp/graphics/Text.hpp"
#include "../../include/mglpp/graphics/Font.hpp"

extern "C" {
#include <mgl/mgl.h>
}

namespace mgl {
    Text::Text() : font(nullptr) {
        mgl_text_init(&text, nullptr, nullptr, 0);
    }

    Text::Text(std::string str, Font &font) : Text(std::move(str), vec2f(0.0f, 0.0f), font) {}

    Text::Text(std::string str, vec2f position, Font &font) : font(&font), str(std::move(str)) {
        mgl_text_init(&text, font.internal_font(), this->str.c_str(), this->str.size());
        mgl_text_set_position(&text, { position.x, position.y });
    }

    Text::Text(const Text &other) {
        *this = other;
    }

    Text& Text::operator=(const Text &other) {
        font = other.font;
        str = other.str;
        text = other.text;
        mgl_text_set_string(&text, str.c_str(), str.size());
        return *this;
    }

    Text::Text(Text &&other) {
        font = std::move(other.font);
        text = std::move(other.text);
        set_string(std::move(other.str));

        other.font = nullptr;
        other.str.clear();
        mgl_text_deinit(&other.text);
    }

    Text::~Text() {
        mgl_text_deinit(&text);
    }

    void Text::set_position(vec2f position) {
        mgl_text_set_position(&text, {position.x, position.y});
    }

    vec2f Text::get_position() const {
        return { text.position.x, text.position.y };
    }

    void Text::set_color(Color color) {
        mgl_text_set_color(&text, {color.r, color.g, color.b, color.a});
    }

    void Text::set_max_width(float max_width) {
        mgl_text_set_max_width(&text, max_width);
    }

    void Text::set_max_rows(unsigned int max_rows) {
        mgl_text_set_max_rows(&text, max_rows);
    }

    FloatRect Text::get_bounds() {
        mgl_vec2f bounds = mgl_text_get_bounds(&text);
        FloatRect rect(get_position(), { bounds.x, bounds.y });
        return rect;
    }

    void Text::set_string(std::string str) {
        this->str = std::move(str);
        mgl_text_set_string(&text, this->str.c_str(), this->str.size());
    }

    const std::string& Text::get_string() const {
        return str;
    }

    void Text::append_string(const std::string &str) {
        this->str += str;
        mgl_text_set_string(&text, this->str.c_str(), this->str.size());
    }

    vec2f Text::find_character_pos(size_t index) {
        mgl_vec2f pos = mgl_text_find_character_pos(&text, index);
        return vec2f(pos.x, pos.y);
    }

    Font* Text::get_font() {
        return font;
    }

    void Text::draw(Window&) {
        mgl_text_draw(mgl_get_context(), &text);
    }
}