4 // Copyright (C) 2005 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 "video/screen.hpp"
32 #include "gui/menu.hpp"
33 #include "lisp/parser.hpp"
34 #include "lisp/lisp.hpp"
35 #include "audio/sound_manager.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)) {
140 main_loop->exit_screen();
143 scroll += speed * elapsed_time;
150 TextScroller::draw(DrawingContext& context)
152 context.draw_surface(background.get(), Vector(0,0), 0);
154 float y = SCREEN_HEIGHT - scroll;
155 for(size_t i = 0; i < lines.size(); i++) {
156 const std::string& line = lines[i];
157 if(line.size() == 0) {
158 y += white_text->get_height() + ITEMS_SPACE;
162 const Font* font = 0;
163 const Surface* image = 0;
167 case ' ': font = white_small_text; break;
168 case '\t': font = white_text; break;
169 case '-': font = white_big_text; break;
170 case '*': font = blue_text; break;
171 case '#': font = white_text; center = false; break;
173 std::string imagename = line.substr(1, line.size()-1);
174 image = images[imagename];
178 log_warning << "text contains an unformated line" << std::endl;
186 context.draw_text(font,
187 line.substr(1, line.size()-1),
188 Vector(SCREEN_WIDTH/2, y),
189 CENTER_ALLIGN, LAYER_FOREGROUND1);
191 context.draw_text(font,
192 line.substr(1, line.size()-1),
193 Vector(LEFT_BORDER, y),
194 LEFT_ALLIGN, LAYER_FOREGROUND1);
196 y += font->get_height() + ITEMS_SPACE;
199 context.draw_surface(image,
200 Vector( (SCREEN_WIDTH - image->get_width()) / 2, y), 255);
201 y += image->get_height() + ITEMS_SPACE;
207 main_loop->exit_screen();
211 InfoBox::InfoBox(const std::string& text)
214 split_text(text, lines);
216 for(size_t i = 0; i < lines.size(); ++i) {
217 if(lines[i].size() == 0)
219 if(lines[i][0] == '!') {
220 std::string imagename = lines[i].substr(1, lines[i].size()-1);
221 images.insert(std::make_pair(imagename, new Surface(imagename)));
227 // get the arrow sprites
228 arrow_scrollup = new Surface("images/engine/menu/scroll-up.png");
229 arrow_scrolldown = new Surface("images/engine/menu/scroll-down.png");
231 catch (std::exception& e)
233 log_warning << "Could not load scrolling images: " << e.what() << std::endl;
235 arrow_scrolldown = 0;
241 for(std::map<std::string, Surface*>::iterator i = images.begin();
242 i != images.end(); ++i)
244 delete arrow_scrollup;
245 delete arrow_scrolldown;
249 InfoBox::draw(DrawingContext& context)
251 const Font* heading_font = white_big_text;
252 const Font* normal_font = white_text;
253 const Font* small_font = white_small_text;
254 const Font* reference_font = blue_text;
261 context.draw_filled_rect(Vector(x1, y1), Vector(width, height),
262 Color(0.6f, 0.7f, 0.8f, 0.5f), LAYER_GUI-1);
265 for(size_t i = firstline; i < lines.size(); ++i) {
266 const std::string& line = lines[i];
270 if(line.size() == 0) {
271 y += normal_font->get_height() + ITEMS_SPACE;
275 const Font* font = 0;
276 const Surface* image = 0;
280 case ' ': font = small_font; break;
281 case '\t': font = normal_font; break;
282 case '-': font = heading_font; break;
283 case '*': font = reference_font; break;
284 case '#': font = normal_font; center = false; break;
286 std::string imagename = line.substr(1, line.size()-1);
287 image = images[imagename];
291 log_warning << "text contains an unformatted line" << std::endl;
298 context.draw_surface(image,
299 Vector( (SCREEN_WIDTH - image->get_width()) / 2,
301 y += image->get_height() + ITEMS_SPACE;
303 context.draw_text(font,
304 line.substr(1, line.size()-1),
305 Vector(SCREEN_WIDTH/2, y),
306 CENTER_ALLIGN, LAYER_GUI);
307 y += font->get_height() + ITEMS_SPACE;
309 context.draw_text(font,
310 line.substr(1, line.size()-1),
312 LEFT_ALLIGN, LAYER_GUI);
313 y += font->get_height() + ITEMS_SPACE;
316 // draw the scrolling arrows
317 if (arrow_scrollup && firstline > 0)
318 context.draw_surface(arrow_scrollup,
319 Vector( x1 + width - arrow_scrollup->get_width(), // top-right corner of box
322 if (arrow_scrolldown && firstline < lines.size()-1)
323 context.draw_surface(arrow_scrolldown,
324 Vector( x1 + width - arrow_scrolldown->get_width(), // bottom-light corner of box
325 y1 + height - arrow_scrolldown->get_height()),
338 InfoBox::scrolldown()
340 if(firstline < lines.size()-1)