-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimatedLabel.cpp
More file actions
131 lines (109 loc) · 3.31 KB
/
AnimatedLabel.cpp
File metadata and controls
131 lines (109 loc) · 3.31 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
#include "AnimatedLabel.hpp"
#include <iostream>
using namespace gui;
AnimatedLabel::AnimatedLabel(const sf::Font &font)
: mFinalSprite(),
mRenderTexture(),
mTextSprite(),
mFont(font),
mHorizontalPosition(0),
mDoLoop(true)
{
init();
}
AnimatedLabel::AnimatedLabel(const sf::Font &font,
const sf::IntRect & layout)
: mFinalSprite(),
mRenderTexture(),
mTextSprite(),
mFont(font),
mHorizontalPosition(layout.width),
mDoLoop(true)
{
init();
setLayout(layout); // will call innerUpdate();
}
void AnimatedLabel::setLayout(const sf::IntRect & layout)
{
mFinalSprite.setPosition(layout.left, layout.top);
mRenderTexture.create(layout.width, layout.height);
mFinalSprite.setTexture(mRenderTexture.getTexture());
mHorizontalPosition = layout.width;
innerUpdate();
}
void AnimatedLabel::setFont(const sf::Font &font)
{
mFont = font;
mTextSprite.setFont(mFont);
innerUpdate();
}
void AnimatedLabel::setString(const sf::String &string)
{
mTextSprite.setString(string);
mTextSprite.setCharacterSize(40);
innerUpdate();
}
void AnimatedLabel::setLoop(bool loop)
{
mDoLoop = loop;
}
void AnimatedLabel::update(const sf::Time &elapsed)
{
const int margin = 5;
float textWidth = mTextSprite.getGlobalBounds().width;
float widgetWidth = mFinalSprite.getGlobalBounds().width;
if (mHorizontalPosition + textWidth < -margin)
mHorizontalPosition += widgetWidth + textWidth + margin;
float centerPosition = (widgetWidth - textWidth) / 2;
float velocity = -80 * elapsed.asSeconds();
if (mDoLoop ||
(!mDoLoop && // even if loop is false, continue until label is centered
(mHorizontalPosition > centerPosition ||
mHorizontalPosition < (centerPosition + velocity - 5)
)))
mHorizontalPosition += velocity;
innerUpdate();
}
void AnimatedLabel::init()
{
mTextSprite.setFont(mFont);
mTextSprite.setColor(sf::Color::Black);
mDegradeQuad.setPrimitiveType(sf::Quads);
mDegradeQuad.append(sf::Vertex(sf::Vector2f(0,0), sf::Color::White));
mDegradeQuad.append(sf::Vertex(sf::Vector2f(1,0), sf::Color(255,255,255,0)));
mDegradeQuad.append(sf::Vertex(sf::Vector2f(1,1), sf::Color(255,255,255,0)));
mDegradeQuad.append(sf::Vertex(sf::Vector2f(0,1), sf::Color::White));
innerUpdate();
}
void AnimatedLabel::innerUpdate()
{
float finalSpriteHeight = mFinalSprite.getGlobalBounds().height;
//std::cout << mTextSprite.getGlobalBounds().height << std::endl;
float finalSpriteWidth = mFinalSprite.getGlobalBounds().width;
float verticalPosition = (finalSpriteHeight-
(mTextSprite.getGlobalBounds().height+
mTextSprite.getCharacterSize()))/2;
//(finalSpriteHeight +
// mTextSprite.getGlobalBounds().height /* 1.3f*/ ) / 2;
mTextSprite.setPosition(static_cast<int>(mHorizontalPosition),
static_cast<int>(verticalPosition));
mRenderTexture.clear(sf::Color::White);
mRenderTexture.draw(mTextSprite);
// degrade
{
sf::RenderStates states;
states.transform.scale(30, finalSpriteHeight);
mRenderTexture.draw(mDegradeQuad, states);
}
{
sf::RenderStates states;
states.transform.translate(finalSpriteWidth,0);
states.transform.scale(-30, finalSpriteHeight);
mRenderTexture.draw(mDegradeQuad, states);
}
mRenderTexture.display();
}
void AnimatedLabel::draw(sf::RenderTarget &target, sf::RenderStates states) const
{
target.draw(mFinalSprite, states);
}