Refactored the editor's select tool into its own files.

This commit is contained in:
fendrin 2013-06-21 17:40:40 +02:00
parent 80b7d40bc2
commit 0858130792
12 changed files with 376 additions and 240 deletions

View File

@ -655,11 +655,13 @@ set(wesnoth-main_SRC
editor/action/action_label.cpp
editor/action/action_unit.cpp
editor/action/action_village.cpp
editor/action/action_select.cpp
editor/action/mouse/mouse_action.cpp
editor/action/mouse/mouse_action_item.cpp
editor/action/mouse/mouse_action_map_label.cpp
editor/action/mouse/mouse_action_unit.cpp
editor/action/mouse/mouse_action_village.cpp
editor/action/mouse/mouse_action_select.cpp
editor/map/editor_map.cpp
editor/map/map_context.cpp
editor/map/map_fragment.cpp

View File

@ -227,11 +227,13 @@ wesnoth_sources = Split("""
editor/action/action_label.cpp
editor/action/action_village.cpp
editor/action/action_item.cpp
editor/action/action_select.cpp
editor/action/mouse/mouse_action.cpp
editor/action/mouse/mouse_action_map_label.cpp
editor/action/mouse/mouse_action_unit.cpp
editor/action/mouse/mouse_action_village.cpp
editor/action/mouse/mouse_action_item.cpp
editor/action/mouse/mouse_action_select.cpp
editor/map/editor_map.cpp
editor/map/map_context.cpp
editor/map/map_fragment.cpp

View File

@ -253,95 +253,7 @@ void editor_action_starting_position::perform_without_undo(map_context& mc) cons
}
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)
{
BOOST_FOREACH(const map_location& loc, locs) {
LOG_ED << "Extending by " << loc << "\n";
area_.insert(loc);
}
}
editor_action* editor_action_select::perform(map_context& mc) const
{
std::set<map_location> undo_locs;
BOOST_FOREACH(const map_location& loc, area_) {
undo_locs.insert(loc);
mc.add_changed_location(loc);
}
perform_without_undo(mc);
return new editor_action_select(undo_locs);
}
void editor_action_select::perform_without_undo(map_context& mc) const
{
BOOST_FOREACH(const map_location& loc, area_) {
// if (undo_locs)
if (!mc.get_map().in_selection(loc))
mc.get_map().add_to_selection(loc);
else
mc.get_map().remove_from_selection(loc);
mc.add_changed_location(loc);
}
}
editor_action_select_all* editor_action_select_all::clone() const
{
return new editor_action_select_all(*this);
}
editor_action_select* editor_action_select_all::perform(map_context& mc) const
{
std::set<map_location> current = mc.get_map().selection();
mc.get_map().select_all();
std::set<map_location> all = mc.get_map().selection();
std::set<map_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(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_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();
mc.get_map().clear_selection();
mc.set_everything_changed();
return new editor_action_select(current);
}
void editor_action_select_none::perform_without_undo(map_context& mc) const
{
mc.get_map().clear_selection();
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);
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();
}
editor_action_resize_map* editor_action_resize_map::clone() const
{

View File

@ -296,67 +296,6 @@ class editor_action_starting_position : public editor_action_location
};
/**
* Select the given locations
*/
class editor_action_select : public editor_action_area
{
public:
editor_action_select(const std::set<map_location>& 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;
const char* get_name() const { return "select"; }
};
/**
* Select the entire map
*/
class editor_action_select_all : public editor_action
{
public:
editor_action_select_all()
{
}
editor_action_select_all* 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_all"; }
};
/**
* Clear selection
*/
class editor_action_select_none : public editor_action
{
public:
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"; }
};
/**
* Invert the selection
*/
class editor_action_select_inverse : public editor_action
{
public:
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"; }
};
/**
* Resize the map. The offsets specify, indirectly, the direction of expanding/shrinking,

View File

@ -0,0 +1,122 @@
/*
Copyright (C) 2008 - 2011 by Fabian Mueller <fabianmueller5@gmx.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 as published by
the Free Software Foundation; either version 2 of the License, 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.
*/
/**
* @file
* Editor label action classes
*/
#define GETTEXT_DOMAIN "wesnoth-editor"
#include "editor/action/action_select.hpp"
#include "editor/map/map_context.hpp"
#include <boost/foreach.hpp>
namespace editor {
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)
{
BOOST_FOREACH(const map_location& loc, locs) {
LOG_ED << "Extending by " << loc << "\n";
area_.insert(loc);
}
}
editor_action* editor_action_select::perform(map_context& mc) const
{
std::set<map_location> undo_locs;
BOOST_FOREACH(const map_location& loc, area_) {
undo_locs.insert(loc);
mc.add_changed_location(loc);
}
perform_without_undo(mc);
return new editor_action_select(undo_locs);
}
void editor_action_select::perform_without_undo(map_context& mc) const
{
BOOST_FOREACH(const map_location& loc, area_) {
// if (undo_locs)
if (!mc.get_map().in_selection(loc))
mc.get_map().add_to_selection(loc);
else
mc.get_map().remove_from_selection(loc);
mc.add_changed_location(loc);
}
}
editor_action_select_all* editor_action_select_all::clone() const
{
return new editor_action_select_all(*this);
}
editor_action_select* editor_action_select_all::perform(map_context& mc) const
{
std::set<map_location> current = mc.get_map().selection();
mc.get_map().select_all();
std::set<map_location> all = mc.get_map().selection();
std::set<map_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(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_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();
mc.get_map().clear_selection();
mc.set_everything_changed();
return new editor_action_select(current);
}
void editor_action_select_none::perform_without_undo(map_context& mc) const
{
mc.get_map().clear_selection();
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);
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();
}
} //end namespace editor

View File

@ -0,0 +1,100 @@
/*
Copyright (C) 2008 - 2011 by Fabian Mueller <fabianmueller5@gmx.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 as published by
the Free Software Foundation; either version 2 of the License, 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.
*/
/**
* @file
* Editor action classes. Some important points:
* - This is a polymorphic hierarchy of classes, so actions are usually passed around
* as editor_action pointers
* - The pointers can, in general, be null. Always check for null before doing anything.
* The helper functions perform_ that take a pointer do that.
* - The perform() functions can throw when an error occurs. Use smart pointers if you
* need to ensure the pointer is deleted.
*/
#ifndef EDITOR_ACTION_SELECT_HPP
#define EDITOR_ACTION_SELECT_HPP
#include "action.hpp"
namespace editor {
/**
* Select the given locations
*/
class editor_action_select : public editor_action_area
{
public:
editor_action_select(const std::set<map_location>& 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;
const char* get_name() const { return "select"; }
};
/**
* Select the entire map
*/
class editor_action_select_all : public editor_action
{
public:
editor_action_select_all()
{
}
editor_action_select_all* 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_all"; }
};
/**
* Clear selection
*/
class editor_action_select_none : public editor_action
{
public:
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"; }
};
/**
* Invert the selection
*/
class editor_action_select_inverse : public editor_action
{
public:
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"; }
};
} //end namespace editor
#endif

View File

@ -302,52 +302,7 @@ void mouse_action_paint::set_mouse_overlay(editor_display& disp)
}
std::set<map_location> mouse_action_select::affected_hexes(
editor_display& disp, const map_location& hex)
{
if (has_shift_modifier()) {
return disp.map().get_contiguous_terrain_tiles(hex);
} else {
return brush_drag_mouse_action::affected_hexes(disp, hex);
}
}
editor_action* mouse_action_select::key_event(
editor_display& disp, const SDL_Event& event)
{
editor_action* ret = mouse_action::key_event(disp, event);
update_brush_highlights(disp, previous_move_hex_);
return ret;
}
editor_action* mouse_action_select::click_perform_left(
editor_display& /*disp*/, const std::set<map_location>& hexes)
{
return new editor_action_chain(new editor_action_select(hexes));
}
editor_action* mouse_action_select::click_perform_right(
editor_display& /*disp*/, const std::set<map_location>& /*hexes*/)
{
return NULL;
}
void mouse_action_select::set_mouse_overlay(editor_display& disp)
{
surface image;
if (has_shift_modifier()) {
image = image::get_image("editor/tool-overlay-select-wand.png");
} else {
image = image::get_image("editor/tool-overlay-select-brush.png");
}
Uint8 alpha = 196;
int size = image->w;
int zoom = static_cast<int>(size * disp.get_zoom_factor());
// Add the alpha factor and scale the image
image = scale_surface(adjust_surface_alpha(image, alpha), zoom, zoom);
disp.set_mouseover_hex_overlay(image);
}
bool mouse_action_paste::has_context_menu() const
@ -373,11 +328,6 @@ editor_action* mouse_action_paste::click_right(editor_display& /*disp*/, int /*x
return NULL;
}
editor_action* mouse_action_select::click_right(editor_display& /*disp*/, int /*x*/, int /*y*/)
{
return NULL;
}
void mouse_action_paste::set_mouse_overlay(editor_display& disp)
{
surface image60 = image::get_image("icons/action/editor-paste_60.png");

View File

@ -286,48 +286,7 @@ protected:
};
/**
* Select (and deselect) action, by brush or "magic wand" (via keyboard modifier)
*/
class mouse_action_select : public brush_drag_mouse_action
{
public:
mouse_action_select(const brush* const * const brush, const CKey& key, empty_palette& palette)
: brush_drag_mouse_action(palette, brush, key)
{
}
/**
* Overridden to allow special behavior based on modifier keys
*/
std::set<map_location> affected_hexes(editor_display& disp, const map_location& hex);
/**
* Force a fake "move" event to update brush overlay on key event
*/
editor_action* key_event(editor_display& disp, const SDL_Event& e);
/**
* Left click/drag selects
*/
editor_action* click_perform_left(editor_display& disp, const std::set<map_location>& hexes);
/**
* Right click does nothing for now
*/
editor_action* click_right(editor_display& disp, int x, int y);
/**
* Right click/drag deselects
*/
editor_action* click_perform_right(editor_display& disp, const std::set<map_location>& hexes);
virtual void set_mouse_overlay(editor_display& disp);
bool has_context_menu() const { return true; };
bool supports_brushes() { return true; }
};
/**
* Paste action. No dragging capabilities.

View File

@ -0,0 +1,77 @@
/*
Copyright (C) 2008 - 2011 by Fabian Mueller <fabianmueller5@gmx.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 as published by
the Free Software Foundation; either version 2 of the License, 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.
*/
//TODO uncomment or remove
//#define GETTEXT_DOMAIN "wesnoth-editor"
#include "mouse_action_select.hpp"
#include "../action_select.hpp"
#include "../../editor_display.hpp"
namespace editor {
std::set<map_location> mouse_action_select::affected_hexes(
editor_display& disp, const map_location& hex)
{
if (has_shift_modifier()) {
return disp.map().get_contiguous_terrain_tiles(hex);
} else {
return brush_drag_mouse_action::affected_hexes(disp, hex);
}
}
editor_action* mouse_action_select::key_event(
editor_display& disp, const SDL_Event& event)
{
editor_action* ret = mouse_action::key_event(disp, event);
update_brush_highlights(disp, previous_move_hex_);
return ret;
}
editor_action* mouse_action_select::click_perform_left(
editor_display& /*disp*/, const std::set<map_location>& hexes)
{
return new editor_action_chain(new editor_action_select(hexes));
}
editor_action* mouse_action_select::click_perform_right(
editor_display& /*disp*/, const std::set<map_location>& /*hexes*/)
{
return NULL;
}
editor_action* mouse_action_select::click_right(editor_display& /*disp*/, int /*x*/, int /*y*/)
{
return NULL;
}
void mouse_action_select::set_mouse_overlay(editor_display& disp)
{
surface image;
if (has_shift_modifier()) {
image = image::get_image("editor/tool-overlay-select-wand.png");
} else {
image = image::get_image("editor/tool-overlay-select-brush.png");
}
Uint8 alpha = 196;
int size = image->w;
int zoom = static_cast<int>(size * disp.get_zoom_factor());
// Add the alpha factor and scale the image
image = scale_surface(adjust_surface_alpha(image, alpha), zoom, zoom);
disp.set_mouseover_hex_overlay(image);
}
} //end namespace editor

View File

@ -0,0 +1,71 @@
/*
Copyright (C) 2008 - 2011 by Fabian Mueller <fabianmueller5@gmx.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 as published by
the Free Software Foundation; either version 2 of the License, 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 EDITOR_MOUSE_ACTION_SELECT_HPP
#define EDITOR_MOUSE_ACTION_SELECT_HPP
#include "mouse_action.hpp"
class CKey;
class empty_palette;
namespace editor {
/**
* Select (and deselect) action, by brush or "magic wand" (via keyboard modifier)
*/
class mouse_action_select : public brush_drag_mouse_action
{
public:
mouse_action_select(const brush* const * const brush, const CKey& key, empty_palette& palette)
: brush_drag_mouse_action(palette, brush, key)
{
}
/**
* Overridden to allow special behavior based on modifier keys
*/
std::set<map_location> affected_hexes(editor_display& disp, const map_location& hex);
/**
* Force a fake "move" event to update brush overlay on key event
*/
editor_action* key_event(editor_display& disp, const SDL_Event& e);
/**
* Left click/drag selects
*/
editor_action* click_perform_left(editor_display& disp, const std::set<map_location>& hexes);
/**
* Right click does nothing for now
*/
editor_action* click_right(editor_display& disp, int x, int y);
/**
* Right click/drag
*/
editor_action* click_perform_right(editor_display& disp, const std::set<map_location>& hexes);
virtual void set_mouse_overlay(editor_display& disp);
bool has_context_menu() const { return true; };
bool supports_brushes() { return true; }
};
} //end namespace editor
#endif

View File

@ -20,6 +20,7 @@
#include "asserts.hpp"
#include "editor/action/action.hpp"
#include "editor/action/action_unit.hpp"
#include "editor/action/action_select.hpp"
#include "editor_controller.hpp"
#include "editor/palette/terrain_palettes.hpp"

View File

@ -20,6 +20,7 @@
#include "editor/action/mouse/mouse_action_unit.hpp"
#include "editor/action/mouse/mouse_action_village.hpp"
#include "editor/action/mouse/mouse_action_item.hpp"
#include "editor/action/mouse/mouse_action_select.hpp"
#include <boost/foreach.hpp>