The Battle for Wesnoth  1.17.12+dev
generate_map.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2022
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  : modal_dialog(window_id())
42  , map_generators_(mg)
43  , last_map_generator_(nullptr)
44  , current_map_generator_(0)
45  , random_seed_()
46 {
47 }
48 
50 {
51  listbox& list = find_widget<listbox>(get_window(), "generators_list", false);
52  const int current = list.get_selected_row();
53 
54  if(current == -1 || static_cast<unsigned>(current) > map_generators_.size()) {
55  return; // shouldn't happen!
56  }
57 
58  button& settings = find_widget<button>(get_window(), "settings", false);
59  settings.set_active(map_generators_[current]->allow_user_config());
60 
61  current_map_generator_ = current;
62 }
63 
65 {
67 }
68 
70 {
71  assert(static_cast<std::size_t>(current_map_generator_)
72  < map_generators_.size());
74 }
75 
77 {
79 }
80 
82 {
83  assert(!map_generators_.empty());
84 
85  register_text("seed_textbox", false, random_seed_, false);
86 
87  listbox& list = find_widget<listbox>(&window, "generators_list", false);
88  window.keyboard_capture(&list);
89 
90  widget_data lrow;
91  for(const auto & gen : map_generators_)
92  {
93  assert(gen);
94  lrow["generator_name"]["label"] = gen->config_name();
95  // lrow["generator_id"]["label"] = gen->name();
96 
97  list.add_row(lrow);
98 
99  if(gen.get() == last_map_generator_) {
100  list.select_last_row();
101  }
102  }
103 
104  if (last_map_generator_ != nullptr) {
105  // We need to call this manually because it won't be called by
106  // list.select_row() even if we set the callback before
107  // calling it
108  this->do_generator_selected();
109  }
110 
113 
114  button& settings_button = find_widget<button>(&window, "settings", false);
116  settings_button,
117  std::bind(&editor_generate_map::do_settings,this));
118 }
119 
120 std::optional<uint32_t> editor_generate_map::get_seed()
121 {
122  try {
123  return lexical_cast<uint32_t>(random_seed_);
124  }
125  catch(const bad_lexical_cast& ) {
126  return std::nullopt;
127  }
128 }
129 
130 } // namespace dialogs
window(const builder_window::window_resolution &definition)
< Needs to be initialized in show.
Definition: window.cpp:263
int current_map_generator_
Current map generator index.
void do_settings()
Callback for the generator settings button.
void connect_signal_mouse_left_click(dispatcher &dispatcher, const signal &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.cpp:179
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...
grid & add_row(const widget_item &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:61
void do_generator_selected()
Callback for generator list selection changes.
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:270
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:45
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.
virtual void user_config()
Display the interactive screen, which allows the user to modify how the generator behaves...
window * get_window()
Returns a pointer to the dialog&#39;s window.
Contains the general settings which have a default.
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
Abstract base class for all modal dialogs.
Simple push button.
Definition: button.hpp:36
std::string random_seed_
random seed integer input
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:35
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:66
Thrown when a lexical_cast fails.
void connect_signal_notify_modified(dispatcher &dispatcher, const signal_notification &signal)
Connects a signal handler for getting a notification upon modification.
Definition: dispatcher.cpp:205