96 lines
2.2 KiB
C++
96 lines
2.2 KiB
C++
#include "core/rendering/writer.h"
|
|
|
|
#include "core/rendering/text.h" // for Text
|
|
|
|
// Constructor
|
|
Writer::Writer(Text *text)
|
|
: text_(text) {
|
|
}
|
|
|
|
// Avança un caracter cada `seconds_per_char_` i un cop completat es queda
|
|
// visible `remaining_time_s_` segons abans de finalitzar.
|
|
void Writer::update(float dt_s) {
|
|
if (!enabled_) { return; }
|
|
|
|
if (!completed_) {
|
|
char_timer_s_ += dt_s;
|
|
while (char_timer_s_ >= seconds_per_char_ && index_ < length_) {
|
|
char_timer_s_ -= seconds_per_char_;
|
|
++index_;
|
|
}
|
|
if (index_ >= length_) {
|
|
completed_ = true;
|
|
}
|
|
}
|
|
|
|
if (completed_) {
|
|
if (remaining_time_s_ <= 0.0F) {
|
|
finished_ = true;
|
|
} else {
|
|
remaining_time_s_ -= dt_s;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Dibuja el objeto en pantalla
|
|
void Writer::render() {
|
|
if (enabled_) {
|
|
text_->write(pos_x_, pos_y_, caption_, kerning_, index_);
|
|
}
|
|
}
|
|
|
|
// Establece el valor de la variable
|
|
void Writer::setPosX(int value) {
|
|
pos_x_ = value;
|
|
}
|
|
|
|
// Establece el valor de la variable
|
|
void Writer::setPosY(int value) {
|
|
pos_y_ = value;
|
|
}
|
|
|
|
// Establece el valor de la variable
|
|
void Writer::setKerning(int value) {
|
|
kerning_ = value;
|
|
}
|
|
|
|
// Establece el valor de la variable
|
|
void Writer::setCaption(const std::string &text) {
|
|
caption_ = text;
|
|
length_ = text.length();
|
|
}
|
|
|
|
// Segons per caracter. Quan s'usa, l'update(dt) avança index.
|
|
void Writer::setSecondsPerChar(float seconds) {
|
|
seconds_per_char_ = seconds;
|
|
char_timer_s_ = 0.0F;
|
|
}
|
|
|
|
// Establece el valor de la variable
|
|
void Writer::setEnabled(bool value) {
|
|
enabled_ = value;
|
|
}
|
|
|
|
// Obtiene el valor de la variable
|
|
auto Writer::isEnabled() const -> bool {
|
|
return enabled_;
|
|
}
|
|
|
|
// Temps que es mante visible despres de completar el text.
|
|
void Writer::setRemainingTime(float seconds) {
|
|
remaining_time_s_ = seconds;
|
|
}
|
|
|
|
auto Writer::getRemainingTime() const -> float {
|
|
return remaining_time_s_;
|
|
}
|
|
|
|
// Centra la cadena de texto a un punto X
|
|
void Writer::center(int x) {
|
|
setPosX(x - (text_->lenght(caption_, kerning_) / 2));
|
|
}
|
|
|
|
// Obtiene el valor de la variable
|
|
auto Writer::hasFinished() const -> bool {
|
|
return finished_;
|
|
} |