2 * Copyright (c) 2007-2013, Czirkos Zoltan http://code.google.com/p/gdash/
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include "cave/object/caveobjectfloodfill.hpp"
21 #include <glib/gi18n.h>
23 #include "fileops/bdcffhelper.hpp"
24 #include "cave/caverendered.hpp"
25 #include "misc/printf.hpp"
26 #include "cave/elementproperties.hpp"
28 /// Create a floodfill object.
29 /// @param _start Start coordinates of fill.
30 /// @param _search_element Overwrite this element.
31 /// @param _fill_element Draw this element.
32 CaveFloodFill::CaveFloodFill(Coordinate _start
, GdElementEnum _fill_element
, GdElementEnum _search_element
)
33 : CaveFill(GD_FLOODFILL_REPLACE
, _start
, _fill_element
),
34 search_element(_search_element
)
38 std::string
CaveFloodFill::get_bdcff() const
40 return BdcffFormat("FloodFill") << start
<< fill_element
<< search_element
;
43 CaveFloodFill
* CaveFloodFill::clone_from_bdcff(const std::string
&name
, std::istream
&is
) const
46 GdElementEnum fill
, search
;
47 if (!(is
>> start
>> fill
>> search
))
50 return new CaveFloodFill(start
, fill
, search
);
53 /// Standard recursive floodfill algorithm.
54 void CaveFloodFill::draw_proc(CaveRendered
&cave
, int x
, int y
) const
56 cave
.store_rc(x
, y
, fill_element
, this);
58 if (x
>0 && cave
.map(x
-1, y
)==search_element
) draw_proc(cave
, x
-1, y
);
59 if (y
>0 && cave
.map(x
, y
-1)==search_element
) draw_proc(cave
, x
, y
-1);
60 if (x
<cave
.w
-1 && cave
.map(x
+1, y
)==search_element
) draw_proc(cave
, x
+1, y
);
61 if (y
<cave
.h
-1 && cave
.map(x
, y
+1)==search_element
) draw_proc(cave
, x
, y
+1);
64 void CaveFloodFill::draw(CaveRendered
&cave
) const
67 if (start
.x
<0 || start
.y
<0 || start
.x
>=cave
.w
|| start
.y
>=cave
.h
)
69 if (search_element
==fill_element
)
71 /* this procedure fills the area with the object->element. */
72 draw_proc(cave
, start
.x
, start
.y
);
75 PropertyDescription
const CaveFloodFill::descriptor
[] = {
76 {"", GD_TAB
, 0, N_("Flood fill")},
77 {"", GD_TYPE_BOOLEAN_LEVELS
, 0, N_("Levels"), GetterBase::create_new(&CaveFloodFill::seen_on
), N_("Levels on which this object is visible.")},
78 {"", GD_TYPE_COORDINATE
, 0, N_("Start"), GetterBase::create_new(&CaveFloodFill::start
), N_("The coordinate to start the filling at."), 0, 127},
79 {"", GD_TYPE_ELEMENT
, 0, N_("Search element"), GetterBase::create_new(&CaveFloodFill::search_element
), N_("The element which specifies the area to be filled, and will be overwritten.")},
80 {"", GD_TYPE_ELEMENT
, 0, N_("Fill element"), GetterBase::create_new(&CaveFloodFill::fill_element
), N_("The element used to fill the area.")},
84 PropertyDescription
const* CaveFloodFill::get_description_array() const
89 std::string
CaveFloodFill::get_description_markup() const
91 return SPrintf(_("Flood fill from %d,%d of <b>%s</b>, replacing <b>%s</b>"))
92 % start
.x
% start
.y
% gd_element_properties
[fill_element
].lowercase_name
% gd_element_properties
[search_element
].lowercase_name
;