New grow and skid sounds from remaxim
[supertux.git] / src / textscroller.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
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.
10 //
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.
15 //
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
19 //  02111-1307, USA.
20 #include <config.h>
21
22 #include "textscroller.hpp"
23
24 #include <stdexcept>
25 #include "log.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"
35 #include "main.hpp"
36 #include "fadeout.hpp"
37 #include "control/joystickkeyboardcontroller.hpp"
38
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;
43
44 TextScroller::TextScroller(const std::string& filename)
45 {
46   defaultspeed = DEFAULT_SPEED;
47   speed = defaultspeed;
48
49   std::string text;
50   std::string background_file;
51
52   lisp::Parser parser;
53   try {
54     const lisp::Lisp* root = parser.parse(filename);
55
56     const lisp::Lisp* text_lisp = root->get_lisp("supertux-text");
57     if(!text_lisp)
58       throw std::runtime_error("File isn't a supertux-text file");
59
60     if(!text_lisp->get("text", text))
61       throw std::runtime_error("file doesn't contain a text field");
62     if(!text_lisp->get("background", background_file))
63       throw std::runtime_error("file doesn't contain a background file");
64     text_lisp->get("speed", defaultspeed);
65     text_lisp->get("music", music);
66   } catch(std::exception& e) {
67     std::ostringstream msg;
68     msg << "Couldn't load file '" << filename << "': " << e.what() << std::endl;
69     throw std::runtime_error(msg.str());
70   }
71
72   // Split text string lines into a vector
73   lines = InfoBoxLine::split(text, SCREEN_WIDTH - 2*LEFT_BORDER);
74
75   // load background image
76   background.reset(new Surface("images/background/" + background_file));
77
78   scroll = 0;
79   fading = false;
80 }
81
82 TextScroller::~TextScroller()
83 {
84   for(std::vector<InfoBoxLine*>::iterator i = lines.begin(); i != lines.end(); i++) delete *i;
85 }
86
87 void
88 TextScroller::setup()
89 {
90   sound_manager->play_music(music);
91 }
92
93 void
94 TextScroller::update(float elapsed_time)
95 {
96   if(main_controller->hold(Controller::UP)) {
97     speed = -defaultspeed*5;
98   } else if(main_controller->hold(Controller::DOWN)) {
99     speed = defaultspeed*5;
100   } else {
101     speed = defaultspeed;
102   }
103   if(main_controller->pressed(Controller::JUMP)
104       || main_controller->pressed(Controller::ACTION)
105       || main_controller->pressed(Controller::MENU_SELECT))
106     scroll += SCROLL;
107   if(main_controller->pressed(Controller::PAUSE_MENU)) {
108     main_loop->exit_screen(new FadeOut(0.5));
109   }
110
111   scroll += speed * elapsed_time;
112
113   if(scroll < 0)
114     scroll = 0;
115 }
116
117 void
118 TextScroller::draw(DrawingContext& context)
119 {
120   context.draw_filled_rect(Vector(0, 0), Vector(SCREEN_WIDTH, SCREEN_HEIGHT),
121       Color(0.6f, 0.7f, 0.8f, 0.5f), 0);
122   context.draw_surface(background.get(), Vector(SCREEN_WIDTH/2 - background->get_width()/2 , SCREEN_HEIGHT/2 - background->get_height()/2), 0);
123
124   float y = SCREEN_HEIGHT - scroll;
125   for(size_t i = 0; i < lines.size(); i++) {
126     if (y + lines[i]->get_height() >= 0 && SCREEN_HEIGHT - y >= 0) {
127         lines[i]->draw(context, Rect(LEFT_BORDER, y, SCREEN_WIDTH - 2*LEFT_BORDER, y), LAYER_GUI);
128     }
129
130     y += lines[i]->get_height();
131   }
132
133   if(y < 0 && !fading ) {
134     fading = true;
135     main_loop->exit_screen(new FadeOut(0.5));
136   }
137 }
138
139 InfoBox::InfoBox(const std::string& text)
140   : firstline(0)
141 {
142   // Split text string lines into a vector
143   lines = InfoBoxLine::split(text, 400);
144
145   try
146   {
147     // get the arrow sprites
148     arrow_scrollup   = new Surface("images/engine/menu/scroll-up.png");
149     arrow_scrolldown = new Surface("images/engine/menu/scroll-down.png");
150   }
151   catch (std::exception& e)
152   {
153     log_warning << "Could not load scrolling images: " << e.what() << std::endl;
154     arrow_scrollup = 0;
155     arrow_scrolldown = 0;
156   }
157 }
158
159 InfoBox::~InfoBox()
160 {
161   for(std::vector<InfoBoxLine*>::iterator i = lines.begin();
162       i != lines.end(); i++)
163     delete *i;
164   delete arrow_scrollup;
165   delete arrow_scrolldown;
166 }
167
168 void
169 InfoBox::draw(DrawingContext& context)
170 {
171   float x1 = SCREEN_WIDTH/2-200;
172   float y1 = SCREEN_HEIGHT/2-200;
173   float width = 400;
174   float height = 200;
175
176   context.draw_filled_rect(Vector(x1, y1), Vector(width, height),
177       Color(0.6f, 0.7f, 0.8f, 0.5f), LAYER_GUI-1);
178
179   float y = y1;
180   bool linesLeft = false;
181   for(size_t i = firstline; i < lines.size(); ++i) {
182     if(y >= y1 + height) {
183       linesLeft = true;
184       break;
185     }
186
187     lines[i]->draw(context, Rect(x1, y, x1+width, y), LAYER_GUI);
188     y += lines[i]->get_height();
189   }
190
191   {
192     // draw the scrolling arrows
193     if (arrow_scrollup && firstline > 0)
194       context.draw_surface(arrow_scrollup,
195       Vector( x1 + width  - arrow_scrollup->get_width(),  // top-right corner of box
196               y1), LAYER_GUI);
197
198     if (arrow_scrolldown && linesLeft && firstline < lines.size()-1)
199       context.draw_surface(arrow_scrolldown,
200       Vector( x1 + width  - arrow_scrolldown->get_width(),  // bottom-light corner of box
201               y1 + height - arrow_scrolldown->get_height()),
202               LAYER_GUI);
203   }
204 }
205
206 void
207 InfoBox::scrollup()
208 {
209   if(firstline > 0)
210     firstline--;
211 }
212
213 void
214 InfoBox::scrolldown()
215 {
216   if(firstline < lines.size()-1)
217     firstline++;
218 }
219
220 void
221 InfoBox::pageup()
222 {
223 }
224
225 void
226 InfoBox::pagedown()
227 {
228 }
229
230 namespace {
231 Font* get_font_by_format_char(char format_char) {
232   switch(format_char)
233   {
234     case ' ':
235       return small_font;
236       break;
237     case '-':
238       return big_font;
239       break;
240     case '\t':
241     case '*':
242     case '#':
243     case '!':
244       return normal_font;
245       break;
246     default:
247       return normal_font;
248       log_warning << "Unknown format_char: '" << format_char << "'" << std::endl;
249       break;
250   }
251 }
252
253 Color get_color_by_format_char(char format_char) {
254   switch(format_char)
255   {
256     case ' ':
257       return TextScroller::small_color;
258       break;
259     case '-':
260       return TextScroller::heading_color;
261       break;
262     case '*':
263       return TextScroller::reference_color;
264     case '\t':
265     case '#':
266     case '!':
267       return TextScroller::normal_color;
268       break;
269     default:
270       return Color(0,0,0);
271       log_warning << "Unknown format_char: '" << format_char << "'" << std::endl;
272       break;
273   }
274 }
275
276 InfoBoxLine::LineType get_linetype_by_format_char(char format_char) {
277   switch(format_char)
278   {
279     case ' ':
280       return InfoBoxLine::SMALL;
281       break;
282     case '\t':
283       return InfoBoxLine::NORMAL;
284       break;
285     case '-':
286       return InfoBoxLine::HEADING;
287       break;
288     case '*':
289       return InfoBoxLine::REFERENCE;
290       break;
291     case '#':
292       return InfoBoxLine::NORMAL_LEFT;
293       break;
294     case '!':
295       return InfoBoxLine::IMAGE;
296       break;
297     default:
298       return InfoBoxLine::SMALL;
299       log_warning << "Unknown format_char: '" << format_char << "'" << std::endl;
300       break;
301   }
302 }
303 }
304
305 InfoBoxLine::InfoBoxLine(char format_char, const std::string& text) : lineType(NORMAL), font(normal_font), text(text), image(0)
306 {
307   font = get_font_by_format_char(format_char);
308   lineType = get_linetype_by_format_char(format_char);
309   color = get_color_by_format_char(format_char);
310   if (lineType == IMAGE) image = new Surface(text);
311 }
312
313 InfoBoxLine::~InfoBoxLine()
314 {
315   delete image;
316 }
317
318 const std::vector<InfoBoxLine*>
319 InfoBoxLine::split(const std::string& text, float width)
320 {
321   std::vector<InfoBoxLine*> lines;
322
323   std::string::size_type i = 0;
324   std::string::size_type l;
325   char format_char = '#';
326   while(i < text.size()) {
327     // take care of empty lines - represent them as blank lines of normal text
328     if (text[i] == '\n') {
329       lines.push_back(new InfoBoxLine('\t', ""));
330       i++;
331       continue;
332     }
333
334     // extract the format_char
335     format_char = text[i];
336     i++;
337     if (i >= text.size()) break;
338
339     // extract one line
340     l = text.find("\n", i);
341     if (l == std::string::npos) l=text.size();
342     std::string s = text.substr(i, l-i);
343     i = l+1;
344
345     // if we are dealing with an image, just store the line
346     if (format_char == '!') {
347       lines.push_back(new InfoBoxLine(format_char, s));
348       continue;
349     }
350
351     // append wrapped parts of line into list
352     std::string overflow;
353     do {
354       Font* font = get_font_by_format_char(format_char);
355       std::string s2 = s;
356       if (font) s2 = font->wrap_to_width(s2, width, &overflow);
357       lines.push_back(new InfoBoxLine(format_char, s2));
358       s = overflow;
359     } while (s.length() > 0);
360   }
361
362   return lines;
363 }
364
365 void
366 InfoBoxLine::draw(DrawingContext& context, const Rect& bbox, int layer)
367 {
368   Vector position = bbox.p1;
369   switch (lineType) {
370     case IMAGE:
371       context.draw_surface(image, Vector( (bbox.p1.x + bbox.p2.x - image->get_width()) / 2, position.y), layer);
372       break;
373     case NORMAL_LEFT:
374       context.draw_text(font, text, Vector(position.x, position.y), ALIGN_LEFT, layer, color);
375       break;
376     default:
377       context.draw_text(font, text, Vector((bbox.p1.x + bbox.p2.x) / 2, position.y), ALIGN_CENTER, layer, color);
378       break;
379   }
380 }
381
382 float
383 InfoBoxLine::get_height()
384 {
385   switch (lineType) {
386     case IMAGE:
387       return image->get_height() + ITEMS_SPACE;
388     case NORMAL_LEFT:
389       return font->get_height() + ITEMS_SPACE;
390     default:
391       return font->get_height() + ITEMS_SPACE;
392   }
393 }