small fix to rebuilding

This commit is contained in:
Kristoffer Erlandsson 2004-05-17 20:13:03 +00:00
parent 2adcf2b26e
commit 83b748db1a
4 changed files with 135 additions and 108 deletions

View File

@ -51,20 +51,6 @@ namespace {
+ "/preferences";
void terrain_changed(gamemap &map, const gamemap::location &hex) {
// If we painted something else than a keep on a starting position,
// unset the starting position.
int start_side = -1;
for (int i = 0; i < 10; i++) {
if (map.starting_position(i) == hex) {
start_side = i;
}
}
if (start_side != -1 && map.get_terrain(hex) != gamemap::KEEP) {
map.set_starting_position(start_side, gamemap::location());
}
}
double location_distance(const gamemap::location loc1, const gamemap::location loc2) {
const double xdiff = loc1.x - loc2.x;
const double ydiff = loc1.y - loc2.y;
@ -355,19 +341,18 @@ void map_editor::edit_paste() {
gui_.add_highlighted_loc(target);
}
}
undo_action.set_selection(filled, selected_hexes_);
undo_action.set_selection(selected_hexes_, filled);
invalidate_all_and_adjacent(filled);
selected_hexes_ = filled;
save_undo_action(undo_action, false);
save_undo_action(undo_action);
}
void map_editor::edit_revert() {
const std::string new_map = load_map(filename_);
if (new_map != "") {
map_undo_action action;
action.set_type(map_undo_action::WHOLE_MAP);
action.set_map_data(map_.write(), new_map);
save_undo_action(action, false);
save_undo_action(action);
throw new_map_exception(new_map, filename_);
}
}
@ -380,9 +365,8 @@ void map_editor::edit_resize() {
resize_map(map_, new_size.first, new_size.second, palette_.selected_terrain());
if (resized_map != "") {
map_undo_action action;
action.set_type(map_undo_action::WHOLE_MAP);
action.set_map_data(map_.write(), resized_map);
save_undo_action(action, false);
save_undo_action(action);
throw new_map_exception(resized_map, filename_);
}
}
@ -474,10 +458,7 @@ void map_editor::toggle_grid() {
gui_.invalidate_all();
}
void map_editor::save_undo_action(map_undo_action &action, const bool keep_selection) {
if (keep_selection) {
action.set_selection(selected_hexes_, selected_hexes_);
}
void map_editor::save_undo_action(const map_undo_action &action) {
add_undo_action(action);
num_operations_since_save_++;
}
@ -485,22 +466,24 @@ void map_editor::save_undo_action(map_undo_action &action, const bool keep_selec
void map_editor::undo() {
if(exist_undo_actions()) {
--num_operations_since_save_;
std::vector<gamemap::location> to_invalidate;
map_undo_action action = pop_undo_action();
if (action.undo_type() == map_undo_action::REGULAR) {
if (action.selection_set()) {
selected_hexes_ = action.undo_selection();
highlight_selected_hexes(true);
std::vector<gamemap::location> to_invalidate;
std::copy(selected_hexes_.begin(), selected_hexes_.end(),
std::back_inserter(to_invalidate));
}
if (action.terrain_set()) {
for(std::map<gamemap::location,gamemap::TERRAIN>::const_iterator it =
action.undo_terrains().begin();
it != action.undo_terrains().end(); ++it) {
map_.set_terrain(it->first, it->second);
to_invalidate.push_back(it->first);
}
std::copy(selected_hexes_.begin(), selected_hexes_.end(),
std::back_inserter(to_invalidate));
invalidate_all_and_adjacent(to_invalidate);
}
else if (action.undo_type() == map_undo_action::WHOLE_MAP) {
invalidate_all_and_adjacent(to_invalidate);
if (action.map_data_set()) {
throw new_map_exception(action.old_map_data(), filename_);
}
}
@ -510,21 +493,23 @@ void map_editor::redo() {
if(exist_redo_actions()) {
++num_operations_since_save_;
map_undo_action action = pop_redo_action();
selected_hexes_ = action.redo_selection();
if (action.undo_type() == map_undo_action::REGULAR) {
std::vector<gamemap::location> to_invalidate;
if (action.selection_set()) {
selected_hexes_ = action.redo_selection();
highlight_selected_hexes(true);
std::vector<gamemap::location> to_invalidate;
std::copy(selected_hexes_.begin(), selected_hexes_.end(),
std::back_inserter(to_invalidate));
}
if (action.terrain_set()) {
for(std::map<gamemap::location,gamemap::TERRAIN>::const_iterator it =
action.redo_terrains().begin();
it != action.redo_terrains().end(); ++it) {
map_.set_terrain(it->first, it->second);
to_invalidate.push_back(it->first);
}
std::copy(selected_hexes_.begin(), selected_hexes_.end(),
std::back_inserter(to_invalidate));
invalidate_all_and_adjacent(to_invalidate);
}
else if (action.undo_type() == map_undo_action::WHOLE_MAP) {
invalidate_all_and_adjacent(to_invalidate);
if (action.map_data_set()) {
throw new_map_exception(action.new_map_data(), filename_);
}
}
@ -560,10 +545,9 @@ bool map_editor::changed_since_save() const {
void map_editor::set_starting_position(const int player, const gamemap::location loc) {
if(map_.on_board(loc)) {
map_undo_action action;
num_operations_since_save_ = 1000;
map_.set_terrain(selected_hex_, gamemap::KEEP);
// This operation is currently not undoable, so we need to make sure
// that save is always asked for after it is performed.
num_operations_since_save_++;
map_.set_starting_position(player, loc);
invalidate_adjacent(loc);
}
@ -712,24 +696,41 @@ void map_editor::perform_selection_move() {
map_.set_terrain(*it, palette_.selected_terrain());
}
}
undo_action.set_selection(new_selection, selected_hexes_);
undo_action.set_selection(selected_hexes_, new_selection);
invalidate_all_and_adjacent(selected_hexes_);
selected_hexes_ = new_selection;
invalidate_all_and_adjacent(selected_hexes_);
save_undo_action(undo_action, false);
save_undo_action(undo_action);
}
void map_editor::draw_terrain(const gamemap::TERRAIN terrain,
const gamemap::location hex) {
const gamemap::TERRAIN current_terrain = map_[hex.x][hex.y];
map_undo_action undo_action(current_terrain, terrain, hex);
map_undo_action undo_action;
undo_action.add_terrain(current_terrain, terrain, hex);
save_undo_action(undo_action);
map_.set_terrain(hex, terrain);
invalidate_adjacent(hex);
}
void map_editor::terrain_changed(const gamemap::location &hex) {
// If we painted something else than a keep on a starting position,
// unset the starting position.
int start_side = -1;
for (int i = 0; i < 10; i++) {
if (map_.starting_position(i) == hex) {
start_side = i;
}
}
if (start_side != -1 && map_.get_terrain(hex) != gamemap::KEEP) {
//undo_action.add_starting_location(
map_.set_starting_position(start_side, gamemap::location());
}
}
void map_editor::invalidate_adjacent(const gamemap::location hex) {
terrain_changed(map_, hex);
terrain_changed(hex);
gamemap::location locs[7];
locs[0] = hex;
get_adjacent_tiles(hex,locs+1);
@ -744,9 +745,7 @@ void map_editor::invalidate_adjacent(const gamemap::location hex) {
invalidate_adjacent(locs[i]);
}
}
if (map_.is_built(locs[i])) {
gui_.rebuild_terrain(locs[i]);
}
gui_.rebuild_terrain(locs[i]);
gui_.invalidate(locs[i]);
}
map_dirty_ = true;
@ -756,7 +755,7 @@ void map_editor::invalidate_all_and_adjacent(const std::vector<gamemap::location
std::set<gamemap::location> to_invalidate;
std::vector<gamemap::location>::const_iterator it;
for (it = hexes.begin(); it != hexes.end(); it++) {
terrain_changed(map_, *it);
terrain_changed(*it);
gamemap::location locs[7];
locs[0] = *it;
get_adjacent_tiles(*it, locs+1);
@ -774,9 +773,7 @@ void map_editor::invalidate_all_and_adjacent(const std::vector<gamemap::location
invalidate_adjacent(*its);
}
}
if (map_.is_built(*its)) {
gui_.rebuild_terrain(*its);
}
gui_.rebuild_terrain(*its);
gui_.invalidate(*its);
}
map_dirty_ = true;

View File

@ -198,12 +198,14 @@ private:
/// highlighted.
void highlight_selected_hexes(const bool clear_old=true);
/// If a starting position is at the specified hex, unset it if
/// something else than a keep is the terrain at the hex. Save the
/// starting position change in undo_action.
void terrain_changed(const gamemap::location &hex);
/// Save an action so that it may be undone. Add an operation to the
/// number done since save. If keep_selection is true, it indicates
/// that the selection has not changed and the currently selected
/// terrain should be kept if this action is redone/undone.
void save_undo_action(map_undo_action &action,
const bool keep_selection=true);
/// number done since save.
void save_undo_action(const map_undo_action &action);
/// An item in the clipboard. Consists of the copied terrain and an
/// offset. When pasting stuff, the offset is used to calculate

View File

@ -21,15 +21,11 @@ namespace {
namespace map_editor {
map_undo_action::map_undo_action(const gamemap::TERRAIN& old_tr,
const gamemap::TERRAIN& new_tr,
const gamemap::location& lc){
add_terrain(old_tr, new_tr, lc);
undo_type_ = REGULAR;
}
map_undo_action::map_undo_action() {
undo_type_ = REGULAR;
terrain_set_ = false;
selection_set_ = false;
map_data_set_ = false;
starting_locations_set_ = false;
}
const std::map<gamemap::location,gamemap::TERRAIN>& map_undo_action::undo_terrains() const {
@ -48,19 +44,6 @@ const std::set<gamemap::location> map_undo_action::redo_selection() const {
return new_selection_;
}
void map_undo_action::add_terrain(const gamemap::TERRAIN& old_tr,
const gamemap::TERRAIN& new_tr,
const gamemap::location& lc) {
old_terrain_[lc] = old_tr;
new_terrain_[lc] = new_tr;
}
void map_undo_action::set_selection(const std::set<gamemap::location> &new_selection,
const std::set<gamemap::location> &old_selection) {
new_selection_ = new_selection;
old_selection_ = old_selection;
}
std::string map_undo_action::old_map_data() const {
return old_map_data_;
}
@ -69,22 +52,56 @@ std::string map_undo_action::new_map_data() const {
return new_map_data_;
}
const std::map<gamemap::location, int>& map_undo_action::undo_starting_locations() const {
return old_starting_locations_;
}
const std::map<gamemap::location, int>& map_undo_action::redo_starting_locations() const {
return new_starting_locations_;
}
void map_undo_action::add_terrain(const gamemap::TERRAIN& old_tr,
const gamemap::TERRAIN& new_tr,
const gamemap::location& lc) {
old_terrain_[lc] = old_tr;
new_terrain_[lc] = new_tr;
terrain_set_ = true;
}
bool map_undo_action::terrain_set() const {
return terrain_set_;
}
void map_undo_action::set_selection(const std::set<gamemap::location> &old_selection,
const std::set<gamemap::location> &new_selection) {
old_selection_ = old_selection;
new_selection_ = new_selection;
selection_set_ = true;
}
bool map_undo_action::selection_set() const {
return selection_set_;
}
void map_undo_action::set_map_data(const std::string &old_data,
const std::string &new_data) {
old_map_data_ = old_data;
new_map_data_ = new_data;
map_data_set_ = true;
}
void map_undo_action::set_type(const UNDO_TYPE new_type) {
undo_type_ = new_type;
bool map_undo_action::map_data_set() const {
return map_data_set_;
}
map_undo_action::UNDO_TYPE map_undo_action::undo_type() const {
return undo_type_;
void map_undo_action::add_starting_location(const int old_side, const int new_side,
const gamemap::location &loc) {
old_starting_locations_[loc] = old_side;
new_starting_locations_[loc] = new_side;
starting_locations_set_ = true;
}
void add_undo_action(map_undo_action &action) {
void add_undo_action(const map_undo_action &action) {
undo_stack.push_back(action);
if (undo_stack.size() > undo_limit) {
undo_stack.pop_front();

View File

@ -27,47 +27,58 @@ namespace map_editor {
/// A saved action that may be undone.
class map_undo_action {
public:
enum UNDO_TYPE { REGULAR, WHOLE_MAP };
map_undo_action();
map_undo_action(const gamemap::TERRAIN& old_tr,
const gamemap::TERRAIN& new_tr,
const gamemap::location& lc);
const std::map<gamemap::location,gamemap::TERRAIN>& undo_terrains() const;
const std::map<gamemap::location,gamemap::TERRAIN>& redo_terrains() const;
const std::set<gamemap::location> undo_selection() const;
const std::set<gamemap::location> redo_selection() const;
void add_terrain(const gamemap::TERRAIN& old_tr,
const gamemap::TERRAIN& new_tr,
const gamemap::location& lc);
void set_selection(const std::set<gamemap::location> &new_selection,
const std::set<gamemap::location> &old_selection);
void set_map_data(const std::string &old_data,
const std::string &new_data);
std::string new_map_data() const;
std::string old_map_data() const;
void set_type(const UNDO_TYPE new_type);
UNDO_TYPE undo_type() const;
const std::map<gamemap::location, int>& undo_starting_locations() const;
const std::map<gamemap::location, int>& redo_starting_locations() const;
void add_terrain(const gamemap::TERRAIN& old_tr,
const gamemap::TERRAIN& new_tr,
const gamemap::location& lc);
/// Return true if a terrain change has been saved in this undo
/// action.
bool terrain_set() const;
void set_selection(const std::set<gamemap::location> &old_selection,
const std::set<gamemap::location> &new_selection);
/// Return true if a selection change has been saved in this undo
/// action.
bool selection_set() const;
void set_map_data(const std::string &old_data,
const std::string &new_data);
/// Return true if a map data change has been saved in this undo
/// action.
bool map_data_set() const;
void add_starting_location(const int old_side, const int new_side,
const gamemap::location &loc);
private:
std::map<gamemap::location,gamemap::TERRAIN> old_terrain_;
std::map<gamemap::location,gamemap::TERRAIN> new_terrain_;
std::set<gamemap::location> new_selection_;
bool terrain_set_;
std::set<gamemap::location> old_selection_;
std::string new_map_data_;
std::set<gamemap::location> new_selection_;
bool selection_set_;
std::string old_map_data_;
UNDO_TYPE undo_type_;
std::string new_map_data_;
bool map_data_set_;
std::map<gamemap::location,int> old_starting_locations_;
std::map<gamemap::location,int> new_starting_locations_;
bool starting_locations_set_;
};
typedef std::deque<map_undo_action> map_undo_list;
@ -77,7 +88,7 @@ typedef std::deque<map_undo_action> map_undo_list;
/// since save. If keep_selection is true, it indicates that the
/// selection has not changed and the currently selected terrain should
/// be kept if this action is redone/undone. Also clear the redo stack.
void add_undo_action(map_undo_action &action);
void add_undo_action(const map_undo_action &action);
/// Return true if there exist any undo actions in the undo stack.
bool exist_undo_actions();