The Battle for Wesnoth  1.19.13+dev
generator_settings.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2025
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 "formula/string_utils.hpp"
21 #include "gui/auxiliary/field.hpp"
22 #include "gui/widgets/slider.hpp"
24 #include "gettext.hpp"
25 
26 #include <functional>
27 
28 namespace gui2::dialogs
29 {
30 namespace
31 {
32 constexpr int max_coastal = 5;
33 constexpr int extra_size_per_player = 2;
34 constexpr int min_size = 20;
35 
36 std::string get_village_description(int count)
37 {
38  return VGETTEXT("$count/1000 tiles", {{"count", std::to_string(count)}});
39 }
40 
41 std::string get_landform_description(int count)
42 {
43  return count == 0 ? _("Inland") : (count < max_coastal ? _("Coastal") : _("Island"));
44 }
45 
46 } // namespace
47 
48 REGISTER_DIALOG(generator_settings)
49 
51  : modal_dialog(window_id())
52  , players_(register_integer("players", true, data.nplayers))
53  , width_(register_integer("width", true, data.width))
54  , height_(register_integer("height", true, data.height))
55 {
56  register_integer("hills_num", true, data.iterations);
57  register_integer("hills_size", true, data.hill_size);
58  register_integer("villages", true, data.nvillages);
59  register_integer("castle_size", true, data.castle_size);
60  register_integer("landform", true, data.island_size);
61 
62  register_bool("connect_castles", true, data.link_castles);
63  register_bool("show_labels", true, data.show_labels);
64 }
65 
67 {
68  // We adjust the minimum values of the width and height sliders when the number of players changes.
69  // This is done because the map generator needs more space to generate more castles for more players.
72 
73  gui2::bind_default_status_label(find_widget<slider>("players"));
74  gui2::bind_default_status_label(find_widget<slider>("castle_size"));
75  gui2::bind_default_status_label(find_widget<slider>("width"));
76  gui2::bind_default_status_label(find_widget<slider>("height"));
77 
78  gui2::bind_status_label(find_widget<slider>("villages"),
79  [](const slider& s) { return get_village_description(s.get_value()); });
80  gui2::bind_status_label(find_widget<slider>("landform"),
81  [](const slider& s) { return get_landform_description(s.get_value()); });
82 
83  // Update min size initially.
85 }
86 
88 {
89  const int extra_size = (players_->get_widget_value() - 2) * extra_size_per_player;
90 
91  const auto update_dimension_slider = [&](field_integer* field) {
92  slider& w = dynamic_cast<slider&>(*field->get_widget());
93  w.set_value_range(min_size + extra_size, w.get_maximum_value());
94  };
95 
96  update_dimension_slider(width_);
97  update_dimension_slider(height_);
98 }
99 
100 } // namespace dialogs
virtual void pre_show() override
The execute function.
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.
#define VGETTEXT(msgid,...)
Handy wrappers around interpolate_variables_into_string and gettext.
int w
static std::string _(const char *str)
Definition: gettext.hpp:97
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:189
void bind_default_status_label(W &source)
Binds a status label using the default value getter and default target ID.
void bind_status_label(W &source, const F &value_getter, styled_widget *target_ptr=nullptr)
Binds a given target widget to reflect another widget's label.
std::string_view data
Definition: picture.cpp:188
static map_location::direction s