30
30
#include "sdlx/surface.h"
31
31
#include "sdlx/rect.h"
32
32
#include "resource_manager.h"
34
35
#define LABEL_SPEED (30)
36
37
Label::Label(const sdlx::Font *font, const std::string &label) :
37
_font(font), _label(label), _label_size(_font->render(0, 0, 0, _label)),
38
_max_width(0), _max_height(0), x_pos(0), x_vel(LABEL_SPEED) {}
38
_font(font), _label(label), _max_width(0), _max_height(0), x_pos(0), x_vel(LABEL_SPEED) {
39
_font->render_multiline(_label_w, _label_h, NULL, 0, 0, label);
40
42
Label::Label(const std::string &font, const std::string &label) :
41
_font(ResourceManager->loadFont(font, true)), _label(label), _label_size(_font->render(0, 0, 0, _label)),
42
_max_width(0), _max_height(0), x_pos(0), x_vel(LABEL_SPEED) {}
43
_font(ResourceManager->loadFont(font, true)), _label(label),
44
_max_width(0), _max_height(0), x_pos(0), x_vel(LABEL_SPEED) {
45
_font->render_multiline(_label_w, _label_h, NULL, 0, 0, label);
44
48
void Label::get_size(int &w, int &h) const {
45
w = _max_width <= 0? _label_size: (_label_size < _max_width? _label_size: _max_width);
46
h = _font->get_height();
49
w = (_max_width == 0 || _label_w < _max_width) ? _label_w: _max_width;
49
53
void Label::setFont(const std::string &font) {
50
54
_font = ResourceManager->loadFont(font, true);
51
_label_size = _font->render(0, 0, 0, _label);
55
_font->render_multiline(_label_w, _label_h, NULL, 0, 0, _label);
54
58
void Label::set(const std::string &label) {
56
_label_size = _font->render(0, 0, 0, _label);
60
_font->render_multiline(_label_w, _label_h, NULL, 0, 0, _label);
63
void Label::set(const std::string &base, const std::string &id) {
64
_label = I18n->get(base, id);
65
_font->render_multiline(_label_w, _label_h, NULL, 0, 0, _label);
59
68
const std::string Label::get() const {
63
72
void Label::render(sdlx::Surface& surface, int x, int y) const {
64
73
if (_max_width <= 0) {
65
_font->render(surface, x, y, _label);
74
_font->render_multiline(_label_w, _label_h, &surface, x, y, _label);
70
79
surface.get_clip_rect(clip);
71
80
surface.set_clip_rect(sdlx::Rect(x, y, _max_width, _max_height));
72
_font->render(surface, x - (int)x_pos, y, _label);
81
_font->render_multiline(_label_w, _label_h, &surface, x - (int)x_pos, y, _label);
73
82
surface.set_clip_rect(clip);
85
94
void Label::tick(const float dt) {
86
95
TextualControl::tick(dt);
89
if (_label_size <= _max_width) {
96
if (_max_width <= 0 || _label_w <= _max_width) {
93
int delta = _label_size - _max_width;
100
int delta = _label_w - _max_width;
94
101
//LOG_DEBUG(("%d",delta));
95
102
float m = delta >= MARGIN? 1.0f: 1.0f * (delta + MARGIN / 2) / (MARGIN + MARGIN / 2);
97
104
x_pos += x_vel * dt * m;
98
if (x_pos + _max_width - OVERLAP > _label_size) {
99
x_pos = _label_size - _max_width + OVERLAP;
105
if (x_pos + _max_width - OVERLAP > _label_w) {
106
x_pos = _label_w - _max_width + OVERLAP;
100
107
x_vel = -LABEL_SPEED;
102
109
if (x_pos < - OVERLAP) {