add basic and partial brush support

This commit is contained in:
Tomasz Śniatowski 2008-07-12 23:28:19 +01:00
parent 8633ed93c4
commit 1b96524ce6
7 changed files with 154 additions and 11 deletions

View file

@ -26,10 +26,6 @@
namespace editor2 {
class brush
{
};
/**
* Replace contents of the entire map,
* Useful as a fallback undo method when something else would be impractical
@ -168,14 +164,14 @@ class editor_action_paint_brush : public editor_action_location_terrain
{
public:
editor_action_paint_brush(gamemap::location loc,
t_translation::t_terrain t, brush b)
t_translation::t_terrain t, const brush& b)
: editor_action_location_terrain(loc, t), b_(b)
{
}
editor_action_paste* perform(editor_map& map) const;
void perform_without_undo(editor_map& map) const;
protected:
brush b_;
const brush& b_;
};
//flood fill
@ -183,14 +179,12 @@ class editor_action_fill : public editor_action_location_terrain
{
public:
editor_action_fill(gamemap::location loc,
t_translation::t_terrain t, brush b)
: editor_action_location_terrain(loc, t), b_(b)
t_translation::t_terrain t)
: editor_action_location_terrain(loc, t)
{
}
editor_action_fill* perform(editor_map& map) const;
void perform_without_undo(editor_map& map) const;
protected:
brush b_;
};
//resize map (streching / clipping behaviour?)

63
src/editor2/brush.cpp Normal file
View file

@ -0,0 +1,63 @@
/* $Id$ */
/*
Copyright (C) 2008 by Tomasz Sniatowski <kailoran@gmail.com>
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 version 2
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.
*/
#include "brush.hpp"
#include "editor_map.hpp"
#include "../foreach.hpp"
#include <vector>
namespace editor2 {
brush::brush()
{
}
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);
foreach (gamemap::location& loc, in_radius) {
add_relative_location(loc.x, loc.y);
}
}
config::const_child_itors cfg_range = cfg.child_range("relative");
while (cfg_range.first != cfg_range.second) {
const config& relative = **cfg_range.first;
int x = lexical_cast_default<int>(relative["x"], 0);
int y = lexical_cast_default<int>(relative["y"], 0);
add_relative_location(x, y);
++cfg_range.first;
}
}
void brush::add_relative_location(int relative_x, int relative_y)
{
relative_tiles_.insert(gamemap::location(relative_x, relative_y));
}
std::set<gamemap::location> brush::project(const gamemap::location& hotspot) const
{
std::set<gamemap::location> result;
foreach (const gamemap::location& relative, relative_tiles_) {
result.insert(hotspot + relative);
}
return result;
}
} //end namespace editor2

57
src/editor2/brush.hpp Normal file
View file

@ -0,0 +1,57 @@
/* $Id$ */
/*
Copyright (C) 2008 by Tomasz Sniatowski <kailoran@gmail.com>
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 version 2
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 EDITOR2_BRUSH_HPP_INCLUDED
#define EDITOR2_BRUSH_HPP_INCLUDED
#include "editor_map.hpp"
#include "../config.hpp"
#include <set>
namespace editor2 {
class brush
{
public:
/**
* Construct a default (empty) brush. Note that not even the hotspot is affected by default
*/
brush();
/**
* Construct a brush object from config
*/
brush(const config& cfg);
/**
* Add a location to the brush. If it already exists nothing will change.
*/
void add_relative_location(int relative_x, int relative_y);
/**
* Get a set of locations affected (i.e. under the brush) when the center (hotspot)
* is in given location
*/
std::set<gamemap::location> project(const gamemap::location& hotspot) const;
protected:
std::set<gamemap::location> relative_tiles_;
};
} //end namespace editor2
#endif

View file

@ -20,7 +20,6 @@
#include "../hotkeys.hpp"
#include "../preferences.hpp"
#include "SDL.h"
namespace editor2 {
@ -42,6 +41,11 @@ editor_controller::editor_controller(const config &game_config, CVideo& video)
gui_->invalidate_all();
gui_->draw();
events::raise_draw_event();
brushes_.push_back(brush());
brushes_[0].add_relative_location(0,0);
brushes_[0].add_relative_location(1,0);
brushes_[0].add_relative_location(-1,0);
set_brush(&brushes_[0]);
//redraw_everything();
}
@ -210,6 +214,10 @@ void editor_controller::mouse_motion(int x, int y, const bool browse, bool updat
} else {
LOG_ED << __FUNCTION__ << ": There is no mouse action active!\n";
}
} else {
if (get_mouse_action() != NULL) {
get_mouse_action()->move(*gui_, x, y);
}
}
const gamemap::location new_hex = gui().hex_clicked_on(x,y);
gui().highlight_hex(new_hex);

View file

@ -14,6 +14,7 @@
#ifndef EDITOR2_EDITOR_CONTROLLER_HPP_INCLUDED
#define EDITOR2_EDITOR_CONTROLLER_HPP_INCLUDED
#include "brush.hpp"
#include "action_base.hpp"
#include "editor_common.hpp"
#include "editor_display.hpp"
@ -133,6 +134,8 @@ class editor_controller : public controller_base,
* Action stack (i.e. undo and redo) maximum size
*/
static const int max_action_stack_size_;
std::vector<brush> brushes_;
};
} //end namespace editor2

View file

@ -13,6 +13,7 @@
*/
#include "action.hpp"
#include "brush.hpp"
#include "editor_common.hpp"
#include "editor_display.hpp"
#include "editor_mode.hpp"
@ -22,6 +23,10 @@
namespace editor2 {
void mouse_action::move(editor_display& disp, int x, int y)
{
}
editor_action* mouse_action::drag(editor_display& disp, int x, int y)
{
return NULL;
@ -32,6 +37,16 @@ editor_action* mouse_action::drag_end(editor_display& disp, int x, int y)
return NULL;
}
void mouse_action_paint::move(editor_display& disp, int x, int y)
{
disp.clear_highlighted_locs();
if (mode_.get_brush() != NULL) {
foreach (gamemap::location loc, mode_.get_brush()->project(disp.hex_clicked_on(x,y))) {
disp.add_highlighted_loc(loc);
}
}
}
editor_action* mouse_action_paint::click(editor_display& disp, int x, int y)
{
gamemap::location hex = disp.hex_clicked_on(x, y);

View file

@ -39,6 +39,8 @@ public:
virtual ~mouse_action() {}
virtual void move(editor_display& disp, int x, int y);
/**
* A click, possibly the beginning of a drag
*/
@ -65,6 +67,7 @@ public:
: mouse_action(mode)
{
}
void move(editor_display& disp, int x, int y);
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);