3 #include "textscroller.h"
7 #include "video/font.h"
8 #include "video/drawing_context.h"
9 #include "app/globals.h"
10 #include "lisp/parser.h"
11 #include "lisp/lisp.h"
13 static const float DEFAULT_SPEED = 1.0;
14 static const float MAX_VEL = 10;
15 static const float SPEED_INC = 0.01;
16 static const float SCROLL = 60;
17 static const float ITEMS_SPACE = 4;
19 static void split_text(const std::string& text, std::vector<std::string>& lines)
21 // Split text string lines into a vector
23 std::string::size_type i, l;
26 l = text.find("\n", i);
28 if(l == std::string::npos) {
29 lines.push_back(text.substr(i, text.size()-i));
33 lines.push_back(text.substr(i, l-i));
38 void display_text_file(const std::string& file)
40 const Font* heading_font = white_big_text;
41 const Font* normal_font = white_text;
42 const Font* small_font = white_small_text;
43 const Font* reference_font = blue_text;
44 float speed = DEFAULT_SPEED;
47 std::string background_file;
48 std::vector<std::string> lines;
50 std::string filename = datadir + "/" + file;
53 std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
55 const lisp::Lisp* text_lisp = root->get_lisp("supertux-text");
57 throw std::runtime_error("File isn't a supertux-text file");
59 if(!text_lisp->get("text", text))
60 throw std::runtime_error("file doesn't contain a text field");
61 if(!text_lisp->get("background", background_file))
62 throw std::runtime_error("file doesn't contain a background file");
63 text_lisp->get("speed", speed);
64 } catch(std::exception& e) {
65 std::cerr << "Couldn't load file '" << filename << "': " << e.what() <<
70 // Split text string lines into a vector
71 split_text(text, lines);
73 // load background image
74 Surface* background = new Surface(
75 get_resource_filename("images/background/" + background_file), false);
80 float left_border = 50;
82 DrawingContext context;
83 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
85 Uint32 lastticks = SDL_GetTicks();
88 /* in case of input, exit */
90 while(SDL_PollEvent(&event))
94 switch(event.key.keysym.sym)
123 else if(speed < -MAX_VEL)
126 /* draw the credits */
127 context.draw_surface(background, Vector(0,0), 0);
130 for(size_t i = 0; i < lines.size(); i++) {
131 const std::string& line = lines[i];
132 if(line.size() == 0) {
133 y += normal_font->get_height() + ITEMS_SPACE;
137 const Font* font = 0;
141 case ' ': font = small_font; break;
142 case '\t': font = normal_font; break;
143 case '-': font = heading_font; break;
144 case '*': font = reference_font; break;
145 case '#': font = normal_font; center = false; break;
147 std::cerr << "Warning: text contains an unformated line.\n";
154 context.draw_text(font,
155 line.substr(1, line.size()-1),
156 Vector(screen->w/2, screen->h + y - scroll),
157 CENTER_ALLIGN, LAYER_FOREGROUND1);
159 context.draw_text(font,
160 line.substr(1, line.size()-1),
161 Vector(left_border, screen->h + y - scroll),
162 LEFT_ALLIGN, LAYER_FOREGROUND1);
165 y += font->get_height() + ITEMS_SPACE;
168 context.do_drawing();
170 if(screen->h+y-scroll < 0 && 20+screen->h+y-scroll < 0)
173 Uint32 ticks = SDL_GetTicks();
174 scroll += speed * (ticks - lastticks);
182 SDL_EnableKeyRepeat(0, 0); // disables key repeating
186 InfoBox::InfoBox(const std::string& text)
189 split_text(text, lines);
197 InfoBox::draw(DrawingContext& context)
199 const Font* heading_font = white_big_text;
200 const Font* normal_font = white_text;
201 const Font* small_font = white_small_text;
202 const Font* reference_font = blue_text;
209 context.draw_filled_rect(Vector(x1, y1), Vector(width, height),
210 Color(150, 180, 200, 125), LAYER_GUI-1);
213 for(size_t i = firstline; i < lines.size(); ++i) {
214 const std::string& line = lines[i];
218 if(line.size() == 0) {
219 y += normal_font->get_height() + ITEMS_SPACE;
223 const Font* font = 0;
227 case ' ': font = small_font; break;
228 case '\t': font = normal_font; break;
229 case '-': font = heading_font; break;
230 case '*': font = reference_font; break;
231 case '#': font = normal_font; center = false; break;
233 std::cerr << "Warning: text contains an unformated line.\n";
240 context.draw_text(font,
241 line.substr(1, line.size()-1),
242 Vector(screen->w/2, y),
243 CENTER_ALLIGN, LAYER_GUI);
245 context.draw_text(font,
246 line.substr(1, line.size()-1),
248 LEFT_ALLIGN, LAYER_GUI);
251 y += font->get_height() + ITEMS_SPACE;
263 InfoBox::scrolldown()
265 if(firstline < lines.size()-1)