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 static void split_text(const std::string& text, std::vector<std::string>& lines)
46 // Split text string lines into a vector
48 std::string::size_type i, l;
51 l = text.find("\n", i);
53 if(l == std::string::npos) {
54 lines.push_back(text.substr(i, text.size()-i));
58 lines.push_back(text.substr(i, l-i));
63 TextScroller::TextScroller(const std::string& filename)
65 defaultspeed = DEFAULT_SPEED;
69 std::string background_file;
73 std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
75 const lisp::Lisp* text_lisp = root->get_lisp("supertux-text");
77 throw std::runtime_error("File isn't a supertux-text file");
79 if(!text_lisp->get("text", text))
80 throw std::runtime_error("file doesn't contain a text field");
81 if(!text_lisp->get("background", background_file))
82 throw std::runtime_error("file doesn't contain a background file");
83 text_lisp->get("speed", defaultspeed);
84 text_lisp->get("music", music);
85 } catch(std::exception& e) {
86 std::ostringstream msg;
87 msg << "Couldn't load file '" << filename << "': " << e.what() << std::endl;
88 throw std::runtime_error(msg.str());
91 // Split text string lines into a vector
92 split_text(text, lines);
94 for(size_t i = 0; i < lines.size(); ++i) {
95 const std::string& line = lines[i];
99 std::string imagename = line.substr(1, line.size()-1);
100 images.insert(std::make_pair(imagename, new Surface(imagename)));
104 // load background image
105 background.reset(new Surface("images/background/" + background_file));
110 TextScroller::~TextScroller()
112 for(std::map<std::string, Surface*>::iterator i = images.begin();
113 i != images.end(); ++i)
118 TextScroller::setup()
120 sound_manager->play_music(music);
121 Menu::set_current(NULL);
125 TextScroller::update(float elapsed_time)
127 if(main_controller->hold(Controller::UP)) {
128 speed = -defaultspeed*5;
129 } else if(main_controller->hold(Controller::DOWN)) {
130 speed = defaultspeed*5;
132 speed = defaultspeed;
134 if(main_controller->pressed(Controller::JUMP)
135 || main_controller->pressed(Controller::ACTION)
136 || main_controller->pressed(Controller::MENU_SELECT))
138 if(main_controller->pressed(Controller::PAUSE_MENU)) {
139 main_loop->exit_screen(new FadeOut(0.5));
142 scroll += speed * elapsed_time;
149 TextScroller::draw(DrawingContext& context)
151 context.draw_surface(background.get(), Vector(0,0), 0);
153 float y = SCREEN_HEIGHT - scroll;
154 for(size_t i = 0; i < lines.size(); i++) {
155 const std::string& line = lines[i];
156 if(line.size() == 0) {
157 y += white_text->get_height() + ITEMS_SPACE;
161 const Font* font = 0;
162 const Surface* image = 0;
166 case ' ': font = white_small_text; break;
167 case '\t': font = white_text; break;
168 case '-': font = white_big_text; break;
169 case '*': font = blue_text; break;
170 case '#': font = white_text; center = false; break;
172 std::string imagename = line.substr(1, line.size()-1);
173 image = images[imagename];
177 log_warning << "text contains an unformated line" << std::endl;
185 context.draw_text(font,
186 line.substr(1, line.size()-1),
187 Vector(SCREEN_WIDTH/2, y),
188 CENTER_ALLIGN, LAYER_FOREGROUND1);
190 context.draw_text(font,
191 line.substr(1, line.size()-1),
192 Vector(LEFT_BORDER, y),
193 LEFT_ALLIGN, LAYER_FOREGROUND1);
195 y += font->get_height() + ITEMS_SPACE;
198 context.draw_surface(image,
199 Vector( (SCREEN_WIDTH - image->get_width()) / 2, y), 255);
200 y += image->get_height() + ITEMS_SPACE;
205 main_loop->exit_screen(new FadeOut(0.5));
209 InfoBox::InfoBox(const std::string& text)
212 split_text(text, lines);
214 for(size_t i = 0; i < lines.size(); ++i) {
215 if(lines[i].size() == 0)
217 if(lines[i][0] == '!') {
218 std::string imagename = lines[i].substr(1, lines[i].size()-1);
219 images.insert(std::make_pair(imagename, new Surface(imagename)));
225 // get the arrow sprites
226 arrow_scrollup = new Surface("images/engine/menu/scroll-up.png");
227 arrow_scrolldown = new Surface("images/engine/menu/scroll-down.png");
229 catch (std::exception& e)
231 log_warning << "Could not load scrolling images: " << e.what() << std::endl;
233 arrow_scrolldown = 0;
239 for(std::map<std::string, Surface*>::iterator i = images.begin();
240 i != images.end(); ++i)
242 delete arrow_scrollup;
243 delete arrow_scrolldown;
247 InfoBox::draw(DrawingContext& context)
249 const Font* heading_font = white_big_text;
250 const Font* normal_font = white_text;
251 const Font* small_font = white_small_text;
252 const Font* reference_font = blue_text;
259 context.draw_filled_rect(Vector(x1, y1), Vector(width, height),
260 Color(0.6f, 0.7f, 0.8f, 0.5f), LAYER_GUI-1);
263 for(size_t i = firstline; i < lines.size(); ++i) {
264 const std::string& line = lines[i];
268 if(line.size() == 0) {
269 y += normal_font->get_height() + ITEMS_SPACE;
273 const Font* font = 0;
274 const Surface* image = 0;
278 case ' ': font = small_font; break;
279 case '\t': font = normal_font; break;
280 case '-': font = heading_font; break;
281 case '*': font = reference_font; break;
282 case '#': font = normal_font; center = false; break;
284 std::string imagename = line.substr(1, line.size()-1);
285 image = images[imagename];
289 log_warning << "text contains an unformatted line" << std::endl;
296 context.draw_surface(image,
297 Vector( (SCREEN_WIDTH - image->get_width()) / 2,
299 y += image->get_height() + ITEMS_SPACE;
301 context.draw_text(font,
302 line.substr(1, line.size()-1),
303 Vector(SCREEN_WIDTH/2, y),
304 CENTER_ALLIGN, LAYER_GUI);
305 y += font->get_height() + ITEMS_SPACE;
307 context.draw_text(font,
308 line.substr(1, line.size()-1),
310 LEFT_ALLIGN, LAYER_GUI);
311 y += font->get_height() + ITEMS_SPACE;
314 // draw the scrolling arrows
315 if (arrow_scrollup && firstline > 0)
316 context.draw_surface(arrow_scrollup,
317 Vector( x1 + width - arrow_scrollup->get_width(), // top-right corner of box
320 if (arrow_scrolldown && firstline < lines.size()-1)
321 context.draw_surface(arrow_scrolldown,
322 Vector( x1 + width - arrow_scrolldown->get_width(), // bottom-light corner of box
323 y1 + height - arrow_scrolldown->get_height()),
336 InfoBox::scrolldown()
338 if(firstline < lines.size()-1)