2 more evil mainloops are gone
[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 static void split_text(const std::string& text, std::vector<std::string>& lines)
45 {
46   // Split text string lines into a vector
47   lines.clear();
48   std::string::size_type i, l;
49   i = 0;
50   while(true) {
51     l = text.find("\n", i);
52
53     if(l == std::string::npos) {
54       lines.push_back(text.substr(i, text.size()-i));
55       break;
56     }
57
58     lines.push_back(text.substr(i, l-i));
59     i = l+1;
60   }
61 }
62
63 TextScroller::TextScroller(const std::string& filename)
64 {
65   defaultspeed = DEFAULT_SPEED;
66   speed = defaultspeed;
67   
68   std::string text;
69   std::string background_file;
70
71   lisp::Parser parser;
72   try {
73     std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
74
75     const lisp::Lisp* text_lisp = root->get_lisp("supertux-text");
76     if(!text_lisp)
77       throw std::runtime_error("File isn't a supertux-text file");
78     
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());
89   }
90
91   // Split text string lines into a vector
92   split_text(text, lines);
93
94   for(size_t i = 0; i < lines.size(); ++i) {
95     const std::string& line = lines[i];
96     if(line.size() == 0)
97       continue;
98     if(line[0] == '!') {
99       std::string imagename = line.substr(1, line.size()-1);
100       images.insert(std::make_pair(imagename, new Surface(imagename)));
101     }
102   }
103
104   // load background image
105   background.reset(new Surface("images/background/" + background_file));
106
107   scroll = 0;
108 }
109
110 TextScroller::~TextScroller()
111 {
112   for(std::map<std::string, Surface*>::iterator i = images.begin();
113       i != images.end(); ++i)
114     delete i->second; 
115 }
116
117 void
118 TextScroller::setup()
119 {
120   sound_manager->play_music(music);
121   Menu::set_current(NULL);
122 }
123
124 void
125 TextScroller::update(float elapsed_time)
126 {
127   if(main_controller->hold(Controller::UP)) {
128     speed = -defaultspeed*5;
129   } else if(main_controller->hold(Controller::DOWN)) {
130     speed = defaultspeed*5;
131   } else {
132     speed = defaultspeed;
133   }
134   if(main_controller->pressed(Controller::JUMP)
135       || main_controller->pressed(Controller::ACTION)
136       || main_controller->pressed(Controller::MENU_SELECT))
137     scroll += SCROLL;    
138   if(main_controller->pressed(Controller::PAUSE_MENU)) {
139     main_loop->exit_screen(new FadeOut(0.5));
140   }
141
142   scroll += speed * elapsed_time;
143     
144   if(scroll < 0)
145     scroll = 0;
146 }
147
148 void
149 TextScroller::draw(DrawingContext& context)
150 {
151   context.draw_surface(background.get(), Vector(0,0), 0);
152
153   float y = SCREEN_HEIGHT - scroll;
154   for(size_t i = 0; i < lines.size(); i++) {
155     const std::string& line = lines[i];
156     if(line.size() == 0) {
157       y += white_text->get_height() + ITEMS_SPACE;
158       continue;
159     }
160       
161     const Font* font = 0;
162     const Surface* image = 0;
163     bool center = true;
164     switch(line[0])
165     {
166       case ' ': font = white_small_text; break;
167       case '\t': font = white_text; break;
168       case '-': font = white_big_text; break;
169       case '*': font = blue_text; break;
170       case '#': font = white_text; center = false; break;
171       case '!': {
172                   std::string imagename = line.substr(1, line.size()-1);
173                   image = images[imagename];
174                   break;
175                 }
176       default:
177                 log_warning << "text contains an unformated line" << std::endl;
178                 font = white_text;
179                 center = false;
180                 break;
181     }
182     
183     if(font != 0) {
184       if(center) {
185         context.draw_text(font,
186             line.substr(1, line.size()-1),
187             Vector(SCREEN_WIDTH/2, y),
188             CENTER_ALLIGN, LAYER_FOREGROUND1);
189       } else {
190         context.draw_text(font,
191             line.substr(1, line.size()-1),
192             Vector(LEFT_BORDER, y),
193             LEFT_ALLIGN, LAYER_FOREGROUND1);
194       }
195       y += font->get_height() + ITEMS_SPACE;
196     }
197     if(image != 0) {
198       context.draw_surface(image,
199           Vector( (SCREEN_WIDTH - image->get_width()) / 2, y), 255);
200       y += image->get_height() + ITEMS_SPACE;
201     }
202   }
203
204   if(y < 0) {
205     main_loop->exit_screen(new FadeOut(0.5));
206   }
207 }
208
209 InfoBox::InfoBox(const std::string& text)
210   : firstline(0)
211 {
212   split_text(text, lines);
213
214   for(size_t i = 0; i < lines.size(); ++i) {
215     if(lines[i].size() == 0)
216       continue;
217     if(lines[i][0] == '!') {
218       std::string imagename = lines[i].substr(1, lines[i].size()-1);
219       images.insert(std::make_pair(imagename, new Surface(imagename)));
220     }
221   }
222
223   try
224   {
225     // get the arrow sprites
226     arrow_scrollup   = new Surface("images/engine/menu/scroll-up.png");
227     arrow_scrolldown = new Surface("images/engine/menu/scroll-down.png");
228   }
229   catch (std::exception& e)
230   {
231     log_warning << "Could not load scrolling images: " << e.what() << std::endl;
232     arrow_scrollup = 0;
233     arrow_scrolldown = 0;
234   }
235 }
236
237 InfoBox::~InfoBox()
238 {
239   for(std::map<std::string, Surface*>::iterator i = images.begin();
240     i != images.end(); ++i)
241     delete i->second;
242   delete arrow_scrollup;
243   delete arrow_scrolldown;
244 }
245
246 void
247 InfoBox::draw(DrawingContext& context)
248 {
249   const Font* heading_font = white_big_text;
250   const Font* normal_font = white_text;
251   const Font* small_font = white_small_text;
252   const Font* reference_font = blue_text;
253
254   float x1 = 200;
255   float y1 = 100;
256   float width = 400;
257   float height = 200;
258
259   context.draw_filled_rect(Vector(x1, y1), Vector(width, height),
260       Color(0.6f, 0.7f, 0.8f, 0.5f), LAYER_GUI-1);
261
262   float y = y1;
263   for(size_t i = firstline; i < lines.size(); ++i) {
264     const std::string& line = lines[i];
265     if(y >= y1 + height)
266       break;
267
268     if(line.size() == 0) {
269       y += normal_font->get_height() + ITEMS_SPACE;    
270       continue;                                        
271     }
272
273     const Font* font = 0;
274     const Surface* image = 0;
275     bool center = true;
276     switch(line[0])
277     {
278       case ' ': font = small_font; break;
279       case '\t': font = normal_font; break;
280       case '-': font = heading_font; break;
281       case '*': font = reference_font; break;
282       case '#': font = normal_font; center = false; break;
283       case '!': {
284         std::string imagename = line.substr(1, line.size()-1);
285         image = images[imagename];
286         break;
287       }
288       default:
289         log_warning << "text contains an unformatted line" << std::endl;
290         font = normal_font;
291         center = false;
292         break;
293     }
294
295     if(image != 0) {
296       context.draw_surface(image,
297       Vector( (SCREEN_WIDTH - image->get_width()) / 2,
298               y), LAYER_GUI);
299       y += image->get_height() + ITEMS_SPACE;
300     } else if(center) {
301       context.draw_text(font,
302           line.substr(1, line.size()-1),
303           Vector(SCREEN_WIDTH/2, y),
304           CENTER_ALLIGN, LAYER_GUI);
305       y += font->get_height() + ITEMS_SPACE;
306     } else {
307       context.draw_text(font,
308           line.substr(1, line.size()-1),
309           Vector(x1, y),
310           LEFT_ALLIGN, LAYER_GUI);
311       y += font->get_height() + ITEMS_SPACE;
312     }
313
314     // draw the scrolling arrows
315     if (arrow_scrollup && firstline > 0)
316       context.draw_surface(arrow_scrollup,
317       Vector( x1 + width  - arrow_scrollup->get_width(),  // top-right corner of box
318               y1), LAYER_GUI);
319
320     if (arrow_scrolldown && firstline < lines.size()-1)
321       context.draw_surface(arrow_scrolldown,
322       Vector( x1 + width  - arrow_scrolldown->get_width(),  // bottom-light corner of box
323               y1 + height - arrow_scrolldown->get_height()),
324               LAYER_GUI);
325   }
326 }
327
328 void
329 InfoBox::scrollup()
330 {
331   if(firstline > 0)
332     firstline--;
333 }
334
335 void
336 InfoBox::scrolldown()
337 {
338   if(firstline < lines.size()-1)
339     firstline++;
340 }
341
342 void
343 InfoBox::pageup()
344 {
345 }
346
347 void
348 InfoBox::pagedown()
349 {
350 }
351