The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
resize_map.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2016 by Mark de Wever <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 #define GETTEXT_DOMAIN "wesnoth-editor"
15 
17 
18 #include "gui/auxiliary/field.hpp"
19 #include "gui/dialogs/helper.hpp"
21 #include "gui/widgets/settings.hpp"
22 #include "gui/widgets/slider.hpp"
23 
24 #include "utils/functional.hpp"
25 
26 namespace gui2
27 {
28 
29 /*WIKI
30  * @page = GUIWindowDefinitionWML
31  * @order = 2_editor_resize_map
32  *
33  * == Editor resize map ==
34  *
35  * This shows the dialog to resize the current map.
36  *
37  * @begin{table}{dialog_widgets}
38  *
39  * old_width & & label & o &
40  * Shows the old width of the map. $
41  *
42  * old_height & & label & o &
43  * Shows the old height of the map. $
44  *
45  * width & & slider & m &
46  * Determines the new width of the map. $
47  *
48  * height & & slider & m &
49  * Determines the new height of the map. $
50  *
51  * copy_edge_terrain & & boolean_selector & m &
52  * Determines whether the border terrains should be used to expand or
53  * not. $
54  *
55  * expand0 & & toggle_button & m &
56  * Determines in which direction to expand, shows the north east
57  * marker. $
58  *
59  * expand1 & & toggle_button & m &
60  * Determines in which direction to expand, shows the north marker. $
61  *
62  * expand2 & & toggle_button & m &
63  * Determines in which direction to expand, shows the north west
64  * marker. $
65  *
66  * expand3 & & toggle_button & m &
67  * Determines in which direction to expand, shows the east marker. $
68  *
69  * expand4 & & toggle_button & m &
70  * Determines in which direction to expand, shows the center marker. $
71  *
72  * expand5 & & toggle_button & m &
73  * Determines in which direction to expand, shows the west marker. $
74  *
75  * expand6 & & toggle_button & m &
76  * Determines in which direction to expand, shows the south east
77  * marker. $
78  *
79  * expand7 & & toggle_button & m &
80  * Determines in which direction to expand, shows the south marker. $
81  *
82  * expand8 & & toggle_button & m &
83  * Determines in which direction to expand, shows the south west
84  * marker. $
85  *
86  * @end{table}
87  */
88 
89 REGISTER_DIALOG(editor_resize_map)
90 
92  int& height,
93  EXPAND_DIRECTION& expand_direction,
94  bool& copy_edge_terrain)
95  : width_(register_integer("width", true, width))
96  , height_(register_integer("height", true, height))
97  , old_width_(width)
98  , old_height_(height)
99  , expand_direction_(expand_direction)
100 {
101  register_bool("copy_edge_terrain", false, copy_edge_terrain);
102 
103  register_label("old_width", false, std::to_string(width));
104  register_label("old_height", false, std::to_string(height));
105 }
106 
108 {
109  tslider& height = find_widget<tslider>(&window, "height", false);
111  height,
113  this,
114  std::ref(window)));
115 
116  tslider& width = find_widget<tslider>(&window, "width", false);
118  width,
120  this,
121  std::ref(window)));
122 
123  std::string name_prefix = "expand";
124  for(int i = 0; i < 9; ++i) {
125  std::string name = name_prefix + std::to_string(i);
127  = find_widget<ttoggle_button>(&window, name, false, true);
128 
132  }
133  direction_buttons_[0]->set_value(true);
134  update_expand_direction(window);
135 }
136 
137 /**
138  * Convert a coordinate on a 3 by 3 grid to an index, return 9 for out of
139  * bounds
140  */
141 static int resize_grid_xy_to_idx(const int x, const int y)
142 {
143  if(x < 0 || x > 2 || y < 0 || y > 2) {
144  return 9;
145  } else {
146  return y * 3 + x;
147  }
148 }
149 
151 {
152  if(index < 9) {
153  direction_buttons_[index]->set_icon_name("icons/arrows/arrows_blank_"
154  + icon + "_30.png");
155  }
156 }
157 
159 {
160  for(int i = 0; i < 9; ++i) {
161  if(direction_buttons_[i]->get_value()
162  && static_cast<int>(expand_direction_) != i) {
163 
164  expand_direction_ = static_cast<EXPAND_DIRECTION>(i);
165  break;
166  }
167  }
168  for(int i = 0; i < static_cast<int>(expand_direction_); ++i) {
169  direction_buttons_[i]->set_value(false);
170  set_direction_icon(i, "none");
171  }
173  for(int i = expand_direction_ + 1; i < 9; ++i) {
174  direction_buttons_[i]->set_value(false);
175  set_direction_icon(i, "none");
176  }
177 
178  int xdiff = width_->get_widget_value(window) - old_width_;
179  int ydiff = height_->get_widget_value(window) - old_height_;
180  int x = static_cast<int>(expand_direction_) % 3;
181  int y = static_cast<int>(expand_direction_) / 3;
183  if(xdiff != 0) {
184  int left = resize_grid_xy_to_idx(x - 1, y);
185  int right = resize_grid_xy_to_idx(x + 1, y);
186  if(xdiff < 0)
187  std::swap(left, right);
188  set_direction_icon(left, "left");
189  set_direction_icon(right, "right");
190  }
191  if(ydiff != 0) {
192  int top = resize_grid_xy_to_idx(x, y - 1);
193  int bottom = resize_grid_xy_to_idx(x, y + 1);
194  if(ydiff < 0)
195  std::swap(top, bottom);
196  set_direction_icon(top, "up");
197  set_direction_icon(bottom, "down");
198  }
199  if(xdiff < 0 || ydiff < 0 || (xdiff > 0 && ydiff > 0)) {
200  int nw = resize_grid_xy_to_idx(x - 1, y - 1);
201  int ne = resize_grid_xy_to_idx(x + 1, y - 1);
202  int sw = resize_grid_xy_to_idx(x - 1, y + 1);
203  int se = resize_grid_xy_to_idx(x + 1, y + 1);
204  if(xdiff < 0 || ydiff < 0) {
205  std::swap(nw, se);
206  std::swap(ne, sw);
207  }
208  set_direction_icon(nw, "topleft");
209  set_direction_icon(ne, "topright");
210  set_direction_icon(sw, "bottomleft");
211  set_direction_icon(se, "bottomright");
212  }
213 }
214 
215 } // namespace gui2
ttoggle_button * direction_buttons_[9]
The toggle buttons show the state of expand_direction_.
Definition: resize_map.hpp:107
int old_width_
The original width.
Definition: resize_map.hpp:93
void set_icon_name(const std::string &icon_name)
void connect_signal_notify_modified(tdispatcher &dispatcher, const tsignal_notification_function &signal)
Connects a signal handler for getting a notification upon modification.
Definition: dispatcher.hpp:725
REGISTER_DIALOG(label_settings)
static int resize_grid_xy_to_idx(const int x, const int y)
Convert a coordinate on a 3 by 3 grid to an index, return 9 for out of bounds.
Definition: resize_map.cpp:141
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1220
T get_widget_value(twindow &window)
Gets the value of the field.
Definition: field.hpp:398
Implements some helper classes to ease adding fields to a dialog and hide the synchronization needed...
tfield_integer * height_
The currently selected height.
Definition: resize_map.hpp:90
void update_expand_direction(twindow &window)
Definition: resize_map.cpp:158
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:62
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
This file contains the settings handling of the widget library.
void set_callback_state_change(std::function< void(twidget &)> callback)
Inherited from tselectable_.
tfield_integer * width_
The currently selected width.
Definition: resize_map.hpp:87
EXPAND_DIRECTION & expand_direction_
The selected expansion direction.
Definition: resize_map.hpp:99
static map_location::DIRECTION se
lu_byte right
Definition: lparser.cpp:1020
void pre_show(twindow &window)
Inherited from tdialog.
Definition: resize_map.cpp:107
GLint left
Definition: glew.h:5907
GLuint index
Definition: glew.h:1782
size_t i
Definition: function.cpp:1057
GLint GLint GLint GLint GLint x
Definition: glew.h:1220
int old_height_
The original height.
Definition: resize_map.hpp:96
static map_location::DIRECTION sw
GLint GLint GLint GLint GLint GLint GLsizei GLsizei height
Definition: glew.h:1220
GLuint const GLchar * name
Definition: glew.h:1782
GLenum GLint ref
Definition: glew.h:1813
static map_location::DIRECTION nw
void swap(game_board &one, game_board &other)
Definition: game_board.cpp:56
void set_direction_icon(int index, std::string icon)
Definition: resize_map.cpp:150
GLint GLint bottom
Definition: glew.h:5907
static map_location::DIRECTION ne
GLint GLint GLint GLint GLint GLint GLsizei width
Definition: glew.h:1220
A slider.
Definition: slider.hpp:30
void set_value(const unsigned selected)
Inherited from tselectable_.
GLsizei const GLcharARB ** string
Definition: glew.h:4503
void dialog_callback(twidget &caller)
Template for dialog callbacks.
Definition: helper.hpp:31