The Battle for Wesnoth  1.15.12+dev
resize_map.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2018 by Mark de Wever <koraq@xs4all.nl>
3  Part of the Battle for Wesnoth Project https://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"
20 #include "gui/widgets/settings.hpp"
21 #include "gui/widgets/slider.hpp"
22 
23 #include <functional>
24 
25 namespace gui2::dialogs
26 {
27 
28 REGISTER_DIALOG(editor_resize_map)
29 
31  int& height,
32  EXPAND_DIRECTION& expand_direction,
33  bool& copy_edge_terrain)
34  : width_(register_integer("width", true, width))
35  , height_(register_integer("height", true, height))
36  , old_width_(width)
37  , old_height_(height)
38  , expand_direction_(expand_direction)
39  , direction_buttons_{}
40 {
41  register_bool("copy_edge_terrain", false, copy_edge_terrain);
42 
43  register_label("old_width", false, std::to_string(width));
44  register_label("old_height", false, std::to_string(height));
45 }
46 
48 {
49  slider& height = find_widget<slider>(&window, "height", false);
51  height,
53 
54  slider& width = find_widget<slider>(&window, "width", false);
56  width,
58 
59  std::string name_prefix = "expand";
60  for(int i = 0; i < 9; ++i) {
61  std::string name = name_prefix + std::to_string(i);
63  = find_widget<toggle_button>(&window, name, false, true);
64 
67  }
68  direction_buttons_[0]->set_value(true);
70 }
71 
72 /**
73  * Convert a coordinate on a 3 by 3 grid to an index, return 9 for out of
74  * bounds
75  */
76 static int resize_grid_xy_to_idx(const int x, const int y)
77 {
78  if(x < 0 || x > 2 || y < 0 || y > 2) {
79  return 9;
80  } else {
81  return y * 3 + x;
82  }
83 }
84 
85 void editor_resize_map::set_direction_icon(int index, std::string icon)
86 {
87  if(index < 9) {
88  direction_buttons_[index]->set_icon_name("icons/arrows/arrows_blank_"
89  + icon + "_30.png");
90  }
91 }
92 
94 {
95  for(int i = 0; i < 9; ++i) {
96  if(direction_buttons_[i]->get_value()
97  && static_cast<int>(expand_direction_) != i) {
98 
99  expand_direction_ = static_cast<EXPAND_DIRECTION>(i);
100  break;
101  }
102  }
103  for(int i = 0; i < static_cast<int>(expand_direction_); ++i) {
104  direction_buttons_[i]->set_value(false);
105  set_direction_icon(i, "none");
106  }
108  for(int i = expand_direction_ + 1; i < 9; ++i) {
109  direction_buttons_[i]->set_value(false);
110  set_direction_icon(i, "none");
111  }
112 
113  int xdiff = width_->get_widget_value(*get_window()) - old_width_;
114  int ydiff = height_->get_widget_value(*get_window()) - old_height_;
115  int x = static_cast<int>(expand_direction_) % 3;
116  int y = static_cast<int>(expand_direction_) / 3;
118  if(xdiff != 0) {
119  int left = resize_grid_xy_to_idx(x - 1, y);
120  int right = resize_grid_xy_to_idx(x + 1, y);
121  if(xdiff < 0)
122  std::swap(left, right);
123  set_direction_icon(left, "left");
124  set_direction_icon(right, "right");
125  }
126  if(ydiff != 0) {
127  int top = resize_grid_xy_to_idx(x, y - 1);
128  int bottom = resize_grid_xy_to_idx(x, y + 1);
129  if(ydiff < 0)
130  std::swap(top, bottom);
131  set_direction_icon(top, "up");
132  set_direction_icon(bottom, "down");
133  }
134  if(xdiff < 0 || ydiff < 0 || (xdiff > 0 && ydiff > 0)) {
135  int nw = resize_grid_xy_to_idx(x - 1, y - 1);
136  int ne = resize_grid_xy_to_idx(x + 1, y - 1);
137  int sw = resize_grid_xy_to_idx(x - 1, y + 1);
138  int se = resize_grid_xy_to_idx(x + 1, y + 1);
139  if(xdiff < 0 || ydiff < 0) {
140  std::swap(nw, se);
141  std::swap(ne, sw);
142  }
143  set_direction_icon(nw, "topleft");
144  set_direction_icon(ne, "topright");
145  set_direction_icon(sw, "bottomleft");
146  set_direction_icon(se, "bottomright");
147  }
148 }
149 
150 } // namespace dialogs
virtual void set_value(unsigned selected, bool fire_event=false) override
Inherited from selectable_item.
window * get_window() const
Returns a pointer to the dialog&#39;s window.
T get_widget_value(window &window)
Gets the value of the field.
Definition: field.hpp:395
Implements some helper classes to ease adding fields to a dialog and hide the synchronization needed...
field_integer * height_
The currently selected height.
Definition: resize_map.hpp:104
toggle_button * direction_buttons_[9]
The toggle buttons show the state of expand_direction_.
Definition: resize_map.hpp:121
void connect_signal_notify_modified(dispatcher &dispatcher, const signal_notification_function &signal)
Connects a signal handler for getting a notification upon modification.
Definition: dispatcher.cpp:186
This file contains the settings handling of the widget library.
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
Definition: resize_map.cpp:47
void swap(config &lhs, config &rhs)
Implement non-member swap function for std::swap (calls config::swap).
Definition: config.cpp:1431
static map_location::DIRECTION se
lu_byte right
Definition: lparser.cpp:1227
This shows the dialog to resize the current map.
Definition: resize_map.hpp:48
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:76
std::size_t i
Definition: function.cpp:940
EXPAND_DIRECTION & expand_direction_
The selected expansion direction.
Definition: resize_map.hpp:113
void set_direction_icon(int index, std::string icon)
Definition: resize_map.cpp:85
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:71
static map_location::DIRECTION sw
int old_width_
The original width.
Definition: resize_map.hpp:107
static map_location::DIRECTION nw
lu_byte left
Definition: lparser.cpp:1226
A slider is a control that can select a value by moving a grip on a groove.
Definition: slider.hpp:58
int old_height_
The original height.
Definition: resize_map.hpp:110
static map_location::DIRECTION ne
field_integer * width_
The execute function.
Definition: resize_map.hpp:101
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:64
void set_icon_name(const std::string &icon_name)