4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22 #include "textscroller.hpp"
26 #include "mainloop.hpp"
27 #include "resources.hpp"
28 #include "video/font.hpp"
29 #include "video/drawing_context.hpp"
30 #include "video/surface.hpp"
31 #include "gui/menu.hpp"
32 #include "lisp/parser.hpp"
33 #include "lisp/lisp.hpp"
34 #include "audio/sound_manager.hpp"
36 #include "fadeout.hpp"
37 #include "control/joystickkeyboardcontroller.hpp"
39 static const float DEFAULT_SPEED = 20;
40 static const float LEFT_BORDER = 50;
41 static const float SCROLL = 60;
42 static const float ITEMS_SPACE = 4;
44 TextScroller::TextScroller(const std::string& filename)
46 defaultspeed = DEFAULT_SPEED;
50 std::string background_file;
54 const lisp::Lisp* root = parser.parse(filename);
56 const lisp::Lisp* text_lisp = root->get_lisp("supertux-text");
58 throw std::runtime_error("File isn't a supertux-text file");
60 if(!text_lisp->get("text", text))
61 throw std::runtime_error("file doesn't contain a text field");
62 if(!text_lisp->get("background", background_file))
63 throw std::runtime_error("file doesn't contain a background file");
64 text_lisp->get("speed", defaultspeed);
65 text_lisp->get("music", music);
66 } catch(std::exception& e) {
67 std::ostringstream msg;
68 msg << "Couldn't load file '" << filename << "': " << e.what() << std::endl;
69 throw std::runtime_error(msg.str());
72 // Split text string lines into a vector
73 lines = InfoBoxLine::split(text, SCREEN_WIDTH - 2*LEFT_BORDER);
75 // load background image
76 background.reset(new Surface("images/background/" + background_file));
82 TextScroller::~TextScroller()
84 for(std::vector<InfoBoxLine*>::iterator i = lines.begin(); i != lines.end(); i++) delete *i;
90 sound_manager->play_music(music);
91 Menu::set_current(NULL);
95 TextScroller::update(float elapsed_time)
97 if(main_controller->hold(Controller::UP)) {
98 speed = -defaultspeed*5;
99 } else if(main_controller->hold(Controller::DOWN)) {
100 speed = defaultspeed*5;
102 speed = defaultspeed;
104 if(main_controller->pressed(Controller::JUMP)
105 || main_controller->pressed(Controller::ACTION)
106 || main_controller->pressed(Controller::MENU_SELECT))
108 if(main_controller->pressed(Controller::PAUSE_MENU)) {
109 main_loop->exit_screen(new FadeOut(0.5));
112 scroll += speed * elapsed_time;
119 TextScroller::draw(DrawingContext& context)
121 context.draw_filled_rect(Vector(0, 0), Vector(SCREEN_WIDTH, SCREEN_HEIGHT),
122 Color(0.6f, 0.7f, 0.8f, 0.5f), 0);
123 context.draw_surface(background.get(), Vector(SCREEN_WIDTH/2 - background->get_width()/2 , SCREEN_HEIGHT/2 - background->get_height()/2), 0);
125 float y = SCREEN_HEIGHT - scroll;
126 for(size_t i = 0; i < lines.size(); i++) {
127 lines[i]->draw(context, Rect(LEFT_BORDER, y, SCREEN_WIDTH - 2*LEFT_BORDER, y), LAYER_GUI);
128 y += lines[i]->get_height();
131 if(y < 0 && !fading ) {
133 main_loop->exit_screen(new FadeOut(0.5));
137 InfoBox::InfoBox(const std::string& text)
140 // Split text string lines into a vector
141 lines = InfoBoxLine::split(text, 400);
145 // get the arrow sprites
146 arrow_scrollup = new Surface("images/engine/menu/scroll-up.png");
147 arrow_scrolldown = new Surface("images/engine/menu/scroll-down.png");
149 catch (std::exception& e)
151 log_warning << "Could not load scrolling images: " << e.what() << std::endl;
153 arrow_scrolldown = 0;
159 for(std::vector<InfoBoxLine*>::iterator i = lines.begin();
160 i != lines.end(); i++)
162 delete arrow_scrollup;
163 delete arrow_scrolldown;
167 InfoBox::draw(DrawingContext& context)
169 float x1 = SCREEN_WIDTH/2-200;
170 float y1 = SCREEN_HEIGHT/2-200;
174 context.draw_filled_rect(Vector(x1, y1), Vector(width, height),
175 Color(0.6f, 0.7f, 0.8f, 0.5f), LAYER_GUI-1);
178 bool linesLeft = false;
179 for(size_t i = firstline; i < lines.size(); ++i) {
180 if(y >= y1 + height) {
185 lines[i]->draw(context, Rect(x1, y, x1+width, y), LAYER_GUI);
186 y += lines[i]->get_height();
190 // draw the scrolling arrows
191 if (arrow_scrollup && firstline > 0)
192 context.draw_surface(arrow_scrollup,
193 Vector( x1 + width - arrow_scrollup->get_width(), // top-right corner of box
196 if (arrow_scrolldown && linesLeft && firstline < lines.size()-1)
197 context.draw_surface(arrow_scrolldown,
198 Vector( x1 + width - arrow_scrolldown->get_width(), // bottom-light corner of box
199 y1 + height - arrow_scrolldown->get_height()),
212 InfoBox::scrolldown()
214 if(firstline < lines.size()-1)
229 Font* get_font_by_format_char(char format_char) {
233 return white_small_text;
239 return white_big_text;
252 log_warning << "Unknown format_char: '" << format_char << "'" << std::endl;
257 InfoBoxLine::LineType get_linetype_by_format_char(char format_char) {
261 return InfoBoxLine::SMALL;
264 return InfoBoxLine::NORMAL;
267 return InfoBoxLine::HEADING;
270 return InfoBoxLine::REFERENCE;
273 return InfoBoxLine::NORMAL_LEFT;
276 return InfoBoxLine::IMAGE;
279 return InfoBoxLine::SMALL;
280 log_warning << "Unknown format_char: '" << format_char << "'" << std::endl;
286 InfoBoxLine::InfoBoxLine(char format_char, const std::string& text) : lineType(NORMAL), font(white_text), text(text), image(0)
288 font = get_font_by_format_char(format_char);
289 lineType = get_linetype_by_format_char(format_char);
290 if (lineType == IMAGE) image = new Surface(text);
293 InfoBoxLine::~InfoBoxLine()
298 const std::vector<InfoBoxLine*>
299 InfoBoxLine::split(const std::string& text, float width)
301 std::vector<InfoBoxLine*> lines;
303 std::string::size_type i = 0;
304 std::string::size_type l;
305 char format_char = '#';
306 while(i < text.size()) {
307 // take care of empty lines - represent them as blank lines of normal text
308 if (text[i] == '\n') {
309 lines.push_back(new InfoBoxLine('\t', ""));
314 // extract the format_char
315 format_char = text[i];
317 if (i >= text.size()) break;
320 l = text.find("\n", i);
321 if (l == std::string::npos) l=text.size();
322 std::string s = text.substr(i, l-i);
325 // if we are dealing with an image, just store the line
326 if (format_char == '!') {
327 lines.push_back(new InfoBoxLine(format_char, s));
331 // append wrapped parts of line into list
332 std::string overflow;
334 Font* font = get_font_by_format_char(format_char);
336 if (font) s2 = font->wrap_to_width(s2, width, &overflow);
337 lines.push_back(new InfoBoxLine(format_char, s2));
339 } while (s.length() > 0);
347 InfoBoxLine::draw(DrawingContext& context, const Rect& bbox, int layer)
349 Vector position = bbox.p1;
352 context.draw_surface(image, Vector( (bbox.p1.x + bbox.p2.x - image->get_width()) / 2, position.y), layer);
355 context.draw_text(font, text, Vector(position.x, position.y), ALIGN_LEFT, layer);
358 context.draw_text(font, text, Vector((bbox.p1.x + bbox.p2.x) / 2, position.y), ALIGN_CENTER, layer);
364 InfoBoxLine::get_height()
368 return image->get_height() + ITEMS_SPACE;
370 return font->get_height() + ITEMS_SPACE;
372 return font->get_height() + ITEMS_SPACE;