9f7a5dd56a4e6b7c98e0802f69a86b62794bc947
[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
21 #include <config.h>
22
23 #include "textscroller.h"
24
25 #include <stdexcept>
26 #include "resources.h"
27 #include "video/font.h"
28 #include "video/drawing_context.h"
29 #include "app/globals.h"
30 #include "lisp/parser.h"
31 #include "lisp/lisp.h"
32 #include "main.h"
33 #include "control/joystickkeyboardcontroller.h"
34
35 static const float DEFAULT_SPEED = .02;
36 static const float SCROLL = 60;
37 static const float ITEMS_SPACE = 4;
38
39 static void split_text(const std::string& text, std::vector<std::string>& lines)
40 {
41   // Split text string lines into a vector
42   lines.clear();
43   std::string::size_type i, l;
44   i = 0;
45   while(true) {
46     l = text.find("\n", i);
47
48     if(l == std::string::npos) {
49       lines.push_back(text.substr(i, text.size()-i));
50       break;
51     }
52
53     lines.push_back(text.substr(i, l-i));
54     i = l+1;
55   }
56 }
57
58 void display_text_file(const std::string& file)
59 {
60   const Font* heading_font = white_big_text;
61   const Font* normal_font = white_text;
62   const Font* small_font = white_small_text;
63   const Font* reference_font = blue_text;
64   float defaultspeed = DEFAULT_SPEED;
65   float speed = defaultspeed;
66   
67   std::string text;
68   std::string background_file;
69   std::vector<std::string> lines;
70
71   std::string filename = datadir + "/" + file;
72   lisp::Parser parser;
73   try {
74     std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
75
76     const lisp::Lisp* text_lisp = root->get_lisp("supertux-text");
77     if(!text_lisp)
78       throw std::runtime_error("File isn't a supertux-text file");
79     
80     if(!text_lisp->get("text", text))
81       throw std::runtime_error("file doesn't contain a text field");
82     if(!text_lisp->get("background", background_file))
83       throw std::runtime_error("file doesn't contain a background file");
84     if(text_lisp->get("speed", defaultspeed))
85       defaultspeed /= 50;
86   } catch(std::exception& e) {
87     std::cerr << "Couldn't load file '" << filename << "': " << e.what() <<
88       "\n";
89     return;
90   }
91
92   // Split text string lines into a vector
93   split_text(text, lines);
94
95   // load background image
96   Surface* background = new Surface(
97       get_resource_filename("images/background/" + background_file), false);
98
99   bool done = false;
100   float scroll = 0;
101   float left_border = 50;
102
103   DrawingContext context;
104   SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
105
106   Uint32 lastticks = SDL_GetTicks();
107   while(!done) {
108     main_controller->update();
109     /* in case of input, exit */
110     SDL_Event event;
111     while(SDL_PollEvent(&event)) {
112       main_controller->process_event(event);
113       if(event.type == SDL_QUIT)
114         throw std::runtime_error("received window close");
115     }
116
117     if(main_controller->hold(Controller::UP)) {
118       speed = -defaultspeed*5;
119     } else if(main_controller->hold(Controller::DOWN)) {
120       speed = defaultspeed*5;
121     } else {
122       speed = defaultspeed;
123     }
124     if(main_controller->pressed(Controller::JUMP)
125        || main_controller->pressed(Controller::ACTION)
126        || main_controller->pressed(Controller::MENU_SELECT))
127       scroll += SCROLL;    
128     if(main_controller->pressed(Controller::PAUSE_MENU))
129       done = true;
130     
131     /* draw the credits */
132     context.draw_surface(background, Vector(0,0), 0);
133
134     float y = 0;
135     for(size_t i = 0; i < lines.size(); i++) {
136       const std::string& line = lines[i];
137       if(line.size() == 0) {
138         y += normal_font->get_height() + ITEMS_SPACE;
139         continue;
140       }
141       
142       const Font* font = 0;
143       bool center = true;
144       switch(line[0])
145       {
146         case ' ': font = small_font; break;
147         case '\t': font = normal_font; break;
148         case '-': font = heading_font; break;
149         case '*': font = reference_font; break;
150         case '#': font = normal_font; center = false; break;
151         default:
152           std::cerr << "Warning: text contains an unformated line.\n";
153           font = normal_font;
154           center = false;
155           break;
156       }
157       
158       if(center) {
159         context.draw_text(font,
160                           line.substr(1, line.size()-1),
161                           Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT + y - scroll),
162                           CENTER_ALLIGN, LAYER_FOREGROUND1);
163       } else {
164         context.draw_text(font,
165                           line.substr(1, line.size()-1),
166                           Vector(left_border, SCREEN_HEIGHT + y - scroll),
167                           LEFT_ALLIGN, LAYER_FOREGROUND1);
168       }
169       
170       y += font->get_height() + ITEMS_SPACE;
171     }
172     
173     context.do_drawing();
174     
175     if(SCREEN_HEIGHT+y-scroll < 0 && 20+SCREEN_HEIGHT+y-scroll < 0)
176       done = 1;
177     
178     Uint32 ticks = SDL_GetTicks();
179     scroll += speed * (ticks - lastticks);
180     lastticks = ticks;
181     if(scroll < 0)
182       scroll = 0;
183     
184     SDL_Delay(10);
185   }
186   
187   SDL_EnableKeyRepeat(0, 0);    // disables key repeating
188   delete background;
189 }
190
191 InfoBox::InfoBox(const std::string& text)
192   : firstline(0)
193 {
194   split_text(text, lines);
195 }
196
197 InfoBox::~InfoBox()
198 {
199 }
200
201 void
202 InfoBox::draw(DrawingContext& context)
203 {
204   const Font* heading_font = white_big_text;
205   const Font* normal_font = white_text;
206   const Font* small_font = white_small_text;
207   const Font* reference_font = blue_text;
208   
209   float x1 = 200;
210   float y1 = 100;
211   float width = 400;
212   float height = 200;
213   
214   context.draw_filled_rect(Vector(x1, y1), Vector(width, height),
215       Color(150, 180, 200, 125), LAYER_GUI-1);
216
217   float y = y1;
218   for(size_t i = firstline; i < lines.size(); ++i) {
219     const std::string& line = lines[i];
220     if(y >= y1 + height)
221       break;
222
223     if(line.size() == 0) {
224       y += normal_font->get_height() + ITEMS_SPACE;    
225       continue;                                        
226     }
227
228     const Font* font = 0;
229     bool center = true;
230     switch(line[0])
231     {
232       case ' ': font = small_font; break;
233       case '\t': font = normal_font; break;
234       case '-': font = heading_font; break;
235       case '*': font = reference_font; break;
236       case '#': font = normal_font; center = false; break;
237       default:
238         std::cerr << "Warning: text contains an unformated line.\n";
239         font = normal_font;
240         center = false;
241         break;
242     }
243     
244     if(center) {
245       context.draw_text(font,
246           line.substr(1, line.size()-1),
247           Vector(SCREEN_WIDTH/2, y),
248           CENTER_ALLIGN, LAYER_GUI);
249     } else {
250       context.draw_text(font,
251           line.substr(1, line.size()-1),
252           Vector(x1, y),
253           LEFT_ALLIGN, LAYER_GUI);
254     }
255       
256     y += font->get_height() + ITEMS_SPACE;
257   }
258 }
259
260 void
261 InfoBox::scrollup()
262 {
263   if(firstline > 0)
264     firstline--;
265 }
266
267 void
268 InfoBox::scrolldown()
269 {
270   if(firstline < lines.size()-1)
271     firstline++;
272 }
273
274 void
275 InfoBox::pageup()
276 {
277 }
278
279 void
280 InfoBox::pagedown()
281 {
282 }
283