Fix coverity #29604
[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   // Split text string lines into a vector
28   lines(InfoBoxLine::split(text, 400)),
29   images(),
30   arrow_scrollup(),
31   arrow_scrolldown()
32 {
33   try
34   {
35     // get the arrow sprites
36     arrow_scrollup   = Surface::create("images/engine/menu/scroll-up.png");
37     arrow_scrolldown = Surface::create("images/engine/menu/scroll-down.png");
38   }
39   catch (std::exception& e)
40   {
41     log_warning << "Could not load scrolling images: " << e.what() << std::endl;
42     arrow_scrollup.reset();
43     arrow_scrolldown.reset();
44   }
45 }
46
47 InfoBox::~InfoBox()
48 {
49   for(std::vector<InfoBoxLine*>::iterator i = lines.begin();
50       i != lines.end(); ++i)
51     delete *i;
52 }
53
54 void
55 InfoBox::draw(DrawingContext& context)
56 {
57   float x1 = SCREEN_WIDTH/2-200;
58   float y1 = SCREEN_HEIGHT/2-200;
59   float width = 400;
60   float height = 200;
61
62   context.draw_filled_rect(Vector(x1, y1), Vector(width, height),
63                            Color(0.6f, 0.7f, 0.8f, 0.5f), LAYER_GUI-1);
64
65   float y = y1;
66   bool linesLeft = false;
67   for(size_t i = firstline; i < lines.size(); ++i) {
68     if(y >= y1 + height) {
69       linesLeft = true;
70       break;
71     }
72
73     lines[i]->draw(context, Rectf(x1, y, x1+width, y), LAYER_GUI);
74     y += lines[i]->get_height();
75   }
76
77   {
78     // draw the scrolling arrows
79     if (arrow_scrollup.get() && firstline > 0)
80       context.draw_surface(arrow_scrollup,
81                            Vector( x1 + width  - arrow_scrollup->get_width(),  // top-right corner of box
82                                    y1), LAYER_GUI);
83
84     if (arrow_scrolldown.get() && linesLeft && firstline < lines.size()-1)
85       context.draw_surface(arrow_scrolldown,
86                            Vector( x1 + width  - arrow_scrolldown->get_width(),  // bottom-light corner of box
87                                    y1 + height - arrow_scrolldown->get_height()),
88                            LAYER_GUI);
89   }
90 }
91
92 void
93 InfoBox::scrollup()
94 {
95   if(firstline > 0)
96     firstline--;
97 }
98
99 void
100 InfoBox::scrolldown()
101 {
102   if(firstline < lines.size()-1)
103     firstline++;
104 }
105
106 void
107 InfoBox::pageup()
108 {
109 }
110
111 void
112 InfoBox::pagedown()
113 {
114 }
115
116 /* EOF */