editor2: resize map dialog,

...fix resizing issues that also came up in flipping, made size
members in map protected to allow said fix. Flipping still broken but
at least will no longer occasionally crash.
This commit is contained in:
Tomasz Śniatowski 2008-08-08 16:41:21 +01:00
parent 4110e8564f
commit 0c918613e2
13 changed files with 386 additions and 6 deletions

View file

@ -0,0 +1,168 @@
###
### Definition of the window to create a new map in editor2.
###
[window]
id = "editor_resize_map"
description = "Resize map dialog."
[resolution]
definition = "default"
automatic_placement = "true"
vertical_placement = "center"
horizontal_placement = "center"
[grid]
[row]
grow_factor = 0
[column]
grow_factor = 1
border = "all"
border_size = 5
horizontal_alignment = "left"
[label]
definition = "title"
label = _ "Resize Map"
[/label]
[/column]
[/row]
[row]
grow_factor = 0
[column]
grow_factor = 1
horizontal_grow = "true"
[grid]
[row]
grow_factor = 1
[column]
grow_factor = 0
border = "all"
border_size = 5
horizontal_alignment = "left"
[label]
definition = "default"
label = _ "New width:"
[/label]
[/column]
[column]
grow_factor = 1
border = "all"
border_size = 5
horizontal_alignment = "left"
[slider]
id = "width"
definition = "default"
minimum_value = 1
maximum_value = 200
step_size = 1
[/slider]
[/column]
[column]
grow_factor = 0
border = "all"
border_size = 5
horizontal_alignment = "left"
[label]
definition = "default"
label = _ "Old width:"
[/label]
[/column]
[column]
grow_factor = 0
border = "all"
border_size = 5
horizontal_alignment = "left"
[label]
id = "old_width"
definition = "default"
label = _ "-1"
[/label]
[/column]
[/row]
[row]
grow_factor = 1
[column]
grow_factor = 0
border = "all"
border_size = 5
horizontal_alignment = "left"
[label]
definition = "default"
label = _ "New height:"
[/label]
[/column]
[column]
grow_factor = 1
border = "all"
border_size = 5
horizontal_alignment = "left"
[slider]
id = "height"
definition = "default"
minimum_value = 1
maximum_value = 200
step_size = 1
[/slider]
[/column]
[column]
grow_factor = 0
border = "all"
border_size = 5
horizontal_alignment = "left"
[label]
definition = "default"
label = _ "Old height:"
[/label]
[/column]
[column]
grow_factor = 0
border = "all"
border_size = 5
horizontal_alignment = "left"
[label]
id = "old_height"
definition = "default"
label = _ "-1"
[/label]
[/column]
[/row]
[/grid]
[/column]
[/row]
[row]
grow_factor = 0
[column]
grow_factor = 1
horizontal_grow = "true"
[grid]
[row]
grow_factor = 0
[column]
border = "all"
border_size = 5
horizontal_alignment = "right"
[button]
id = "ok"
definition = "default"
size_text = _ "Resize map"
label = _ "Resize map"
[/button]
[/column]
[column]
border = "all"
border_size = 5
horizontal_alignment = "right"
[button]
id = "cancel"
definition = "default"
label = _ "Cancel"
[/button]
[/column]
[/row]
[/grid]
[/column]
[/row]
[/grid]
[/resolution]
[/window]

View file

