File: tooltip.cpp

package info (click to toggle)
btanks 0.9.8083-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 43,616 kB
  • sloc: cpp: 46,425; ansic: 12,005; xml: 4,262; python: 313; sh: 13; makefile: 13
file content (186 lines) | stat: -rw-r--r-- 5,361 bytes parent folder | download | duplicates (5)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186

/* Battle Tanks Game
 * Copyright (C) 2006-2009 Battle Tanks team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

/* 
 * Additional rights can be granted beyond the GNU General Public License 
 * on the terms provided in the Exception. If you modify this file, 
 * you may extend this exception to your version of the file, 
 * but you are not obligated to do so. If you do not wish to provide this
 * exception without modification, you must delete this exception statement
 * from your version and license this file solely under the GPL without exception. 
*/

#include <ctype.h>
#include "tooltip.h"

#include "mrt/logger.h"
#include "menu/box.h"
#include "resource_manager.h"
#include "config.h"
#include "sdlx/font.h"
#include "mrt/utf8_utils.h"
#include "i18n.h"

#include <math.h>
#include <assert.h>
#include <deque>

Tooltip::Tooltip(const std::string &area, const std::string &message, const bool use_background, int w)  : 
area(area), message(message) {
	init(I18n->get(area, message), use_background, w);
}

Tooltip::Tooltip(const std::string &area, const std::string &message, const std::string &text, const bool use_background, int w) : 
area(area), message(message) {
	init(text, use_background, w);
}

void Tooltip::init(const std::string &_text, const bool use_background, int width) {
	_use_background = use_background;
	std::string text;
	bool space = true;
	size_t i;
	for(i = 0; i < _text.size(); ++i) {
		const char c = _text[i];
		const bool c_space = c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == '\v' || c == '\f';
		if (c == '\\' && i + 1 < _text.size() && _text[i + 1] == 'n') {
			//\n hack :)
			++i;
			if (space) {
				text += "\n ";
			} else {
				text += " \n ";
				space = true;
			}
			continue;
		}
		
		//LOG_DEBUG(("%d '%c': %s %s", c, c, space?"true":"false", c_space?"true":"false"));
		if (space) {
			if (c_space)
				continue;
			space = false;
			text += c;
		} else {
			if (c_space) {
				space = true;
				text += ' ';
			} else 
				text += c;
		}
	}
	if (text.empty())
		throw_ex(("tooltip with empty text is not allowed"));
	//LOG_DEBUG(("trimmed string : '%s'", text.c_str()));
	
	GET_CONFIG_VALUE("engine.tooltip-speed", float, td, 20);
	_time = ((float)mrt::utf8_length(text)) / td;

	std::vector<std::string> words;
	mrt::split(words, text, " ");
	for(size_t i = 0; i < words.size(); ++i) {
		mrt::replace(words[i], "\\s", " ");
	}
	std::vector<int> lens;
	lens.resize(words.size());

	const sdlx::Font * font = ResourceManager->loadFont("small", false);
	int line_h = font->get_height();

	int total = 0, nl_n = 0;
	for(size_t i = 0; i < words.size(); ++i) {
		const std::string &word = words[i];
		if (word == "\n") {
			nl_n += line_h;
			continue;
		}
			
		lens[i] = font->render(NULL, 0, 0, word + " ");
		total += lens[i] * line_h;
	}
	bool hard;
	if (width == 0) {
		width = (int)(sqrt(total * 2 / 3.0f + nl_n * nl_n / 4.0f) + 0.5f);
		hard = false;
	} else {
		hard = !_use_background;
	}
	
	std::vector<std::string> lines;

	int line_w = 0, real_width = 1;
	std::string line;
	for(size_t i = 0; i < words.size(); ++i) {
		const std::string &word = words[i];
		const int len = lens[i];
		line_w += len;
		
		bool last = i + 1 == words.size();
		bool nl = line_w >= width || word == "\n" || last;
		
		if (nl) {
			if (hard && !line.empty() && !last) { //hard edge
				line_w -= len;
				if (line_w > real_width)
					real_width = line_w;
				lines.push_back(line);
				line = word + " ";
				line_w = len;
			} else {
				if (line_w > real_width)
					real_width = line_w;
				lines.push_back(line + word);
				line.clear();
				line_w = 0;
			}
		} else {
			line += word + " ";
		}
	}
	
	//LOG_DEBUG(("line width: %d, lines: %u", width, lines.size()));
	int xp = 0, yp = 0;
	int height = line_h * lines.size();
	if (_use_background) {
		const sdlx::Surface *bg = ResourceManager->load_surface("menu/background_box.png");
		int mx = bg->get_width() / 3, my =  bg->get_height() / 3;
		_background.init("menu/background_box.png", real_width + mx * 2, height + my * 2);
		_surface.create_rgb(_background.w, _background.h, 32, SDL_SRCALPHA);
		xp = (_background.w - real_width) / 2;
		yp = (_background.h - height) / 2;
	} else {
		_surface.create_rgb(real_width, height, 32, SDL_SRCALPHA);
	}
	_surface.display_format_alpha();
	
	for(size_t i = 0; i < lines.size(); ++i) {
		font->render(_surface, xp, yp + i * line_h, lines[i]);
	}
}

void Tooltip::render(sdlx::Surface &surface, const int x, const int y) const {
	if (_use_background)
		_background.render(surface, x, y);
	surface.blit(_surface, x, y);
}

void Tooltip::get_size(int &w, int &h) const {
	w = _surface.get_width();
	h = _surface.get_height();
}