4 // Copyright (C) 2005 Matthias Braun <matze@braunis.de>
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.
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.
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
23 #include "textscroller.h"
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"
33 #include "control/joystickkeyboardcontroller.h"
35 static const float DEFAULT_SPEED = .02;
36 static const float SCROLL = 60;
37 static const float ITEMS_SPACE = 4;
39 static void split_text(const std::string& text, std::vector<std::string>& lines)
41 // Split text string lines into a vector
43 std::string::size_type i, l;
46 l = text.find("\n", i);
48 if(l == std::string::npos) {
49 lines.push_back(text.substr(i, text.size()-i));
53 lines.push_back(text.substr(i, l-i));
58 void display_text_file(const std::string& file)
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;
68 std::string background_file;
69 std::vector<std::string> lines;
71 std::string filename = datadir + "/" + file;
74 std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
76 const lisp::Lisp* text_lisp = root->get_lisp("supertux-text");
78 throw std::runtime_error("File isn't a supertux-text file");
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))
86 } catch(std::exception& e) {
87 std::cerr << "Couldn't load file '" << filename << "': " << e.what() <<
92 // Split text string lines into a vector
93 split_text(text, lines);
95 // load background image
96 Surface* background = new Surface(
97 get_resource_filename("images/background/" + background_file), false);
101 float left_border = 50;
103 DrawingContext context;
104 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
106 Uint32 lastticks = SDL_GetTicks();
108 main_controller->update();
109 /* in case of input, exit */
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");
117 if(main_controller->hold(Controller::UP)) {
118 speed = -defaultspeed*5;
119 } else if(main_controller->hold(Controller::DOWN)) {
120 speed = defaultspeed*5;
122 speed = defaultspeed;
124 if(main_controller->pressed(Controller::JUMP)
125 || main_controller->pressed(Controller::ACTION)
126 || main_controller->pressed(Controller::MENU_SELECT))
128 if(main_controller->pressed(Controller::PAUSE_MENU))
131 /* draw the credits */
132 context.draw_surface(background, Vector(0,0), 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;
142 const Font* font = 0;
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;
152 std::cerr << "Warning: text contains an unformated line.\n";
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);
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);
170 y += font->get_height() + ITEMS_SPACE;
173 context.do_drawing();
175 if(SCREEN_HEIGHT+y-scroll < 0 && 20+SCREEN_HEIGHT+y-scroll < 0)
178 Uint32 ticks = SDL_GetTicks();
179 scroll += speed * (ticks - lastticks);
187 SDL_EnableKeyRepeat(0, 0); // disables key repeating
191 InfoBox::InfoBox(const std::string& text)
194 split_text(text, lines);
202 InfoBox::draw(DrawingContext& context)
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;
214 context.draw_filled_rect(Vector(x1, y1), Vector(width, height),
215 Color(150, 180, 200, 125), LAYER_GUI-1);
218 for(size_t i = firstline; i < lines.size(); ++i) {
219 const std::string& line = lines[i];
223 if(line.size() == 0) {
224 y += normal_font->get_height() + ITEMS_SPACE;
228 const Font* font = 0;
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;
238 std::cerr << "Warning: text contains an unformated line.\n";
245 context.draw_text(font,
246 line.substr(1, line.size()-1),
247 Vector(SCREEN_WIDTH/2, y),
248 CENTER_ALLIGN, LAYER_GUI);
250 context.draw_text(font,
251 line.substr(1, line.size()-1),
253 LEFT_ALLIGN, LAYER_GUI);
256 y += font->get_height() + ITEMS_SPACE;
268 InfoBox::scrolldown()
270 if(firstline < lines.size()-1)