2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "supertux/info_box_line.hpp"
19 #include "supertux/textscroller.hpp"
20 #include "supertux/resources.hpp"
21 #include "video/drawing_context.hpp"
22 #include "video/font.hpp"
23 #include "video/surface.hpp"
25 static const float ITEMS_SPACE = 4;
29 Font* get_font_by_format_char(char format_char) {
46 log_warning << "Unknown format_char: '" << format_char << "'" << std::endl;
51 Color get_color_by_format_char(char format_char) {
55 return TextScroller::small_color;
58 return TextScroller::heading_color;
61 return TextScroller::reference_color;
65 return TextScroller::normal_color;
69 log_warning << "Unknown format_char: '" << format_char << "'" << std::endl;
74 InfoBoxLine::LineType get_linetype_by_format_char(char format_char) {
78 return InfoBoxLine::SMALL;
81 return InfoBoxLine::NORMAL;
84 return InfoBoxLine::HEADING;
87 return InfoBoxLine::REFERENCE;
90 return InfoBoxLine::NORMAL_LEFT;
93 return InfoBoxLine::IMAGE;
96 return InfoBoxLine::SMALL;
97 log_warning << "Unknown format_char: '" << format_char << "'" << std::endl;
104 InfoBoxLine::InfoBoxLine(char format_char, const std::string& text) :
111 font = get_font_by_format_char(format_char);
112 lineType = get_linetype_by_format_char(format_char);
113 color = get_color_by_format_char(format_char);
114 if (lineType == IMAGE) image = new Surface(text);
117 InfoBoxLine::~InfoBoxLine()
122 const std::vector<InfoBoxLine*>
123 InfoBoxLine::split(const std::string& text, float width)
125 std::vector<InfoBoxLine*> lines;
127 std::string::size_type i = 0;
128 std::string::size_type l;
129 char format_char = '#';
130 while(i < text.size()) {
131 // take care of empty lines - represent them as blank lines of normal text
132 if (text[i] == '\n') {
133 lines.push_back(new InfoBoxLine('\t', ""));
138 // extract the format_char
139 format_char = text[i];
141 if (i >= text.size()) break;
144 l = text.find("\n", i);
145 if (l == std::string::npos) l=text.size();
146 std::string s = text.substr(i, l-i);
149 // if we are dealing with an image, just store the line
150 if (format_char == '!') {
151 lines.push_back(new InfoBoxLine(format_char, s));
155 // append wrapped parts of line into list
156 std::string overflow;
158 Font* font = get_font_by_format_char(format_char);
160 if (font) s2 = font->wrap_to_width(s2, width, &overflow);
161 lines.push_back(new InfoBoxLine(format_char, s2));
163 } while (s.length() > 0);
170 InfoBoxLine::draw(DrawingContext& context, const Rect& bbox, int layer)
172 Vector position = bbox.p1;
175 context.draw_surface(image, Vector( (bbox.p1.x + bbox.p2.x - image->get_width()) / 2, position.y), layer);
178 context.draw_text(font, text, Vector(position.x, position.y), ALIGN_LEFT, layer, color);
181 context.draw_text(font, text, Vector((bbox.p1.x + bbox.p2.x) / 2, position.y), ALIGN_CENTER, layer, color);
187 InfoBoxLine::get_height()
191 return image->get_height() + ITEMS_SPACE;
193 return font->get_height() + ITEMS_SPACE;
195 return font->get_height() + ITEMS_SPACE;