gui/dialogs/editor_resize_map.cpp

Go to the documentation of this file.
00001 /* $Id: editor_resize_map.cpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2008 - 2012 by Mark de Wever <koraq@xs4all.nl>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 #define GETTEXT_DOMAIN "wesnoth-editor"
00016 
00017 #include "gui/dialogs/editor_resize_map.hpp"
00018 
00019 #include "gui/dialogs/field.hpp"
00020 #include "gui/dialogs/helper.hpp"
00021 #include "gui/widgets/toggle_button.hpp"
00022 #include "gui/widgets/settings.hpp"
00023 #include "gui/widgets/slider.hpp"
00024 
00025 #include <boost/bind.hpp>
00026 
00027 namespace gui2 {
00028 
00029 /*WIKI
00030  * @page = GUIWindowDefinitionWML
00031  * @order = 2_editor_resize_map
00032  *
00033  * == Editor resize map ==
00034  *
00035  * This shows the dialog to resize the current map.
00036  *
00037  * @begin{table}{dialog_widgets}
00038  *
00039  * old_width & & label & o &
00040  *         Shows the old width of the map. $
00041  *
00042  * old_height & & label & o &
00043  *         Shows the old height of the map. $
00044  *
00045  * width & & slider & m &
00046  *         Determines the new width of the map. $
00047  *
00048  * height & & slider & m &
00049  *         Determines the new height of the map. $
00050  *
00051  * copy_edge_terrain & & boolean_selector & m &
00052  *         Determines whether the border terrains should be used to expand or
00053  *         not. $
00054  *
00055  * expand0 & & toggle_button & m &
00056  *         Determines in which direction to expand, shows the north east
00057  *         marker. $
00058  *
00059  * expand1 & & toggle_button & m &
00060  *         Determines in which direction to expand, shows the north marker. $
00061  *
00062  * expand2 & & toggle_button & m &
00063  *         Determines in which direction to expand, shows the north west
00064  *         marker. $
00065  *
00066  * expand3 & & toggle_button & m &
00067  *         Determines in which direction to expand, shows the east marker. $
00068  *
00069  * expand4 & & toggle_button & m &
00070  *         Determines in which direction to expand, shows the centre marker. $
00071  *
00072  * expand5 & & toggle_button & m &
00073  *         Determines in which direction to expand, shows the west marker. $
00074  *
00075  * expand6 & & toggle_button & m &
00076  *         Determines in which direction to expand, shows the south east
00077  *         marker. $
00078  *
00079  * expand7 & & toggle_button & m &
00080  *         Determines in which direction to expand, shows the south marker. $
00081  *
00082  * expand8 & & toggle_button & m &
00083  *         Determines in which direction to expand, shows the south west
00084  *         marker. $
00085  *
00086  * @end{table}
00087  */
00088 
00089 REGISTER_DIALOG(editor_resize_map)
00090 
00091 teditor_resize_map::teditor_resize_map(
00092               int& width
00093             , int& height
00094             , EXPAND_DIRECTION& expand_direction
00095             , bool& copy_edge_terrain)
00096     : width_(register_integer("width", true, width))
00097     , height_(register_integer("height", true, height))
00098     , old_width_(width)
00099     , old_height_(height)
00100     , expand_direction_(expand_direction)
00101 {
00102     register_bool("copy_edge_terrain", false, copy_edge_terrain);
00103 
00104     register_label("old_width", false, str_cast(width));
00105     register_label("old_height", false, str_cast(height));
00106 }
00107 
00108 void teditor_resize_map::pre_show(CVideo& /*video*/, twindow& window)
00109 {
00110     tslider& height = find_widget<tslider>(&window, "height", false);
00111     connect_signal_notify_modified(height
00112             , boost::bind(
00113                   &teditor_resize_map::update_expand_direction
00114                 , this
00115                 , boost::ref(window)));
00116 
00117     tslider& width = find_widget<tslider>(&window, "width", false);
00118     connect_signal_notify_modified(width
00119             , boost::bind(
00120                   &teditor_resize_map::update_expand_direction
00121                 , this
00122                 , boost::ref(window)));
00123 
00124     std::string name_prefix = "expand";
00125     for (int i = 0; i < 9; ++i) {
00126         std::string name = name_prefix + lexical_cast<std::string>(i);
00127         direction_buttons_[i] = find_widget<ttoggle_button>(
00128                 &window, name, false, true);
00129 
00130         direction_buttons_[i]->set_callback_state_change(
00131                 dialog_callback<teditor_resize_map
00132                     , &teditor_resize_map::update_expand_direction>);
00133     }
00134     direction_buttons_[0]->set_value(true);
00135     update_expand_direction(window);
00136 }
00137 
00138 /**
00139  * Convert a coordinate on a 3  by 3 grid to an index, return 9 for out of
00140  * bounds
00141  */
00142 static int resize_grid_xy_to_idx(const int x, const int y)
00143 {
00144     if (x < 0 || x > 2 || y < 0 || y > 2) {
00145         return 9;
00146     } else {
00147         return y * 3 + x;
00148     }
00149 }
00150 
00151 void teditor_resize_map::set_direction_icon(int index, std::string icon)
00152 {
00153     if (index < 9) {
00154         direction_buttons_[index]->set_icon_name(
00155                 "buttons/resize-direction-" + icon + ".png");
00156     }
00157 }
00158 
00159 void teditor_resize_map::update_expand_direction(twindow& window)
00160 {
00161     for (int i = 0; i < 9; ++i) {
00162         if (direction_buttons_[i]->get_value()
00163                     && static_cast<int>(expand_direction_) != i) {
00164 
00165             expand_direction_ = static_cast<EXPAND_DIRECTION>(i);
00166             break;
00167         }
00168     }
00169     for (int i = 0; i < static_cast<int>(expand_direction_); ++i) {
00170         direction_buttons_[i]->set_value(false);
00171         set_direction_icon(i, "none");
00172     }
00173     direction_buttons_[expand_direction_]->set_value(true);
00174     for (int i = expand_direction_ + 1; i < 9; ++i) {
00175         direction_buttons_[i]->set_value(false);
00176         set_direction_icon(i, "none");
00177     }
00178 
00179     int xdiff = width_->get_widget_value(window) - old_width_ ;
00180     int ydiff = height_->get_widget_value(window) - old_height_ ;
00181     int x = static_cast<int>(expand_direction_) % 3;
00182     int y = static_cast<int>(expand_direction_) / 3;
00183     set_direction_icon(expand_direction_, "center");
00184     if (xdiff != 0) {
00185         int left = resize_grid_xy_to_idx(x - 1, y);
00186         int right = resize_grid_xy_to_idx(x + 1, y);
00187         if (xdiff < 0) std::swap(left, right);
00188         set_direction_icon(left, "left");
00189         set_direction_icon(right, "right");
00190     }
00191     if (ydiff != 0) {
00192         int top = resize_grid_xy_to_idx(x, y - 1);
00193         int bottom = resize_grid_xy_to_idx(x, y + 1);
00194         if (ydiff < 0) std::swap(top, bottom);
00195         set_direction_icon(top, "top");
00196         set_direction_icon(bottom, "bottom");
00197     }
00198     if (xdiff < 0 || ydiff < 0 || (xdiff > 0 && ydiff > 0)) {
00199         int nw = resize_grid_xy_to_idx(x - 1, y - 1);
00200         int ne = resize_grid_xy_to_idx(x + 1, y - 1);
00201         int sw = resize_grid_xy_to_idx(x - 1, y + 1);
00202         int se = resize_grid_xy_to_idx(x + 1, y + 1);
00203         if (xdiff < 0 || ydiff < 0) {
00204             std::swap(nw, se);
00205             std::swap(ne, sw);
00206         }
00207         set_direction_icon(nw, "top-left");
00208         set_direction_icon(ne, "top-right");
00209         set_direction_icon(sw, "bottom-left");
00210         set_direction_icon(se, "bottom-right");
00211     }
00212 }
00213 
00214 } // namespace gui2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Thu May 24 2012 01:02:42 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs