Merge lp:~widelands-dev/widelands/save_dialog_improvements into lp:widelands

Proposed by cghislai
Status: Merged
Merged at revision: 6624
Proposed branch: lp:~widelands-dev/widelands/save_dialog_improvements
Merge into: lp:widelands
Diff against target: 482 lines (+135/-38)
11 files modified
src/game_io/game_preload_data_packet.cc (+20/-7)
src/logic/game.cc (+12/-6)
src/logic/game.h (+2/-2)
src/save_handler.cc (+5/-5)
src/save_handler.h (+11/-8)
src/ui_fsmenu/loadgame.cc (+17/-2)
src/ui_fsmenu/loadgame.h (+3/-0)
src/ui_fsmenu/loadreplay.cc (+26/-2)
src/ui_fsmenu/loadreplay.h (+3/-0)
src/wui/game_main_menu_save_game.cc (+33/-5)
src/wui/game_main_menu_save_game.h (+3/-1)
To merge this branch: bzr merge lp:~widelands-dev/widelands/save_dialog_improvements
Reviewer Review Type Date Requested Status
cghislai (community) Approve
SirVer Needs Fixing
Review via email: mp+174566@code.launchpad.net

Description of the change

I added extra info already preloaded in the various io dialogs : save game, load game and load replay.

I also added a field in the game SaveHandler to remember a loaded savegame filename. When opening the save dialog, the entries are parsed and the corresponding filename is selected. Otherwise, no selection is performed and the field is left blank.

To retrieve the localized string for the win condition, the scripts are loaded in the dialog constructors (or on each selection in for replays), and run on each selection. I am not sure if they are cached or not. Code has been ported from the launchSGP dialog.

To post a comment you must log in.
Revision history for this message
SirVer (sirver) wrote :

I did a review in r6620. There is some code duplication which should be factored into a method and a bunch of comments. As before just grep() for #cghislai to find all comments or look at the diff.

review: Needs Fixing
Revision history for this message
cghislai (charlyghislain) wrote :

Should be ready if not bad design decision have been made

Revision history for this message
SirVer (sirver) wrote :

I had one more issue and proposed a fix for it in my latest revision. Can you have a quick look and say if it is fine?

lgtm otherwise - I will merge as soon as I hear back from you.

Revision history for this message
cghislai (charlyghislain) wrote :

Yep looks good to me.
And sorry for the braquets includes, i will double check next commits

review: Approve
Revision history for this message
SirVer (sirver) wrote :

