editor2: select all, invert selection, everything_changed member in map_context

This commit is contained in:
Tomasz Śniatowski 2008-07-30 23:58:00 +01:00
parent 8332d57348
commit 6d34944ad7
10 changed files with 113 additions and 10 deletions

View file

@ -130,7 +130,7 @@
id=menu-editor-edit
title= _ "Edit"
image=lite
items=undo,redo,editor-cut,editor-copy,editor-paste,editor-select-all,editor-selection-rotate,editor-selection-flip,editor-selection-generate,editor-selection-randomize
items=undo,redo,editor-cut,editor-copy,editor-paste,editor-select-all,editor-select-inverse,editor-selection-rotate,editor-selection-flip,editor-selection-generate,editor-selection-randomize
rect="+2,=,+100,="
xanchor=fixed
yanchor=fixed

View file

@ -21,6 +21,8 @@
#include "../foreach.hpp"
#include <algorithm>
namespace editor2 {
int editor_action::next_id_ = 1;
@ -181,6 +183,36 @@ void editor_action_deselect::perform_without_undo(map_context& mc) const
}
}
editor_action_select_xor* editor_action_select_all::perform(map_context& mc) const
{
std::set<gamemap::location> current = mc.get_map().selection();
mc.get_map().select_all();
std::set<gamemap::location> all = mc.get_map().selection();
std::set<gamemap::location> undo_locs;
std::set_difference(all.begin(), all.end(),
current.begin(), current.end(),
std::inserter(undo_locs, undo_locs.begin()));
mc.set_everything_changed();
return new editor_action_select_xor(undo_locs);
}
void editor_action_select_all::perform_without_undo(map_context& mc) const
{
mc.get_map().select_all();
mc.set_everything_changed();
}
editor_action_select_inverse* editor_action_select_inverse::perform(map_context& mc) const
{
perform_without_undo(mc);
return new editor_action_select_inverse();
}
void editor_action_select_inverse::perform_without_undo(map_context& mc) const
{
mc.get_map().invert_selection();
mc.set_everything_changed();
}
void editor_action_resize_map::perform_without_undo(map_context& mc) const
{
mc.get_map().resize(x_size_, y_size_, x_offset_, y_offset_);

View file

@ -189,6 +189,26 @@ class editor_action_deselect : public editor_action_area
void perform_without_undo(map_context& mc) const;
};
class editor_action_select_all : public editor_action
{
public:
editor_action_select_all()
{
}
editor_action_select_xor* perform(map_context& mc) const;
void perform_without_undo(map_context& mc) const;
};
class editor_action_select_inverse : public editor_action
{
public:
editor_action_select_inverse()
{
}
editor_action_select_inverse* perform(map_context& mc) const;
void perform_without_undo(map_context& mc) const;
};
//resize map (streching / clipping behaviour?)
class editor_action_resize_map : public editor_action
{

View file

@ -248,6 +248,7 @@ void editor_controller::load_map(const std::string& filename)
void editor_controller::revert_map()
{
if (!confirm_discard()) return;
const std::string& filename = get_map_context().get_filename();
if (filename.empty()) {
ERR_ED << "Empty filename in map revert\n";
@ -385,6 +386,16 @@ bool editor_controller::execute_command(hotkey::HOTKEY_COMMAND command, int inde
case HOTKEY_EDITOR_CUT:
cut_selection();
return true;
case HOTKEY_EDITOR_SELECT_ALL:
if (!get_map_context().get_map().everything_selected()) {
get_map_context().perform_action(editor_action_select_all());
refresh_after_action();
return true;
} //else intentionally fall through
case HOTKEY_EDITOR_SELECT_INVERSE:
get_map_context().perform_action(editor_action_select_inverse());
refresh_after_action();
return true;
case HOTKEY_EDITOR_MAP_FLIP_X: {
editor_action_flip_x fx;
get_map_context().perform_action(fx);
@ -569,7 +580,11 @@ void editor_controller::refresh_after_action()
get_map_context().set_needs_terrain_rebuild(false);
get_map_context().clear_changed_locations();
} else {
gui().invalidate(get_map_context().changed_locations());
if (get_map_context().everything_changed()) {
gui().invalidate_all();
} else {
gui().invalidate(get_map_context().changed_locations());
}
get_map_context().clear_changed_locations();
}
gui().recalculate_minimap();

View file

@ -102,8 +102,8 @@ void editor_map::clear_selection()
void editor_map::invert_selection()
{
std::set<gamemap::location> new_selection;
for (int x = 0; x < w(); ++x) {
for (int y = 0; y < h(); ++y) {
for (int x = -1; x < w() + 1; ++x) {
for (int y = -1; y < h() + 1; ++y) {
if (selection_.find(gamemap::location(x, y)) == selection_.end()) {
new_selection.insert(gamemap::location(x, y));
}
@ -118,6 +118,12 @@ void editor_map::select_all()
invert_selection();
}
bool editor_map::everything_selected() const
{
LOG_ED << selection_.size() << " " << total_width() * total_height() << "\n";
return selection_.size() == total_width() * total_height();
}
void editor_map::resize(int width, int height, int x_offset, int y_offset,
t_translation::t_terrain filler)
{

View file

@ -85,6 +85,8 @@ public:
* Select all map hexes
*/
void select_all();
bool everything_selected() const;
/**
* Resize the map. If the filler is NONE, the border terrain will be copied

View file

@ -32,7 +32,7 @@ const int map_context::max_action_stack_size_ = 100;
map_context::map_context(const editor_map& map)
: map_(map), filename_(), actions_since_save_(0),
needs_reload_(false), needs_terrain_rebuild_(false)
needs_reload_(false), needs_terrain_rebuild_(false), everything_changed_(false)
{
}
@ -82,13 +82,36 @@ void map_context::draw_terrain(t_translation::t_terrain terrain,
}
}
void map_context::add_changed_location(const std::set<gamemap::location>& locs)
void map_context::clear_changed_locations()
{
foreach (const gamemap::location& loc, locs) {
everything_changed_ = false;
changed_locations_.clear();
}
void map_context::add_changed_location(const gamemap::location& loc)
{
if (!everything_changed()) {
changed_locations_.insert(loc);
}
}
void map_context::add_changed_location(const std::set<gamemap::location>& locs)
{
if (!everything_changed()) {
changed_locations_.insert(locs.begin(), locs.end());
}
}
void map_context::set_everything_changed()
{
everything_changed_ = true;
}
bool map_context::everything_changed() const
{
return everything_changed_;
}
void map_context::clear_starting_position_labels(display& disp)
{
foreach (const gamemap::location& loc, starting_position_label_locs_) {

View file

@ -49,9 +49,11 @@ public:
void set_needs_terrain_rebuild(bool value=true) { needs_terrain_rebuild_ = value; }
const std::set<gamemap::location> changed_locations() const { return changed_locations_; }
void clear_changed_locations() { changed_locations_.clear(); }
void add_changed_location(const gamemap::location& loc) { changed_locations_.insert(loc); }
void clear_changed_locations();
void add_changed_location(const gamemap::location& loc);
void add_changed_location(const std::set<gamemap::location>& locs);
void set_everything_changed();
bool everything_changed() const;
void clear_starting_position_labels(display& disp);
@ -152,6 +154,7 @@ protected:
bool needs_reload_;
bool needs_terrain_rebuild_;
std::set<gamemap::location> changed_locations_;
bool everything_changed_;
};

View file

@ -152,6 +152,8 @@ const struct {
{ hotkey::HOTKEY_EDITOR_PASTE, "editor-paste", N_("Paste"), false, hotkey::SCOPE_EDITOR },
{ hotkey::HOTKEY_EDITOR_SELECT_ALL, "editor-select-all",
N_("Select All"), false, hotkey::SCOPE_EDITOR },
{ hotkey::HOTKEY_EDITOR_SELECT_INVERSE, "editor-select-inverse",
N_("Invert Selection"), false, hotkey::SCOPE_EDITOR },
{ hotkey::HOTKEY_EDITOR_SELECTION_ROTATE, "editor-selection-rotate",
N_("Rotate Selection"), false, hotkey::SCOPE_EDITOR },
{ hotkey::HOTKEY_EDITOR_SELECTION_FLIP, "editor-selection-flip",

View file

@ -79,7 +79,7 @@ enum HOTKEY_COMMAND {
HOTKEY_EDITOR_TOOL_SELECT, HOTKEY_EDITOR_TOOL_STARTING_POSITION,
HOTKEY_EDITOR_BRUSH_NEXT, HOTKEY_EDITOR_BRUSH_DEFAULT,
HOTKEY_EDITOR_CUT, HOTKEY_EDITOR_COPY, HOTKEY_EDITOR_PASTE,
HOTKEY_EDITOR_SELECT_ALL,
HOTKEY_EDITOR_SELECT_ALL, HOTKEY_EDITOR_SELECT_INVERSE,
HOTKEY_EDITOR_SELECTION_ROTATE, HOTKEY_EDITOR_SELECTION_FLIP,
HOTKEY_EDITOR_SELECTION_GENERATE, HOTKEY_EDITOR_SELECTION_RANDOMIZE,
HOTKEY_EDITOR_MAP_RESIZE, HOTKEY_EDITOR_MAP_ROTATE,