~ubuntu-branches/ubuntu/natty/plee-the-bear/natty

« back to all changes in this revision

Viewing changes to plee-the-bear/src/ptb/frame/code/frame_level_score.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 frame_level_score.cpp
 
26
 * \brief Implementation of the ptb::frame_level_score class.
 
27
 * \author Julien Jorge
 
28
 */
 
29
#include "ptb/frame/frame_level_score.hpp"
 
30
 
 
31
#include "ptb/frame/theme_colors.hpp"
 
32
#include "ptb/player.hpp"
 
33
#include "ptb/score_table.hpp"
 
34
#include "ptb/util.hpp"
 
35
 
 
36
#include "gui/button.hpp"
 
37
#include "gui/callback_function.hpp"
 
38
#include "gui/static_text.hpp"
 
39
#include "gui/picture.hpp"
 
40
 
 
41
#include "engine/game.hpp"
 
42
#include "engine/level.hpp"
 
43
#include "engine/level_globals.hpp"
 
44
 
 
45
#include <boost/bind.hpp>
 
46
#include <claw/string_algorithm.hpp>
 
47
#include <libintl.h>
 
48
 
 
49
/*----------------------------------------------------------------------------*/
 
50
/**
 
51
 * \brief Constructor.
 
52
 * \param in_layer The layer owning the window.
 
53
 * \param score_table The table in which the scores are saved.
 
54
 * \param score The score to save.
 
55
 * \param medal The type of the medal corresponging to this score ("gold",
 
56
 *        "silver", "bronze", "empty").
 
57
 * \param format How to format the value of the score.
 
58
 * \param next_level The name path to the level to load once the score is saved.
 
59
 */
 
60
ptb::frame_level_score::frame_level_score
 
61
( windows_layer* in_layer, score_table& t, double score,
 
62
  const std::string& medal, const std::string& format,
 
63
  const std::string& next_level )
 
64
  : frame(in_layer), m_text(NULL), m_score_table(t), m_score(score),
 
65
    m_next_level(next_level),
 
66
    m_new_record(m_score_table.is_good_candidate(m_score))
 
67
{
 
68
  create_controls(medal, format);
 
69
 
 
70
  player* p = util::find_player( get_layer().get_level().get_globals(), 1 );
 
71
 
 
72
  if ( p != NULL )
 
73
    p->set_marionette(true);
 
74
 
 
75
  p = util::find_player( get_layer().get_level().get_globals(), 2 );
 
76
 
 
77
  if ( p != NULL )
 
78
    p->set_marionette(true);
 
79
} // frame_level_score::frame_level_score()
 
80
 
 
81
/*----------------------------------------------------------------------------*/
 
82
/**
 
83
 * \brief Create the controls.
 
84
 * \param medal The type of the medal to show.
 
85
 * \param format How to format the value of the score.
 
86
 */
 
87
void ptb::frame_level_score::create_controls
 
88
( const std::string& medal, const std::string& format )
 
89
{
 
90
  if ( m_new_record )
 
91
    create_new_record_controls(medal, format);
 
92
  else
 
93
    create_no_new_record_controls(medal, format);
 
94
 
 
95
  fit( get_margin() );
 
96
} // frame_level_score::create_controls()
 
97
 
 
98
/*----------------------------------------------------------------------------*/
 
99
/**
 
100
 * \brief Create the controls for the case of a new record.
 
101
 * \param medal The type of the medal to show.
 
102
 * \param format How to format the value of the score.
 
103
 */
 
104
void ptb::frame_level_score::create_new_record_controls
 
105
( const std::string& medal, const std::string& format )
 
106
{
 
107
  CLAW_PRECOND( m_new_record );
 
108
 
 
109
  set_title( gettext("New record") );
 
110
 
 
111
  bear::gui::visual_component* text = create_input();
 
112
  bear::gui::visual_component* discard = create_discard_button();
 
113
  create_save_button();
 
114
 
 
115
  text->set_bottom( discard->top() + get_margin() );
 
116
 
 
117
  bear::gui::visual_component* score = create_score(format);
 
118
  bear::gui::visual_component* m = create_medal(medal);
 
119
 
 
120
  const bear::visual::size_type w( std::max(text->width(), score->width()) );
 
121
 
 
122
  score->set_bottom_left
 
123
    ( (w - score->width()) / 2, text->top() + get_margin() );
 
124
 
 
125
  m->set_bottom_left( w + get_margin(), discard->top() + get_margin() );
 
126
 
 
127
  const double r = m->width() / m->height();
 
128
  m->set_height( score->top() - m->bottom() );
 
129
  m->set_width( r * m->height()) ;
 
130
 
 
131
  discard->set_right( m->right() );
 
132
} // frame_level_score::create_new_record_controls()
 
133
 
 
134
/*----------------------------------------------------------------------------*/
 
135
/**
 
136
 * \brief Create the controls for the case where there is no new record.
 
137
 * \param medal The type of the medal to show.
 
138
 * \param format How to format the value of the score.
 
139
 */
 
140
void ptb::frame_level_score::create_no_new_record_controls
 
141
( const std::string& medal, const std::string& format )
 
