-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlabel.cpp
More file actions
64 lines (53 loc) · 1.83 KB
/
label.cpp
File metadata and controls
64 lines (53 loc) · 1.83 KB
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
#include "label.h"
#include <string>
void Label::reloadPos()
{
sf::Text text = sf::Text(font, sf::String::fromUtf8(content.begin(), content.end()), size);
text.setPosition(pos);
pos = pos - (text.getGlobalBounds().position - pos);
/*
* При задании позиции текста по факту текст чуть сдвинут от задаваемых координат,
* причем сдвинут по разному для каждого шрифта. Костыль, но по-другому не работает.
*/
}
Label::Label(sf::Font newFont, std::string string, sf::Color newColor, int newSize, sf::Vector2f newPos) : font{newFont}, content{string}, color{newColor}, size{newSize}, pos{newPos}{}
void Label::render(sf::RenderWindow *window)
{
sf::Text text = sf::Text(font, sf::String::fromUtf8(content.begin(), content.end()), size);
text.setFillColor(color);
text.setPosition(pos);
window->draw(text);
}
void Label::setString(std::string string)
{
content = string;
}
void Label::setPosition(sf::Vector2f newPos)
{
pos = newPos;
reloadPos();
}
void Label::setPositionCenter(sf::Vector2f newPos)
{
sf::Text text = sf::Text(font, sf::String::fromUtf8(content.begin(), content.end()), size);
text.setFillColor(color);
text.setPosition(pos);
pos = { newPos.x - text.getGlobalBounds().size.x / 2, newPos.y - text.getGlobalBounds().size.y / 2 };
reloadPos();
}
void Label::setColor(sf::Color newColor)
{
color = newColor;
}
void Label::setSize(int newSize)
{
size = newSize;
}
sf::FloatRect Label::getBounds()
{
return sf::Text(font, sf::String::fromUtf8(content.begin(), content.end()), size).getGlobalBounds();
}
int Label::getSize()
{
return sf::Text(font, sf::String::fromUtf8(content.begin(), content.end()), size).getCharacterSize();
}