editor2: WIP fill tool (use the menu to choose).
Uses some quick hacks that will be cleaned up later. Also removed code that duplicated pathutils.?pp functionality.
This commit is contained in:
parent
17ed111a90
commit
5cefca8793
8 changed files with 100 additions and 36 deletions
|
@ -70,11 +70,8 @@ void editor_action_paste::perform_without_undo(editor_map& /*map*/) const
|
|||
|
||||
editor_action_paint_hex* editor_action_paint_hex::perform(editor_map& map) const
|
||||
{
|
||||
SCOPE_ED;
|
||||
LOG_ED << "Old terrain is " << map.get_terrain_string(loc_) << ", new will be " << map.get_terrain_string(t_) << "\n";
|
||||
editor_action_paint_hex* undo = new editor_action_paint_hex(loc_, map.get_terrain(loc_));
|
||||
perform_without_undo(map);
|
||||
LOG_ED << "Now the terrain is " << map.get_terrain_string(loc_) << "\n";
|
||||
return undo;
|
||||
}
|
||||
void editor_action_paint_hex::perform_without_undo(editor_map& map) const
|
||||
|
@ -91,13 +88,18 @@ void editor_action_paint_brush::perform_without_undo(editor_map& /*map*/) const
|
|||
throw editor_action_not_implemented();
|
||||
}
|
||||
|
||||
editor_action_fill* editor_action_fill::perform(editor_map& /*map*/) const
|
||||
editor_action_fill* editor_action_fill::perform(editor_map& map) const
|
||||
{
|
||||
throw editor_action_not_implemented();
|
||||
editor_action_fill* undo = new editor_action_fill(loc_, map.get_terrain(loc_));
|
||||
perform_without_undo(map);
|
||||
return undo;
|
||||
}
|
||||
void editor_action_fill::perform_without_undo(editor_map& /*map*/) const
|
||||
void editor_action_fill::perform_without_undo(editor_map& map) const
|
||||
{
|
||||
throw editor_action_not_implemented();
|
||||
std::set<gamemap::location> to_fill = map.get_contigious_terrain_tiles(loc_);
|
||||
foreach (gamemap::location l, to_fill) {
|
||||
map.set_terrain(l, t_);
|
||||
}
|
||||
}
|
||||
|
||||
editor_action_whole_map* editor_action_resize_map::perform(editor_map& /*map*/) const
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
#include "editor_map.hpp"
|
||||
|
||||
#include "../foreach.hpp"
|
||||
#include "../pathutils.hpp"
|
||||
|
||||
#include <vector>
|
||||
|
||||
|
@ -29,8 +30,8 @@ brush::brush(const config& cfg)
|
|||
{
|
||||
int radius = lexical_cast_default<int>(cfg["radius"], 0);
|
||||
if (radius > 0) {
|
||||
std::vector<gamemap::location> in_radius =
|
||||
editor_map::get_tiles_in_radius(gamemap::location(0, 0), radius);
|
||||
std::vector<gamemap::location> in_radius;
|
||||
get_tiles_in_radius(gamemap::location(0, 0), radius, in_radius);
|
||||
foreach (gamemap::location& loc, in_radius) {
|
||||
add_relative_location(loc.x, loc.y);
|
||||
}
|
||||
|
|
|
@ -37,7 +37,6 @@ editor_controller::editor_controller(const config &game_config, CVideo& video)
|
|||
hotkey::set_scope_active(hotkey::SCOPE_GENERAL);
|
||||
hotkey::set_scope_active(hotkey::SCOPE_EDITOR);
|
||||
init(video);
|
||||
set_mouse_action(new mouse_action_paint(*this));
|
||||
cursor::set(cursor::NORMAL);
|
||||
gui_->invalidate_game_status();
|
||||
gui_->invalidate_all();
|
||||
|
@ -47,8 +46,11 @@ editor_controller::editor_controller(const config &game_config, CVideo& video)
|
|||
brushes_[0].add_relative_location(0,0);
|
||||
brushes_[0].add_relative_location(1,0);
|
||||
brushes_[0].add_relative_location(-1,0);
|
||||
brushes_[0].add_relative_location(-2,0);
|
||||
set_brush(&brushes_[0]);
|
||||
//redraw_everything();
|
||||
mouse_actions_.push_back(new mouse_action_paint(*this));
|
||||
mouse_actions_.push_back(new mouse_action_fill(*this));
|
||||
set_mouse_action(mouse_actions_[0]);
|
||||
|
||||
}
|
||||
|
||||
|
@ -66,6 +68,9 @@ editor_controller::~editor_controller()
|
|||
delete gui_;
|
||||
clear_stack(undo_stack_);
|
||||
clear_stack(redo_stack_);
|
||||
foreach (mouse_action* a, mouse_actions_) {
|
||||
delete a;
|
||||
}
|
||||
}
|
||||
|
||||
void editor_controller::main_loop()
|
||||
|
@ -78,7 +83,7 @@ void editor_controller::main_loop()
|
|||
bool editor_controller::can_execute_command(hotkey::HOTKEY_COMMAND command, int /*index*/) const
|
||||
{
|
||||
using namespace hotkey; //reduce hotkey:: clutter
|
||||
switch(command) {
|
||||
switch (command) {
|
||||
case HOTKEY_ZOOM_IN:
|
||||
case HOTKEY_ZOOM_OUT:
|
||||
case HOTKEY_ZOOM_DEFAULT:
|
||||
|
@ -127,6 +132,21 @@ bool editor_controller::can_execute_command(hotkey::HOTKEY_COMMAND command, int
|
|||
}
|
||||
}
|
||||
|
||||
bool editor_controller::execute_command(hotkey::HOTKEY_COMMAND command, int index)
|
||||
{
|
||||
using namespace hotkey;
|
||||
switch (command) {
|
||||
case HOTKEY_EDITOR_TOOL_PAINT:
|
||||
set_mouse_action(mouse_actions_[0]);
|
||||
return true;
|
||||
case HOTKEY_EDITOR_TOOL_FILL:
|
||||
set_mouse_action(mouse_actions_[1]);
|
||||
return true;
|
||||
default:
|
||||
return controller_base::execute_command(command, index);
|
||||
}
|
||||
}
|
||||
|
||||
void editor_controller::toggle_grid()
|
||||
{
|
||||
preferences::set_grid(!preferences::grid());
|
||||
|
|
|
@ -44,6 +44,7 @@ class editor_controller : public controller_base,
|
|||
~editor_controller();
|
||||
void main_loop();
|
||||
bool can_execute_command(hotkey::HOTKEY_COMMAND, int index = -1) const;
|
||||
bool execute_command(hotkey::HOTKEY_COMMAND command, int index = -1);
|
||||
void preferences();
|
||||
void toggle_grid();
|
||||
|
||||
|
@ -137,6 +138,7 @@ class editor_controller : public controller_base,
|
|||
static const int max_action_stack_size_;
|
||||
|
||||
std::vector<brush> brushes_;
|
||||
std::vector<mouse_action*> mouse_actions_;
|
||||
};
|
||||
|
||||
} //end namespace editor2
|
||||
|
|
|
@ -14,6 +14,11 @@
|
|||
|
||||
#include "editor_map.hpp"
|
||||
|
||||
#include "../pathutils.hpp"
|
||||
|
||||
#include <deque>
|
||||
|
||||
|
||||
namespace editor2 {
|
||||
|
||||
editor_map::editor_map(const config& terrain_cfg, const std::string& data)
|
||||
|
@ -27,29 +32,28 @@ editor_map editor_map::new_map(const config& terrain_cfg, size_t width, size_t h
|
|||
const t_translation::t_map map(width, column);
|
||||
return editor_map(terrain_cfg, gamemap::default_map_header + t_translation::write_game_map(map));
|
||||
}
|
||||
|
||||
std::vector<gamemap::location> editor_map::get_tiles_in_radius(const gamemap::location& center, const unsigned int radius) {
|
||||
const unsigned int distance = radius - 1;
|
||||
std::vector<gamemap::location> res;
|
||||
res.push_back(center);
|
||||
for (unsigned int d = 1; d <= distance; d++) {
|
||||
gamemap::location loc = center;
|
||||
unsigned int i;
|
||||
// Get the starting point.
|
||||
for (i = 1; i <= d; i++) {
|
||||
loc = loc.get_direction(gamemap::location::NORTH, 1);
|
||||
}
|
||||
// Get all the tiles clockwise with distance d.
|
||||
const gamemap::location::DIRECTION direction[6] =
|
||||
{gamemap::location::SOUTH_EAST, gamemap::location::SOUTH, gamemap::location::SOUTH_WEST,
|
||||
gamemap::location::NORTH_WEST, gamemap::location::NORTH, gamemap::location::NORTH_EAST};
|
||||
for (i = 0; i < 6; i++) {
|
||||
for (unsigned int j = 1; j <= d; j++) {
|
||||
loc = loc.get_direction(direction[i], 1);
|
||||
|
||||
std::set<gamemap::location> editor_map::get_contigious_terrain_tiles(const gamemap::location& start) const
|
||||
{
|
||||
t_translation::t_terrain terrain = get_terrain(start);
|
||||
std::set<gamemap::location> result;
|
||||
std::deque<gamemap::location> queue;
|
||||
result.insert(start);
|
||||
queue.push_back(start);
|
||||
//this is basically a breadth-first search along adjacent hexes
|
||||
do {
|
||||
gamemap::location adj[6];
|
||||
get_adjacent_tiles(queue.front(), adj);
|
||||
for (int i = 0; i < 6; ++i) {
|
||||
if (on_board_with_border(adj[i]) && get_terrain(adj[i]) == terrain
|
||||
&& result.find(adj[i]) == result.end()) {
|
||||
result.insert(adj[i]);
|
||||
queue.push_back(adj[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return res;
|
||||
queue.pop_front();
|
||||
} while (!queue.empty());
|
||||
return result;
|
||||
}
|
||||
|
||||
bool editor_map::in_selection(const gamemap::location& loc) const
|
||||
|
|
|
@ -27,9 +27,15 @@ class editor_map : public gamemap
|
|||
{
|
||||
public:
|
||||
editor_map(const config& terrain_cfg, const std::string& data);
|
||||
static std::vector<gamemap::location> get_tiles_in_radius(const gamemap::location& center, const unsigned int radius);
|
||||
static editor_map new_map(const config& terrain_cfg, size_t width, size_t height, t_translation::t_terrain filler);
|
||||
|
||||
/**
|
||||
* Get a contigious set of tiles having the same terrain as the starting location.
|
||||
* Useful for flood fill or magic wand selection
|
||||
* @return a contigious set of locations that will always contain at least the starting element
|
||||
*/
|
||||
std::set<gamemap::location> get_contigious_terrain_tiles(const gamemap::location& start) const;
|
||||
|
||||
/**
|
||||
* @return true when the location is part of the selection, false otherwise
|
||||
*/
|
||||
|
|
|
@ -71,4 +71,24 @@ editor_action* mouse_action_paint::drag_end(editor_display& disp, int x, int y)
|
|||
return drag(disp, x, y);
|
||||
}
|
||||
|
||||
void mouse_action_fill::move(editor_display& disp, int x, int y)
|
||||
{
|
||||
disp.clear_highlighted_locs();
|
||||
std::set<gamemap::location> affected =
|
||||
dynamic_cast<const editor_map&>(disp.get_map()).
|
||||
get_contigious_terrain_tiles(disp.hex_clicked_on(x, y));
|
||||
foreach (gamemap::location loc, affected) {
|
||||
disp.add_highlighted_loc(loc);
|
||||
}
|
||||
}
|
||||
|
||||
editor_action* mouse_action_fill::click(editor_display& disp, int x, int y)
|
||||
{
|
||||
gamemap::location hex = disp.hex_clicked_on(x, y);
|
||||
t_translation::t_terrain terrain = mode_.get_foreground_terrain();
|
||||
editor_action_fill* a = new editor_action_fill(hex, terrain);
|
||||
previous_hex_ = hex;
|
||||
return a;
|
||||
}
|
||||
|
||||
} //end namespace editor2
|
||||
|
|
|
@ -58,6 +58,7 @@ public:
|
|||
|
||||
protected:
|
||||
editor_mode& mode_;
|
||||
gamemap::location previous_hex_;
|
||||
};
|
||||
|
||||
class mouse_action_paint : public mouse_action
|
||||
|
@ -71,10 +72,18 @@ public:
|
|||
editor_action* click(editor_display& disp, int x, int y);
|
||||
editor_action* drag(editor_display& disp, int x, int y);
|
||||
editor_action* drag_end(editor_display& disp, int x, int y);
|
||||
protected:
|
||||
gamemap::location previous_hex_;
|
||||
};
|
||||
|
||||
class mouse_action_fill : public mouse_action
|
||||
{
|
||||
public:
|
||||
mouse_action_fill(editor_mode& mode)
|
||||
: mouse_action(mode)
|
||||
{
|
||||
}
|
||||
void move(editor_display& disp, int x, int y);
|
||||
editor_action* click(editor_display& disp, int x, int y);
|
||||
};
|
||||
|
||||
} //end namespace editor2
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue