2c7139df58168f7b067ff995bf78362cf1c58f13
[supertux.git] / src / textscroller.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2005 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 "video/screen.hpp"
32 #include "gui/menu.hpp"
33 #include "lisp/parser.hpp"
34 #include "lisp/lisp.hpp"
35 #include "audio/sound_manager.hpp"
36 #include "main.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     fadeout(500);
140     main_loop->exit_screen();
141   }
142
143   scroll += speed * elapsed_time;
144     
145   if(scroll < 0)
146     scroll = 0;
147 }
148
149 void
150 TextScroller::draw(DrawingContext& context)
151 {
152   context.draw_surface(background.get(), Vector(0,0), 0);
153
154   float y = SCREEN_HEIGHT - scroll;
155   for(size_t i = 0; i < lines.size(); i++) {
156     const std::string& line = lines[i];
157     if(line.size() == 0) {
158       y += white_text->get_height() + ITEMS_SPACE;
159       continue;
160     }
161       
162     const Font* font = 0;
163     const Surface* image = 0;
164     bool center = true;
165     switch(line[0])
166     {
167       case ' ': font = white_small_text; break;
168       case '\t': font = white_text; break;
169       case '-': font = white_big_text; break;
170       case '*': font = blue_text; break;
171       case '#': font = white_text; center = false; break;
172       case '!': {
173                   std::string imagename = line.substr(1, line.size()-1);
174                   image = images[imagename];
175                   break;
176                 }
177       default:
178                 log_warning << "text contains an unformated line" << std::endl;
179                 font = white_text;
180                 center = false;
181                 break;
182     }
183     
184     if(font != 0) {
185       if(center) {
186         context.draw_text(font,
187             line.substr(1, line.size()-1),
188             Vector(SCREEN_WIDTH/2, y),
189             CENTER_ALLIGN, LAYER_FOREGROUND1);
190       } else {
191         context.draw_text(font,
192             line.substr(1, line.size()-1),
193             Vector(LEFT_BORDER, y),
194             LEFT_ALLIGN, LAYER_FOREGROUND1);
195       }
196       y += font->get_height() + ITEMS_SPACE;
197     }
198     if(image != 0) {
199       context.draw_surface(image,
200           Vector( (SCREEN_WIDTH - image->get_width()) / 2, y), 255);
201       y += image->get_height() + ITEMS_SPACE;
202     }
203   }
204
205   if(y < 0) {
206     fadeout(500); 
207     main_loop->exit_screen();
208   }
209 }
210
211 InfoBox::InfoBox(const std::string& text)
212   : firstline(0)
213 {
214   split_text(text, lines);
215
216   for(size_t i = 0; i < lines.size(); ++i) {
217     if(lines[i].size() == 0)
218       continue;
219     if(lines[i][0] == '!') {
220       std::string imagename = lines[i].substr(1, lines[i].size()-1);
221       images.insert(std::make_pair(imagename, new Surface(imagename)));
222     }
223   }
224
225   try
226   {
227     // get the arrow sprites
228     arrow_scrollup   = new Surface("images/engine/menu/scroll-up.png");
229     arrow_scrolldown = new Surface("images/engine/menu/scroll-down.png");
230   }
231   catch (std::exception& e)
232   {
233     log_warning << "Could not load scrolling images: " << e.what() << std::endl;
234     arrow_scrollup = 0;
235     arrow_scrolldown = 0;
236   }
237 }
238
239 InfoBox::~InfoBox()
240 {
241   for(std::map<std::string, Surface*>::iterator i = images.begin();
242     i != images.end(); ++i)
243     delete i->second;
244   delete arrow_scrollup;
245   delete arrow_scrolldown;
246 }
247
248 void
249 InfoBox::draw(DrawingContext& context)
250 {
251   const Font* heading_font = white_big_text;
252   const Font* normal_font = white_text;
253   const Font* small_font = white_small_text;
254   const Font* reference_font = blue_text;
255
256   float x1 = 200;
257   float y1 = 100;
258   float width = 400;
259   float height = 200;
260
261   context.draw_filled_rect(Vector(x1, y1), Vector(width, height),
262       Color(0.6f, 0.7f, 0.8f, 0.5f), LAYER_GUI-1);
263
264   float y = y1;
265   for(size_t i = firstline; i < lines.size(); ++i) {
266     const std::string& line = lines[i];
267     if(y >= y1 + height)
268       break;
269
270     if(line.size() == 0) {
271       y += normal_font->get_height() + ITEMS_SPACE;    
272       continue;                                        
273     }
274
275     const Font* font = 0;
276     const Surface* image = 0;
277     bool center = true;
278     switch(line[0])
279     {
280       case ' ': font = small_font; break;
281       case '\t': font = normal_font; break;
282       case '-': font = heading_font; break;
283       case '*': font = reference_font; break;
284       case '#': font = normal_font; center = false; break;
285       case '!': {
286         std::string imagename = line.substr(1, line.size()-1);
287         image = images[imagename];
288         break;
289       }
290       default:
291         log_warning << "text contains an unformatted line" << std::endl;
292         font = normal_font;
293         center = false;
294         break;
295     }
296
297     if(image != 0) {
298       context.draw_surface(image,
299       Vector( (SCREEN_WIDTH - image->get_width()) / 2,
300               y), LAYER_GUI);
301       y += image->get_height() + ITEMS_SPACE;
302     } else if(center) {
303       context.draw_text(font,
304           line.substr(1, line.size()-1),
305           Vector(SCREEN_WIDTH/2, y),
306           CENTER_ALLIGN, LAYER_GUI);
307       y += font->get_height() + ITEMS_SPACE;
308     } else {
309       context.draw_text(font,
310           line.substr(1, line.size()-1),
311           Vector(x1, y),
312           LEFT_ALLIGN, LAYER_GUI);
313       y += font->get_height() + ITEMS_SPACE;
314     }
315
316     // draw the scrolling arrows
317     if (arrow_scrollup && firstline > 0)
318       context.draw_surface(arrow_scrollup,
319       Vector( x1 + width  - arrow_scrollup->get_width(),  // top-right corner of box
320               y1), LAYER_GUI);
321
322     if (arrow_scrolldown && firstline < lines.size()-1)
323       context.draw_surface(arrow_scrolldown,
324       Vector( x1 + width  - arrow_scrolldown->get_width(),  // bottom-light corner of box
325               y1 + height - arrow_scrolldown->get_height()),
326               LAYER_GUI);
327   }
328 }
329
330 void
331 InfoBox::scrollup()
332 {
333   if(firstline > 0)
334     firstline--;
335 }
336
337 void
338 InfoBox::scrolldown()
339 {
340   if(firstline < lines.size()-1)
341     firstline++;
342 }
343
344 void
345 InfoBox::pageup()
346 {
347 }
348
349 void
350 InfoBox::pagedown()
351 {
352 }
353