4
Copyright (C) 2005-2010 Julien Jorge, Sebastien Angibaud
6
This program is free software; you can redistribute it and/or modify it
7
under the terms of the GNU General Public License as published by the
8
Free Software Foundation; either version 2 of the License, or (at your
9
option) any later version.
11
This program is distributed in the hope that it will be useful, but WITHOUT
12
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16
You should have received a copy of the GNU General Public License along
17
with this program; if not, write to the Free Software Foundation, Inc.,
18
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
contact: plee-the-bear@gamned.org
22
Please add the tag [PTB] in the subject of your mails.
25
* \file horizontal_gauge.cpp
26
* \brief Implementation of the ptb::horizontal_gauge class.
27
* \author Julien Jorge
29
#include "ptb/gui/horizontal_gauge.hpp"
31
#include "visual/scene_sprite.hpp"
33
/*----------------------------------------------------------------------------*/
36
* \param glob Level globals in which we take the resources.
37
* \param length The length of the bar.
38
* \param level_sprite The name of the sprite in "gfx/tube.png" of the image to
39
* use for the main level.
40
* \param loss_sprite The name of the sprite in "gfx/tube.png" of the image to
41
* use for the loss level.
42
* \param clamp_flashing Indicates if the clamp flashes when the level is low.
44
ptb::horizontal_gauge::horizontal_gauge
45
( bear::engine::level_globals& glob, unsigned int length,
46
const std::string& level_sprite, const std::string& loss_sprite,
47
const std::string& icon_name, bool clamp_flashing )
48
: m_max_value(1), m_level_value(0), m_clamp_green_intensity(1),
49
m_clamp_increment_direction(0)
51
m_level = glob.auto_sprite("gfx/tube.png", level_sprite);
53
if ( !loss_sprite.empty() )
54
m_loss = glob.auto_sprite("gfx/tube.png", loss_sprite);
56
if ( !icon_name.empty() )
57
m_icon = glob.auto_sprite("gfx/tube-vertical.png", icon_name);
59
m_loss.set_opacity(0);
62
m_clamp_increment_direction = 1;
64
m_tube_clamp = glob.auto_sprite("gfx/tube.png", "clamp");
65
m_glass_tube = glob.auto_sprite("gfx/tube.png", "tube");
67
m_glass_tube.set_width(length);
70
} // horizontal_gauge::horizontal_gauge()
72
/*----------------------------------------------------------------------------*/
74
* \brief Set the length.
76
void ptb::horizontal_gauge::set_length(unsigned int length)
78
m_glass_tube.set_width(length);
79
} // horizontal_gauge::set_length()
81
/*----------------------------------------------------------------------------*/
83
* \brief Get the visual length of the bar.
85
unsigned int ptb::horizontal_gauge::length() const
87
return m_glass_tube.width();
88
} // horizontal_gauge::length()
90
/*----------------------------------------------------------------------------*/
92
* \brief Get the visual width of the bar.
94
unsigned int ptb::horizontal_gauge::width() const
96
return 2 * m_tube_clamp.width() + m_glass_tube.width();
97
} // horizontal_gauge::width()
99
/*----------------------------------------------------------------------------*/
101
* \brief Get the visual height of the bar.
103
unsigned int ptb::horizontal_gauge::height() const
106
( m_tube_clamp.height() + m_icon.height() / 2, m_glass_tube.height() );
107
} // horizontal_gauge::height()
109
/*----------------------------------------------------------------------------*/
111
* \brief Set the level of the bar.
112
* \param lev The new level.
114
void ptb::horizontal_gauge::set_level( double lev )
116
if ( lev != m_level_value )
118
if ( lev < m_level_value )
119
m_loss.set_opacity(1);
121
m_loss.set_opacity(0);
123
m_level_value = std::min(lev, m_max_value);
125
} // horizontal_gauge::set_level()
127
/*----------------------------------------------------------------------------*/
129
* \brief Get the level of the bar.
131
double ptb::horizontal_gauge::get_level() const
133
return m_level_value;
134
} // horizontal_gauge::get_level()
136
/*----------------------------------------------------------------------------*/
138
* \brief Set the maximum level of the bar.
139
* \param lev The new maximum level.
141
void ptb::horizontal_gauge::set_max_level( double lev )
144
m_level_value = std::min(m_level_value, m_max_value);
145
} // horizontal_gauge::set_max_level()
147
/*----------------------------------------------------------------------------*/
149
* \brief Update the bar.
150
* \param elapsed_time Elapsed time since the last call.
152
void ptb::horizontal_gauge::progress
153
( bear::universe::time_type elapsed_time )
155
if ( m_loss.get_opacity() >= 0 )
157
( std::max(0.0, m_loss.get_opacity() - elapsed_time) );
161
(m_level_value * m_glass_tube.width() / m_max_value + 0.5) );
162
m_loss.set_width( m_level.width() );
164
if ( (4 * m_level_value <= m_max_value)
165
&& (m_clamp_increment_direction != 0) )
167
m_clamp_green_intensity +=
168
2 * m_clamp_increment_direction * elapsed_time;
170
if ( m_clamp_green_intensity > 1 )
172
m_clamp_green_intensity = 1;
173
m_clamp_increment_direction = -1;
175
else if ( m_clamp_green_intensity < 0 )
177
m_clamp_green_intensity = 0;
178
m_clamp_increment_direction = 1;
181
m_loss.set_opacity(1);
184
m_clamp_green_intensity = 1;
185
} // horizontal_gauge::progress()
187
/*----------------------------------------------------------------------------*/
189
* \brief Display the bar.
190
* \param e (out) The scene elements.
191
* \param pos The position of the bar.
193
void ptb::horizontal_gauge::render
194
( scene_element_list& e, const bear::visual::position_type& pos )
196
bear::universe::position_type p(pos);
197
bear::visual::sprite clamp(m_tube_clamp);
198
const bear::visual::coordinate_type d =
199
( clamp.height() - m_level.height() ) / 2;
201
clamp.set_intensity(1, m_clamp_green_intensity, 1);
207
( bear::visual::scene_sprite(p.x + m_icon.width() / 2, p.y, clamp) );
209
( bear::visual::scene_sprite
210
(p.x, p.y + clamp.height() - m_icon.height() / 2,
213
p.x += clamp.width() + m_icon.width() / 2;
216
e.push_back( bear::visual::scene_sprite(p.x, p.y, m_level) );
218
if ( m_loss.get_opacity() != 0 )
219
e.push_back( bear::visual::scene_sprite(p.x, p.y, m_loss) );
221
e.push_back( bear::visual::scene_sprite(p.x, p.y, m_glass_tube) );
223
p.x += m_glass_tube.width();
226
e.push_back( bear::visual::scene_sprite(p.x, p.y, clamp) );
227
} // horizontal_gauge::render()