More -Weffc++ cleanup
[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   defaultspeed(),
36   speed(),
37   music(),
38   background(),
39   lines(),
40   scroll(),
41   fading()
42 {
43   defaultspeed = DEFAULT_SPEED;
44   speed = defaultspeed;
45
46   std::string text;
47   std::string background_file;
48
49   lisp::Parser parser;
50   try {
51     const lisp::Lisp* root = parser.parse(filename);
52
53     const lisp::Lisp* text_lisp = root->get_lisp("supertux-text");
54     if(!text_lisp)
55       throw std::runtime_error("File isn't a supertux-text file");
56
57     if(!text_lisp->get("text", text))
58       throw std::runtime_error("file doesn't contain a text field");
59     if(!text_lisp->get("background", background_file))
60       throw std::runtime_error("file doesn't contain a background file");
61     text_lisp->get("speed", defaultspeed);
62     text_lisp->get("music", music);
63   } catch(std::exception& e) {
64     std::ostringstream msg;
65     msg << "Couldn't load file '" << filename << "': " << e.what() << std::endl;
66     throw std::runtime_error(msg.str());
67   }
68
69   // Split text string lines into a vector
70   lines = InfoBoxLine::split(text, SCREEN_WIDTH - 2*LEFT_BORDER);
71
72   // load background image
73   background.reset(new Surface("images/background/" + background_file));
74
75   scroll = 0;
76   fading = false;
77 }
78
79 TextScroller::~TextScroller()
80 {
81   for(std::vector<InfoBoxLine*>::iterator i = lines.begin(); i != lines.end(); i++) delete *i;
82 }
83
84 void
85 TextScroller::setup()
86 {
87   sound_manager->play_music(music);
88 }
89
90 void
91 TextScroller::update(float elapsed_time)
92 {
93   if(g_main_controller->hold(Controller::UP)) {
94     speed = -defaultspeed*5;
95   } else if(g_main_controller->hold(Controller::DOWN)) {
96     speed = defaultspeed*5;
97   } else {
98     speed = defaultspeed;
99   }
100   if(g_main_controller->pressed(Controller::JUMP)
101      || g_main_controller->pressed(Controller::ACTION)
102      || g_main_controller->pressed(Controller::MENU_SELECT))
103     scroll += SCROLL;
104   if(g_main_controller->pressed(Controller::PAUSE_MENU)) {
105     g_main_loop->exit_screen(new FadeOut(0.5));
106   }
107
108   scroll += speed * elapsed_time;
109
110   if(scroll < 0)
111     scroll = 0;
112 }
113
114 void
115 TextScroller::draw(DrawingContext& context)
116 {
117   context.draw_filled_rect(Vector(0, 0), Vector(SCREEN_WIDTH, SCREEN_HEIGHT),
118                            Color(0.6f, 0.7f, 0.8f, 0.5f), 0);
119   context.draw_surface(background.get(), Vector(SCREEN_WIDTH/2 - background->get_width()/2 , SCREEN_HEIGHT/2 - background->get_height()/2), 0);
120
121   float y = SCREEN_HEIGHT - scroll;
122   for(size_t i = 0; i < lines.size(); i++) {
123     if (y + lines[i]->get_height() >= 0 && SCREEN_HEIGHT - y >= 0) {
124       lines[i]->draw(context, Rect(LEFT_BORDER, y, SCREEN_WIDTH - 2*LEFT_BORDER, y), LAYER_GUI);
125     }
126
127     y += lines[i]->get_height();
128   }
129
130   if(y < 0 && !fading ) {
131     fading = true;
132     g_main_loop->exit_screen(new FadeOut(0.5));
133   }
134 }
135
136 InfoBox::InfoBox(const std::string& text)
137   : firstline(0)
138 {
139   // Split text string lines into a vector
140   lines = InfoBoxLine::split(text, 400);
141
142   try
143   {
144     // get the arrow sprites
145     arrow_scrollup   = new Surface("images/engine/menu/scroll-up.png");
146     arrow_scrolldown = new Surface("images/engine/menu/scroll-down.png");
147   }
148   catch (std::exception& e)
149   {
150     log_warning << "Could not load scrolling images: " << e.what() << std::endl;
151     arrow_scrollup = 0;
152     arrow_scrolldown = 0;
153   }
154 }
155
156 InfoBox::~InfoBox()
157 {
158   for(std::vector<InfoBoxLine*>::iterator i = lines.begin();
159       i != lines.end(); i++)
160     delete *i;
161   delete arrow_scrollup;
162   delete arrow_scrolldown;
163 }
164
165 void
166 InfoBox::draw(DrawingContext& context)
167 {
168   float x1 = SCREEN_WIDTH/2-200;
169   float y1 = SCREEN_HEIGHT/2-200;
170   float width = 400;
171   float height = 200;
172
173   context.draw_filled_rect(Vector(x1, y1), Vector(width, height),
174                            Color(0.6f, 0.7f, 0.8f, 0.5f), LAYER_GUI-1);
175
176   float y = y1;
177   bool linesLeft = false;
178   for(size_t i = firstline; i < lines.size(); ++i) {
179     if(y >= y1 + height) {
180       linesLeft = true;
181       break;
182     }
183
184     lines[i]->draw(context, Rect(x1, y, x1+width, y), LAYER_GUI);
185     y += lines[i]->get_height();
186   }
187
188   {
189     // draw the scrolling arrows
190     if (arrow_scrollup && firstline > 0)
191       context.draw_surface(arrow_scrollup,
192                            Vector( x1 + width  - arrow_scrollup->get_width(),  // top-right corner of box
193                                    y1), LAYER_GUI);
194
195     if (arrow_scrolldown && linesLeft && firstline < lines.size()-1)
196       context.draw_surface(arrow_scrolldown,
197                            Vector( x1 + width  - arrow_scrolldown->get_width(),  // bottom-light corner of box
198                                    y1 + height - arrow_scrolldown->get_height()),
199                            LAYER_GUI);
200   }
201 }
202
203 void
204 InfoBox::scrollup()
205 {
206   if(firstline > 0)
207     firstline--;
208 }
209
210 void
211 InfoBox::scrolldown()
212 {
213   if(firstline < lines.size()-1)
214     firstline++;
215 }
216
217 void
218 InfoBox::pageup()
219 {
220 }
221
222 void
223 InfoBox::pagedown()
224 {
225 }
226
227 namespace {
228 Font* get_font_by_format_char(char format_char) {
229   switch(format_char)
230   {
231     case ' ':
232       return small_font;
233       break;
234     case '-':
235       return big_font;
236       break;
237     case '\t':
238     case '*':
239     case '#':
240     case '!':
241       return normal_font;
242     break;
243     default:
244       return normal_font;
245       log_warning << "Unknown format_char: '" << format_char << "'" << std::endl;
246       break;
247   }
248 }
249
250 Color get_color_by_format_char(char format_char) {
251   switch(format_char)
252   {
253     case ' ':
254       return TextScroller::small_color;
255       break;
256     case '-':
257       return TextScroller::heading_color;
258       break;
259     case '*':
260       return TextScroller::reference_color;
261     case '\t':
262     case '#':
263     case '!':
264       return TextScroller::normal_color;
265     break;
266     default:
267       return Color(0,0,0);
268       log_warning << "Unknown format_char: '" << format_char << "'" << std::endl;
269       break;
270   }
271 }
272
273 InfoBoxLine::LineType get_linetype_by_format_char(char format_char) {
274   switch(format_char)
275   {
276     case ' ':
277       return InfoBoxLine::SMALL;
278       break;
279     case '\t':
280       return InfoBoxLine::NORMAL;
281       break;
282     case '-':
283       return InfoBoxLine::HEADING;
284       break;
285     case '*':
286       return InfoBoxLine::REFERENCE;
287       break;
288     case '#':
289       return InfoBoxLine::NORMAL_LEFT;
290       break;
291     case '!':
292       return InfoBoxLine::IMAGE;
293       break;
294     default:
295       return InfoBoxLine::SMALL;
296       log_warning << "Unknown format_char: '" << format_char << "'" << std::endl;
297       break;
298   }
299 }
300 }
301
302 InfoBoxLine::InfoBoxLine(char format_char, const std::string& text) : 
303   lineType(NORMAL),
304   font(normal_font), 
305   text(text), 
306   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 }
395
396 /* EOF */