The Battle for Wesnoth  1.15.2+dev
generate_map.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2018 by Mark de Wever <koraq@xs4all.nl>
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 #define GETTEXT_DOMAIN "wesnoth-editor"
15 
17 
19 
20 #include "gui/widgets/button.hpp"
21 #include "gui/widgets/label.hpp"
22 #include "gui/widgets/listbox.hpp"
23 #include "gui/widgets/settings.hpp"
24 #include "gui/widgets/text_box.hpp"
25 #include "gui/widgets/window.hpp"
27 #include "lexical_cast.hpp"
28 
29 #include "utils/functional.hpp"
30 
31 #define ERR_ED LOG_STREAM_INDENT(err, editor)
32 
33 namespace gui2
34 {
35 namespace dialogs
36 {
37 
38 /*WIKI
39  * @page = GUIWindowDefinitionWML
40  * @order = 2_editor_generate_map
41  *
42  * == Editor generate map ==
43  *
44  * This shows the dialog in the editor to select which random generator
45  * should be used to generate a map.
46  *
47  * @begin{table}{dialog_widgets}
48  *
49  * generators_list & & listbox & m &
50  * Listbox displaying known map generators. $
51  *
52  * settings & & button & m &
53  * When clicked this button opens the generator settings dialog. $
54  *
55  * seed_textbox & & text_box & m &
56  * Allows entering a seed for the map generator. $
57  *
58  * @end{table}
59  */
60 
61 REGISTER_DIALOG(editor_generate_map)
62 
64  : map_generators_(mg)
65  , last_map_generator_(nullptr)
66  , current_map_generator_(0)
67  , random_seed_()
68 {
69 }
70 
72 {
73  listbox& list = find_widget<listbox>(&window, "generators_list", false);
74  const int current = list.get_selected_row();
75 
76  if(current == -1 || static_cast<unsigned>(current) > map_generators_.size()) {
77  return; // shouldn't happen!
78  }
79 
80  button& settings = find_widget<button>(&window, "settings", false);
81  settings.set_active(map_generators_[current]->allow_user_config());
82 
83  current_map_generator_ = current;
84 }
85 
87 {
89 }
90 
92 {
93  assert(static_cast<std::size_t>(current_map_generator_)
94  < map_generators_.size());
96 }
97 
99 {
100  last_map_generator_ = mg;
101 }
102 
104 {
105  assert(!map_generators_.empty());
106 
107  register_text("seed_textbox", false, random_seed_, false);
108 
109  listbox& list = find_widget<listbox>(&window, "generators_list", false);
110  window.keyboard_capture(&list);
111 
112  std::map<std::string, string_map> lrow;
113  for(const auto & gen : map_generators_)
114  {
115  assert(gen);
116  lrow["generator_name"]["label"] = gen->config_name();
117  // lrow["generator_id"]["label"] = gen->name();
118 
119  list.add_row(lrow);
120 
121  if(gen.get() == last_map_generator_) {
122  list.select_last_row();
123  }
124  }
125 
126  if (last_map_generator_ != nullptr) {
127  // We need to call this manually because it won't be called by
128  // list.select_row() even if we set the callback before
129  // calling it
130  this->do_generator_selected(window);
131  }
132 
134  std::bind(&editor_generate_map::do_generator_selected, this, std::ref(window)));
135 
136  button& settings_button = find_widget<button>(&window, "settings", false);
138  settings_button,
139  std::bind(&editor_generate_map::do_settings,this));
140 }
141 
142 boost::optional<uint32_t> editor_generate_map::get_seed()
143 {
144  try {
145  return lexical_cast<uint32_t>(random_seed_);
146  }
147  catch(const bad_lexical_cast& ) {
148  return boost::none;
149  }
150 }
151 
152 } // namespace dialogs
153 } // namespace gui2
int current_map_generator_
Current map generator index.
boost::optional< uint32_t > get_seed()
void do_settings()
Callback for the generator settings button.
New lexcical_cast header.
This file contains the window object, this object is a top level container which has the event manage...
STL namespace.
bool select_last_row(const bool select=true)
Does exactly as advertised: selects the list&#39;s last row.
Definition: listbox.hpp:192
int get_selected_row() const
Returns the first selected row.
Definition: listbox.cpp:272
To lexical_cast(From value)
Lexical cast converts one type to another.
map_generator * get_selected_map_generator()
Generic file dialog.
Definition: field-fwd.hpp:22
field_text * register_text(const std::string &id, const bool mandatory, const std::function< std::string()> callback_load_value=nullptr, const std::function< void(const std::string &)> callback_save_value=nullptr, const bool capture_focus=false)
Creates a new text field.
map_generator * last_map_generator_
Last used map generator, must be in map_generators_.
The listbox class.
Definition: listbox.hpp:40
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:248
This file contains the settings handling of the widget library.
void select_map_generator(map_generator *mg)
std::vector< std::unique_ptr< map_generator > > & map_generators_
Available map generators.
void connect_signal_mouse_left_click(dispatcher &dispatcher, const signal_function &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.cpp:233
virtual void user_config()
Display the interactive screen, which allows the user to modify how the generator behaves...
Various uncategorised dialogs.
Contains the general settings which have a default.
grid & add_row(const string_map &item, const int index=-1)
When an item in the list is selected by the user we need to update the state.
Definition: listbox.cpp:66
virtual void pre_show(window &window) override
Inherited from modal_dialog.
virtual void set_active(const bool active) override
See styled_widget::set_active.
Definition: button.cpp:62
Simple push button.
Definition: button.hpp:35
void do_generator_selected(window &window)
Callback for generator list selection changes.
std::string random_seed_
random seed integer input
The dialog for selecting which random generator to use in the editor.
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:63
Thrown when a lexical_cast fails.