Thanks and np! I enjoy your contributions very much :)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/game_io/game_preload_data_packet.cc'
2--- src/game_io/game_preload_data_packet.cc 2013-02-10 19:36:24 +0000
3+++ src/game_io/game_preload_data_packet.cc 2013-07-14 17:07:28 +0000
4@@ -24,6 +24,7 @@
5 #include "wui/interactive_player.h"
6 #include "logic/map.h"
7 #include "profile/profile.h"
8+#include "scripting/scripting.h"
9
10 namespace Widelands {
11
12@@ -33,13 +34,14 @@
13
14
15 void Game_Preload_Data_Packet::Read
16- (FileSystem & fs, Game &, Map_Map_Object_Loader * const)
17+ (FileSystem & fs, Game & game, Map_Map_Object_Loader * const)
18 {
19 try {
20 Profile prof;
21 prof.read("preload", 0, fs);
22 Section & s = prof.get_safe_section("global");
23 int32_t const packet_version = s.get_int("packet_version");
24+ std::string win_condition = "";
25 if (1 <= packet_version && packet_version <= CURRENT_PACKET_VERSION) {
26 m_gametime = s.get_safe_int ("gametime");
27 m_mapname = s.get_safe_string("mapname");
28@@ -48,19 +50,30 @@
29 m_background = s.get_safe_string("background");
30 m_player_nr = s.get_safe_int ("player_nr");
31 if (packet_version >= 3)
32- m_win_condition = s.get_safe_string("win_condition");
33+ win_condition = s.get_safe_string("win_condition");
34 else
35- m_win_condition = "00_endless_game";
36+ win_condition = "00_endless_game";
37 } else {
38 m_background = "pics/progress.png";
39 // Of course this is wrong, but at least player 1 is always in game
40 // so widelands won't crash with this setting.
41 m_player_nr = 1;
42- m_win_condition = "00_endless_game";
43- }
44- } else
45+ win_condition = "00_endless_game";
46+ }
47+ // Get win condition string
48+ try {
49+ boost::shared_ptr<LuaTable> t = game.lua().run_script
50+ ("win_conditions", win_condition);
51+ m_win_condition = t->get_string("name");
52+ } catch (LuaScriptNotExistingError &) {
53+ m_win_condition = _("Scenario");
54+ } catch (LuaTableKeyError &) {
55+ m_win_condition = win_condition;
56+ }
57+ } else {
58 throw game_data_error
59 (_("unknown/unhandled version %i"), packet_version);
60+ }
61 } catch (const _wexception & e) {
62 throw game_data_error(_("preload: %s"), e.what());
63 }
64@@ -101,7 +114,7 @@
65 bg = map.get_world_name();
66 s.set_string("background", bg);
67
68- s.set_string("win_condition", game.get_win_condition_string());
69+ s.set_string("win_condition", game.get_win_condition_displayname());
70
71 prof.write("preload", false, fs);
72 }
73
74=== modified file 'src/logic/game.cc'
75--- src/logic/game.cc 2013-07-08 03:35:09 +0000
76+++ src/logic/game.cc 2013-07-14 17:07:28 +0000
77@@ -136,8 +136,10 @@
78 m_state (gs_notrunning),
79 m_cmdqueue (*this),
80 m_replaywriter (0),
81- m_win_condition_string("not_set")
82+ m_win_condition_displayname("not_set")
83 {
84+ // Preload win_conditions as they are displayed in UI
85+ lua().register_scripts(*g_fs, "win_conditions", "scripting/win_conditions");
86 }
87
88 Game::~Game()
89@@ -331,10 +333,11 @@
90
91 // Check for win_conditions
92 if (!settings.scenario) {
93- m_win_condition_string = settings.win_condition;
94- LuaCoroutine * cr = lua().run_script
95- (*g_fs, "scripting/win_conditions/" + settings.win_condition + ".lua", "win_conditions")
96- ->get_coroutine("func");
97+ boost::shared_ptr<LuaTable> table
98+ (lua().run_script
99+ (*g_fs, "scripting/win_conditions/" + settings.win_condition + ".lua", "win_conditions"));
100+ m_win_condition_displayname = table->get_string("name");
101+ LuaCoroutine * cr = table->get_coroutine("func");
102 enqueue_command(new Cmd_LuaCoroutine(get_gametime() + 100, cr));
103 }
104 }
105@@ -364,7 +367,7 @@
106
107 Widelands::Game_Preload_Data_Packet gpdp;
108 gl.preload_game(gpdp);
109- m_win_condition_string = gpdp.get_win_condition();
110+ m_win_condition_displayname = gpdp.get_win_condition();
111 if (loaderUI) {
112 std::string background(gpdp.get_background());
113 loaderUI->set_background(background);
114@@ -412,6 +415,9 @@
115 gl.load_game();
116 }
117
118+ // Store the filename for further saves
119+ save_handler().set_current_filename(filename);
120+
121 set_game_controller(GameController::createSinglePlayer(*this, true, player_nr));
122 try {
123 bool const result = run(&loaderUI, Loaded);
124
125=== modified file 'src/logic/game.h'
126--- src/logic/game.h 2013-07-08 03:35:09 +0000
127+++ src/logic/game.h 2013-07-14 17:07:28 +0000
128@@ -189,7 +189,7 @@
129
130 void sample_statistics();
131
132- const std::string & get_win_condition_string() {return m_win_condition_string;}
133+ const std::string & get_win_condition_displayname() {return m_win_condition_displayname;}
134
135 private:
136 void SyncReset();
137@@ -254,7 +254,7 @@
138 General_Stats_vector m_general_stats;
139
140 /// For save games and statistics generation
141- std::string m_win_condition_string;
142+ std::string m_win_condition_displayname;
143 };
144
145 inline Coords Game::random_location(Coords location, uint8_t radius) {
146
147=== modified file 'src/save_handler.cc'
148--- src/save_handler.cc 2013-02-25 17:03:31 +0000
149+++ src/save_handler.cc 2013-07-14 17:07:28 +0000
150@@ -47,7 +47,7 @@
151 if (autosaveInterval <= 0)
152 return; // no autosave requested
153
154- int32_t const elapsed = (realtime - m_lastSaveTime) / 1000;
155+ int32_t const elapsed = (realtime - m_last_saved_time) / 1000;
156 if (elapsed < autosaveInterval)
157 return;
158
159@@ -79,7 +79,7 @@
160 g_fs->Rename(backup_filename, complete_filename);
161 }
162 // Wait 30 seconds until next save try
163- m_lastSaveTime = m_lastSaveTime + 30000;
164+ m_last_saved_time = m_last_saved_time + 30000;
165 return;
166 } else {
167 // if backup file was created, time to remove it
168@@ -87,7 +87,7 @@
169 g_fs->Unlink(backup_filename);
170 }
171
172- log("Autosave: save took %d ms\n", m_lastSaveTime - realtime);
173+ log("Autosave: save took %d ms\n", m_last_saved_time - realtime);
174 }
175
176 /**
177@@ -97,7 +97,7 @@
178 if (m_initialized)
179 return;
180
181- m_lastSaveTime = currenttime;
182+ m_last_saved_time = currenttime;
183 log("Autosave: initialized\n");
184 m_initialized = true;
185 }
186@@ -162,7 +162,7 @@
187 }
188
189 if (result)
190- m_lastSaveTime = WLApplication::get()->get_time();
191+ m_last_saved_time = WLApplication::get()->get_time();
192
193 return result;
194 }
195
196=== modified file 'src/save_handler.h'
197--- src/save_handler.h 2013-02-10 19:36:24 +0000
198+++ src/save_handler.h 2013-07-14 17:07:28 +0000
199@@ -31,15 +31,8 @@
200 #define DEFAULT_AUTOSAVE_INTERVAL 15
201
202 class SaveHandler {
203- int32_t m_lastSaveTime;
204- bool m_initialized;
205- bool m_allow_autosaving;
206-
207- void initialize(int32_t currenttime);
208-
209-
210 public:
211- SaveHandler() : m_lastSaveTime(0), m_initialized(false), m_allow_autosaving(true) {}
212+ SaveHandler() : m_last_saved_time(0), m_initialized(false), m_allow_autosaving(true) {}
213 void think(Widelands::Game &, int32_t currenttime);
214 std::string create_file_name(std::string dir, std::string filename);
215 bool save_game
216@@ -48,8 +41,18 @@
217 std::string * error = 0);
218
219 static std::string get_base_dir() {return "save";}
220+ const std::string get_cur_filename() {return m_current_filename;}
221+ void set_current_filename(std::string filename) {m_current_filename = filename;}
222 void set_allow_autosaving(bool t) {m_allow_autosaving = t;}
223 bool get_allow_autosaving() {return m_allow_autosaving;}
224+
225+private:
226+ int32_t m_last_saved_time;
227+ bool m_initialized;
228+ bool m_allow_autosaving;
229+ std::string m_current_filename;
230+
231+ void initialize(int32_t currenttime);
232 };
233
234 #endif
235
236=== modified file 'src/ui_fsmenu/loadgame.cc'
237--- src/ui_fsmenu/loadgame.cc 2013-02-21 19:02:21 +0000
238+++ src/ui_fsmenu/loadgame.cc 2013-07-14 17:07:28 +0000
239@@ -19,10 +19,10 @@
240
241 #include "loadgame.h"
242
243+#include "game_io/game_loader.h"
244+#include "game_io/game_preload_data_packet.h"
245 #include "gamecontroller.h"
246 #include "gamesettings.h"
247-#include "game_io/game_loader.h"
248-#include "game_io/game_preload_data_packet.h"
249 #include "graphic/graphic.h"
250 #include "i18n.h"
251 #include "io/filesystem/layered_filesystem.h"
252@@ -82,6 +82,14 @@
253 get_w() * 7 / 10, get_h() * 3 / 8,
254 _("Gametime:"), UI::Align_Right),
255 m_tagametime(this, get_w() * 71 / 100, get_h() * 3 / 8),
256+ m_label_players
257+ (this,
258+ get_w() * 7 / 10, get_h() * 41 / 100,
259+ _("Players:"), UI::Align_Right),
260+ m_ta_players
261+ (this, get_w() * 71 / 100, get_h() * 41 / 100),
262+ m_ta_win_condition
263+ (this, get_w() * 71 / 100, get_h() * 9 / 20),
264
265 m_settings(gsp),
266 m_ctrl(gc)
267@@ -101,6 +109,9 @@
268 m_tamapname .set_font(m_fn, m_fs, UI_FONT_CLR_FG);
269 m_label_gametime.set_font(m_fn, m_fs, UI_FONT_CLR_FG);
270 m_tagametime .set_font(m_fn, m_fs, UI_FONT_CLR_FG);
271+ m_label_players .set_font(m_fn, m_fs, UI_FONT_CLR_FG);
272+ m_ta_players .set_font(m_fn, m_fs, UI_FONT_CLR_FG);
273+ m_ta_win_condition.set_font(m_fn, m_fs, UI_FONT_CLR_FG);
274 m_list .set_font(m_fn, m_fs);
275 m_list.selected.connect(boost::bind(&Fullscreen_Menu_LoadGame::map_selected, this, _1));
276 m_list.double_clicked.connect(boost::bind(&Fullscreen_Menu_LoadGame::double_clicked, this, _1));
277@@ -201,6 +212,10 @@
278
279 sprintf(buf, "%02i:%02i", hours, minutes);
280 m_tagametime.set_text(buf);
281+
282+ sprintf(buf, "%i", gpdp.get_player_nr());
283+ m_ta_players.set_text(buf);
284+ m_ta_win_condition.set_text(gpdp.get_win_condition());
285 } else {
286 no_selection();
287 }
288
289=== modified file 'src/ui_fsmenu/loadgame.h'
290--- src/ui_fsmenu/loadgame.h 2012-12-31 11:21:15 +0000
291+++ src/ui_fsmenu/loadgame.h 2013-07-14 17:07:28 +0000
292@@ -73,6 +73,9 @@
293 UI::Textarea m_tamapname;
294 UI::Textarea m_label_gametime;
295 UI::Textarea m_tagametime;
296+ UI::Textarea m_label_players;
297+ UI::Textarea m_ta_players;
298+ UI::Textarea m_ta_win_condition;
299 std::string m_filename;
300
301 filenameset_t m_gamefiles;
302
303=== modified file 'src/ui_fsmenu/loadreplay.cc'
304--- src/ui_fsmenu/loadreplay.cc 2013-02-21 19:02:21 +0000
305+++ src/ui_fsmenu/loadreplay.cc 2013-07-14 17:07:28 +0000
306@@ -73,7 +73,15 @@
307 (this,
308 get_w() * 7 / 10, get_h() * 3 / 8,
309 _("Gametime:"), UI::Align_Right),
310- m_tagametime(this, get_w() * 71 / 100, get_h() * 3 / 8)
311+ m_tagametime(this, get_w() * 71 / 100, get_h() * 3 / 8),
312+ m_label_players
313+ (this,
314+ get_w() * 7 / 10, get_h() * 41 / 100,
315+ _("Players:"), UI::Align_Right),
316+ m_ta_players
317+ (this, get_w() * 71 / 100, get_h() * 41 / 100),
318+ m_ta_win_condition
319+ (this, get_w() * 71 / 100, get_h() * 9 / 20)
320 {
321 m_back.sigclicked.connect(boost::bind(&Fullscreen_Menu_LoadReplay::end_modal, boost::ref(*this), 0));
322 m_ok.sigclicked.connect(boost::bind(&Fullscreen_Menu_LoadReplay::clicked_ok, boost::ref(*this)));
323@@ -81,13 +89,24 @@
324 (boost::bind
325 (&Fullscreen_Menu_LoadReplay::clicked_delete, boost::ref(*this)));
326
327- m_title.set_textstyle(ts_big());
328 m_list.set_font(ui_fn(), fs_small());
329 m_list.selected.connect(boost::bind(&Fullscreen_Menu_LoadReplay::replay_selected, this, _1));
330 m_list.double_clicked.connect
331 (boost::bind(&Fullscreen_Menu_LoadReplay::double_clicked, this, _1));
332+
333+ m_title .set_font(ui_fn(), fs_big(), UI_FONT_CLR_FG);
334+ m_label_mapname .set_font(ui_fn(), fs_small(), UI_FONT_CLR_FG);
335+ m_tamapname .set_font(ui_fn(), fs_small(), UI_FONT_CLR_FG);
336+ m_label_gametime.set_font(ui_fn(), fs_small(), UI_FONT_CLR_FG);
337+ m_tagametime .set_font(ui_fn(), fs_small(), UI_FONT_CLR_FG);
338+ m_label_players .set_font(ui_fn(), fs_small(), UI_FONT_CLR_FG);
339+ m_ta_players .set_font(ui_fn(), fs_small(), UI_FONT_CLR_FG);
340+ m_ta_win_condition.set_font(ui_fn(), fs_small(), UI_FONT_CLR_FG);
341+ m_list .set_font(ui_fn(), fs_small());
342 m_back.set_font(font_small());
343 m_ok.set_font(font_small());
344+ m_delete.set_font(font_small());
345+
346 fill_list();
347 }
348
349@@ -176,6 +195,11 @@
350
351 sprintf(buf, "%02i:%02i", hours, minutes);
352 m_tagametime.set_text(buf);
353+
354+ sprintf(buf, "%i", gpdp.get_player_nr());
355+ m_ta_players.set_text(buf);
356+
357+ m_ta_win_condition.set_text(gpdp.get_win_condition());
358 } else {
359 no_selection();
360 }
361
362=== modified file 'src/ui_fsmenu/loadreplay.h'
363--- src/ui_fsmenu/loadreplay.h 2013-02-10 19:36:24 +0000
364+++ src/ui_fsmenu/loadreplay.h 2013-07-14 17:07:28 +0000
365@@ -58,6 +58,9 @@
366 UI::Textarea m_tamapname;
367 UI::Textarea m_label_gametime;
368 UI::Textarea m_tagametime;
369+ UI::Textarea m_label_players;
370+ UI::Textarea m_ta_players;
371+ UI::Textarea m_ta_win_condition;
372 std::string m_filename;
373 };
374
375
376=== modified file 'src/wui/game_main_menu_save_game.cc'
377--- src/wui/game_main_menu_save_game.cc 2013-03-01 23:12:08 +0000
378+++ src/wui/game_main_menu_save_game.cc 2013-07-14 17:07:28 +0000
379@@ -19,17 +19,20 @@
380
381 #include "game_main_menu_save_game.h"
382
383-#include "io/filesystem/filesystem.h"
384+#include <boost/format.hpp>
385+#include <libintl.h>
386+
387 #include "constants.h"
388-#include "logic/game.h"
389 #include "game_io/game_loader.h"
390 #include "game_io/game_preload_data_packet.h"
391 #include "game_io/game_saver.h"
392+#include "i18n.h"
393 #include "interactive_gamebase.h"
394+#include "io/filesystem/filesystem.h"
395 #include "io/filesystem/layered_filesystem.h"
396+#include "logic/game.h"
397 #include "profile/profile.h"
398
399-#include <boost/format.hpp>
400 using boost::format;
401
402 Interactive_GameBase & Game_Main_Menu_Save_Game::igbase() {
403@@ -67,6 +70,12 @@
404 (this, DESCRIPTION_X, 45, 0, 20, _("Game Time: "), UI::Align_CenterLeft),
405 m_gametime
406 (this, DESCRIPTION_X, 60, 0, 20, " ", UI::Align_CenterLeft),
407+ m_players_label
408+ (this, DESCRIPTION_X, 85, 0, 20, " ", UI::Align_CenterLeft),
409+ m_win_condition_label
410+ (this, DESCRIPTION_X, 110, 0, 20, _("Win condition: "), UI::Align_CenterLeft),
411+ m_win_condition
412+ (this, DESCRIPTION_X, 125, 0, 20, " ", UI::Align_CenterLeft),
413 m_curdir(SaveHandler::get_base_dir())
414 {
415 m_editbox =
416@@ -110,6 +119,11 @@
417 center_to_parent();
418 move_to_top();
419
420+ std::string cur_filename = parent.game().save_handler().get_cur_filename();
421+ if (!cur_filename.empty()) {
422+ select_by_name(cur_filename);
423+ }
424+
425 m_editbox->focus();
426 }
427
428@@ -143,6 +157,12 @@
429 _("%02ud%02uh%02u'%02u\"%03u"),
430 days, hours, minutes, seconds, gametime);
431 m_gametime.set_text(buf);
432+
433+ sprintf
434+ (buf, "%i %s", gpdp.get_player_nr(),
435+ ngettext(_("player"), _("players"), gpdp.get_player_nr()));
436+ m_players_label.set_text(buf);
437+ m_win_condition.set_text(gpdp.get_win_condition());
438 }
439
440 /**
441@@ -177,9 +197,17 @@
442 m_ls.add(FileSystem::FS_FilenameWoExt(name).c_str(), name);
443 } catch (const _wexception &) {} // we simply skip illegal entries
444 }
445+}
446
447- if (m_ls.size())
448- m_ls.select(0);
449+void Game_Main_Menu_Save_Game::select_by_name(std::string name)
450+{
451+ for (uint idx = 0; idx < m_ls.size(); idx++) {
452+ const std::string val = m_ls[idx];
453+ if (name == val) {
454+ m_ls.select(idx);
455+ return;
456+ }
457+ }
458 }
459
460 /*
461
462=== modified file 'src/wui/game_main_menu_save_game.h'
463--- src/wui/game_main_menu_save_game.h 2013-01-20 21:27:20 +0000
464+++ src/wui/game_main_menu_save_game.h 2013-07-14 17:07:28 +0000
465@@ -40,6 +40,7 @@
466 (Interactive_GameBase &, UI::UniqueWindow::Registry & registry);
467
468 void fill_list();
469+ void select_by_name(std::string name);
470 private:
471 Interactive_GameBase & igbase();
472 void die() {UI::UniqueWindow::die();}
473@@ -53,7 +54,8 @@
474
475 UI::Listselect<std::string> m_ls;
476 UI::EditBox * m_editbox;
477- UI::Textarea m_name_label, m_name, m_gametime_label, m_gametime;
478+ UI::Textarea m_name_label, m_name, m_gametime_label, m_gametime, m_players_label,
479+ m_win_condition_label, m_win_condition;
480 UI::Button * m_button_ok;
481 std::string m_curdir;
482 std::string m_parentdir;

Subscribers

People subscribed via source and target branches

to status/vote changes: