Widescreen-Patch by Klaus Denker. It includes a new
[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     std::auto_ptr<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, 40);
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     lines[i]->draw(context, Vector(LEFT_BORDER, y), LAYER_GUI);
128     y += lines[i]->get_height();
129   }
130
131   if(y < 0 && !fading ) {
132     fading = true;
133     main_loop->exit_screen(new FadeOut(0.5));
134   }
135 }
136
137 InfoBox::InfoBox(const std::string& text)
138   : firstline(0)
139 {
140   // Split text string lines into a vector
141   lines = InfoBoxLine::split(text, 23);
142
143   try
144   {
145     // get the arrow sprites
146     arrow_scrollup   = new Surface("images/engine/menu/scroll-up.png");
147     arrow_scrolldown = new Surface("images/engine/menu/scroll-down.png");
148   }
149   catch (std::exception& e)
150   {
151     log_warning << "Could not load scrolling images: " << e.what() << std::endl;
152     arrow_scrollup = 0;
153     arrow_scrolldown = 0;
154   }
155 }
156
157 InfoBox::~InfoBox()
158 {
159   for(std::vector<InfoBoxLine*>::iterator i = lines.begin();
160       i != lines.end(); i++)
161     delete *i;
162   delete arrow_scrollup;
163   delete arrow_scrolldown;
164 }
165
166 void
167 InfoBox::draw(DrawingContext& context)
168 {
169   float x1 = SCREEN_WIDTH/2-200;
170   float y1 = SCREEN_HEIGHT/2-200;
171   float width = 400;
172   float height = 200;
173
174   context.draw_filled_rect(Vector(x1, y1), Vector(width, height),
175       Color(0.6f, 0.7f, 0.8f, 0.5f), LAYER_GUI-1);
176
177   float y = y1;
178   for(size_t i = firstline; i < lines.size(); ++i) {
179     if(y >= y1 + height) break;
180
181     lines[i]->draw(context, Vector(x1, y), LAYER_GUI);
182     y += lines[i]->get_height();
183
184     // draw the scrolling arrows
185     if (arrow_scrollup && firstline > 0)
186       context.draw_surface(arrow_scrollup,
187       Vector( x1 + width  - arrow_scrollup->get_width(),  // top-right corner of box
188               y1), LAYER_GUI);
189
190     if (arrow_scrolldown && firstline < lines.size()-1)
191       context.draw_surface(arrow_scrolldown,
192       Vector( x1 + width  - arrow_scrolldown->get_width(),  // bottom-light corner of box
193               y1 + height - arrow_scrolldown->get_height()),
194               LAYER_GUI);
195   }
196 }
197
198 void
199 InfoBox::scrollup()
200 {
201   if(firstline > 0)
202     firstline--;
203 }
204
205 void
206 InfoBox::scrolldown()
207 {
208   if(firstline < lines.size()-1)
209     firstline++;
210 }
211
212 void
213 InfoBox::pageup()
214 {
215 }
216
217 void
218 InfoBox::pagedown()
219 {
220 }
221
222 InfoBoxLine::InfoBoxLine(char format_char, const std::string& text) : lineType(NORMAL), font(white_text), text(text), image(0)
223 {
224   switch(format_char)
225   {
226     case ' ':
227       lineType = SMALL;
228       font = white_small_text;
229       break;
230     case '\t':
231       lineType = NORMAL;
232       font = white_text;
233       break;
234     case '-':
235       lineType = HEADING;
236       font = white_big_text;
237       break;
238     case '*':
239       lineType = REFERENCE;
240       font = blue_text;
241       break;
242     case '#':
243       lineType = NORMAL_LEFT;
244       font = white_text;
245       break;
246     case '!':
247       lineType = IMAGE;
248       image = new Surface(text);
249       break;
250     default:
251       log_warning << "Unknown format_char: '" << format_char << "'" << std::endl;
252       break;
253   }
254 }
255
256 InfoBoxLine::~InfoBoxLine()
257 {
258   delete image;
259 }
260
261 const std::vector<InfoBoxLine*>
262 InfoBoxLine::split(const std::string& text, int line_length)
263 {
264   std::vector<InfoBoxLine*> lines;
265
266   std::string::size_type i = 0;
267   std::string::size_type l;
268   char format_char = '#';
269   while(i < text.size()) {
270     // take care of empty lines - represent them as blank lines of normal text
271     if (text[i] == '\n') {
272       lines.push_back(new InfoBoxLine('\t', ""));
273       i++;
274       continue;
275     }
276
277     // extract the format_char
278     format_char = text[i];
279     i++;
280     if (i >= text.size()) break;
281
282     // extract one line
283     l = text.find("\n", i);
284     if (l == std::string::npos) l=text.size();
285     std::string s = text.substr(i, l-i);
286     i = l+1;
287
288     // if we are dealing with an image, just store the line
289     if (format_char == '!') {
290       lines.push_back(new InfoBoxLine(format_char, s));
291       continue;
292     }
293
294     // append wrapped parts of line into list
295     std::string overflow;
296     do {
297       lines.push_back(new InfoBoxLine(format_char, Font::wrap_to_chars(s, line_length, &overflow)));
298       s = overflow;
299     } while (s.length() > 0);
300
301   }
302
303   return lines;
304 }
305
306 void
307 InfoBoxLine::draw(DrawingContext& context, const Vector& position, int layer)
308 {
309   switch (lineType) {
310     case IMAGE:
311       context.draw_surface(image, Vector( (SCREEN_WIDTH - image->get_width()) / 2, position.y), layer);
312       break;
313     case NORMAL_LEFT:
314       context.draw_text(font, text, Vector(position.x, position.y), LEFT_ALLIGN, layer);
315       break;
316     default:
317       context.draw_text(font, text, Vector(SCREEN_WIDTH/2, position.y), CENTER_ALLIGN, layer);
318       break;
319   }
320 }
321
322 float
323 InfoBoxLine::get_height()
324 {
325   switch (lineType) {
326     case IMAGE:
327       return image->get_height() + ITEMS_SPACE;
328     case NORMAL_LEFT:
329       return font->get_height() + ITEMS_SPACE;
330     default:
331       return font->get_height() + ITEMS_SPACE;
332   }
333 }