Added supertux/globals.?pp to collect all the random global variables
[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   = new Surface("images/engine/menu/scroll-up.png");
39     arrow_scrolldown = new Surface("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 = 0;
45     arrow_scrolldown = 0;
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   delete arrow_scrollup;
56   delete arrow_scrolldown;
57 }
58
59 void
60 InfoBox::draw(DrawingContext& context)
61 {
62   float x1 = SCREEN_WIDTH/2-200;
63   float y1 = SCREEN_HEIGHT/2-200;
64   float width = 400;
65   float height = 200;
66
67   context.draw_filled_rect(Vector(x1, y1), Vector(width, height),
68                            Color(0.6f, 0.7f, 0.8f, 0.5f), LAYER_GUI-1);
69
70   float y = y1;
71   bool linesLeft = false;
72   for(size_t i = firstline; i < lines.size(); ++i) {
73     if(y >= y1 + height) {
74       linesLeft = true;
75       break;
76     }
77
78     lines[i]->draw(context, Rect(x1, y, x1+width, y), LAYER_GUI);
79     y += lines[i]->get_height();
80   }
81
82   {
83     // draw the scrolling arrows
84     if (arrow_scrollup && firstline > 0)
85       context.draw_surface(arrow_scrollup,
86                            Vector( x1 + width  - arrow_scrollup->get_width(),  // top-right corner of box
87                                    y1), LAYER_GUI);
88
89     if (arrow_scrolldown && linesLeft && firstline < lines.size()-1)
90       context.draw_surface(arrow_scrolldown,
91                            Vector( x1 + width  - arrow_scrolldown->get_width(),  // bottom-light corner of box
92                                    y1 + height - arrow_scrolldown->get_height()),
93                            LAYER_GUI);
94   }
95 }
96
97 void
98 InfoBox::scrollup()
99 {
100   if(firstline > 0)
101     firstline--;
102 }
103
104 void
105 InfoBox::scrolldown()
106 {
107   if(firstline < lines.size()-1)
108     firstline++;
109 }
110
111 void
112 InfoBox::pageup()
113 {
114 }
115
116 void
117 InfoBox::pagedown()
118 {
119 }
120
121 /* EOF */