@ -209,7 +209,7 @@ SET(wesnoth-main_SRC
attack_prediction.cpp
attack_prediction_display.cpp
callable_objects.cpp
config_cache.cpp
config_cache.cpp
config_adapter.cpp
controller_base.cpp
dialogs.cpp
@ -300,8 +300,8 @@ SET(wesnoth-main_SRC
variable.cpp
variant.cpp
widgets/combo.cpp
widgets/combo_drag.cpp
widgets/drop_target.cpp
widgets/combo_drag.cpp
widgets/drop_target.cpp
widgets/scrollpane.cpp
)
@ -310,6 +310,7 @@ IF(ENABLE_EDITOR2)
SET(wesnoth-editor2_SRC
gui/dialogs/editor_generate_map.cpp
gui/dialogs/editor_resize_map.cpp
gui/dialogs/editor_new_map.cpp
editor2/action.cpp
editor2/brush.cpp

View file

@ -156,6 +156,7 @@ wesnoth_source = \
wesnoth_editor2_SOURCES = \
gui/dialogs/editor_generate_map.cpp \
gui/dialogs/editor_new_map.cpp \
gui/dialogs/editor_resize_map.cpp \
editor2/action.cpp \
editor2/brush.cpp \
editor2/editor_main.cpp \

View file

@ -248,6 +248,7 @@ wesnoth_sources.extend(python_env.Object(Split("""
wesnoth_editor2_sources = Split("""
gui/dialogs/editor_generate_map.cpp
gui/dialogs/editor_new_map.cpp
gui/dialogs/editor_resize_map.cpp
editor2/action.cpp
editor2/brush.cpp
editor2/editor_main.cpp

View file

@ -21,6 +21,7 @@
#include "gui/dialogs/editor_new_map.hpp"
#include "gui/dialogs/editor_generate_map.hpp"
#include "gui/dialogs/editor_resize_map.hpp"
#include "gui/widgets/button.hpp"
#include "gui/widgets/window.hpp"
@ -204,7 +205,6 @@ void editor_controller::save_map_as_dialog()
save_map_as(input_name);
}
void editor_controller::generate_map_dialog()
{
if (map_generator_ == NULL) {
@ -228,6 +228,24 @@ void editor_controller::generate_map_dialog()
}
}
void editor_controller::resize_map_dialog()
{
gui2::teditor_resize_map dialog;
dialog.set_map_width(get_map().total_width());
dialog.set_map_height(get_map().total_height());
dialog.show(gui().video());
int res = dialog.get_retval();
if(res == gui2::twindow::OK) {
int w = dialog.map_width();
int h = dialog.map_height();
t_translation::t_terrain fill = t_translation::GRASS_LAND;
editor_action_resize_map a(w, h, 0, 0);
get_map_context().perform_action(a);
refresh_after_action();
}
}
bool editor_controller::save_map_as(const std::string& filename)
{
std::string old_filename = get_map_context().get_filename();
@ -470,6 +488,10 @@ bool editor_controller::execute_command(hotkey::HOTKEY_COMMAND command, int inde
case HOTKEY_EDITOR_MAP_GENERATE:
generate_map_dialog();
return true;
case HOTKEY_EDITOR_MAP_RESIZE:
std::cerr << "A";
resize_map_dialog();
return true;
case HOTKEY_EDITOR_AUTO_UPDATE_TRANSITIONS:
auto_update_transitions_ = !auto_update_transitions_;
if (!auto_update_transitions_) {

View file

@ -71,6 +71,7 @@ class editor_controller : public controller_base,
void new_map_dialog();
void save_map_as_dialog();
void generate_map_dialog();
void resize_map_dialog();
bool save_map(bool display_confirmation = false);
bool save_map_as(const std::string& filename);
void new_map(int width, int height, t_translation::t_terrain fill);

View file

@ -30,18 +30,44 @@ namespace editor2 {
editor_map::editor_map(const config& terrain_cfg, const std::string& data)
: gamemap(terrain_cfg, data)
{
sanity_check();
}
editor_map::editor_map(const config& terrain_cfg, size_t width, size_t height, t_translation::t_terrain filler)
: gamemap(terrain_cfg, gamemap::default_map_header + t_translation::write_game_map(
t_translation::t_map(width, t_translation::t_list(height, filler))))
{
sanity_check();
}
editor_map::~editor_map()
{
}
void editor_map::sanity_check()
{
int errors = 0;
if (total_width() != tiles_.size()) {
ERR_ED << "total_width is " << total_width() << " but tiles_.size() is " << tiles_.size() << "\n";
++errors;
}
if (total_height() != tiles_[0].size()) {
ERR_ED << "total_height is " << total_height() << " but tiles_[0].size() is " << tiles_.size() << "\n";
++errors;
}
if (w() + 2 * border_size() != total_width()) {
ERR_ED << "h is " << h_ << " and border_size is " << border_size() << " but total_width is " << total_width() << "\n";
++errors;
}
if (h() + 2 * border_size() != total_height()) {
ERR_ED << "w is " << w_ << " and border_size is " << border_size() << " but total_height is " << total_height() << "\n";
++errors;
}
if (errors) {
throw editor_map_integrity_error();
}
}
std::set<gamemap::location> editor_map::get_contigious_terrain_tiles(const gamemap::location& start) const
{
t_translation::t_terrain terrain = get_terrain(start);
@ -246,6 +272,8 @@ void editor_map::expand_right(int count, t_translation::t_terrain filler)
for (int x = 0; x < count; ++x) {
tiles_.push_back(clone_column(w, filler));
}
w_ += count;
total_width_ += count;
}
void editor_map::expand_left(int count, t_translation::t_terrain filler)
@ -254,6 +282,8 @@ void editor_map::expand_left(int count, t_translation::t_terrain filler)
tiles_.insert(tiles_.begin(), 1, clone_column(0, filler));
clear_border_cache();
}
w_ += count;
total_width_ += count;
}
void editor_map::expand_top(int count, t_translation::t_terrain filler)
@ -269,6 +299,8 @@ void editor_map::expand_top(int count, t_translation::t_terrain filler)
clear_border_cache();
}
}
h_ += count;
total_height_ += count;
}
void editor_map::expand_bottom(int count, t_translation::t_terrain filler)
@ -284,6 +316,8 @@ void editor_map::expand_bottom(int count, t_translation::t_terrain filler)
tiles_[x].push_back(terrain);
}
}
h_ += count;
total_height_ += count;
}
void editor_map::shrink_right(int count)
@ -292,6 +326,8 @@ void editor_map::shrink_right(int count)
throw editor_map_operation_exception();
}
tiles_.resize(tiles_.size() - count);
w_ -= count;
total_width_ -= count;
}
void editor_map::shrink_left(int count)
@ -300,6 +336,8 @@ void editor_map::shrink_left(int count)
throw editor_map_operation_exception();
}
tiles_.erase(tiles_.begin(), tiles_.begin() + count);
w_ -= count;
total_width_ -= count;
}
void editor_map::shrink_top(int count)
@ -310,6 +348,8 @@ void editor_map::shrink_top(int count)
for (size_t x = 0; x < tiles_.size(); ++x) {
tiles_[x].erase(tiles_[x].begin(), tiles_[x].begin() + count);
}
h_ -= count;
total_height_ -= count;
}
void editor_map::shrink_bottom(int count)
@ -320,6 +360,8 @@ void editor_map::shrink_bottom(int count)
for (size_t x = 0; x < tiles_.size(); ++x) {
tiles_[x].erase(tiles_[x].end() - count, tiles_[x].end());
}
h_ -= count;
total_height_ -= count;
}

