Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / object / infoblock.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 "object/infoblock.hpp"
18
19 #include "object/player.hpp"
20 #include "sprite/sprite_manager.hpp"
21 #include "supertux/object_factory.hpp"
22 #include "supertux/sector.hpp"
23 #include "util/reader.hpp"
24 #include "video/drawing_context.hpp"
25
26 namespace {
27 const float SCROLL_DELAY = 0.5;
28 const float SCROLL_DISTANCE = 16;
29 const float WIDTH = 400;
30 const float HEIGHT = 200;
31 }
32
33 InfoBlock::InfoBlock(const Reader& lisp) :
34   Block(sprite_manager->create("images/objects/bonus_block/infoblock.sprite")), 
35   shown_pct(0), 
36   dest_pct(0)
37 {
38   Vector pos;
39   lisp.get("x", pos.x);
40   lisp.get("y", pos.y);
41   bbox.set_pos(pos);
42
43   if(!lisp.get("message", message)) {
44     log_warning << "No message in InfoBlock" << std::endl;
45   }
46   //stopped = false;
47   //ringing = new AmbientSound(get_pos(), 0.5, 300, 1, "sounds/phone.wav");
48   //Sector::current()->add_object(ringing);
49
50   // Split text string lines into a vector
51   lines = InfoBoxLine::split(message, 400);
52   lines_height = 0;
53   for(size_t i = 0; i < lines.size(); ++i) lines_height+=lines[i]->get_height();
54 }
55
56 InfoBlock::~InfoBlock()
57 {
58   for(std::vector<InfoBoxLine*>::iterator i = lines.begin(); i != lines.end(); i++) {
59     delete *i;
60   }
61 }
62
63 void
64 InfoBlock::hit(Player& player)
65 {
66   start_bounce(&player);
67
68   //if (!stopped) {
69   //  ringing->remove_me();
70   //  stopped = true;
71   //}
72
73   if (dest_pct != 1) {
74
75     // first hide all other InfoBlocks' messages in same sector
76     Sector* parent = Sector::current();
77     if (!parent) return;
78     for (Sector::GameObjects::iterator i = parent->gameobjects.begin(); i != parent->gameobjects.end(); i++) {
79       InfoBlock* block = dynamic_cast<InfoBlock*>(*i);
80       if (!block) continue;
81       if (block != this) block->hide_message();
82     }
83
84     // show our message
85     show_message();
86
87   } else {
88     hide_message();
89   }
90 }
91
92 Player*
93 InfoBlock::get_nearest_player()
94 {
95   // FIXME: does not really return nearest player
96
97   std::vector<Player*> players = Sector::current()->get_players();
98   for (std::vector<Player*>::iterator playerIter = players.begin(); playerIter != players.end(); ++playerIter) {
99     Player* player = *playerIter;
100     return player;
101   }
102
103   return 0;
104 }
105
106 void
107 InfoBlock::update(float delta)
108 {
109   Block::update(delta);
110
111   if (delta == 0) return;
112
113   // hide message if player is too far away or above infoblock
114   if (dest_pct > 0) {
115     Player* player = get_nearest_player();
116     if (player) {
117       Vector p1 = this->get_pos() + (this->get_bbox().p2 - this->get_bbox().p1) / 2;
118       Vector p2 = player->get_pos() + (player->get_bbox().p2 - player->get_bbox().p1) / 2;
119       Vector dist = (p2 - p1);
120       float d = dist.norm();
121       if (d > 128 || dist.y < 0) dest_pct = 0;
122     }
123   }
124
125   // handle soft fade-in and fade-out
126   if (shown_pct != dest_pct) {
127     if (dest_pct > shown_pct) shown_pct = std::min(shown_pct + 2*delta, dest_pct);
128     if (dest_pct < shown_pct) shown_pct = std::max(shown_pct - 2*delta, dest_pct);
129   }
130 }
131
132 void
133 InfoBlock::draw(DrawingContext& context)
134 {
135   Block::draw(context);
136
137   if (shown_pct <= 0) return;
138
139   context.push_transform();
140   //context.set_translation(Vector(0, 0));
141   context.set_alpha(shown_pct);
142
143   //float x1 = SCREEN_WIDTH/2-200;
144   //float y1 = SCREEN_HEIGHT/2-200;
145   float border = 8;
146   float width = 400; // this is the text width only
147   float height = lines_height; // this is the text height only
148   float x1 = (get_bbox().p1.x + get_bbox().p2.x)/2 - width/2;
149   float x2 = (get_bbox().p1.x + get_bbox().p2.x)/2 + width/2;
150   float y1 = original_y - height;
151
152   if(x1 < 0) {
153     x1 = 0;
154     x2 = width;
155   }
156
157   if(x2 > Sector::current()->get_width()) {
158     x2 = Sector::current()->get_width();
159     x1 = x2 - width;
160   }
161
162   // lines_height includes one ITEMS_SPACE too much, so the bottom border is reduced by 4px
163   context.draw_filled_rect(Vector(x1-border, y1-border), Vector(width+2*border, height+2*border-4), Color(0.6f, 0.7f, 0.8f, 0.5f), LAYER_GUI-50);
164
165   float y = y1;
166   for(size_t i = 0; i < lines.size(); ++i) {
167     if(y >= y1 + height) {
168       //log_warning << "Too many lines of text in InfoBlock" << std::endl;
169       //dest_pct = 0;
170       //shown_pct = 0;
171       break;
172     }
173
174     lines[i]->draw(context, Rect(x1, y, x2, y), LAYER_GUI-50+1);
175     y += lines[i]->get_height();
176   }
177
178   context.pop_transform();
179 }
180
181 void
182 InfoBlock::show_message()
183 {
184   dest_pct = 1;
185 }
186
187 void
188 InfoBlock::hide_message()
189 {
190   dest_pct = 0;
191 }
192
193 IMPLEMENT_FACTORY(InfoBlock, "infoblock");
194
195 /* EOF */