Revert from disk functionality added.

This commit is contained in:
Kristoffer Erlandsson 2004-05-04 20:48:30 +00:00
parent 451757d32e
commit bda20ac65f
8 changed files with 74 additions and 50 deletions

View file

@ -146,6 +146,14 @@ alt=no
shift=no
[/hotkey]
[hotkey]
command="editrevert"
key="r"
ctrl=yes
alt=no
shift=no
[/hotkey]
[resolution]
width=800
height=600
@ -159,7 +167,7 @@ height=600
[menu]
title=main_menu
image=lite
items=editnewmap,editloadmap,undo,redo,editsavemap,editsaveas,togglegrid,editquit,
items=editnewmap,editloadmap,editrevert,undo,redo,editsavemap,editsaveas,togglegrid,editquit,
rect=3,1,107,22
xanchor=fixed
yanchor=fixed

View file

@ -481,6 +481,7 @@ action_editfillselection="Fill Selection"
action_editcut="Cut"
action_editcopy="Copy"
action_editpaste="Paste"
action_editrevert="Revert from Disk"
save_hotkeys_button="Save Hotkeys"
change_hotkey_button="Change Hotkey"
hotkeys_dialog="Hotkey Settings"

View file

@ -285,10 +285,14 @@ void map_editor::edit_new_map() {
}
void map_editor::edit_load_map() {
std::string fn = "";
const std::string map = load_map_dialog(gui_, changed_since_save(), fn);
if (map != "") {
throw new_map_exception(map, fn);
const std::string fn = load_map_dialog(gui_);
if (fn != "") {
const std::string new_map = load_map(fn);
if (new_map != "") {
if (!changed_since_save() || confirm_modification_disposal(gui_)) {
throw new_map_exception(new_map, fn);
}
}
}
}
@ -344,6 +348,41 @@ void map_editor::edit_paste() {
add_undo_action(undo_action);
}
void map_editor::edit_revert() {
const std::string new_map = load_map(filename_);
if (new_map != "") {
if (!changed_since_save() || confirm_modification_disposal(gui_)) {
throw new_map_exception(new_map, filename_);
}
}
}
std::string map_editor::load_map(const std::string filename) {
bool load_successful = true;
std::string msg;
std::string new_map;
if (!file_exists(filename) || is_directory(filename)) {
load_successful = false;
msg = filename + " does not exist or can't be read as a file.";
}
else {
try {
new_map = read_file(filename);
}
catch (io_exception e) {
load_successful = false;
msg = e.what();
}
}
if (!load_successful) {
gui::show_dialog(gui_, NULL, "", std::string("Load failed: ") + msg, gui::OK_ONLY);
return "";
}
else {
return new_map;
}
}
void map_editor::insert_selection_in_clipboard() {
if (selected_hexes_.empty()) {
@ -389,6 +428,7 @@ bool map_editor::can_execute_command(hotkey::HOTKEY_COMMAND command) const {
case hotkey::HOTKEY_EDIT_COPY:
case hotkey::HOTKEY_EDIT_CUT:
case hotkey::HOTKEY_EDIT_PASTE:
case hotkey::HOTKEY_EDIT_REVERT:
return true;
default:
return false;
@ -619,7 +659,6 @@ void map_editor::perform_selection_move() {
void map_editor::draw_terrain(const gamemap::TERRAIN terrain,
const gamemap::location hex) {
++num_operations_since_save_;
redo_stack_.clear();
const gamemap::TERRAIN current_terrain = map_[hex.x][hex.y];
add_undo_action(map_undo_action(current_terrain, terrain, hex));

View file

@ -89,6 +89,10 @@ public:
/// display_confirmation is true. Return false if the save failed.
bool save_map(const std::string filename="",
const bool display_confirmation=true);
/// Load the map from filename. Return the string representation of
/// the map, or the empty string of the load failed.
std::string load_map(const std::string filename);
virtual void handle_event(const SDL_Event &event);
@ -126,6 +130,7 @@ public:
virtual void edit_cut();
virtual void edit_copy();
virtual void edit_paste();
virtual void edit_revert();
virtual bool can_execute_command(hotkey::HOTKEY_COMMAND command) const;

View file

@ -211,10 +211,8 @@ std::string new_map_dialog(display& disp, gamemap::TERRAIN fill_terrain,
}
std::string load_map_dialog(display &disp, bool confirmation_needed,
std::string &loaded_file) {
std::string load_map_dialog(display &disp) {
const std::string system_path = game_config::path + "/data/maps/";
std::vector<std::string> files;
get_files_in_dir(system_path,&files);
files.push_back("Enter Path...");
@ -249,36 +247,7 @@ std::string load_map_dialog(display &disp, bool confirmation_needed,
else {
filename = system_path + files[res];
}
std::string map;
bool load_successful = true;
std::string msg;
if (!file_exists(filename) || is_directory(filename)) {
load_successful = false;
msg = filename + " does not exist or can't be read as a file.";
}
else {
try {
map = read_file(filename);
}
catch (io_exception e) {
load_successful = false;
msg = e.what();
}
}
if (!load_successful) {
gui::show_dialog(disp, NULL, "", std::string("Load failed: ") + msg, gui::OK_ONLY);
return "";
}
else {
if (confirmation_needed
&& !confirm_modification_disposal(disp)) {
return "";
}
loaded_file = filename;
return map;
}
return filename;
}
}

View file

@ -35,14 +35,10 @@ std::string new_map_dialog(display &disp, gamemap::TERRAIN fill_terrain,
bool confirmation_needed, const config &gconfig);
/// Show a dialog where the user may chose a map to load. If
/// confirmation_needed is true, the user will be asked if she wants to
/// continue even though the changes to the current map is lost. Return
/// the string representation of the map that is loaded, or the empty
/// string if none was and the filename. loaded_file will be set to the
/// filename loaded if the load succeeded.
std::string load_map_dialog(display &disp, bool confirmation_needed,
std::string &loaded_file);
/// Show a dialog where the user may chose a map to load. Return the
/// filename of the chosen file. An empty string is returned if no file
/// is chosen.
std::string load_map_dialog(display &disp);

View file

@ -78,18 +78,19 @@ HOTKEY_COMMAND string_to_command(const std::string& str)
m.insert(val("editcut",HOTKEY_EDIT_CUT));
m.insert(val("editcopy",HOTKEY_EDIT_COPY));
m.insert(val("editpaste",HOTKEY_EDIT_PASTE));
m.insert(val("editrevert",HOTKEY_EDIT_REVERT));
m.insert(val("delayshroud",HOTKEY_DELAY_SHROUD));
m.insert(val("updateshroud",HOTKEY_UPDATE_SHROUD));
m.insert(val("continue",HOTKEY_CONTINUE_MOVE));
}
const std::map<std::string,HOTKEY_COMMAND>::const_iterator i = m.find(str);
if(i == m.end())
return HOTKEY_NULL;
else
return i->second;
}
std::string command_to_string(const HOTKEY_COMMAND &command)
{
for(std::map<std::string,HOTKEY_COMMAND>::iterator i = m.begin(); i != m.end(); ++i) {
@ -451,6 +452,10 @@ void execute_command(display& disp, HOTKEY_COMMAND command, command_executor* ex
if(executor)
executor->edit_copy();
break;
case HOTKEY_EDIT_REVERT:
if(executor)
executor->edit_revert();
break;
default:
break;
}

View file

@ -42,7 +42,7 @@ enum HOTKEY_COMMAND { HOTKEY_CYCLE_UNITS, HOTKEY_END_UNIT_TURN, HOTKEY_LEADER,
HOTKEY_EDIT_SAVE_AS, HOTKEY_EDIT_SET_START_POS,
HOTKEY_EDIT_NEW_MAP, HOTKEY_EDIT_LOAD_MAP, HOTKEY_EDIT_FLOOD_FILL,
HOTKEY_EDIT_FILL_SELECTION, HOTKEY_EDIT_CUT, HOTKEY_EDIT_COPY,
HOTKEY_EDIT_PASTE,
HOTKEY_EDIT_PASTE, HOTKEY_EDIT_REVERT,
HOTKEY_NULL };
struct hotkey_item {
@ -129,6 +129,7 @@ public:
virtual void edit_cut() {}
virtual void edit_copy() {}
virtual void edit_paste() {}
virtual void edit_revert() {}
//Gets the action's image (if any). Displayed left of the action text in menus.
virtual std::string get_action_image(hotkey::HOTKEY_COMMAND command) const { return ""; }