20a287cba855dc9876d7628951f5c05728091124
[supertux.git] / src / supertux / info_box_line.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
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.
8 //
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.
13 //
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/>.
16
17 #include "supertux/info_box_line.hpp"
18
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"
24
25 static const float ITEMS_SPACE = 4;
26
27 namespace {
28
29 FontPtr get_font_by_format_char(char format_char) {
30   switch(format_char)
31   {
32     case ' ':
33       return Resources::small_font;
34     case '-':
35       return Resources::big_font;
36     case '\t':
37     case '*':
38     case '#':
39     case '!':
40       return Resources::normal_font;
41     default:
42       return Resources::normal_font;
43       //log_warning << "Unknown format_char: '" << format_char << "'" << std::endl;
44   }
45 }
46
47 Color get_color_by_format_char(char format_char) {
48   switch(format_char)
49   {
50     case ' ':
51       return TextScroller::small_color;
52     case '-':
53       return TextScroller::heading_color;
54     case '*':
55       return TextScroller::reference_color;
56     case '\t':
57     case '#':
58     case '!':
59       return TextScroller::normal_color;
60     default:
61       return Color(0,0,0);
62       //log_warning << "Unknown format_char: '" << format_char << "'" << std::endl;
63   }
64 }
65
66 InfoBoxLine::LineType get_linetype_by_format_char(char format_char) {
67   switch(format_char)
68   {
69     case ' ':
70       return InfoBoxLine::SMALL;
71
72     case '\t':
73       return InfoBoxLine::NORMAL;
74     case '-':
75       return InfoBoxLine::HEADING;
76     case '*':
77       return InfoBoxLine::REFERENCE;
78     case '#':
79       return InfoBoxLine::NORMAL_LEFT;
80     case '!':
81       return InfoBoxLine::IMAGE;
82     default:
83       return InfoBoxLine::SMALL;
84       //log_warning << "Unknown format_char: '" << format_char << "'" << std::endl;
85   }
86 }
87
88 } // namespace
89
90 InfoBoxLine::InfoBoxLine(char format_char, const std::string& text_) :
91   lineType(NORMAL),
92   font(Resources::normal_font),
93   color(),
94   text(text_),
95   image()
96 {
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)
101   {
102     image = Surface::create(text);
103   }
104 }
105
106 InfoBoxLine::~InfoBoxLine()
107 {
108 }
109
110 const std::vector<InfoBoxLine*>
111 InfoBoxLine::split(const std::string& text, float width)
112 {
113   std::vector<InfoBoxLine*> lines;
114
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', ""));
122       i++;
123       continue;
124     }
125
126     // extract the format_char
127     format_char = text[i];
128     i++;
129     if (i >= text.size()) break;
130
131     // extract one line
132     l = text.find("\n", i);
133     if (l == std::string::npos) l=text.size();
134     std::string s = text.substr(i, l-i);
135     i = l+1;
136
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));
140       continue;
141     }
142
143     // append wrapped parts of line into list
144     std::string overflow;
145     do {
146       FontPtr font = get_font_by_format_char(format_char);
147       std::string s2 = s;
148       if (font) s2 = font->wrap_to_width(s2, width, &overflow);
149       lines.push_back(new InfoBoxLine(format_char, s2));
150       s = overflow;
151     } while (s.length() > 0);
152   }
153
154   return lines;
155 }
156
157 void
158 InfoBoxLine::draw(DrawingContext& context, const Rectf& bbox, int layer)
159 {
160   Vector position = bbox.p1;
161   switch (lineType) {
162     case IMAGE:
163       context.draw_surface(image, Vector( (bbox.p1.x + bbox.p2.x - image->get_width()) / 2, position.y), layer);
164       break;
165     case NORMAL_LEFT:
166       context.draw_text(font, text, Vector(position.x, position.y), ALIGN_LEFT, layer, color);
167       break;
168     default:
169       context.draw_text(font, text, Vector((bbox.p1.x + bbox.p2.x) / 2, position.y), ALIGN_CENTER, layer, color);
170       break;
171   }
172 }
173
174 float
175 InfoBoxLine::get_height()
176 {
177   switch (lineType) {
178     case IMAGE:
179       return image->get_height() + ITEMS_SPACE;
180     case NORMAL_LEFT:
181       return font->get_height() + ITEMS_SPACE;
182     default:
183       return font->get_height() + ITEMS_SPACE;
184   }
185 }
186
187 /* EOF */