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 FontPtr get_font_by_format_char(char format_char) {
33 return Resources::small_font;
35 return Resources::big_font;
40 return Resources::normal_font;
42 return Resources::normal_font;
43 //log_warning << "Unknown format_char: '" << format_char << "'" << std::endl;
47 Color get_color_by_format_char(char format_char) {
51 return TextScroller::small_color;
53 return TextScroller::heading_color;
55 return TextScroller::reference_color;
59 return TextScroller::normal_color;
62 //log_warning << "Unknown format_char: '" << format_char << "'" << std::endl;
66 InfoBoxLine::LineType get_linetype_by_format_char(char format_char) {
70 return InfoBoxLine::SMALL;
73 return InfoBoxLine::NORMAL;
75 return InfoBoxLine::HEADING;
77 return InfoBoxLine::REFERENCE;
79 return InfoBoxLine::NORMAL_LEFT;
81 return InfoBoxLine::IMAGE;
83 return InfoBoxLine::SMALL;
84 //log_warning << "Unknown format_char: '" << format_char << "'" << std::endl;
90 InfoBoxLine::InfoBoxLine(char format_char, const std::string& text) :
92 font(Resources::normal_font),
97 font = get_font_by_format_char(format_char);
98 lineType = get_linetype_by_format_char(format_char);
99 color = get_color_by_format_char(format_char);
100 if (lineType == IMAGE)
102 image = Surface::create(text);
106 InfoBoxLine::~InfoBoxLine()
110 const std::vector<InfoBoxLine*>
111 InfoBoxLine::split(const std::string& text, float width)
113 std::vector<InfoBoxLine*> lines;
115 std::string::size_type i = 0;
116 std::string::size_type l;
117 char format_char = '#';
118 while(i < text.size()) {
119 // take care of empty lines - represent them as blank lines of normal text
120 if (text[i] == '\n') {
121 lines.push_back(new InfoBoxLine('\t', ""));
126 // extract the format_char
127 format_char = text[i];
129 if (i >= text.size()) break;
132 l = text.find("\n", i);
133 if (l == std::string::npos) l=text.size();
134 std::string s = text.substr(i, l-i);
137 // if we are dealing with an image, just store the line
138 if (format_char == '!') {
139 lines.push_back(new InfoBoxLine(format_char, s));
143 // append wrapped parts of line into list
144 std::string overflow;
146 FontPtr font = get_font_by_format_char(format_char);
148 if (font) s2 = font->wrap_to_width(s2, width, &overflow);
149 lines.push_back(new InfoBoxLine(format_char, s2));
151 } while (s.length() > 0);
158 InfoBoxLine::draw(DrawingContext& context, const Rectf& bbox, int layer)
160 Vector position = bbox.p1;
163 context.draw_surface(image, Vector( (bbox.p1.x + bbox.p2.x - image->get_width()) / 2, position.y), layer);
166 context.draw_text(font, text, Vector(position.x, position.y), ALIGN_LEFT, layer, color);
169 context.draw_text(font, text, Vector((bbox.p1.x + bbox.p2.x) / 2, position.y), ALIGN_CENTER, layer, color);
175 InfoBoxLine::get_height()
179 return image->get_height() + ITEMS_SPACE;
181 return font->get_height() + ITEMS_SPACE;
183 return font->get_height() + ITEMS_SPACE;