Replacing one savegame dialog with the new message dialog class.

This also shows work in progress on the load-game dialog. It's not
really functional yet but there is something to see already so i am
comitting this already. If you are curious for the dialog, remove the
comments in savegame.cpp (line 336/337).
This commit is contained in:
Jörg Hinrichs 2009-07-06 22:08:06 +00:00
parent 51357cc9e8
commit b2fa008a80
10 changed files with 307 additions and 43 deletions

View file

@ -93,8 +93,9 @@
[/label_definition]
#enddef
{_GUI_DEFINITION "default" "default label" DEFAULT () DEFAULT }
{_GUI_DEFINITION "title" "label used for titles" TITLE "bold" TITLE }
{_GUI_DEFINITION "default" "default label" DEFAULT () DEFAULT }
{_GUI_DEFINITION "title" "label used for titles" TITLE "bold" TITLE }
{_GUI_DEFINITION "default_small" "default, small font size" SMALL () DEFAULT }
#undef _GUI_DEFINITION
#undef _GUI_RESOLUTION

View file

@ -291,6 +291,7 @@ SET(wesnoth-main_SRC
gui/dialogs/addon_list.cpp
gui/dialogs/campaign_selection.cpp
gui/dialogs/dialog.cpp
gui/dialogs/game_load.cpp
gui/dialogs/game_save.cpp
gui/dialogs/language_selection.cpp
gui/dialogs/lobby_main.cpp

View file

@ -272,6 +272,7 @@ wesnoth_sources = Split("""
gui/dialogs/addon_list.cpp
gui/dialogs/campaign_selection.cpp
gui/dialogs/dialog.cpp
gui/dialogs/game_load.cpp
gui/dialogs/game_save.cpp
gui/dialogs/language_selection.cpp
gui/dialogs/lobby_main.cpp

View file

@ -0,0 +1,231 @@
/* $Id$ */
/*
Copyright (C) 2008 - 2009 by Jörg Hinrichs <joerg.hinrichs@alice-dsl.de>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
or at your option any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#define GETTEXT_DOMAIN "wesnoth-lib"
#include "gui/dialogs/game_load.hpp"
#include "foreach.hpp"
#include "formula_string_utils.hpp"
#include "gettext.hpp"
#include "gui/auxiliary/log.hpp"
#include "gui/dialogs/helper.hpp"
#include "gui/widgets/label.hpp"
#include "gui/widgets/listbox.hpp"
#include "gui/widgets/minimap.hpp"
#include "gui/widgets/window.hpp"
#include "language.hpp"
#include "marked-up_text.hpp"
namespace gui2 {
/*WIKI
* @page = GUIWindowDefinitionWML
* @order = 2_game_load
*
* == Load a game ==
*
* This shows the dialog to select and load a savegame file.
*
* @start_table = container
* txtFilename_ (text_box) The name of the savefile.
* @end_table
*/
tgame_load::tgame_load(const config& cache_config)
: filename_(),
cache_config_(cache_config)
{
}
twindow* tgame_load::build_window(CVideo& video)
{
return build(video, get_id(GAME_LOAD));
}
void tgame_load::pre_show(CVideo& /*video*/, twindow& window)
{
tminimap* minimap = dynamic_cast<tminimap*>(window.find_widget("minimap", false));
VALIDATE(minimap, missing_widget("minimap"));
minimap->set_config(&cache_config_);
tlistbox* list = dynamic_cast<tlistbox*>(window.find_widget("savegame_list", false));
VALIDATE(list, missing_widget("savegame_list"));
window.keyboard_capture(list);
list->set_callback_value_change(dialog_callback<tgame_load, &tgame_load::list_item_clicked>);
{
cursor::setter cur(cursor::WAIT);
games_ = savegame_manager::get_saves_list();
}
foreach(const save_info game, games_) {
std::map<std::string, string_map> data;
string_map item;
item["label"] = game.name;
data.insert(std::make_pair("filename", item));
item["label"] = format_time_summary(game.time_modified);
data.insert(std::make_pair("date", item));
list->add_row(data);
}
}
void tgame_load::list_item_clicked(twindow& window){
tlistbox* list = dynamic_cast<tlistbox*>(window.find_widget("savegame_list", false));
VALIDATE(list, missing_widget("savegame_list"));
config summary;
std::string dummy;
save_info game = games_[list->get_selected_row()];
try {
savegame_manager::load_summary(game.name, summary, &dummy);
} catch(game::load_game_failed&) {
summary["corrupt"] = "yes";
}
tminimap* minimap = dynamic_cast<tminimap*>(window.find_widget("minimap", false));
VALIDATE(minimap, missing_widget("minimap"));
minimap->set_map_data(summary["map_data"]);
tlabel* scenario = dynamic_cast<tlabel*>(window.find_widget("lblScenario", false));
scenario->set_label(game.name);
std::stringstream str;
char time_buf[256] = {0};
tm* tm_l = localtime(&game.time_modified);
if (tm_l) {
const size_t res = strftime(time_buf,sizeof(time_buf),_("%a %b %d %H:%M %Y"),tm_l);
if(res == 0) {
time_buf[0] = 0;
}
} else {
LOG_GUI_G << "localtime() returned null for time " << game.time_modified << ", save " << game.name;
}
str << time_buf;
const std::string& campaign_type = summary["campaign_type"];
if(utils::string_bool(summary["corrupt"], false)) {
str << "\n" << _("#(Invalid)");
} else if (!campaign_type.empty()) {
str << "\n";
if(campaign_type == "scenario") {
const std::string campaign_id = summary["campaign"];
const config *campaign = NULL;
if (!campaign_id.empty()) {
if (const config &c = cache_config_.find_child("campaign", "id", campaign_id))
campaign = &c;
}
utils::string_map symbols;
if (campaign != NULL) {
symbols["campaign_name"] = (*campaign)["name"];
} else {
// Fallback to nontranslatable campaign id.
symbols["campaign_name"] = "(" + campaign_id + ")";
}
str << vgettext("Campaign: $campaign_name", symbols);
// Display internal id for debug purposes if we didn't above
if (game_config::debug && (campaign != NULL)) {
str << '\n' << "(" << campaign_id << ")";
}
} else if(campaign_type == "multiplayer") {
str << _("Multiplayer");
} else if(campaign_type == "tutorial") {
str << _("Tutorial");
} else {
str << campaign_type;
}
str << "\n";
if(utils::string_bool(summary["replay"], false) && !utils::string_bool(summary["snapshot"], true)) {
str << _("replay");
} else if (!summary["turn"].empty()) {
str << _("Turn") << " " << summary["turn"];
} else {
str << _("Scenario Start");
}
str << "\n" << _("Difficulty: ") << string_table[summary["difficulty"]];
if(!summary["version"].empty()) {
str << "\n" << _("Version: ") << summary["version"];
}
}
tlabel* lblSummary = dynamic_cast<tlabel*>(window.find_widget("lblSummary", false));
lblSummary->set_label(str.str());
// FIXME: Find a better way to change the label width
window.invalidate_layout();
}
void tgame_load::post_show(twindow& window)
{
//filename_ = txtFilename_->get_widget_value(window);
}
std::string tgame_load::format_time_summary(time_t t)
{
time_t curtime = time(NULL);
const struct tm* timeptr = localtime(&curtime);
if(timeptr == NULL) {
return "";
}
const struct tm current_time = *timeptr;
timeptr = localtime(&t);
if(timeptr == NULL) {
return "";
}
const struct tm save_time = *timeptr;
const char* format_string = _("%b %d %y");
if(current_time.tm_year == save_time.tm_year) {
const int days_apart = current_time.tm_yday - save_time.tm_yday;
if(days_apart == 0) {
// save is from today
format_string = _("%H:%M");
} else if(days_apart > 0 && days_apart <= current_time.tm_wday) {
// save is from this week
format_string = _("%A, %H:%M");
} else {
// save is from current year
format_string = _("%b %d");
}
} else {
// save is from a different year
format_string = _("%b %d %y");
}
char buf[40];
const size_t res = strftime(buf,sizeof(buf),format_string,&save_time);
if(res == 0) {
buf[0] = 0;
}
return buf;
}
} // namespace gui2

View file

@ -0,0 +1,57 @@
/* $Id$ */
/*
Copyright (C) 2008 - 2009 by Jörg Hinrichs <joerg.hinrichs@alice-dsl.de>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
or at your option any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#ifndef GUI_DIALOGS_LOAD_GAME_HPP_INCLUDED
#define GUI_DIALOGS_LOAD_GAME_HPP_INCLUDED
#include "gui/dialogs/dialog.hpp"
#include "gui/widgets/listbox.hpp"
#include "savegame.hpp"
#include "tstring.hpp"
namespace gui2 {
class tgame_load : public tdialog
{
public:
tgame_load(const config& cache_config);
const std::string& filename() const { return filename_; }
protected:
/** Inherited from tdialog. */
void pre_show(CVideo& video, twindow& window);
/** Inherited from tdialog. */
void post_show(twindow& window);
private:
/** Inherited from tdialog. */
twindow* build_window(CVideo& video);
void list_item_clicked(twindow& window);
std::string format_time_summary(time_t t);
//tfield_text* txtFilename_;
std::string filename_;
std::vector<save_info> games_;
const config& cache_config_;
};
}
#endif

View file

@ -102,21 +102,5 @@ void tgame_save_oos::post_show(twindow& window)
ignore_all_ = btnIgnoreAll_->get_widget_value(window);
}
tgame_save_overwrite::tgame_save_overwrite(const std::string& filename)
: filename_(filename)
{}
twindow* tgame_save_overwrite::build_window(CVideo& video)
{
return build(video, get_id(GAME_SAVE_OVERWRITE));
}
void tgame_save_overwrite::pre_show(CVideo& /*video*/, twindow& window)
{
tlabel* lblMessage = dynamic_cast<tlabel*>(window.find_widget("lblMessage", false));
VALIDATE(lblMessage, missing_widget("lblMessage"));
lblMessage->set_label(lblMessage->label() + "\n" + _("Name: ") + filename_);
}
} // namespace gui2

View file

@ -76,22 +76,6 @@ private:
bool ignore_all_;
};
class tgame_save_overwrite : public tdialog
{
public:
tgame_save_overwrite(const std::string& filename);
private:
/** Inherited from tdialog. */
twindow* build_window(CVideo& video);
/** Inherited from tgame_save. */
void pre_show(CVideo& video, twindow& window);
std::string filename_;
};
}
#endif

View file

@ -88,10 +88,10 @@ static void fill_window_types()
window_type_list[MP_CMD_WRAPPER] = "mp_cmd_wrapper";
window_type_list[MP_CREATE_GAME] = "mp_create_game";
window_type_list[TITLE_SCREEN] = "title_screen";
window_type_list[GAME_LOAD] = "game_load";
window_type_list[GAME_SAVE] = "game_save";
window_type_list[GAME_SAVE_MESSAGE] = "game_save_message";
window_type_list[GAME_SAVE_OOS] = "game_save_oos";
window_type_list[GAME_SAVE_OVERWRITE] = "game_save_overwrite";
#ifndef DISABLE_EDITOR2
window_type_list[EDITOR_NEW_MAP] = "editor_new_map";
window_type_list[EDITOR_GENERATE_MAP] = "editor_generate_map";

View file

@ -67,10 +67,10 @@ enum twindow_type {
EDITOR_RESIZE_MAP, /**< Editor resize map dialog. */
EDITOR_SETTINGS, /**< Editor settings dialog. */
#endif
GAME_LOAD, /**< Load game dialog. */
GAME_SAVE, /**< Save game dialog. */
GAME_SAVE_MESSAGE, /**< Save game dialog with additional message. */
GAME_SAVE_OOS, /**< Save game dialog for processing OOS. */
GAME_SAVE_OVERWRITE, /**< Ask for overwriting an existing savegame. */
LOBBY_MAIN, /**< Main MP lobby screen */
COUNT /**<
* The last one to hold the number of items and as

View file

@ -21,6 +21,7 @@
#include "game_end_exceptions.hpp"
#include "game_events.hpp"
#include "gettext.hpp"
#include "gui/dialogs/game_load.hpp"
#include "gui/dialogs/game_save.hpp"
#include "gui/dialogs/message.hpp"
#include "gui/widgets/window.hpp"
@ -331,6 +332,9 @@ void loadgame::show_dialog(bool show_replay, bool cancel_orders)
bool cancel_orders_dialog = cancel_orders;
//FIXME: Integrate the load_game dialog into this class
//something to watch for the curious, but not yet ready to go
//gui2::tgame_load load_dialog(game_config_);
//load_dialog.show(gui_.video());
filename_ = dialogs::load_game_dialog(gui_, game_config_, &show_replay_dialog, &cancel_orders_dialog);
show_replay_ = show_replay_dialog;
@ -401,10 +405,10 @@ void loadgame::check_version_compatibility()
throw load_game_cancelled_exception();
}
const int res = gui::dialog(gui_,"",
_("This save is from a different version of the game. Do you want to try to load it?"),
gui::YES_NO).show();
if(res == 1) {
const int res = gui2::show_message(gui_.video(), "", _("This save is from a different version of the game. Do you want to try to load it?"),
gui2::tmessage::yes_no_buttons);
if(res == gui2::twindow::CANCEL) {
throw load_game_cancelled_exception();
}
}
@ -562,9 +566,10 @@ bool savegame::check_overwrite(CVideo& video)
{
std::string filename = filename_;
if (savegame_manager::save_game_exists(filename, compress_saves_)) {
gui2::tgame_save_overwrite dlg(filename);
dlg.show(video);
return dlg.get_retval() == gui2::twindow::OK;
std::stringstream message;
message << _("Save already exists. Do you want to overwrite it?") << "\n" << _("Name: ") << filename;
int retval = gui2::show_message(video, _("Overwrite?"), message.str(), gui2::tmessage::yes_no_buttons);
return retval == gui2::twindow::OK;
} else {
return true;
}