The Battle for Wesnoth  1.19.5+dev
resize_map.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2024
3  by Mark de Wever <koraq@xs4all.nl>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #define GETTEXT_DOMAIN "wesnoth-editor"
17 
19 
20 #include "gui/auxiliary/field.hpp"
22 #include "gui/widgets/slider.hpp"
23 
24 #include <functional>
25 
26 namespace gui2::dialogs
27 {
28 
29 REGISTER_DIALOG(editor_resize_map)
30 
32  int& height,
33  EXPAND_DIRECTION& expand_direction,
34  bool& copy_edge_terrain)
35  : modal_dialog(window_id())
36  , width_(register_integer("width", true, width))
37  , height_(register_integer("height", true, height))
38  , old_width_(width)
39  , old_height_(height)
40  , expand_direction_(expand_direction)
41  , direction_buttons_{}
42 {
43  register_bool("copy_edge_terrain", false, copy_edge_terrain);
44 
45  register_label("old_width", false, std::to_string(width));
46  register_label("old_height", false, std::to_string(height));
47 }
48 
50 {
51  slider& height = find_widget<slider>("height");
53  height,
55 
56  slider& width = find_widget<slider>("width");
58  width,
60 
61  std::string name_prefix = "expand";
62  for(int i = 0; i < 9; ++i) {
63  std::string name = name_prefix + std::to_string(i);
65  = find_widget<toggle_button>(name, false, true);
66 
69  }
70  direction_buttons_[0]->set_value(true);
72 }
73 
74 /**
75  * Convert a coordinate on a 3 by 3 grid to an index, return 9 for out of
76  * bounds
77  */
78 static int resize_grid_xy_to_idx(const int x, const int y)
79 {
80  if(x < 0 || x > 2 || y < 0 || y > 2) {
81  return 9;
82  } else {
83  return y * 3 + x;
84  }
85 }
86 
87 void editor_resize_map::set_direction_icon(int index, std::string icon)
88 {
89  if(index < 9) {
90  direction_buttons_[index]->set_icon_name("icons/arrows/arrows_blank_"
91  + icon + "_30.png");
92  }
93 }
94 
96 {
97  for(int i = 0; i < 9; ++i) {
98  if(direction_buttons_[i]->get_value()
99  && static_cast<int>(expand_direction_) != i) {
100 
101  expand_direction_ = static_cast<EXPAND_DIRECTION>(i);
102  break;
103  }
104  }
105  for(int i = 0; i < static_cast<int>(expand_direction_); ++i) {
106  direction_buttons_[i]->set_value(false);
107  set_direction_icon(i, "none");
108  }
110  for(int i = expand_direction_ + 1; i < 9; ++i) {
111  direction_buttons_[i]->set_value(false);
112  set_direction_icon(i, "none");
113  }
114 
115  int xdiff = width_->get_widget_value() - old_width_;
116  int ydiff = height_->get_widget_value() - old_height_;
117  int x = static_cast<int>(expand_direction_) % 3;
118  int y = static_cast<int>(expand_direction_) / 3;
120  if(xdiff != 0) {
121  int left = resize_grid_xy_to_idx(x - 1, y);
122  int right = resize_grid_xy_to_idx(x + 1, y);
123  if(xdiff < 0)
124  std::swap(left, right);
125  set_direction_icon(left, "left");
126  set_direction_icon(right, "right");
127  }
128  if(ydiff != 0) {
129  int top = resize_grid_xy_to_idx(x, y - 1);
130  int bottom = resize_grid_xy_to_idx(x, y + 1);
131  if(ydiff < 0)
132  std::swap(top, bottom);
133  set_direction_icon(top, "up");
134  set_direction_icon(bottom, "down");
135  }
136  if(xdiff < 0 || ydiff < 0 || (xdiff > 0 && ydiff > 0)) {
137  int nw = resize_grid_xy_to_idx(x - 1, y - 1);
138  int ne = resize_grid_xy_to_idx(x + 1, y - 1);
139  int sw = resize_grid_xy_to_idx(x - 1, y + 1);
140  int se = resize_grid_xy_to_idx(x + 1, y + 1);
141  if(xdiff < 0 || ydiff < 0) {
142  std::swap(nw, se);
143  std::swap(ne, sw);
144  }
145  set_direction_icon(nw, "topleft");
146  set_direction_icon(ne, "topright");
147  set_direction_icon(sw, "bottomleft");
148  set_direction_icon(se, "bottomright");
149  }
150 }
151 
152 } // namespace dialogs
int old_width_
The original width.
Definition: resize_map.hpp:87
toggle_button * direction_buttons_[9]
The toggle buttons show the state of expand_direction_.
Definition: resize_map.hpp:101
field_integer * height_
The currently selected height.
Definition: resize_map.hpp:84
int old_height_
The original height.
Definition: resize_map.hpp:90
virtual void pre_show() override
Actions to be taken before showing the window.
Definition: resize_map.cpp:49
EXPAND_DIRECTION & expand_direction_
The selected expansion direction.
Definition: resize_map.hpp:93
void set_direction_icon(int index, std::string icon)
Definition: resize_map.cpp:87
field_integer * width_
The execute function.
Definition: resize_map.hpp:81
Abstract base class for all modal dialogs.
T get_widget_value()
Gets the value of the field.
Definition: field.hpp:378
virtual void set_value(unsigned selected, bool fire_event=false) override
Inherited from selectable_item.
void set_icon_name(const std::string &icon_name)
void swap(config &lhs, config &rhs)
Implement non-member swap function for std::swap (calls config::swap).
Definition: config.cpp:1343
Implements some helper classes to ease adding fields to a dialog and hide the synchronization needed.
std::size_t i
Definition: function.cpp:1028
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:78
REGISTER_DIALOG(editor_edit_unit)
void connect_signal_notify_modified(dispatcher &dispatcher, const signal_notification &signal)
Connects a signal handler for getting a notification upon modification.
Definition: dispatcher.cpp:203
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:70
static map_location::direction sw
static map_location::direction se
static map_location::direction nw
static map_location::direction ne