src/supertux/info_box_line.cpp

Go to the documentation of this file.
00001 //  SuperTux
00002 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
00003 //
00004 //  This program is free software: you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation, either version 3 of the License, or
00007 //  (at your option) any later version.
00008 //
00009 //  This program is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 #include "supertux/info_box_line.hpp"
00018 
00019 #include "supertux/textscroller.hpp"
00020 #include "supertux/resources.hpp"
00021 #include "video/drawing_context.hpp"
00022 #include "video/font.hpp"
00023 #include "video/surface.hpp"
00024 
00025 static const float ITEMS_SPACE = 4;
00026 
00027 namespace {
00028 
00029 FontPtr get_font_by_format_char(char format_char) {
00030   switch(format_char)
00031   {
00032     case ' ':
00033       return Resources::small_font;
00034       break;
00035     case '-':
00036       return Resources::big_font;
00037       break;
00038     case '\t':
00039     case '*':
00040     case '#':
00041     case '!':
00042       return Resources::normal_font;
00043     break;
00044     default:
00045       return Resources::normal_font;
00046       log_warning << "Unknown format_char: '" << format_char << "'" << std::endl;
00047       break;
00048   }
00049 }
00050 
00051 Color get_color_by_format_char(char format_char) {
00052   switch(format_char)
00053   {
00054     case ' ':
00055       return TextScroller::small_color;
00056       break;
00057     case '-':
00058       return TextScroller::heading_color;
00059       break;
00060     case '*':
00061       return TextScroller::reference_color;
00062     case '\t':
00063     case '#':
00064     case '!':
00065       return TextScroller::normal_color;
00066     break;
00067     default:
00068       return Color(0,0,0);
00069       log_warning << "Unknown format_char: '" << format_char << "'" << std::endl;
00070       break;
00071   }
00072 }
00073 
00074 InfoBoxLine::LineType get_linetype_by_format_char(char format_char) {
00075   switch(format_char)
00076   {
00077     case ' ':
00078       return InfoBoxLine::SMALL;
00079       break;
00080     case '\t':
00081       return InfoBoxLine::NORMAL;
00082       break;
00083     case '-':
00084       return InfoBoxLine::HEADING;
00085       break;
00086     case '*':
00087       return InfoBoxLine::REFERENCE;
00088       break;
00089     case '#':
00090       return InfoBoxLine::NORMAL_LEFT;
00091       break;
00092     case '!':
00093       return InfoBoxLine::IMAGE;
00094       break;
00095     default:
00096       return InfoBoxLine::SMALL;
00097       log_warning << "Unknown format_char: '" << format_char << "'" << std::endl;
00098       break;
00099   }
00100 }
00101 
00102 } // namespace
00103 
00104 InfoBoxLine::InfoBoxLine(char format_char, const std::string& text) : 
00105   lineType(NORMAL),
00106   font(Resources::normal_font), 
00107   color(),
00108   text(text), 
00109   image()
00110 {
00111   font = get_font_by_format_char(format_char);
00112   lineType = get_linetype_by_format_char(format_char);
00113   color = get_color_by_format_char(format_char);
00114   if (lineType == IMAGE) 
00115   {
00116     image = Surface::create(text);
00117   }
00118 }
00119 
00120 InfoBoxLine::~InfoBoxLine()
00121 {
00122 }
00123 
00124 const std::vector<InfoBoxLine*>
00125 InfoBoxLine::split(const std::string& text, float width)
00126 {
00127   std::vector<InfoBoxLine*> lines;
00128 
00129   std::string::size_type i = 0;
00130   std::string::size_type l;
00131   char format_char = '#';
00132   while(i < text.size()) {
00133     // take care of empty lines - represent them as blank lines of normal text
00134     if (text[i] == '\n') {
00135       lines.push_back(new InfoBoxLine('\t', ""));
00136       i++;
00137       continue;
00138     }
00139 
00140     // extract the format_char
00141     format_char = text[i];
00142     i++;
00143     if (i >= text.size()) break;
00144 
00145     // extract one line
00146     l = text.find("\n", i);
00147     if (l == std::string::npos) l=text.size();
00148     std::string s = text.substr(i, l-i);
00149     i = l+1;
00150 
00151     // if we are dealing with an image, just store the line
00152     if (format_char == '!') {
00153       lines.push_back(new InfoBoxLine(format_char, s));
00154       continue;
00155     }
00156 
00157     // append wrapped parts of line into list
00158     std::string overflow;
00159     do {
00160       FontPtr font = get_font_by_format_char(format_char);
00161       std::string s2 = s;
00162       if (font) s2 = font->wrap_to_width(s2, width, &overflow);
00163       lines.push_back(new InfoBoxLine(format_char, s2));
00164       s = overflow;
00165     } while (s.length() > 0);
00166   }
00167 
00168   return lines;
00169 }
00170 
00171 void
00172 InfoBoxLine::draw(DrawingContext& context, const Rectf& bbox, int layer)
00173 {
00174   Vector position = bbox.p1;
00175   switch (lineType) {
00176     case IMAGE:
00177       context.draw_surface(image, Vector( (bbox.p1.x + bbox.p2.x - image->get_width()) / 2, position.y), layer);
00178       break;
00179     case NORMAL_LEFT:
00180       context.draw_text(font, text, Vector(position.x, position.y), ALIGN_LEFT, layer, color);
00181       break;
00182     default:
00183       context.draw_text(font, text, Vector((bbox.p1.x + bbox.p2.x) / 2, position.y), ALIGN_CENTER, layer, color);
00184       break;
00185   }
00186 }
00187 
00188 float
00189 InfoBoxLine::get_height()
00190 {
00191   switch (lineType) {
00192     case IMAGE:
00193       return image->get_height() + ITEMS_SPACE;
00194     case NORMAL_LEFT:
00195       return font->get_height() + ITEMS_SPACE;
00196     default:
00197       return font->get_height() + ITEMS_SPACE;
00198   }
00199 }
00200 
00201 /* EOF */

Generated on Mon Jun 9 03:38:22 2014 for SuperTux by  doxygen 1.5.1