~ubuntu-branches/ubuntu/maverick/btanks/maverick

« back to all changes in this revision

Viewing changes to engine/menu/label.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Ansgar Burchardt
  • Date: 2010-01-17 00:02:57 UTC
  • mfrom: (4.2.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100117000257-iw6esnvsz1rvp6kb
Tags: 0.9.8083-2
* Fix build failure when building only arch-specific package.
* debian/control: Add DM-Upload-Allowed: yes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
 
2
2
/* Battle Tanks Game
3
 
 * Copyright (C) 2006-2008 Battle Tanks team
 
3
 * Copyright (C) 2006-2009 Battle Tanks team
4
4
 *
5
5
 * This program is free software; you can redistribute it and/or
6
6
 * modify it under the terms of the GNU General Public License
30
30
#include "sdlx/surface.h"
31
31
#include "sdlx/rect.h"
32
32
#include "resource_manager.h"
 
33
#include "i18n.h"
33
34
 
34
35
#define LABEL_SPEED (30)
35
36
 
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
        }
39
41
 
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);
 
46
        }
43
47
 
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;
 
50
        h = _label_h;
47
51
}
48
52
 
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);
52
56
}
53
57
 
54
58
void Label::set(const std::string &label) {
55
59
        _label = label;
56
 
        _label_size = _font->render(0, 0, 0, _label);
 
60
        _font->render_multiline(_label_w, _label_h, NULL, 0, 0, _label);
 
61
}
 
62
 
 
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);
57
66
}
58
67
 
59
68
const std::string Label::get() const { 
62
71
 
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);
66
75
                return;
67
76
        } 
68
77
        //smooth scrolling
69
78
        sdlx::Rect clip;
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);
74
83
}
75
84
 
84
93
 
85
94
void Label::tick(const float dt) {
86
95
        TextualControl::tick(dt);
87
 
        if (_max_width <= 0)
88
 
                return;
89
 
        if (_label_size <= _max_width) {
 
96
        if (_max_width <= 0 || _label_w <= _max_width) {
90
97
                x_pos = 0;
91
98
                return;
92
99
        }
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);
96
103
        
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;
101
108
        }
102
109
        if (x_pos < - OVERLAP) {