add editor action cloning

This commit is contained in:
Tomasz Śniatowski 2009-10-04 15:18:57 +01:00
parent 5b529e5be5
commit ec428d4f24
3 changed files with 105 additions and 2 deletions

View file

@ -62,16 +62,42 @@ editor_action* editor_action::perform(map_context& mc) const
return undo.release();
}
editor_action_whole_map* editor_action_whole_map::clone() const
{
return new editor_action_whole_map(*this);
}
void editor_action_whole_map::perform_without_undo(map_context& mc) const {
mc.set_map(m_);
}
editor_action_chain::editor_action_chain(const editor::editor_action_chain &other)
{
foreach (editor_action* a, other.actions_) {
actions_.push_back(a->clone());
}
}
editor_action_chain& editor_action_chain::operator=(const editor_action_chain& other)
{
if (this == &other) return *this;
foreach (editor_action* a, actions_) {
delete a;
}
actions_.clear();
foreach (editor_action* a, other.actions_) {
actions_.push_back(a->clone());
}
return *this;
}
editor_action_chain::~editor_action_chain()
{
foreach (editor_action* a, actions_) {
delete a;
}
}
editor_action_chain* editor_action_chain::clone() const
{
return new editor_action_chain(*this);
}
int editor_action_chain::action_count() const {
int count = 0;
foreach (const editor_action* a, actions_) {
@ -126,6 +152,10 @@ void editor_action_area::extend(const editor_map& /*map*/, const std::set<map_lo
area_.insert(locs.begin(), locs.end());
}
editor_action_paste* editor_action_paste::clone() const
{
return new editor_action_paste(*this);
}
void editor_action_paste::extend(const editor_map& map, const std::set<map_location>& locs)
{
paste_.add_tiles(map, locs);
@ -144,6 +174,11 @@ void editor_action_paste::perform_without_undo(map_context& mc) const
mc.set_needs_terrain_rebuild();
}
editor_action_paint_area* editor_action_paint_area::clone() const
{
return new editor_action_paint_area(*this);
}
editor_action_paste* editor_action_paint_area::perform(map_context& mc) const
{
map_fragment mf(mc.get_map(), area_);
@ -157,6 +192,10 @@ void editor_action_paint_area::perform_without_undo(map_context& mc) const
mc.set_needs_terrain_rebuild();
}
editor_action_fill* editor_action_fill::clone() const
{
return new editor_action_fill(*this);
}
editor_action_paint_area* editor_action_fill::perform(map_context& mc) const
{
std::set<map_location> to_fill = mc.get_map().get_contigious_terrain_tiles(loc_);
@ -172,6 +211,10 @@ void editor_action_fill::perform_without_undo(map_context& mc) const
mc.set_needs_terrain_rebuild();
}
editor_action_starting_position* editor_action_starting_position::clone() const
{
return new editor_action_starting_position(*this);
}
editor_action* editor_action_starting_position::perform(map_context& mc) const
{
std::auto_ptr<editor_action> undo;
@ -204,6 +247,10 @@ void editor_action_starting_position::perform_without_undo(map_context& mc) cons
mc.set_needs_labels_reset();
}
editor_action_select* editor_action_select::clone() const
{
return new editor_action_select(*this);
}
void editor_action_select::extend(const editor_map& map, const std::set<map_location>& locs)
{
foreach (const map_location& loc, locs) {
@ -234,6 +281,10 @@ void editor_action_select::perform_without_undo(map_context& mc) const
}
}
editor_action_deselect* editor_action_deselect::clone() const
{
return new editor_action_deselect(*this);
}
void editor_action_deselect::extend(const editor_map& map, const std::set<map_location>& locs)
{
foreach (const map_location& loc, locs) {
@ -264,6 +315,10 @@ void editor_action_deselect::perform_without_undo(map_context& mc) const
}
}
editor_action_select_all* editor_action_select_all::clone() const
{
return new editor_action_select_all(*this);
}
editor_action_deselect* editor_action_select_all::perform(map_context& mc) const
{
std::set<map_location> current = mc.get_map().selection();
@ -282,6 +337,10 @@ void editor_action_select_all::perform_without_undo(map_context& mc) const
mc.set_everything_changed();
}
editor_action_select_none* editor_action_select_none::clone() const
{
return new editor_action_select_none(*this);
}
editor_action_select* editor_action_select_none::perform(map_context& mc) const
{
std::set<map_location> current = mc.get_map().selection();
@ -295,7 +354,10 @@ void editor_action_select_none::perform_without_undo(map_context& mc) const
mc.set_everything_changed();
}
editor_action_select_inverse* editor_action_select_inverse::clone() const
{
return new editor_action_select_inverse(*this);
}
editor_action_select_inverse* editor_action_select_inverse::perform(map_context& mc) const
{
perform_without_undo(mc);
@ -307,24 +369,40 @@ void editor_action_select_inverse::perform_without_undo(map_context& mc) const
mc.set_everything_changed();
}
editor_action_resize_map* editor_action_resize_map::clone() const
{
return new editor_action_resize_map(*this);
}
void editor_action_resize_map::perform_without_undo(map_context& mc) const
{
mc.get_map().resize(x_size_, y_size_, x_offset_, y_offset_, fill_);
mc.set_needs_reload();
}
editor_action_apply_mask* editor_action_apply_mask::clone() const
{
return new editor_action_apply_mask(*this);
}
void editor_action_apply_mask::perform_without_undo(map_context& mc) const
{
mc.get_map().overlay(mask_, config(), 0, 0, true);
mc.set_needs_terrain_rebuild();
}
editor_action_create_mask* editor_action_create_mask::clone() const
{
return new editor_action_create_mask(*this);
}
void editor_action_create_mask::perform_without_undo(map_context& mc) const
{
mc.get_map() = editor_map(mc.get_map().mask_to(target_));
mc.set_needs_terrain_rebuild();
}
editor_action_shuffle_area* editor_action_shuffle_area::clone() const
{
return new editor_action_shuffle_area(*this);
}
editor_action_paste* editor_action_shuffle_area::perform(map_context& mc) const
{
map_fragment mf(mc.get_map(), area_);

View file

@ -47,6 +47,7 @@ class editor_action_whole_map : public editor_action
: m_(m)
{
}
editor_action_whole_map* clone() const;
void perform_without_undo(map_context& m) const;
const char* get_name() const { return "whole_map"; }
protected:
@ -91,6 +92,12 @@ class editor_action_chain : public editor_action
{
}
editor_action_chain(const editor_action_chain& other);
editor_action_chain& operator=(const editor_action_chain& other);
editor_action_chain* clone() const;
/**
* Create an action chain from a deque of action pointers.
* Note: the action chain assumes ownership of the pointers.
@ -226,6 +233,7 @@ class editor_action_paste : public editor_action_extendable
: offset_(offset), paste_(paste)
{
}
editor_action_paste* clone() const;
editor_action_paste* perform(map_context& mc) const;
void perform_without_undo(map_context& mc) const;
void extend(const editor_map& map, const std::set<map_location>& locs);
@ -246,6 +254,7 @@ class editor_action_paint_area : public editor_action_area
: editor_action_area(area), t_(t), one_layer_(one_layer)
{
}
editor_action_paint_area* clone() const;
editor_action_paste* perform(map_context& mc) const;
void perform_without_undo(map_context& mc) const;
const char* get_name() const { return "paint_area"; }
@ -255,7 +264,7 @@ class editor_action_paint_area : public editor_action_area
};
/**
* Flood fill. Somewhat redundand with paint_area.
* Flood fill. Somewhat redundant with paint_area.
*/
class editor_action_fill : public editor_action_location_terrain
{
@ -265,6 +274,7 @@ class editor_action_fill : public editor_action_location_terrain
: editor_action_location_terrain(loc, t), one_layer_(one_layer)
{
}
editor_action_fill* clone() const;
editor_action_paint_area* perform(map_context& mc) const;
void perform_without_undo(map_context& mc) const;
const char* get_name() const { return "fill"; }
@ -282,6 +292,7 @@ class editor_action_starting_position : public editor_action_location
: editor_action_location(loc), player_(player)
{
}
editor_action_starting_position* clone() const;
editor_action* perform(map_context& mc) const;
void perform_without_undo(map_context& mc) const;
const char* get_name() const { return "starting_pos"; }
@ -299,6 +310,7 @@ class editor_action_select : public editor_action_area
: editor_action_area(area)
{
}
editor_action_select* clone() const;
void extend(const editor_map& map, const std::set<map_location>& locs);
editor_action* perform(map_context& mc) const;
void perform_without_undo(map_context& mc) const;
@ -315,6 +327,7 @@ class editor_action_deselect : public editor_action_area
: editor_action_area(area)
{
}
editor_action_deselect* clone() const;
void extend(const editor_map& map, const std::set<map_location>& locs);
editor_action* perform(map_context& mc) const;
void perform_without_undo(map_context& mc) const;
@ -330,6 +343,7 @@ class editor_action_select_all : public editor_action
editor_action_select_all()
{
}
editor_action_select_all* clone() const;
editor_action_deselect* perform(map_context& mc) const;
void perform_without_undo(map_context& mc) const;
const char* get_name() const { return "select_all"; }
@ -344,6 +358,7 @@ class editor_action_select_none : public editor_action
editor_action_select_none()
{
}
editor_action_select_none* clone() const;
editor_action_select* perform(map_context& mc) const;
void perform_without_undo(map_context& mc) const;
const char* get_name() const { return "select_none"; }
@ -358,6 +373,7 @@ class editor_action_select_inverse : public editor_action
editor_action_select_inverse()
{
}
editor_action_select_inverse* clone() const;
editor_action_select_inverse* perform(map_context& mc) const;
void perform_without_undo(map_context& mc) const;
const char* get_name() const { return "select_inverse"; }
@ -375,6 +391,7 @@ class editor_action_resize_map : public editor_action
: x_size_(x_size), y_size_(y_size), x_offset_(x_offset), y_offset_(y_offset), fill_(fill)
{
}
editor_action_resize_map* clone() const;
void perform_without_undo(map_context& mc) const;
const char* get_name() const { return "resize"; }
protected:
@ -392,6 +409,7 @@ class editor_action_apply_mask : public editor_action
: mask_(mask)
{
}
editor_action_apply_mask* clone() const;
void perform_without_undo(map_context& mc) const;
const char* get_name() const { return "apply_mask"; }
private:
@ -405,6 +423,7 @@ class editor_action_create_mask : public editor_action
: target_(target)
{
}
editor_action_create_mask* clone() const;
void perform_without_undo(map_context& mc) const;
const char* get_name() const { return "create_mask"; }
private:
@ -421,6 +440,7 @@ class editor_action_shuffle_area : public editor_action_area
: editor_action_area(area)
{
}
editor_action_shuffle_area* clone() const;
editor_action_paste* perform(map_context& mc) const;
void perform_without_undo(map_context& mc) const;
const char* get_name() const { return "shuffle_area"; }

View file

@ -46,6 +46,11 @@ class editor_action
editor_action();
virtual ~editor_action();
/**
* Action cloning
*/
virtual editor_action* clone() const = 0;
/**
* Perform the action, returning an undo action that,
* when performed, shall reverse any effects of this