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/textscroller.hpp"
19 #include "audio/sound_manager.hpp"
20 #include "control/joystickkeyboardcontroller.hpp"
21 #include "lisp/lisp.hpp"
22 #include "lisp/parser.hpp"
23 #include "supertux/fadeout.hpp"
24 #include "supertux/main.hpp"
25 #include "supertux/mainloop.hpp"
26 #include "supertux/resources.hpp"
27 #include "video/drawing_context.hpp"
29 static const float DEFAULT_SPEED = 20;
30 static const float LEFT_BORDER = 50;
31 static const float SCROLL = 60;
32 static const float ITEMS_SPACE = 4;
34 TextScroller::TextScroller(const std::string& filename) :
43 defaultspeed = DEFAULT_SPEED;
47 std::string background_file;
51 const lisp::Lisp* root = parser.parse(filename);
53 const lisp::Lisp* text_lisp = root->get_lisp("supertux-text");
55 throw std::runtime_error("File isn't a supertux-text file");
57 if(!text_lisp->get("text", text))
58 throw std::runtime_error("file doesn't contain a text field");
59 if(!text_lisp->get("background", background_file))
60 throw std::runtime_error("file doesn't contain a background file");
61 text_lisp->get("speed", defaultspeed);
62 text_lisp->get("music", music);
63 } catch(std::exception& e) {
64 std::ostringstream msg;
65 msg << "Couldn't load file '" << filename << "': " << e.what() << std::endl;
66 throw std::runtime_error(msg.str());
69 // Split text string lines into a vector
70 lines = InfoBoxLine::split(text, SCREEN_WIDTH - 2*LEFT_BORDER);
72 // load background image
73 background.reset(new Surface("images/background/" + background_file));
79 TextScroller::~TextScroller()
81 for(std::vector<InfoBoxLine*>::iterator i = lines.begin(); i != lines.end(); i++) delete *i;
87 sound_manager->play_music(music);
91 TextScroller::update(float elapsed_time)
93 if(g_main_controller->hold(Controller::UP)) {
94 speed = -defaultspeed*5;
95 } else if(g_main_controller->hold(Controller::DOWN)) {
96 speed = defaultspeed*5;
100 if(g_main_controller->pressed(Controller::JUMP)
101 || g_main_controller->pressed(Controller::ACTION)
102 || g_main_controller->pressed(Controller::MENU_SELECT))
104 if(g_main_controller->pressed(Controller::PAUSE_MENU)) {
105 g_main_loop->exit_screen(new FadeOut(0.5));
108 scroll += speed * elapsed_time;
115 TextScroller::draw(DrawingContext& context)
117 context.draw_filled_rect(Vector(0, 0), Vector(SCREEN_WIDTH, SCREEN_HEIGHT),
118 Color(0.6f, 0.7f, 0.8f, 0.5f), 0);
119 context.draw_surface(background.get(), Vector(SCREEN_WIDTH/2 - background->get_width()/2 , SCREEN_HEIGHT/2 - background->get_height()/2), 0);
121 float y = SCREEN_HEIGHT - scroll;
122 for(size_t i = 0; i < lines.size(); i++) {
123 if (y + lines[i]->get_height() >= 0 && SCREEN_HEIGHT - y >= 0) {
124 lines[i]->draw(context, Rect(LEFT_BORDER, y, SCREEN_WIDTH - 2*LEFT_BORDER, y), LAYER_GUI);
127 y += lines[i]->get_height();
130 if(y < 0 && !fading ) {
132 g_main_loop->exit_screen(new FadeOut(0.5));
136 InfoBox::InfoBox(const std::string& text)
139 // Split text string lines into a vector
140 lines = InfoBoxLine::split(text, 400);
144 // get the arrow sprites
145 arrow_scrollup = new Surface("images/engine/menu/scroll-up.png");
146 arrow_scrolldown = new Surface("images/engine/menu/scroll-down.png");
148 catch (std::exception& e)
150 log_warning << "Could not load scrolling images: " << e.what() << std::endl;
152 arrow_scrolldown = 0;
158 for(std::vector<InfoBoxLine*>::iterator i = lines.begin();
159 i != lines.end(); i++)
161 delete arrow_scrollup;
162 delete arrow_scrolldown;
166 InfoBox::draw(DrawingContext& context)
168 float x1 = SCREEN_WIDTH/2-200;
169 float y1 = SCREEN_HEIGHT/2-200;
173 context.draw_filled_rect(Vector(x1, y1), Vector(width, height),
174 Color(0.6f, 0.7f, 0.8f, 0.5f), LAYER_GUI-1);
177 bool linesLeft = false;
178 for(size_t i = firstline; i < lines.size(); ++i) {
179 if(y >= y1 + height) {
184 lines[i]->draw(context, Rect(x1, y, x1+width, y), LAYER_GUI);
185 y += lines[i]->get_height();
189 // draw the scrolling arrows
190 if (arrow_scrollup && firstline > 0)
191 context.draw_surface(arrow_scrollup,
192 Vector( x1 + width - arrow_scrollup->get_width(), // top-right corner of box
195 if (arrow_scrolldown && linesLeft && firstline < lines.size()-1)
196 context.draw_surface(arrow_scrolldown,
197 Vector( x1 + width - arrow_scrolldown->get_width(), // bottom-light corner of box
198 y1 + height - arrow_scrolldown->get_height()),
211 InfoBox::scrolldown()
213 if(firstline < lines.size()-1)
228 Font* get_font_by_format_char(char format_char) {
245 log_warning << "Unknown format_char: '" << format_char << "'" << std::endl;
250 Color get_color_by_format_char(char format_char) {
254 return TextScroller::small_color;
257 return TextScroller::heading_color;
260 return TextScroller::reference_color;
264 return TextScroller::normal_color;
268 log_warning << "Unknown format_char: '" << format_char << "'" << std::endl;
273 InfoBoxLine::LineType get_linetype_by_format_char(char format_char) {
277 return InfoBoxLine::SMALL;
280 return InfoBoxLine::NORMAL;
283 return InfoBoxLine::HEADING;
286 return InfoBoxLine::REFERENCE;
289 return InfoBoxLine::NORMAL_LEFT;
292 return InfoBoxLine::IMAGE;
295 return InfoBoxLine::SMALL;
296 log_warning << "Unknown format_char: '" << format_char << "'" << std::endl;
302 InfoBoxLine::InfoBoxLine(char format_char, const std::string& text) :
308 font = get_font_by_format_char(format_char);
309 lineType = get_linetype_by_format_char(format_char);
310 color = get_color_by_format_char(format_char);
311 if (lineType == IMAGE) image = new Surface(text);
314 InfoBoxLine::~InfoBoxLine()
319 const std::vector<InfoBoxLine*>
320 InfoBoxLine::split(const std::string& text, float width)
322 std::vector<InfoBoxLine*> lines;
324 std::string::size_type i = 0;
325 std::string::size_type l;
326 char format_char = '#';
327 while(i < text.size()) {
328 // take care of empty lines - represent them as blank lines of normal text
329 if (text[i] == '\n') {
330 lines.push_back(new InfoBoxLine('\t', ""));
335 // extract the format_char
336 format_char = text[i];
338 if (i >= text.size()) break;
341 l = text.find("\n", i);
342 if (l == std::string::npos) l=text.size();
343 std::string s = text.substr(i, l-i);
346 // if we are dealing with an image, just store the line
347 if (format_char == '!') {
348 lines.push_back(new InfoBoxLine(format_char, s));
352 // append wrapped parts of line into list
353 std::string overflow;
355 Font* font = get_font_by_format_char(format_char);
357 if (font) s2 = font->wrap_to_width(s2, width, &overflow);
358 lines.push_back(new InfoBoxLine(format_char, s2));
360 } while (s.length() > 0);
367 InfoBoxLine::draw(DrawingContext& context, const Rect& bbox, int layer)
369 Vector position = bbox.p1;
372 context.draw_surface(image, Vector( (bbox.p1.x + bbox.p2.x - image->get_width()) / 2, position.y), layer);
375 context.draw_text(font, text, Vector(position.x, position.y), ALIGN_LEFT, layer, color);
378 context.draw_text(font, text, Vector((bbox.p1.x + bbox.p2.x) / 2, position.y), ALIGN_CENTER, layer, color);
384 InfoBoxLine::get_height()
388 return image->get_height() + ITEMS_SPACE;
390 return font->get_height() + ITEMS_SPACE;
392 return font->get_height() + ITEMS_SPACE;