The Battle for Wesnoth  1.15.11+dev
generator_settings.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2018 by the Battle for Wesnoth Project https://www.wesnoth.org/
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY.
10 
11  See the COPYING file for more details.
12 */
13 
14 #define GETTEXT_DOMAIN "wesnoth-lib"
15 
17 
18 #include "formatter.hpp"
19 #include "gui/auxiliary/field.hpp"
20 #include "gui/widgets/settings.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  : players_(register_integer("players", true, data.nplayers))
38  , width_(register_integer("width", true, data.width))
39  , height_(register_integer("height", true, data.height))
40  , update_width_label_()
41  , update_height_label_()
42 {
43  register_integer("hills_num", true, data.iterations);
44  register_integer("hills_size", true, data.hill_size);
45  register_integer("villages", true, data.nvillages);
46  register_integer("castle_size", true, data.castle_size);
47  register_integer("landform", true, data.island_size);
48 
49  register_bool("connect_castles", true, data.link_castles);
50  register_bool("show_labels", true, data.show_labels);
51 }
52 
54 {
55  // We adjust the minimum values of the width and height sliders when the number of players changes.
56  // This is done because the map generator needs more space to generate more castles for more players.
59 
60  gui2::bind_status_label<slider>(&window, "players");
61 
62  update_width_label_ = gui2::bind_status_label<slider>(&window, "width");
63  update_height_label_ = gui2::bind_status_label<slider>(&window, "height");
64 
65  // Update min size initially.
66  // Do this *after* assigning the 'update_*_label_` functions or the game will crash!
68 
69  gui2::bind_status_label<slider>(&window, "villages", [](slider& s)->std::string { return formatter() << s.get_value() << _("/1000 tiles"); });
70  gui2::bind_status_label<slider>(&window, "castle_size");
71  gui2::bind_status_label<slider>(&window, "landform", [](slider& s)->std::string {
72  return s.get_value() == 0 ? _("Inland") : (s.get_value() < max_coastal ? _("Coastal") : _("Island")); });
73 }
74 
76 {
77  const int extra_size = (players_->get_widget_value(*get_window()) - 2) * extra_size_per_player;
78 
79  const auto update_dimension_slider = [&](field_integer* field) {
80  slider& w = dynamic_cast<slider&>(*field->get_widget());
81  w.set_value_range(min_size + extra_size, w.get_maximum_value());
82  };
83 
84  update_dimension_slider(width_);
85  update_dimension_slider(height_);
86 
89 }
90 
91 } // namespace dialogs
static void field(LexState *ls, ConsControl *cc)
Definition: lparser.cpp:891
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 * players_
We need to own these fields to access the underlying widget.
static std::string _(const char *str)
Definition: gettext.hpp:92
virtual void pre_show(window &window) override
The execute function.
static int extra_size_per_player
void set_value_range(int min_value, int max_value)
Definition: slider.cpp:248
virtual int get_maximum_value() const override
Inherited from integer_selector.
Definition: slider.hpp:90
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.
std::ostringstream wrapper.
Definition: formatter.hpp:38
std::function< void()> update_width_label_
std::function< void()> update_height_label_
styled_widget * get_widget()
Definition: field.hpp:206
static map_location::DIRECTION s
int w
A slider is a control that can select a value by moving a grip on a groove.
Definition: slider.hpp:58
static int max_coastal
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:64
Template class to implement the generic field implementation.
Definition: field-fwd.hpp:35