-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
generator_settings.cpp
100 lines (79 loc) · 3.43 KB
/
generator_settings.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
Copyright (C) 2010 - 2018 by the Battle for Wesnoth Project https://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#define GETTEXT_DOMAIN "wesnoth-lib"
#include "gui/dialogs/editor/generator_settings.hpp"
#include "formatter.hpp"
#include "gui/auxiliary/field.hpp"
#include "gui/widgets/settings.hpp"
#include "gui/widgets/slider.hpp"
#include "gui/widgets/status_label_helper.hpp"
#include "gettext.hpp"
#include "formula/string_utils.hpp"
#include "utils/functional.hpp"
namespace gui2
{
namespace dialogs
{
static int max_coastal = 5;
static int extra_size_per_player = 2;
static int min_size = 20;
REGISTER_DIALOG(generator_settings)
generator_settings::generator_settings(generator_data& data)
: players_(register_integer("players", true, data.nplayers))
, width_(register_integer("width", true, data.width))
, height_(register_integer("height", true, data.height))
, update_width_label_()
, update_height_label_()
{
register_integer("hills_num", true, data.iterations);
register_integer("hills_size", true, data.hill_size);
register_integer("villages", true, data.nvillages);
register_integer("castle_size", true, data.castle_size);
register_integer("landform", true, data.island_size);
register_bool("connect_castles", true, data.link_castles);
register_bool("show_labels", true, data.show_labels);
}
void generator_settings::pre_show(window& window)
{
// We adjust the minimum values of the width and height sliders when the number of players changes.
// This is done because the map generator needs more space to generate more castles for more players.
connect_signal_notify_modified(*players_->get_widget(), std::bind(
&generator_settings::adjust_minimum_size_by_players, this, std::ref(window)));
gui2::bind_status_label<slider>(&window, "players");
update_width_label_ = gui2::bind_status_label<slider>(&window, "width");
update_height_label_ = gui2::bind_status_label<slider>(&window, "height");
// Update min size initially.
// Do this *after* assigning the 'update_*_label_` functions or the game will crash!
adjust_minimum_size_by_players(window);
gui2::bind_status_label<slider>(&window, "villages", [](slider& s)->std::string {
std::string val = std::to_string(s.get_value());
utils::string_map args;
args["villages"] = val;
return VGETTEXT("$villages/1000 tiles", args);
});
gui2::bind_status_label<slider>(&window, "castle_size");
gui2::bind_status_label<slider>(&window, "landform", [](slider& s)->std::string {
return s.get_value() == 0 ? _("Inland") : (s.get_value() < max_coastal ? _("Coastal") : _("Island")); });
}
void generator_settings::adjust_minimum_size_by_players(window& window)
{
const int extra_size = (players_->get_widget_value(window) - 2) * extra_size_per_player;
const auto update_dimension_slider = [&](field_integer* field) {
slider& w = dynamic_cast<slider&>(*field->get_widget());
w.set_value_range(min_size + extra_size, w.get_maximum_value());
};
update_dimension_slider(width_);
update_dimension_slider(height_);
update_width_label_();
update_height_label_();
}
} // namespace dialogs
} // namespace gui2