00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "supertux/textscroller.hpp"
00018
00019 #include "audio/sound_manager.hpp"
00020 #include "control/joystickkeyboardcontroller.hpp"
00021 #include "lisp/parser.hpp"
00022 #include "supertux/fadeout.hpp"
00023 #include "supertux/info_box_line.hpp"
00024 #include "supertux/globals.hpp"
00025 #include "supertux/screen_manager.hpp"
00026 #include "supertux/resources.hpp"
00027 #include "util/reader.hpp"
00028 #include "video/drawing_context.hpp"
00029
00030 #include <sstream>
00031 #include <stdexcept>
00032
00033 static const float DEFAULT_SPEED = 20;
00034 static const float LEFT_BORDER = 50;
00035 static const float SCROLL = 60;
00036
00037 TextScroller::TextScroller(const std::string& filename) :
00038 defaultspeed(),
00039 speed(),
00040 music(),
00041 background(),
00042 lines(),
00043 scroll(),
00044 fading()
00045 {
00046 defaultspeed = DEFAULT_SPEED;
00047 speed = defaultspeed;
00048
00049 std::string text;
00050 std::string background_file;
00051
00052 lisp::Parser parser;
00053 try {
00054 const lisp::Lisp* root = parser.parse(filename);
00055
00056 const lisp::Lisp* text_lisp = root->get_lisp("supertux-text");
00057 if(!text_lisp)
00058 throw std::runtime_error("File isn't a supertux-text file");
00059
00060 if(!text_lisp->get("text", text))
00061 throw std::runtime_error("file doesn't contain a text field");
00062 if(!text_lisp->get("background", background_file))
00063 throw std::runtime_error("file doesn't contain a background file");
00064 text_lisp->get("speed", defaultspeed);
00065 text_lisp->get("music", music);
00066 } catch(std::exception& e) {
00067 std::ostringstream msg;
00068 msg << "Couldn't load file '" << filename << "': " << e.what() << std::endl;
00069 throw std::runtime_error(msg.str());
00070 }
00071
00072
00073 lines = InfoBoxLine::split(text, SCREEN_WIDTH - 2*LEFT_BORDER);
00074
00075
00076 background = Surface::create("images/background/" + background_file);
00077
00078 scroll = 0;
00079 fading = false;
00080 }
00081
00082 TextScroller::~TextScroller()
00083 {
00084 for(std::vector<InfoBoxLine*>::iterator i = lines.begin(); i != lines.end(); i++) delete *i;
00085 }
00086
00087 void
00088 TextScroller::setup()
00089 {
00090 sound_manager->play_music(music);
00091 }
00092
00093 void
00094 TextScroller::update(float elapsed_time)
00095 {
00096 Controller *controller = g_jk_controller->get_main_controller();
00097 if(controller->hold(Controller::UP)) {
00098 speed = -defaultspeed*5;
00099 } else if(controller->hold(Controller::DOWN)) {
00100 speed = defaultspeed*5;
00101 } else {
00102 speed = defaultspeed;
00103 }
00104 if(controller->pressed(Controller::JUMP)
00105 || controller->pressed(Controller::ACTION)
00106 || controller->pressed(Controller::MENU_SELECT))
00107 scroll += SCROLL;
00108 if(controller->pressed(Controller::PAUSE_MENU)) {
00109 g_screen_manager->exit_screen(new FadeOut(0.5));
00110 }
00111
00112 scroll += speed * elapsed_time;
00113
00114 if(scroll < 0)
00115 scroll = 0;
00116 }
00117
00118 void
00119 TextScroller::draw(DrawingContext& context)
00120 {
00121 context.draw_filled_rect(Vector(0, 0), Vector(SCREEN_WIDTH, SCREEN_HEIGHT),
00122 Color(0.6f, 0.7f, 0.8f, 0.5f), 0);
00123 context.draw_surface(background, Vector(SCREEN_WIDTH/2 - background->get_width()/2 , SCREEN_HEIGHT/2 - background->get_height()/2), 0);
00124
00125 float y = SCREEN_HEIGHT - scroll;
00126 for(size_t i = 0; i < lines.size(); i++) {
00127 if (y + lines[i]->get_height() >= 0 && SCREEN_HEIGHT - y >= 0) {
00128 lines[i]->draw(context, Rectf(LEFT_BORDER, y, SCREEN_WIDTH - 2*LEFT_BORDER, y), LAYER_GUI);
00129 }
00130
00131 y += lines[i]->get_height();
00132 }
00133
00134 if(y < 0 && !fading ) {
00135 fading = true;
00136 g_screen_manager->exit_screen(new FadeOut(0.5));
00137 }
00138 }
00139
00140