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) {
33 return Resources::small_font;
36 return Resources::big_font;
42 return Resources::normal_font;
45 return Resources::normal_font;
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) :
106 font(Resources::normal_font),
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)
116 image = Surface::create(text);
120 InfoBoxLine::~InfoBoxLine()
124 const std::vector<InfoBoxLine*>
125 InfoBoxLine::split(const std::string& text, float width)
127 std::vector<InfoBoxLine*> lines;
129 std::string::size_type i = 0;
130 std::string::size_type l;
131 char format_char = '#';
132 while(i < text.size()) {
133 // take care of empty lines - represent them as blank lines of normal text
134 if (text[i] == '\n') {
135 lines.push_back(new InfoBoxLine('\t', ""));
140 // extract the format_char
141 format_char = text[i];
143 if (i >= text.size()) break;
146 l = text.find("\n", i);
147 if (l == std::string::npos) l=text.size();
148 std::string s = text.substr(i, l-i);
151 // if we are dealing with an image, just store the line
152 if (format_char == '!') {
153 lines.push_back(new InfoBoxLine(format_char, s));
157 // append wrapped parts of line into list
158 std::string overflow;
160 Font* font = get_font_by_format_char(format_char);
162 if (font) s2 = font->wrap_to_width(s2, width, &overflow);
163 lines.push_back(new InfoBoxLine(format_char, s2));
165 } while (s.length() > 0);
172 InfoBoxLine::draw(DrawingContext& context, const Rectf& bbox, int layer)
174 Vector position = bbox.p1;
177 context.draw_surface(image, Vector( (bbox.p1.x + bbox.p2.x - image->get_width()) / 2, position.y), layer);
180 context.draw_text(font, text, Vector(position.x, position.y), ALIGN_LEFT, layer, color);
183 context.draw_text(font, text, Vector((bbox.p1.x + bbox.p2.x) / 2, position.y), ALIGN_CENTER, layer, color);
189 InfoBoxLine::get_height()
193 return image->get_height() + ITEMS_SPACE;
195 return font->get_height() + ITEMS_SPACE;
197 return font->get_height() + ITEMS_SPACE;