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