The Battle for Wesnoth  1.19.5+dev
generator_settings.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2024
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 
15 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
18 
19 #include "formatter.hpp"
20 #include "gui/auxiliary/field.hpp"
21 #include "gui/widgets/slider.hpp"
23 #include "gettext.hpp"
24 
25 #include <functional>
26 
27 namespace gui2::dialogs
28 {
29 
30 static int max_coastal = 5;
31 static int extra_size_per_player = 2;
32 static int min_size = 20;
33 
35 
37  : modal_dialog(window_id())
38  , players_(register_integer("players", true, data.nplayers))
39  , width_(register_integer("width", true, data.width))
40  , height_(register_integer("height", true, data.height))
41  , update_width_label_()
42  , update_height_label_()
43 {
44  register_integer("hills_num", true, data.iterations);
45  register_integer("hills_size", true, data.hill_size);
46  register_integer("villages", true, data.nvillages);
47  register_integer("castle_size", true, data.castle_size);
48  register_integer("landform", true, data.island_size);
49 
50  register_bool("connect_castles", true, data.link_castles);
51  register_bool("show_labels", true, data.show_labels);
52 }
53 
55 {
56  // We adjust the minimum values of the width and height sliders when the number of players changes.
57  // This is done because the map generator needs more space to generate more castles for more players.
60 
61  gui2::bind_status_label<slider>(this, "players");
62 
63  update_width_label_ = gui2::bind_status_label<slider>(this, "width");
64  update_height_label_ = gui2::bind_status_label<slider>(this, "height");
65 
66  // Update min size initially.
67  // Do this *after* assigning the 'update_*_label_` functions or the game will crash!
69 
70  gui2::bind_status_label<slider>(this, "villages", [](const slider& s) { return t_string(formatter() << s.get_value() << _("/1000 tiles")); });
71  gui2::bind_status_label<slider>(this, "castle_size");
72  gui2::bind_status_label<slider>(this, "landform", [](const slider& s) {
73  return s.get_value() == 0 ? _("Inland") : (s.get_value() < max_coastal ? _("Coastal") : _("Island")); });
74 }
75 
77 {
78  const int extra_size = (players_->get_widget_value() - 2) * extra_size_per_player;
79 
80  const auto update_dimension_slider = [&](field_integer* field) {
81  slider& w = dynamic_cast<slider&>(*field->get_widget());
82  w.set_value_range(min_size + extra_size, w.get_maximum_value());
83  };
84 
85  update_dimension_slider(width_);
86  update_dimension_slider(height_);
87 
90 }
91 
92 } // namespace dialogs
std::ostringstream wrapper.
Definition: formatter.hpp:40
virtual void pre_show() override
The execute function.
std::function< void()> update_height_label_
std::function< void()> update_width_label_
field_integer * players_
We need to own these fields to access the underlying widget.
Abstract base class for all modal dialogs.
styled_widget * get_widget()
Definition: field.hpp:192
Template class to implement the generic field implementation.
Definition: field.hpp:245
T get_widget_value()
Gets the value of the field.
Definition: field.hpp:378
Implements some helper classes to ease adding fields to a dialog and hide the synchronization needed.
int w
static std::string _(const char *str)
Definition: gettext.hpp:93
REGISTER_DIALOG(editor_edit_unit)
static int extra_size_per_player
static int max_coastal
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::string_view data
Definition: picture.cpp:178
static map_location::direction s