20130313
[gdash.git] / src / framework / replaymenuactivity.cpp
blob7c01d861cdc578df424861281502696a85f3ccd1
1 /*
2 * Copyright (c) 2007-2013, Czirkos Zoltan http://code.google.com/p/gdash/
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include "config.h"
19 #include <glib.h>
20 #include <glib/gi18n.h>
22 #include "framework/commands.hpp"
23 #include "framework/gameactivity.hpp"
24 #include "framework/replaysaveractivity.hpp"
25 #include "framework/showtextactivity.hpp"
26 #include "gfx/screen.hpp"
27 #include "gfx/fontmanager.hpp"
28 #include "cave/gamecontrol.hpp"
29 #include "misc/printf.hpp"
30 #include "misc/util.hpp"
31 #include "cave/cavestored.hpp"
32 #include "cave/caveset.hpp"
33 #include "framework/replaymenuactivity.hpp"
35 ReplayMenuActivity::ReplayMenuActivity(App *app)
36 : Activity(app)
38 lines_per_page = (app->screen->get_height() / app->font_manager->get_line_height()) - 5;
40 /* for all caves */
41 for (unsigned n=0; n<app->caveset->caves.size(); ++n) {
42 CaveStored &cave=app->caveset->cave(n);
44 /* if cave has replays... */
45 if (!cave.replays.empty()) {
46 /* add an empty item as separator */
47 if (!items.empty()) {
48 ReplayItem i = { NULL, NULL };
49 items.push_back(i);
51 /* add cave data */
52 ReplayItem i = { &cave, NULL };
53 items.push_back(i);
54 /* add replays, too */
55 for (std::list<CaveReplay>::iterator rit=cave.replays.begin(); rit!=cave.replays.end(); ++rit) {
56 // .cave unchanged, .replay is the address of the replay
57 i.replay=&*rit;
58 items.push_back(i);
62 current=1;
66 void ReplayMenuActivity::pushed_event() {
67 if (items.empty()) {
68 app->enqueue_command(new PopActivityCommand(app)); // pop myself
69 // TRANSLATORS: 40 chars max
70 app->enqueue_command(new PushActivityCommand(app, new ShowTextActivity(app, _("Replays"), _("No replays for caveset."), SmartPtr<Command>())));
74 void ReplayMenuActivity::redraw_event() {
75 app->clear_screen();
77 int page=current/lines_per_page;
78 // TRANSLATORS: 40 chars max
79 app->title_line(CPrintf(_("Replays, Page %d/%d")) % (page+1) % (items.size()/lines_per_page+1));
80 // TRANSLATORS: 40 chars max
81 app->status_line(_("Space: play F1: keys Esc: exit"));
83 for (unsigned n=0; n<lines_per_page && page*lines_per_page+n<items.size(); n++) {
84 unsigned pos=page*lines_per_page+n;
86 int y = (n+2) * app->font_manager->get_line_height();
88 if (items[pos].cave && !items[pos].replay) {
89 /* no replay pointer: this is a cave, so write its name. */
90 app->set_color(GD_GDASH_WHITE);
91 app->blittext_n(0, y, CPrintf(" %s") % items[pos].cave->name);
93 if (items[pos].replay) {
94 /* level, successful/not, saved/not, player name */
95 app->blittext_n(0, y, CPrintf(" %c%c %c%c %cL%d, %s")
96 % GD_COLOR_INDEX_LIGHTBLUE % (items[pos].replay->saved?GD_CHECKED_BOX_CHAR:GD_UNCHECKED_BOX_CHAR)
97 % (items[pos].replay->success?GD_COLOR_INDEX_GREEN:GD_COLOR_INDEX_RED) % GD_BALL_CHAR
98 % (current==pos ? GD_COLOR_INDEX_YELLOW : GD_COLOR_INDEX_GRAY3)
99 % items[pos].replay->level % items[pos].replay->player_name);
103 app->screen->flip();
107 /* the replay saver thing only works in the sdl version */
108 #ifdef HAVE_SDL
110 class SaveReplayCommand: public Command1Param<std::string> {
111 public:
112 SaveReplayCommand(App *app, CaveStored *cave, CaveReplay *replay)
114 Command1Param<std::string>(app),
115 filename_prefix(p1),
116 cave(cave),
117 replay(replay)
120 private:
121 std::string &filename_prefix;
122 CaveStored *cave;
123 CaveReplay *replay;
124 void execute() {
125 app->enqueue_command(new PushActivityCommand(app, new ReplaySaverActivity(app, cave, replay, filename_prefix)));
129 #endif /* IFDEF HAVE_SDL */
132 void ReplayMenuActivity::keypress_event(KeyCode keycode, int gfxlib_keycode) {
133 /* these keys are active also when there are replays and when there aren't any. */
134 switch (keycode) {
135 case App::F1:
137 char const *help[]={
138 _("Cursor keys"), _("Move"),
139 // TRANSLATORS: play here means playing the replay movie
140 _("Space, Enter"), _("Play"),
141 "I", _("Show replay info"),
142 "", "",
143 // TRANSLATORS: this is for a checkbox which selects if the replays is to be saved with the cave or not.
144 "S", _("Toggle if saved with caves"),
145 /* the replay saver thing only works in the sdl version */
146 #ifdef HAVE_SDL
147 "W", _("Save movie"),
148 #endif /* IFDEF HAVE_SDL */
149 "", "",
150 "ESC", _("Main menu"),
151 NULL
153 // TRANSLATORS: title of the cave replays window
154 app->show_text_and_do_command(_("Replays Help"), help_strings_to_string(help));
156 break;
157 case App::Escape:
158 app->enqueue_command(new PopActivityCommand(app));
159 break;
162 /* if no replays, the keys below should be inactive. so return. */
163 if (items.empty())
164 return;
167 switch (keycode) {
168 case App::Up:
169 do {
170 current = gd_clamp(current-1, 1, items.size()-1);
171 } while (items[current].replay==NULL && current>=1);
172 redraw_event();
173 break;
174 case App::Down:
175 do {
176 current = gd_clamp(current+1, 1, items.size()-1);
177 } while (items[current].replay==NULL && current<items.size());
178 redraw_event();
179 break;
180 case App::PageUp:
181 current = gd_clamp(current-lines_per_page, 0, items.size()-1);
182 redraw_event();
183 break;
184 case App::PageDown:
185 current = gd_clamp(current+lines_per_page, 0, items.size()-1);
186 redraw_event();
187 break;
188 case 'i':
189 case 'I':
191 std::string s;
192 CaveReplay *r = items[current].replay;
193 s += SPrintf("%c%s: %c%s\n") % GD_COLOR_INDEX_WHITE % _("Cave") % GD_COLOR_INDEX_YELLOW % items[current].cave->name;
194 // TRANSLATORS: Cave level (1-5)
195 s += SPrintf("%c%s: %c%d\n") % GD_COLOR_INDEX_WHITE % _("Level") % GD_COLOR_INDEX_YELLOW % r->level;
196 // TRANSLATORS: "player" here means the name of the human player who played the replay
197 s += SPrintf("%c%s: %c%s\n") % GD_COLOR_INDEX_WHITE % _("Player") % GD_COLOR_INDEX_YELLOW % r->player_name;
198 s += SPrintf("%c%s: %c%s\n") % GD_COLOR_INDEX_WHITE % _("Successful") % GD_COLOR_INDEX_LIGHTBLUE % (r->success ? _("yes") : _("no"));
199 s += SPrintf("%c%s: %c%d %s\n") % GD_COLOR_INDEX_WHITE % _("Score") % GD_COLOR_INDEX_LIGHTBLUE % r->score % _("points");
200 s += "\n";
201 if (r->comment != "")
202 s += SPrintf("%c%s: %c%s\n") % GD_COLOR_INDEX_WHITE % _("Comment") % GD_COLOR_INDEX_LIGHTBLUE % r->comment;
203 if (r->duration > 0) {
204 // TRANSLATORS: duration of replay in seconds
205 s += SPrintf("%c%s: %c%ds\n") % GD_COLOR_INDEX_WHITE % _("Duration") % GD_COLOR_INDEX_LIGHTBLUE % r->duration;
207 if (r->date != "")
208 s += SPrintf("%c%s: %c%s\n") % GD_COLOR_INDEX_WHITE % _("Date") % GD_COLOR_INDEX_LIGHTBLUE % r->date;
209 if (r->recorded_with != "")
210 // TRANSLATORS: ie. software by which the replay was recorded.
211 s += SPrintf("%c%s: %c%s\n") % GD_COLOR_INDEX_WHITE % _("Recorded with") % GD_COLOR_INDEX_LIGHTBLUE % r->recorded_with;
213 // TRANSLATORS: title of the window showing data about a particular replay
214 app->show_message(_("Replay Info"), s);
216 break;
217 case 's':
218 case 'S':
219 if (items[current].replay) {
220 items[current].replay->saved=!items[current].replay->saved;
221 app->caveset->edited = true;
222 redraw_event();
224 break;
225 /* the replay saver thing only works in the sdl version */
226 #ifdef HAVE_SDL
227 case 'w':
228 case 'W':
230 std::string prefix = SPrintf("%s%sout") % gd_last_folder % G_DIR_SEPARATOR;
231 // TRANSLATE: the prefix of the name of the output files when saving the replay.
232 app->input_text_and_do_command(_("Output filename prefix"), prefix.c_str(), new SaveReplayCommand(app, items[current].cave, items[current].replay));
234 break;
235 #endif /* IFDEF HAVE_SDL */
236 case ' ':
237 case App::Enter:
238 if (items[current].replay) {
239 GameControl *game = GameControl::new_replay(app->caveset, items[current].cave, items[current].replay);
240 app->enqueue_command(new PushActivityCommand(app, new GameActivity(app, game)));
242 break;