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