View file

@ -27,6 +27,10 @@ struct editor_map_operation_exception : public editor_exception
{
};
struct editor_map_integrity_error : public editor_exception
{
};
/**
* This class adds extra editor-specific functionality to a normal gamemap.
*/
@ -40,6 +44,11 @@ public:
~editor_map();
/**
* Debugging aid. Check if the widths and heights correspond to the actual map data sizes.
*/
void sanity_check();
/**
* Get a contigious set of tiles having the same terrain as the starting location.
* Useful for flood fill or magic wand selection

View file

@ -0,0 +1,70 @@
/* $Id$ */
/*
copyright (C) 2008 by mark de wever <koraq@xs4all.nl>
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 "gui/dialogs/editor_resize_map.hpp"
#include "foreach.hpp"
#include "gui/dialogs/field.hpp"
#include "gui/widgets/button.hpp"
#include "gui/widgets/listbox.hpp"
#include "gui/widgets/settings.hpp"
#include "gui/widgets/text_box.hpp"
#include "gui/widgets/slider.hpp"
#include "gui/widgets/window.hpp"
#include "gui/widgets/window_builder.hpp"
#include "language.hpp"
#include "log.hpp"
#include "preferences.hpp"
#include "video.hpp"
#include "wml_exception.hpp"
#define DBG_GUI LOG_STREAM_INDENT(debug, widget)
#define LOG_GUI LOG_STREAM_INDENT(info, widget)
#define WRN_GUI LOG_STREAM_INDENT(warn, widget)
#define ERR_GUI LOG_STREAM_INDENT(err, widget)
namespace gui2 {
teditor_resize_map::teditor_resize_map() :
map_width_(register_integer("width", false)),
map_height_(register_integer("height", false))
{
}
void teditor_resize_map::set_map_width(int value)
{
map_width_->set_value(value);
}
int teditor_resize_map::map_width() const
{
return map_width_->get_value();
}
void teditor_resize_map::set_map_height(int value)
{
map_height_->set_value(value);
}
int teditor_resize_map::map_height() const
{
return map_height_->get_value();
}
twindow teditor_resize_map::build_window(CVideo& video)
{
return build(video, get_id(EDITOR_RESIZE_MAP));
}
} // namespace gui2

View file

@ -0,0 +1,61 @@
/* $Id$ */
/*
copyright (c) 2008 by mark de wever <koraq@xs4all.nl>
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 GUI_DIALOGS_EDITOR_RESIZE_MAP_HPP_INCLUDED
#define GUI_DIALOGS_EDITOR_RESIZE_MAP_HPP_INCLUDED
#include "gui/dialogs/dialog.hpp"
namespace gui2 {
class teditor_resize_map : public tdialog
{
public:
teditor_resize_map();
void set_map_width(int value);
int map_width() const;
void set_map_height(int value);
int map_height() const;
enum EXPAND_DIRECTION {
EXPAND_BOTTOM_RIGHT,
EXPAND_BOTTOM,
EXPAND_BOTTOM_LEFT,
EXPAND_RIGHT,
EXPAND_CENTER,
EXPAND_LEFT,
EXPAND_TOP_RIGHT,
EXPAND_TOP,
EXPAND_TOP_LEFT
};
EXPAND_DIRECTION expand_direction() { return EXPAND_BOTTOM_RIGHT; }
private:
/**
* NOTE the map sizes are stored in a text variable since there is no
* integer edit widget yet.
*/
tfield_integer* map_width_;
tfield_integer* map_height_;
/** Inherited from tdialog. */
twindow build_window(CVideo& video);
};
} // namespace gui2
#endif

