-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathC_UIWorldLabel.cpp
96 lines (73 loc) · 2.13 KB
/
C_UIWorldLabel.cpp
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
#include "C_UIWorldLabel.hpp"
#include "Object.hpp"
C_UIWorldLabel::C_UIWorldLabel(Object* owner) : Component(owner) {}
void C_UIWorldLabel::Start()
{
const int fontID = owner->context->fontAllocator->Add(owner->context->workingDir->Get() + "sansation.ttf");
std::shared_ptr<sf::Font> font = owner->context->fontAllocator->Get(fontID);
text.setFont(*font);
text.setCharacterSize(64);
}
void C_UIWorldLabel::Draw(Window& window)
{
window.Draw(background);
window.Draw(text);
}
bool C_UIWorldLabel::ContinueToDraw() const
{
return !owner->IsQueuedForRemoval();
}
//TODO: UI elements do not often move so we should not need to query the position every frame
void C_UIWorldLabel::LateUpdate(float deltaTime)
{
sf::Vector2f pos = owner->transform->GetPosition();
sf::View view = owner->context->window->GetView();
//const sf::FloatRect& backBounds = background.getLocalBounds();
const sf::FloatRect& backBounds = background.getGlobalBounds();
//const sf::Vector2f centeredPosition = sf::Vector2f(pos.x - (backBounds.width * 0.5f), pos.y - (backBounds.height * 0.5f));
sf::Vector2f centeredPosition = sf::Vector2f(view.getCenter());
centeredPosition.x -= 200;
background.setPosition(centeredPosition);
text.setPosition(centeredPosition);
float maxalive = 2.0f;
float holdtime = 1.0f;
if (holding)
{
aliveTimer += deltaTime;
if (aliveTimer > holdtime)
{
holding = false;
aliveTimer = 0;
}
}
else
{
aliveTimer += deltaTime;
float t = aliveTimer / maxalive;
float lerp = t * 255;
int color = 255 - lerp;
if (color < 0) color = 0;
text.setFillColor(sf::Color(255, 255, 255, color));
}
if (aliveTimer > maxalive)
{
owner->QueueForRemoval();
}
}
void C_UIWorldLabel::SetText(const std::string& text)
{
this->text.setString(text);
}
void C_UIWorldLabel::SetBackgroundSize(const sf::Vector2f& size)
{
background.setSize(size);
}
void C_UIWorldLabel::SetFontColour(const sf::Color& colour)
{
text.setFillColor(colour);
text.setOutlineColor(colour);
}
void C_UIWorldLabel::SetBackgroundColour(const sf::Color& colour)
{
background.setFillColor(colour);
}