- Fixed formatting in credits
[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   Menu::set_current(NULL);
92 }
93
94 void
95 TextScroller::update(float elapsed_time)
96 {
97   if(main_controller->hold(Controller::UP)) {
98     speed = -defaultspeed*5;
99   } else if(main_controller->hold(Controller::DOWN)) {
100     speed = defaultspeed*5;
101   } else {
102     speed = defaultspeed;
103   }
104   if(main_controller->pressed(Controller::JUMP)
105       || main_controller->pressed(Controller::ACTION)
106       || main_controller->pressed(Controller::MENU_SELECT))
107     scroll += SCROLL;
108   if(main_controller->pressed(Controller::PAUSE_MENU)) {
109     main_loop->exit_screen(new FadeOut(0.5));
110   }
111
112   scroll += speed * elapsed_time;
113
114   if(scroll < 0)
115     scroll = 0;
116 }
117
118 void
119 TextScroller::draw(DrawingContext& context)
120 {
121   context.draw_filled_rect(Vector(0, 0), Vector(SCREEN_WIDTH, SCREEN_HEIGHT),
122       Color(0.6f, 0.7f, 0.8f, 0.5f), 0);
123   context.draw_surface(background.get(), Vector(SCREEN_WIDTH/2 - background->get_width()/2 , SCREEN_HEIGHT/2 - background->get_height()/2), 0);
124
125   float y = SCREEN_HEIGHT - scroll;
126   for(size_t i = 0; i < lines.size(); i++) {
127     if (y + lines[i]->get_height() >= 0 && SCREEN_HEIGHT - y >= 0) {
128         lines[i]->draw(context, Rect(LEFT_BORDER, y, SCREEN_WIDTH - 2*LEFT_BORDER, y), LAYER_GUI);
129     }
130
131     y += lines[i]->get_height();
132   }
133
134   if(y < 0 && !fading ) {
135     fading = true;
136     main_loop->exit_screen(new FadeOut(0.5));
137   }
138 }
139
140 InfoBox::InfoBox(const std::string& text)
141   : firstline(0)
142 {
143   // Split text string lines into a vector
144   lines = InfoBoxLine::split(text, 400);
145
146   try
147   {
148     // get the arrow sprites
149     arrow_scrollup   = new Surface("images/engine/menu/scroll-up.png");
150     arrow_scrolldown = new Surface("images/engine/menu/scroll-down.png");
151   }
152   catch (std::exception& e)
153   {
154     log_warning << "Could not load scrolling images: " << e.what() << std::endl;
155     arrow_scrollup = 0;
156     arrow_scrolldown = 0;
157   }
158 }
159
160 InfoBox::~InfoBox()
161 {
162   for(std::vector<InfoBoxLine*>::iterator i = lines.begin();
163       i != lines.end(); i++)
164     delete *i;
165   delete arrow_scrollup;
166   delete arrow_scrolldown;
167 }
168
169 void
170 InfoBox::draw(DrawingContext& context)
171 {
172   float x1 = SCREEN_WIDTH/2-200;
173   float y1 = SCREEN_HEIGHT/2-200;
174   float width = 400;
175   float height = 200;
176
177   context.draw_filled_rect(Vector(x1, y1), Vector(width, height),
178       Color(0.6f, 0.7f, 0.8f, 0.5f), LAYER_GUI-1);
179
180   float y = y1;
181   bool linesLeft = false;
182   for(size_t i = firstline; i < lines.size(); ++i) {
183     if(y >= y1 + height) {
184       linesLeft = true;
185       break;
186     }
187
188     lines[i]->draw(context, Rect(x1, y, x1+width, y), LAYER_GUI);
189     y += lines[i]->get_height();
190   }
191
192   {
193     // draw the scrolling arrows
194     if (arrow_scrollup && firstline > 0)
195       context.draw_surface(arrow_scrollup,
196       Vector( x1 + width  - arrow_scrollup->get_width(),  // top-right corner of box
197               y1), LAYER_GUI);
198
199     if (arrow_scrolldown && linesLeft && firstline < lines.size()-1)
200       context.draw_surface(arrow_scrolldown,
201       Vector( x1 + width  - arrow_scrolldown->get_width(),  // bottom-light corner of box
202               y1 + height - arrow_scrolldown->get_height()),
203               LAYER_GUI);
204   }
205 }
206
207 void
208 InfoBox::scrollup()
209 {
210   if(firstline > 0)
211     firstline--;
212 }
213
214 void
215 InfoBox::scrolldown()
216 {
217   if(firstline < lines.size()-1)
218     firstline++;
219 }
220
221 void
222 InfoBox::pageup()
223 {
224 }
225
226 void
227 InfoBox::pagedown()
228 {
229 }
230
231 namespace {
232 Font* get_font_by_format_char(char format_char) {
233   switch(format_char)
234   {
235     case ' ':
236       return small_font;
237       break;
238     case '-':
239       return big_font;
240       break;
241     case '\t':
242     case '*':
243     case '#':
244     case '!':
245       return normal_font;
246       break;
247     default:
248       return normal_font;
249       log_warning << "Unknown format_char: '" << format_char << "'" << std::endl;
250       break;
251   }
252 }
253
254 Color get_color_by_format_char(char format_char) {
255   switch(format_char)
256   {
257     case ' ':
258       return TextScroller::small_color;
259       break;
260     case '-':
261       return TextScroller::heading_color;
262       break;
263     case '*':
264       return TextScroller::reference_color;
265     case '\t':
266     case '#':
267     case '!':
268       return TextScroller::normal_color;
269       break;
270     default:
271       return Color(0,0,0);
272       log_warning << "Unknown format_char: '" << format_char << "'" << std::endl;
273       break;
274   }
275 }
276
277 InfoBoxLine::LineType get_linetype_by_format_char(char format_char) {
278   switch(format_char)
279   {
280     case ' ':
281       return InfoBoxLine::SMALL;
282       break;
283     case '\t':
284       return InfoBoxLine::NORMAL;
285       break;
286     case '-':
287       return InfoBoxLine::HEADING;
288       break;
289     case '*':
290       return InfoBoxLine::REFERENCE;
291       break;
292     case '#':
293       return InfoBoxLine::NORMAL_LEFT;
294       break;
295     case '!':
296       return InfoBoxLine::IMAGE;
297       break;
298     default:
299       return InfoBoxLine::SMALL;
300       log_warning << "Unknown format_char: '" << format_char << "'" << std::endl;
301       break;
302   }
303 }
304 }
305
306 InfoBoxLine::InfoBoxLine(char format_char, const std::string& text) : lineType(NORMAL), font(normal_font), text(text), image(0)
307 {
308   font = get_font_by_format_char(format_char);
309   lineType = get_linetype_by_format_char(format_char);
310   color = get_color_by_format_char(format_char);
311   if (lineType == IMAGE) image = new Surface(text);
312 }
313
314 InfoBoxLine::~InfoBoxLine()
315 {
316   delete image;
317 }
318
319 const std::vector<InfoBoxLine*>
320 InfoBoxLine::split(const std::string& text, float width)
321 {
322   std::vector<InfoBoxLine*> lines;
323
324   std::string::size_type i = 0;
325   std::string::size_type l;
326   char format_char = '#';
327   while(i < text.size()) {
328     // take care of empty lines - represent them as blank lines of normal text
329     if (text[i] == '\n') {
330       lines.push_back(new InfoBoxLine('\t', ""));
331       i++;
332       continue;
333     }
334
335     // extract the format_char
336     format_char = text[i];
337     i++;
338     if (i >= text.size()) break;
339
340     // extract one line
341     l = text.find("\n", i);
342     if (l == std::string::npos) l=text.size();
343     std::string s = text.substr(i, l-i);
344     i = l+1;
345
346     // if we are dealing with an image, just store the line
347     if (format_char == '!') {
348       lines.push_back(new InfoBoxLine(format_char, s));
349       continue;
350     }
351
352     // append wrapped parts of line into list
353     std::string overflow;
354     do {
355       Font* font = get_font_by_format_char(format_char);
356       std::string s2 = s;
357       if (font) s2 = font->wrap_to_width(s2, width, &overflow);
358       lines.push_back(new InfoBoxLine(format_char, s2));
359       s = overflow;
360     } while (s.length() > 0);
361   }
362
363   return lines;
364 }
365
366 void
367 InfoBoxLine::draw(DrawingContext& context, const Rect& bbox, int layer)
368 {
369   Vector position = bbox.p1;
370   switch (lineType) {
371     case IMAGE:
372       context.draw_surface(image, Vector( (bbox.p1.x + bbox.p2.x - image->get_width()) / 2, position.y), layer);
373       break;
374     case NORMAL_LEFT:
375       context.draw_text(font, text, Vector(position.x, position.y), ALIGN_LEFT, layer, color);
376       break;
377     default:
378       context.draw_text(font, text, Vector((bbox.p1.x + bbox.p2.x) / 2, position.y), ALIGN_CENTER, layer, color);
379       break;
380   }
381 }
382
383 float
384 InfoBoxLine::get_height()
385 {
386   switch (lineType) {
387     case IMAGE:
388       return image->get_height() + ITEMS_SPACE;
389     case NORMAL_LEFT:
390       return font->get_height() + ITEMS_SPACE;
391     default:
392       return font->get_height() + ITEMS_SPACE;
393   }
394 }