a5c62204d4ee60f474a33634b0df65a8eb3dbe16
[supertux.git] / src / supertux / info_box.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "supertux/info_box.hpp"
18
19 #include "supertux/globals.hpp"
20 #include "supertux/info_box_line.hpp"
21 #include "util/log.hpp"
22 #include "video/drawing_context.hpp"
23 #include "video/surface.hpp"
24
25 InfoBox::InfoBox(const std::string& text) :
26   firstline(0),
27   lines(),
28   images(),
29   arrow_scrollup(),
30   arrow_scrolldown()
31 {
32   // Split text string lines into a vector
33   lines = InfoBoxLine::split(text, 400);
34
35   try
36   {
37     // get the arrow sprites
38     arrow_scrollup   = Surface::create("images/engine/menu/scroll-up.png");
39     arrow_scrolldown = Surface::create("images/engine/menu/scroll-down.png");
40   }
41   catch (std::exception& e)
42   {
43     log_warning << "Could not load scrolling images: " << e.what() << std::endl;
44     arrow_scrollup.reset();
45     arrow_scrolldown.reset();
46   }
47 }
48
49 InfoBox::~InfoBox()
50 {
51   for(std::vector<InfoBoxLine*>::iterator i = lines.begin();
52       i != lines.end(); i++)
53     delete *i;
54 }
55
56 void
57 InfoBox::draw(DrawingContext& context)
58 {
59   float x1 = SCREEN_WIDTH/2-200;
60   float y1 = SCREEN_HEIGHT/2-200;
61   float width = 400;
62   float height = 200;
63
64   context.draw_filled_rect(Vector(x1, y1), Vector(width, height),
65                            Color(0.6f, 0.7f, 0.8f, 0.5f), LAYER_GUI-1);
66
67   float y = y1;
68   bool linesLeft = false;
69   for(size_t i = firstline; i < lines.size(); ++i) {
70     if(y >= y1 + height) {
71       linesLeft = true;
72       break;
73     }
74
75     lines[i]->draw(context, Rectf(x1, y, x1+width, y), LAYER_GUI);
76     y += lines[i]->get_height();
77   }
78
79   {
80     // draw the scrolling arrows
81     if (arrow_scrollup.get() && firstline > 0)
82       context.draw_surface(arrow_scrollup,
83                            Vector( x1 + width  - arrow_scrollup->get_width(),  // top-right corner of box
84                                    y1), LAYER_GUI);
85
86     if (arrow_scrolldown.get() && linesLeft && firstline < lines.size()-1)
87       context.draw_surface(arrow_scrolldown,
88                            Vector( x1 + width  - arrow_scrolldown->get_width(),  // bottom-light corner of box
89                                    y1 + height - arrow_scrolldown->get_height()),
90                            LAYER_GUI);
91   }
92 }
93
94 void
95 InfoBox::scrollup()
96 {
97   if(firstline > 0)
98     firstline--;
99 }
100
101 void
102 InfoBox::scrolldown()
103 {
104   if(firstline < lines.size()-1)
105     firstline++;
106 }
107
108 void
109 InfoBox::pageup()
110 {
111 }
112
113 void
114 InfoBox::pagedown()
115 {
116 }
117
118 /* EOF */