142
{
 
143
  CLAW_PRECOND( !m_new_record );
 
144
 
 
145
  set_title( gettext("No new record") );
 
146
 
 
147
  bear::gui::visual_component* discard = create_discard_button();
 
148
 
 
149
  bear::gui::static_text* msg =
 
150
    new bear::gui::static_text( &get_content(), get_font() );
 
151
  bear::gui::visual_component* score = create_score(format);
 
152
  bear::gui::visual_component* m = create_medal(medal);
 
153
 
 
154
  msg->set_auto_size(true);
 
155
  msg->set_text( gettext("You are not in the best scores. Try again!") );
 
156
 
 
157
  const bear::visual::size_type w( std::max(msg->width(), score->width()) );
 
158
 
 
159
  score->set_left( (w - score->width()) / 2 );
 
160
  msg->set_bottom( discard->top() + get_margin() );
 
161
  score->set_bottom( msg->top() + get_margin() );
 
162
  m->set_bottom_left( w + get_margin(), discard->top() + get_margin() );
 
163
 
 
164
  const double r = m->width() / m->height();
 
165
  m->set_height( score->top() - m->bottom() );
 
166
  m->set_width( r * m->height()) ;
 
167
 
 
168
  discard->set_right( m->right() );
 
169
} // frame_level_score::create_no_new_record_controls()
 
170
 
 
171
/*----------------------------------------------------------------------------*/
 
172
/**
 
173
 * \brief Create the text input control.
 
174
 */
 
175
bear::gui::visual_component* ptb::frame_level_score::create_input()
 
176
{
 
177
  m_text =
 
178
    new bear::gui::text_input
 
179
    ( &get_content(), get_font(), claw::graphic::red_pixel );
 
180
 
 
181
  m_text->set_enter_callback
 
182
    ( bear::gui::callback_function_maker
 
183
      ( boost::bind( &frame_level_score::on_save_button, this ) ) );
 
184
  m_text->set_width(500);
 
185
  set_borders_down(*m_text);
 
186
  insert_control(*m_text);
 
187
 
 
188
  return m_text;
 
189
} // frame_level_score::create_input()
 
190
 
 
191
/*----------------------------------------------------------------------------*/
 
192
/**
 
193
 * \brief Create the "discard" button.
 
194
 */
 
195
bear::gui::visual_component* ptb::frame_level_score::create_discard_button()
 
196
{
 
197
  bear::gui::button* result =
 
198
    new bear::gui::button
 
199
    ( &get_content(), get_font(), gettext("Discard"),
 
200
      bear::gui::callback_function_maker
 
201
      ( boost::bind( &frame_level_score::close_window, this ) ) );
 
202
 
 
203
  result->set_margin( get_margin() / 2 );
 
204
  set_borders_up(*result);
 
205
  insert_control(*result);
 
206
 
 
207
  return result;
 
208
} // frame_level_score::create_discard_button()
 
209
 
 
210
/*----------------------------------------------------------------------------*/
 
211
/**
 
212
 * \brief Create the "save" button.
 
213
 */
 
214
bear::gui::visual_component* ptb::frame_level_score::create_save_button()
 
215
{
 
216
  bear::gui::button* result =
 
217
    new bear::gui::button
 
218
    ( &get_content(), get_font(), gettext("Save"),
 
219
      bear::gui::callback_function_maker
 
220
      ( boost::bind( &frame_level_score::on_save_button, this ) ) );
 
221
 
 
222
  result->set_margin( get_margin() / 2 );
 
223
  set_borders_up(*result);
 
224
  insert_control(*result);
 
225
 
 
226
  return result;
 
227
} // frame_level_score::create_save_button()
 
228
 
 
229
/*----------------------------------------------------------------------------*/
 
230
/**
 
231
 * \brief Create the medal picture.
 
232
 * \param medal The type of the medal.
 
233
 */
 
234
bear::gui::visual_component*
 
235
ptb::frame_level_score::create_medal( const std::string& medal )
 
236
{
 
237
  return new bear::gui::picture
 
238
    ( &get_content(), get_layer().get_level().get_globals().auto_sprite
 
239
      ( "gfx/mini-game/medal.png", medal ) );
 
240
} // frame_level_score::create_medal()
 
241
 
 
242
/*----------------------------------------------------------------------------*/
 
243
/**
 
244
 * \brief Create the controls displaying the score
 
245
 * \param format How to format the value of the score.
 
246
 */
 
247
bear::gui::visual_component*
 
248
ptb::frame_level_score::create_score( const std::string& format )
 
249
{
 
250
  bear::gui::static_text* result =
 
251
    new bear::gui::static_text( &get_content(), get_font() );
 
252
 
 
253
  const score_table::entry e(m_score);
 
254
  result->set_auto_size(true);
 
255
  result->set_text( e.format(gettext(format.c_str())) );
 
256
 
 
257
  return result;
 
258
} // frame_level_score::create_score()
 
259
 
 
260
/*----------------------------------------------------------------------------*/
 
261
/**
 
262
 * \brief Exit.
 
263
 */
 
264
bool ptb::frame_level_score::on_close()
 
265
{
 
266
  if ( !m_next_level.empty() )
 
267
    bear::engine::game::get_instance().set_waiting_level(m_next_level);
 
268
 
 
269
  player* p = util::find_player( get_layer().get_level().get_globals(), 1 );
 
270
 
 
271
  if ( p != NULL )
 
272
    p->set_marionette(false);
 
273
 
 
274
  p = util::find_player( get_layer().get_level().get_globals(), 2 );
 
275
 
 
276
  if ( p != NULL )
 
277
    p->set_marionette(false);
 
278
 
 
279
  return true;
 
280
} // frame_level_score::on_close()
 
281
 
 
282
/*----------------------------------------------------------------------------*/
 
283
/**
 
284
 * \brief Action done when the "save" button is pressed.
 
285
 */
 
286
void ptb::frame_level_score::on_save_button()
 
287
{
 
288
  CLAW_PRECOND( m_new_record );
 
289
 
 
290
  std::string text( m_text->get_text() );
 
291
  claw::text::trim( text );
 
292
 
 
293
  m_score_table.insert( text, m_score );
 
294
  m_score_table.save();
 
295
 
 
296
  close_window();
 
297
} // frame_level_score::on_save_button()