The Battle for Wesnoth  1.17.0-dev
generate_map.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2021
3  by Mark de Wever <koraq@xs4all.nl>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #define GETTEXT_DOMAIN "wesnoth-editor"
17 
19 
21 
22 #include "gui/widgets/button.hpp"
23 #include "gui/widgets/label.hpp"
24 #include "gui/widgets/listbox.hpp"
25 #include "gui/widgets/settings.hpp"
26 #include "gui/widgets/text_box.hpp"
27 #include "gui/widgets/window.hpp"
29 #include "lexical_cast.hpp"
30 
31 #include <functional>
32 
33 #define ERR_ED LOG_STREAM_INDENT(err, editor)
34 
35 namespace gui2::dialogs
36 {
37 
38 REGISTER_DIALOG(editor_generate_map)
39 
41  : map_generators_(mg)
42  , last_map_generator_(nullptr)
43  , current_map_generator_(0)
44  , random_seed_()
45 {
46 }
47 
49 {
50  listbox& list = find_widget<listbox>(get_window(), "generators_list", false);
51  const int current = list.get_selected_row();
52 
53  if(current == -1 || static_cast<unsigned>(current) > map_generators_.size()) {
54  return; // shouldn't happen!
55  }
56 
57  button& settings = find_widget<button>(get_window(), "settings", false);
58  settings.set_active(map_generators_[current]->allow_user_config());
59 
60  current_map_generator_ = current;
61 }
62 
64 {
66 }
67 
69 {
70  assert(static_cast<std::size_t>(current_map_generator_)
71  < map_generators_.size());
73 }
74 
76 {
78 }
79 
81 {
82  assert(!map_generators_.empty());
83 
84  register_text("seed_textbox", false, random_seed_, false);
85 
86  listbox& list = find_widget<listbox>(&window, "generators_list", false);
87  window.keyboard_capture(&list);
88 
89  std::map<std::string, string_map> lrow;
90  for(const auto & gen : map_generators_)
91  {
92  assert(gen);
93  lrow["generator_name"]["label"] = gen->config_name();
94  // lrow["generator_id"]["label"] = gen->name();
95 
96  list.add_row(lrow);
97 
98  if(gen.get() == last_map_generator_) {
99  list.select_last_row();
100  }
101  }
102 
103  if (last_map_generator_ != nullptr) {
104  // We need to call this manually because it won't be called by
105  // list.select_row() even if we set the callback before
106  // calling it
107  this->do_generator_selected();
108  }
109 
112 
113  button& settings_button = find_widget<button>(&window, "settings", false);
115  settings_button,
116  std::bind(&editor_generate_map::do_settings,this));
117 }
118 
119 std::optional<uint32_t> editor_generate_map::get_seed()
120 {
121  try {
122  return lexical_cast<uint32_t>(random_seed_);
123  }
124  catch(const bad_lexical_cast& ) {
125  return std::nullopt;
126  }
127 }
128 
129 } // namespace dialogs
int current_map_generator_
Current map generator index.
void do_settings()
Callback for the generator settings button.
New lexcical_cast header.
std::optional< uint32_t > get_seed()
This file contains the window object, this object is a top level container which has the event manage...
void do_generator_selected()
Callback for generator list selection changes.
STL namespace.
window * get_window() const
Returns a pointer to the dialog&#39;s window.
bool select_last_row(const bool select=true)
Does exactly as advertised: selects the list&#39;s last row.
Definition: listbox.hpp:195
int get_selected_row() const
Returns the first selected row.
Definition: listbox.cpp:277
To lexical_cast(From value)
Lexical cast converts one type to another.
map_generator * get_selected_map_generator()
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:43
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:187
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:172
virtual void user_config()
Display the interactive screen, which allows the user to modify how the generator behaves...
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:68
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
virtual void set_active(const bool active) override
See styled_widget::set_active.
Definition: button.cpp:63
Simple push button.
Definition: button.hpp:36
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:65
Thrown when a lexical_cast fails.