~ubuntu-branches/ubuntu/trusty/plee-the-bear/trusty-proposed

« back to all changes in this revision

Viewing changes to plee-the-bear/src/ptb/gui/code/horizontal_gauge.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Julien Jorge, Julien Jorge
  • Date: 2010-11-17 20:13:34 UTC
  • mfrom: (6.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20101117201334-o4dp7uq437to7oxb
Tags: 0.5.1-1
[ Julien Jorge ]
* New upstream release (Closes: #565062, #546514).
* Add armhf architecture (Closes: #604689).
* Remove patches integrated upstream: rpath-editors.diff, rpath-game.diff,
  editors-menu-section.diff.
* Bump the Standard-Version, no changes.
* Update my email address.
* Set build dependency of libclaw to 1.6.0.
* Add the missing ${misc:Depends} in debian/control.
* List gettext translations in bear-factory.install and plee-the-bear.install.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Plee the Bear
 
3
 
 
4
  Copyright (C) 2005-2010 Julien Jorge, Sebastien Angibaud
 
5
 
 
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.
 
10
 
 
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
 
14
  more details.
 
15
 
 
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
 
19
 
 
20
  contact: plee-the-bear@gamned.org
 
21
 
 
22
  Please add the tag [PTB] in the subject of your mails.
 
23
*/
 
24
/**
 
25
 * \file horizontal_gauge.cpp
 
26
 * \brief Implementation of the ptb::horizontal_gauge class.
 
27
 * \author Julien Jorge
 
28
 */
 
29
#include "ptb/gui/horizontal_gauge.hpp"
 
30
 
 
31
#include "visual/scene_sprite.hpp"
 
32
 
 
33
/*----------------------------------------------------------------------------*/
 
34
/**
 
35
 * \brief Constructor.
 
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.
 
43
 */
 
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)
 
50
{
 
51
  m_level = glob.auto_sprite("gfx/tube.png", level_sprite);
 
52
 
 
53
  if ( !loss_sprite.empty() )
 
54
    m_loss = glob.auto_sprite("gfx/tube.png", loss_sprite);
 
55
 
 
56
  if ( !icon_name.empty() )
 
57
    m_icon = glob.auto_sprite("gfx/tube-vertical.png", icon_name);
 
58
 
 
59
  m_loss.set_opacity(0);
 
60
 
 
61
  if ( clamp_flashing )
 
62
    m_clamp_increment_direction = 1;
 
63
 
 
64
  m_tube_clamp = glob.auto_sprite("gfx/tube.png", "clamp");
 
65
  m_glass_tube =  glob.auto_sprite("gfx/tube.png", "tube");
 
66
 
 
67
  m_glass_tube.set_width(length);
 
68
  m_level.set_width(0);
 
69
 
 
70
} // horizontal_gauge::horizontal_gauge()
 
71
 
 
72
/*----------------------------------------------------------------------------*/
 
73
/**
 
74
 * \brief Set the length.
 
75
 */
 
76
void ptb::horizontal_gauge::set_length(unsigned int length)
 
77
{
 
78
  m_glass_tube.set_width(length);
 
79
} // horizontal_gauge::set_length()
 
80
 
 
81
/*----------------------------------------------------------------------------*/
 
82
/**
 
83
 * \brief Get the visual length of the bar.
 
84
 */
 
85
unsigned int ptb::horizontal_gauge::length() const
 
86
{
 
87
  return m_glass_tube.width();
 
88
} // horizontal_gauge::length()
 
89
 
 
90
/*----------------------------------------------------------------------------*/
 
91
/**
 
92
 * \brief Get the visual width of the bar.
 
93
 */
 
94
unsigned int ptb::horizontal_gauge::width() const
 
95
{
 
96
  return 2 * m_tube_clamp.width() + m_glass_tube.width();
 
97
} // horizontal_gauge::width()
 
98
 
 
99
/*----------------------------------------------------------------------------*/
 
100
/**
 
101
 * \brief Get the visual height of the bar.
 
102
 */
 
103
unsigned int ptb::horizontal_gauge::height() const
 
104
{
 
105
  return std::max
 
106
    ( m_tube_clamp.height() + m_icon.height() / 2, m_glass_tube.height() );
 
107
} // horizontal_gauge::height()
 
108
 
 
109
/*----------------------------------------------------------------------------*/
 
110
/**
 
111
 * \brief Set the level of the bar.
 
112
 * \param lev The new level.
 
113
 */
 
114
void ptb::horizontal_gauge::set_level( double lev )
 
115
{
 
116
  if ( lev != m_level_value )
 
117
    {
 
118
      if ( lev < m_level_value )
 
119
        m_loss.set_opacity(1);
 
120
      else
 
121
        m_loss.set_opacity(0);
 
122
 
 
123
      m_level_value = std::min(lev, m_max_value);
 
124
    }
 
125
} // horizontal_gauge::set_level()
 
126
 
 
127
/*----------------------------------------------------------------------------*/
 
128
/**
 
129
 * \brief Get the level of the bar.
 
130
 */
 
131
double ptb::horizontal_gauge::get_level() const
 
132
{
 
133
  return m_level_value;
 
134
} // horizontal_gauge::get_level()
 
135
 
 
136
/*----------------------------------------------------------------------------*/
 
137
/**
 
138
 * \brief Set the maximum level of the bar.
 
139
 * \param lev The new maximum level.
 
140
 */
 
141
void ptb::horizontal_gauge::set_max_level( double lev )
 
142
{
 
143
  m_max_value = lev;
 
144
  m_level_value = std::min(m_level_value, m_max_value);
 
145
} // horizontal_gauge::set_max_level()
 
146
 
 
147
/*----------------------------------------------------------------------------*/
 
148
/**
 
149
 * \brief Update the bar.
 
150
 * \param elapsed_time Elapsed time since the last call.
 
151
 */
 
152
void ptb::horizontal_gauge::progress
 
153
( bear::universe::time_type elapsed_time )
 
154
{
 
155
  if ( m_loss.get_opacity() >= 0 )
 
156
    m_loss.set_opacity
 
157
      ( std::max(0.0, m_loss.get_opacity() - elapsed_time) );
 
158
 
 
159
  m_level.set_width
 
160
    ( (unsigned int)
 
161
      (m_level_value * m_glass_tube.width() / m_max_value + 0.5) );
 
162
  m_loss.set_width( m_level.width() );
 
163
 
 
164
  if ( (4 * m_level_value <= m_max_value)
 
165
       && (m_clamp_increment_direction != 0) )
 
166
    {
 
167
      m_clamp_green_intensity +=
 
168
        2 * m_clamp_increment_direction * elapsed_time;
 
169
 
 
170
      if ( m_clamp_green_intensity > 1 )
 
171
        {
 
172
          m_clamp_green_intensity = 1;
 
173
          m_clamp_increment_direction = -1;
 
174
        }
 
175
      else if ( m_clamp_green_intensity < 0 )
 
176
        {
 
177
          m_clamp_green_intensity = 0;
 
178
          m_clamp_increment_direction = 1;
 
179
        }
 
180
 
 
181
      m_loss.set_opacity(1);
 
182
    }
 
183
  else
 
184
    m_clamp_green_intensity = 1;
 
185
} // horizontal_gauge::progress()
 
186
 
 
187
/*----------------------------------------------------------------------------*/
 
188
/**
 
189
 * \brief Display the bar.
 
190
 * \param e (out) The scene elements.
 
191
 * \param pos The position of the bar.
 
192
 */
 
193
void ptb::horizontal_gauge::render
 
194
( scene_element_list& e, const bear::visual::position_type& pos )
 
195
{
 
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;
 
200
 
 
201
  clamp.set_intensity(1, m_clamp_green_intensity, 1);
 
202
 
 
203
  if (d < 0)
 
204
    p.y -= d;
 
205
 
 
206
  e.push_back
 
207
    ( bear::visual::scene_sprite(p.x + m_icon.width() / 2, p.y, clamp) );
 
208
  e.push_back
 
209
    ( bear::visual::scene_sprite
 
210
      (p.x, p.y + clamp.height() - m_icon.height() / 2,
 
211
       m_icon) );
 
212
 
 
213
  p.x += clamp.width() + m_icon.width() / 2;
 
214
  p.y += d;
 
215
 
 
216
  e.push_back( bear::visual::scene_sprite(p.x, p.y, m_level) );
 
217
 
 
218
  if ( m_loss.get_opacity() != 0 )
 
219
    e.push_back( bear::visual::scene_sprite(p.x, p.y, m_loss) );
 
220
 
 
221
  e.push_back( bear::visual::scene_sprite(p.x, p.y, m_glass_tube) );
 
222
 
 
223
  p.x += m_glass_tube.width();
 
224
  p.y -= d;
 
225
  clamp.mirror(true);
 
226
  e.push_back( bear::visual::scene_sprite(p.x, p.y, clamp) );
 
227
} // horizontal_gauge::render()