[cppcheck] Part 1: Performance
[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(get_font_by_format_char(format_char)),
93   color(),
94   text(text_),
95   image()
96 {
97   lineType = get_linetype_by_format_char(format_char);
98   color = get_color_by_format_char(format_char);
99   if (lineType == IMAGE)
100   {
101     image = Surface::create(text);
102   }
103 }
104
105 InfoBoxLine::~InfoBoxLine()
106 {
107 }
108
109 const std::vector<InfoBoxLine*>
110 InfoBoxLine::split(const std::string& text, float width)
111 {
112   std::vector<InfoBoxLine*> lines;
113
114   std::string::size_type i = 0;
115   std::string::size_type l;
116   char format_char = '#';
117   while(i < text.size()) {
118     // take care of empty lines - represent them as blank lines of normal text
119     if (text[i] == '\n') {
120       lines.push_back(new InfoBoxLine('\t', ""));
121       i++;
122       continue;
123     }
124
125     // extract the format_char
126     format_char = text[i];
127     i++;
128     if (i >= text.size()) break;
129
130     // extract one line
131     l = text.find("\n", i);
132     if (l == std::string::npos) l=text.size();
133     std::string s = text.substr(i, l-i);
134     i = l+1;
135
136     // if we are dealing with an image, just store the line
137     if (format_char == '!') {
138       lines.push_back(new InfoBoxLine(format_char, s));
139       continue;
140     }
141
142     // append wrapped parts of line into list
143     std::string overflow;
144     do {
145       FontPtr font = get_font_by_format_char(format_char);
146       std::string s2 = s;
147       if (font) s2 = font->wrap_to_width(s2, width, &overflow);
148       lines.push_back(new InfoBoxLine(format_char, s2));
149       s = overflow;
150     } while (s.length() > 0);
151   }
152
153   return lines;
154 }
155
156 void
157 InfoBoxLine::draw(DrawingContext& context, const Rectf& bbox, int layer)
158 {
159   Vector position = bbox.p1;
160   switch (lineType) {
161     case IMAGE:
162       context.draw_surface(image, Vector( (bbox.p1.x + bbox.p2.x - image->get_width()) / 2, position.y), layer);
163       break;
164     case NORMAL_LEFT:
165       context.draw_text(font, text, Vector(position.x, position.y), ALIGN_LEFT, layer, color);
166       break;
167     default:
168       context.draw_text(font, text, Vector((bbox.p1.x + bbox.p2.x) / 2, position.y), ALIGN_CENTER, layer, color);
169       break;
170   }
171 }
172
173 float
174 InfoBoxLine::get_height()
175 {
176   switch (lineType) {
177     case IMAGE:
178       return image->get_height() + ITEMS_SPACE;
179     case NORMAL_LEFT:
180       return font->get_height() + ITEMS_SPACE;
181     default:
182       return font->get_height() + ITEMS_SPACE;
183   }
184 }
185
186 /* EOF */