forked from dgcor/DGEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringText.cpp
More file actions
executable file
·174 lines (158 loc) · 3.41 KB
/
Copy pathStringText.cpp
File metadata and controls
executable file
·174 lines (158 loc) · 3.41 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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "StringText.h"
#include "Game.h"
#include "GameUtils.h"
#include <SFML/Config.hpp>
#include "Utils/Utils.h"
StringText::StringText(const std::shared_ptr<FreeTypeFont>& font_, unsigned int characterSize)
: text({}, *font_, characterSize)
{
font = font_;
updateColor();
}
void StringText::calculateDrawPosition()
{
auto size = Size();
auto drawPos = GameUtils::getAlignmentPosition(pos, size, horizAlign, vertAlign);
auto bounds = text.getLocalBounds();
drawPos.x -= bounds.left;
drawPos.y -= bounds.top;
text.setPosition(drawPos);
}
void StringText::setAnchor(const Anchor anchor_)
{
if (anchor != anchor_)
{
anchor = anchor_;
calculateDrawPosition();
}
}
void StringText::updateSize(const Game& game)
{
auto size = Size();
GameUtils::setAnchorPosSize(anchor, pos, size, game.OldDrawRegionSize(), game.DrawRegionSize());
calculateDrawPosition();
}
bool StringText::setText(const std::string& str)
{
if (text.getString() == str)
{
return false;
}
text.setString(str);
calculateDrawPosition();
return true;
}
void StringText::setFont(const std::shared_ptr<FreeTypeFont>& font_)
{
font = font_;
text.setFont(*font_);
updateColor();
}
void StringText::setColor(const sf::Color& color_)
{
color = color_;
updateColor();
}
void StringText::updateColor()
{
if (color == sf::Color::White)
{
text.setFillColor(font->getColor());
}
else
{
text.setFillColor(color);
}
}
void StringText::Position(const sf::Vector2f& position)
{
pos = position;
calculateDrawPosition();
}
sf::Vector2f StringText::Size() const
{
auto bounds = text.getLocalBounds();
return sf::Vector2f(bounds.width, bounds.height);
}
void StringText::setHorizontalAlign(const HorizontalAlign align)
{
if (horizAlign != align)
{
horizAlign = align;
sf::Uint32 style = 0;
switch (align)
{
case HorizontalAlign::Left:
break;
case HorizontalAlign::Center:
style = sf::Text::Style::HorizontalAlignCenter;
break;
case HorizontalAlign::Right:
style = sf::Text::Style::HorizontalAlignRight;
break;
default:
break;
}
text.setStyle(style);
calculateDrawPosition();
}
}
void StringText::setVerticalAlign(const VerticalAlign align)
{
if (vertAlign != align)
{
vertAlign = align;
calculateDrawPosition();
}
}
void StringText::setHorizontalSpaceOffset(int offset) noexcept
{
float factor = 1.f;
if (offset != 0)
{
factor = (float)(text.getCharacterSize() + offset) / (float)text.getCharacterSize();
}
text.setLetterSpacing(factor);
calculateDrawPosition();
}
void StringText::setVerticalSpaceOffset(int offset) noexcept
{
float factor = 1.f;
if (offset != 0)
{
factor = (float)(text.getCharacterSize() + offset) / (float)text.getCharacterSize();
}
text.setLineSpacing(factor);
calculateDrawPosition();
}
void StringText::draw(const Game& game, sf::RenderTarget& target) const
{
if (visible == true)
{
target.draw(text);
}
}
bool StringText::getProperty(const std::string_view prop, Variable& var) const
{
if (prop.size() <= 1)
{
return false;
}
auto props = Utils::splitStringIn2(prop, '.');
auto propHash = str2int16(props.first);
switch (propHash)
{
case str2int16("length"):
var = Variable((int64_t)text.getString().getSize());
break;
case str2int16("lineCount"):
var = Variable((int64_t)text.getLineCount());
break;
case str2int16("text"):
var = Variable(text.getString().toAnsiString());
break;
default:
return getUIObjProp(propHash, props.second, var);
}
return true;
}