View file

@ -91,6 +91,7 @@ static void fill_window_types()
#ifdef USE_EDITOR2
window_type_list[EDITOR_NEW_MAP] = "editor_new_map";
window_type_list[EDITOR_GENERATE_MAP] = "editor_generate_map";
window_type_list[EDITOR_RESIZE_MAP] = "editor_resize_map";
#endif
}

View file

@ -44,6 +44,7 @@ enum twindow_type {
#ifdef USE_EDITOR2
EDITOR_NEW_MAP, //<! New map dialog
EDITOR_GENERATE_MAP, /** Editor random map genarator dialog */
EDITOR_RESIZE_MAP, /** Editor resize map dialog */
#endif
DUMMY //<! Dummy always the last one.
};

View file

@ -281,7 +281,7 @@ public:
//! (using the default base if new terrain is an overlay terrain)
//! Will return the resulting terrain or NONE_TERRAIN if merging failed
t_translation::t_terrain merge_terrains(const t_translation::t_terrain old_t, const t_translation::t_terrain new_t, const tmerge_mode mode, bool replace_if_failed = false);
protected:
t_translation::t_map tiles_;
//! The size of the starting positions array is MAX_PLAYERS + 1,
@ -312,6 +312,7 @@ private:
mutable std::map<location, t_translation::t_terrain> borderCache_;
mutable std::map<t_translation::t_terrain, size_t> terrainFrequencyCache_;
protected:
//! Sizes of the map area.
int w_;
int h_;
@ -319,7 +320,8 @@ private:
//! Sizes of the map including the borders.
int total_width_;
int total_height_;
private:
//! The size of the border around the map.
int border_size_;
//! The kind of map is being loaded.