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;
46 TextScroller::TextScroller(const std::string& filename)
48 defaultspeed = DEFAULT_SPEED;
52 std::string background_file;
56 std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
58 const lisp::Lisp* text_lisp = root->get_lisp("supertux-text");
60 throw std::runtime_error("File isn't a supertux-text file");
62 if(!text_lisp->get("text", text))
63 throw std::runtime_error("file doesn't contain a text field");
64 if(!text_lisp->get("background", background_file))
65 throw std::runtime_error("file doesn't contain a background file");
66 text_lisp->get("speed", defaultspeed);
67 text_lisp->get("music", music);
68 } catch(std::exception& e) {
69 std::ostringstream msg;
70 msg << "Couldn't load file '" << filename << "': " << e.what() << std::endl;
71 throw std::runtime_error(msg.str());
74 // Split text string lines into a vector
75 lines = InfoBoxLine::split(text, 40);
77 // load background image
78 background.reset(new Surface("images/background/" + background_file));
83 TextScroller::~TextScroller()
85 for(std::vector<InfoBoxLine*>::iterator i = lines.begin(); i != lines.end(); i++) delete *i;
91 sound_manager->play_music(music);
92 Menu::set_current(NULL);
96 TextScroller::update(float elapsed_time)
98 if(main_controller->hold(Controller::UP)) {
99 speed = -defaultspeed*5;
100 } else if(main_controller->hold(Controller::DOWN)) {
101 speed = defaultspeed*5;
103 speed = defaultspeed;
105 if(main_controller->pressed(Controller::JUMP)
106 || main_controller->pressed(Controller::ACTION)
107 || main_controller->pressed(Controller::MENU_SELECT))
109 if(main_controller->pressed(Controller::PAUSE_MENU)) {
110 main_loop->exit_screen(new FadeOut(0.5));
113 scroll += speed * elapsed_time;
120 TextScroller::draw(DrawingContext& context)
122 context.draw_surface(background.get(), Vector(0,0), 0);
124 float y = SCREEN_HEIGHT - scroll;
125 for(size_t i = 0; i < lines.size(); i++) {
126 lines[i]->draw(context, Vector(LEFT_BORDER, y), LAYER_GUI);
127 y += lines[i]->get_height();
131 main_loop->exit_screen(new FadeOut(0.5));
135 InfoBox::InfoBox(const std::string& text)
138 // Split text string lines into a vector
139 lines = InfoBoxLine::split(text, 23);
143 // get the arrow sprites
144 arrow_scrollup = new Surface("images/engine/menu/scroll-up.png");
145 arrow_scrolldown = new Surface("images/engine/menu/scroll-down.png");
147 catch (std::exception& e)
149 log_warning << "Could not load scrolling images: " << e.what() << std::endl;
151 arrow_scrolldown = 0;
157 for(std::vector<InfoBoxLine*>::iterator i = lines.begin(); i != lines.end(); i++) delete *i;
158 delete arrow_scrollup;
159 delete arrow_scrolldown;
163 InfoBox::draw(DrawingContext& context)
170 context.draw_filled_rect(Vector(x1, y1), Vector(width, height),
171 Color(0.6f, 0.7f, 0.8f, 0.5f), LAYER_GUI-1);
174 for(size_t i = firstline; i < lines.size(); ++i) {
175 if(y >= y1 + height) break;
177 lines[i]->draw(context, Vector(x1, y), LAYER_GUI);
178 y += lines[i]->get_height();
180 // draw the scrolling arrows
181 if (arrow_scrollup && firstline > 0)
182 context.draw_surface(arrow_scrollup,
183 Vector( x1 + width - arrow_scrollup->get_width(), // top-right corner of box
186 if (arrow_scrolldown && firstline < lines.size()-1)
187 context.draw_surface(arrow_scrolldown,
188 Vector( x1 + width - arrow_scrolldown->get_width(), // bottom-light corner of box
189 y1 + height - arrow_scrolldown->get_height()),
202 InfoBox::scrolldown()
204 if(firstline < lines.size()-1)
218 InfoBoxLine::InfoBoxLine(char format_char, const std::string& text) : lineType(NORMAL), font(white_text), text(text), image(0)
224 font = white_small_text;
232 font = white_big_text;
235 lineType = REFERENCE;
239 lineType = NORMAL_LEFT;
244 image = new Surface(text);
247 log_warning << "Unknown format_char: '" << format_char << "'" << std::endl;
252 InfoBoxLine::~InfoBoxLine()
257 const std::vector<InfoBoxLine*>
258 InfoBoxLine::split(const std::string& text, int line_length)
260 std::vector<InfoBoxLine*> lines;
262 std::string::size_type i = 0;
263 std::string::size_type l;
264 char format_char = '#';
265 while(i < text.size()) {
266 // take care of empty lines - represent them as blank lines of normal text
267 if (text[i] == '\n') {
268 lines.push_back(new InfoBoxLine('\t', ""));
273 // extract the format_char
274 format_char = text[i];
276 if (i >= text.size()) break;
279 l = text.find("\n", i);
280 if (l == std::string::npos) l=text.size();
281 std::string s = text.substr(i, l-i);
284 // if we are dealing with an image, just store the line
285 if (format_char == '!') {
286 lines.push_back(new InfoBoxLine(format_char, s));
290 // if we are dealing with text, wrap long lines
291 while ((int)s.length() > line_length) {
292 int split_at = line_length;
293 while ((split_at > 0) && (s[split_at] != ' ')) split_at--;
294 if (split_at == 0) split_at = line_length;
296 lines.push_back(new InfoBoxLine(format_char, s.substr(0, split_at)));
297 if (s[split_at] == ' ') split_at++;
298 s = s.substr(split_at);
300 lines.push_back(new InfoBoxLine(format_char, s));
308 InfoBoxLine::draw(DrawingContext& context, const Vector& position, int layer)
312 context.draw_surface(image, Vector( (SCREEN_WIDTH - image->get_width()) / 2, position.y), layer);
315 context.draw_text(font, text, Vector(position.x, position.y), LEFT_ALLIGN, layer);
318 context.draw_text(font, text, Vector(SCREEN_WIDTH/2, position.y), CENTER_ALLIGN, layer);
324 InfoBoxLine::get_height()
328 return image->get_height() + ITEMS_SPACE;
330 return font->get_height() + ITEMS_SPACE;
332 return font->get_height() + ITEMS_SPACE;