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 frame_level_score.cpp
26
* \brief Implementation of the ptb::frame_level_score class.
27
* \author Julien Jorge
29
#include "ptb/frame/frame_level_score.hpp"
31
#include "ptb/frame/theme_colors.hpp"
32
#include "ptb/player.hpp"
33
#include "ptb/score_table.hpp"
34
#include "ptb/util.hpp"
36
#include "gui/button.hpp"
37
#include "gui/callback_function.hpp"
38
#include "gui/static_text.hpp"
39
#include "gui/picture.hpp"
41
#include "engine/game.hpp"
42
#include "engine/level.hpp"
43
#include "engine/level_globals.hpp"
45
#include <boost/bind.hpp>
46
#include <claw/string_algorithm.hpp>
49
/*----------------------------------------------------------------------------*/
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.
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))
68
create_controls(medal, format);
70
player* p = util::find_player( get_layer().get_level().get_globals(), 1 );
73
p->set_marionette(true);
75
p = util::find_player( get_layer().get_level().get_globals(), 2 );
78
p->set_marionette(true);
79
} // frame_level_score::frame_level_score()
81
/*----------------------------------------------------------------------------*/
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.
87
void ptb::frame_level_score::create_controls
88
( const std::string& medal, const std::string& format )
91
create_new_record_controls(medal, format);
93
create_no_new_record_controls(medal, format);
96
} // frame_level_score::create_controls()
98
/*----------------------------------------------------------------------------*/
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.
104
void ptb::frame_level_score::create_new_record_controls
105
( const std::string& medal, const std::string& format )
107
CLAW_PRECOND( m_new_record );
109
set_title( gettext("New record") );
111
bear::gui::visual_component* text = create_input();
112
bear::gui::visual_component* discard = create_discard_button();
113
create_save_button();
115
text->set_bottom( discard->top() + get_margin() );
117
bear::gui::visual_component* score = create_score(format);
118
bear::gui::visual_component* m = create_medal(medal);
120
const bear::visual::size_type w( std::max(text->width(), score->width()) );
122
score->set_bottom_left
123
( (w - score->width()) / 2, text->top() + get_margin() );
125
m->set_bottom_left( w + get_margin(), discard->top() + get_margin() );
127
const double r = m->width() / m->height();
128
m->set_height( score->top() - m->bottom() );
129
m->set_width( r * m->height()) ;
131
discard->set_right( m->right() );
132
} // frame_level_score::create_new_record_controls()
134
/*----------------------------------------------------------------------------*/
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.
140
void ptb::frame_level_score::create_no_new_record_controls
141
( const std::string& medal, const std::string& format )
143
CLAW_PRECOND( !m_new_record );
145
set_title( gettext("No new record") );
147
bear::gui::visual_component* discard = create_discard_button();
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);
154
msg->set_auto_size(true);
155
msg->set_text( gettext("You are not in the best scores. Try again!") );
157
const bear::visual::size_type w( std::max(msg->width(), score->width()) );
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() );
164
const double r = m->width() / m->height();
165
m->set_height( score->top() - m->bottom() );
166
m->set_width( r * m->height()) ;
168
discard->set_right( m->right() );
169
} // frame_level_score::create_no_new_record_controls()
171
/*----------------------------------------------------------------------------*/
173
* \brief Create the text input control.
175
bear::gui::visual_component* ptb::frame_level_score::create_input()
178
new bear::gui::text_input
179
( &get_content(), get_font(), claw::graphic::red_pixel );
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);
189
} // frame_level_score::create_input()
191
/*----------------------------------------------------------------------------*/
193
* \brief Create the "discard" button.
195
bear::gui::visual_component* ptb::frame_level_score::create_discard_button()
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 ) ) );
203
result->set_margin( get_margin() / 2 );
204
set_borders_up(*result);
205
insert_control(*result);
208
} // frame_level_score::create_discard_button()
210
/*----------------------------------------------------------------------------*/
212
* \brief Create the "save" button.
214
bear::gui::visual_component* ptb::frame_level_score::create_save_button()
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 ) ) );
222
result->set_margin( get_margin() / 2 );
223
set_borders_up(*result);
224
insert_control(*result);
227
} // frame_level_score::create_save_button()
229
/*----------------------------------------------------------------------------*/
231
* \brief Create the medal picture.
232
* \param medal The type of the medal.
234
bear::gui::visual_component*
235
ptb::frame_level_score::create_medal( const std::string& medal )
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()
242
/*----------------------------------------------------------------------------*/
244
* \brief Create the controls displaying the score
245
* \param format How to format the value of the score.
247
bear::gui::visual_component*
248
ptb::frame_level_score::create_score( const std::string& format )
250
bear::gui::static_text* result =
251
new bear::gui::static_text( &get_content(), get_font() );
253
const score_table::entry e(m_score);
254
result->set_auto_size(true);
255
result->set_text( e.format(gettext(format.c_str())) );
258
} // frame_level_score::create_score()
260
/*----------------------------------------------------------------------------*/
264
bool ptb::frame_level_score::on_close()
266
if ( !m_next_level.empty() )
267
bear::engine::game::get_instance().set_waiting_level(m_next_level);
269
player* p = util::find_player( get_layer().get_level().get_globals(), 1 );
272
p->set_marionette(false);
274
p = util::find_player( get_layer().get_level().get_globals(), 2 );
277
p->set_marionette(false);
280
} // frame_level_score::on_close()
282
/*----------------------------------------------------------------------------*/
284
* \brief Action done when the "save" button is pressed.
286
void ptb::frame_level_score::on_save_button()
288
CLAW_PRECOND( m_new_record );
290
std::string text( m_text->get_text() );
291
claw::text::trim( text );
293
m_score_table.insert( text, m_score );
294
m_score_table.save();
297
} // frame_level_score::on_save_button()