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)
36 defaultspeed = DEFAULT_SPEED;
40 std::string background_file;
44 const lisp::Lisp* root = parser.parse(filename);
46 const lisp::Lisp* text_lisp = root->get_lisp("supertux-text");
48 throw std::runtime_error("File isn't a supertux-text file");
50 if(!text_lisp->get("text", text))
51 throw std::runtime_error("file doesn't contain a text field");
52 if(!text_lisp->get("background", background_file))
53 throw std::runtime_error("file doesn't contain a background file");
54 text_lisp->get("speed", defaultspeed);
55 text_lisp->get("music", music);
56 } catch(std::exception& e) {
57 std::ostringstream msg;
58 msg << "Couldn't load file '" << filename << "': " << e.what() << std::endl;
59 throw std::runtime_error(msg.str());
62 // Split text string lines into a vector
63 lines = InfoBoxLine::split(text, SCREEN_WIDTH - 2*LEFT_BORDER);
65 // load background image
66 background.reset(new Surface("images/background/" + background_file));
72 TextScroller::~TextScroller()
74 for(std::vector<InfoBoxLine*>::iterator i = lines.begin(); i != lines.end(); i++) delete *i;
80 sound_manager->play_music(music);
84 TextScroller::update(float elapsed_time)
86 if(g_main_controller->hold(Controller::UP)) {
87 speed = -defaultspeed*5;
88 } else if(g_main_controller->hold(Controller::DOWN)) {
89 speed = defaultspeed*5;
93 if(g_main_controller->pressed(Controller::JUMP)
94 || g_main_controller->pressed(Controller::ACTION)
95 || g_main_controller->pressed(Controller::MENU_SELECT))
97 if(g_main_controller->pressed(Controller::PAUSE_MENU)) {
98 g_main_loop->exit_screen(new FadeOut(0.5));
101 scroll += speed * elapsed_time;
108 TextScroller::draw(DrawingContext& context)
110 context.draw_filled_rect(Vector(0, 0), Vector(SCREEN_WIDTH, SCREEN_HEIGHT),
111 Color(0.6f, 0.7f, 0.8f, 0.5f), 0);
112 context.draw_surface(background.get(), Vector(SCREEN_WIDTH/2 - background->get_width()/2 , SCREEN_HEIGHT/2 - background->get_height()/2), 0);
114 float y = SCREEN_HEIGHT - scroll;
115 for(size_t i = 0; i < lines.size(); i++) {
116 if (y + lines[i]->get_height() >= 0 && SCREEN_HEIGHT - y >= 0) {
117 lines[i]->draw(context, Rect(LEFT_BORDER, y, SCREEN_WIDTH - 2*LEFT_BORDER, y), LAYER_GUI);
120 y += lines[i]->get_height();
123 if(y < 0 && !fading ) {
125 g_main_loop->exit_screen(new FadeOut(0.5));
129 InfoBox::InfoBox(const std::string& text)
132 // Split text string lines into a vector
133 lines = InfoBoxLine::split(text, 400);
137 // get the arrow sprites
138 arrow_scrollup = new Surface("images/engine/menu/scroll-up.png");
139 arrow_scrolldown = new Surface("images/engine/menu/scroll-down.png");
141 catch (std::exception& e)
143 log_warning << "Could not load scrolling images: " << e.what() << std::endl;
145 arrow_scrolldown = 0;
151 for(std::vector<InfoBoxLine*>::iterator i = lines.begin();
152 i != lines.end(); i++)
154 delete arrow_scrollup;
155 delete arrow_scrolldown;
159 InfoBox::draw(DrawingContext& context)
161 float x1 = SCREEN_WIDTH/2-200;
162 float y1 = SCREEN_HEIGHT/2-200;
166 context.draw_filled_rect(Vector(x1, y1), Vector(width, height),
167 Color(0.6f, 0.7f, 0.8f, 0.5f), LAYER_GUI-1);
170 bool linesLeft = false;
171 for(size_t i = firstline; i < lines.size(); ++i) {
172 if(y >= y1 + height) {
177 lines[i]->draw(context, Rect(x1, y, x1+width, y), LAYER_GUI);
178 y += lines[i]->get_height();
182 // draw the scrolling arrows
183 if (arrow_scrollup && firstline > 0)
184 context.draw_surface(arrow_scrollup,
185 Vector( x1 + width - arrow_scrollup->get_width(), // top-right corner of box
188 if (arrow_scrolldown && linesLeft && firstline < lines.size()-1)
189 context.draw_surface(arrow_scrolldown,
190 Vector( x1 + width - arrow_scrolldown->get_width(), // bottom-light corner of box
191 y1 + height - arrow_scrolldown->get_height()),
204 InfoBox::scrolldown()
206 if(firstline < lines.size()-1)
221 Font* get_font_by_format_char(char format_char) {
238 log_warning << "Unknown format_char: '" << format_char << "'" << std::endl;
243 Color get_color_by_format_char(char format_char) {
247 return TextScroller::small_color;
250 return TextScroller::heading_color;
253 return TextScroller::reference_color;
257 return TextScroller::normal_color;
261 log_warning << "Unknown format_char: '" << format_char << "'" << std::endl;
266 InfoBoxLine::LineType get_linetype_by_format_char(char format_char) {
270 return InfoBoxLine::SMALL;
273 return InfoBoxLine::NORMAL;
276 return InfoBoxLine::HEADING;
279 return InfoBoxLine::REFERENCE;
282 return InfoBoxLine::NORMAL_LEFT;
285 return InfoBoxLine::IMAGE;
288 return InfoBoxLine::SMALL;
289 log_warning << "Unknown format_char: '" << format_char << "'" << std::endl;
295 InfoBoxLine::InfoBoxLine(char format_char, const std::string& text) :
301 font = get_font_by_format_char(format_char);
302 lineType = get_linetype_by_format_char(format_char);
303 color = get_color_by_format_char(format_char);
304 if (lineType == IMAGE) image = new Surface(text);
307 InfoBoxLine::~InfoBoxLine()
312 const std::vector<InfoBoxLine*>
313 InfoBoxLine::split(const std::string& text, float width)
315 std::vector<InfoBoxLine*> lines;
317 std::string::size_type i = 0;
318 std::string::size_type l;
319 char format_char = '#';
320 while(i < text.size()) {
321 // take care of empty lines - represent them as blank lines of normal text
322 if (text[i] == '\n') {
323 lines.push_back(new InfoBoxLine('\t', ""));
328 // extract the format_char
329 format_char = text[i];
331 if (i >= text.size()) break;
334 l = text.find("\n", i);
335 if (l == std::string::npos) l=text.size();
336 std::string s = text.substr(i, l-i);
339 // if we are dealing with an image, just store the line
340 if (format_char == '!') {
341 lines.push_back(new InfoBoxLine(format_char, s));
345 // append wrapped parts of line into list
346 std::string overflow;
348 Font* font = get_font_by_format_char(format_char);
350 if (font) s2 = font->wrap_to_width(s2, width, &overflow);
351 lines.push_back(new InfoBoxLine(format_char, s2));
353 } while (s.length() > 0);
360 InfoBoxLine::draw(DrawingContext& context, const Rect& bbox, int layer)
362 Vector position = bbox.p1;
365 context.draw_surface(image, Vector( (bbox.p1.x + bbox.p2.x - image->get_width()) / 2, position.y), layer);
368 context.draw_text(font, text, Vector(position.x, position.y), ALIGN_LEFT, layer, color);
371 context.draw_text(font, text, Vector((bbox.p1.x + bbox.p2.x) / 2, position.y), ALIGN_CENTER, layer, color);
377 InfoBoxLine::get_height()
381 return image->get_height() + ITEMS_SPACE;
383 return font->get_height() + ITEMS_SPACE;
385 return font->get_height() + ITEMS_SPACE;