The Battle for Wesnoth  1.19.0-dev
generate_map.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2024
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/listbox.hpp"
24 #include "gui/widgets/window.hpp"
26 #include "lexical_cast.hpp"
27 
28 #include <functional>
29 
30 #define ERR_ED LOG_STREAM_INDENT(err, editor)
31 
32 namespace gui2::dialogs
33 {
34 
35 REGISTER_DIALOG(editor_generate_map)
36 
37 editor_generate_map::editor_generate_map(std::vector<std::unique_ptr<map_generator>>& mg)
38  : modal_dialog(window_id())
39  , map_generators_(mg)
40  , last_map_generator_(nullptr)
41  , current_map_generator_(0)
42  , random_seed_()
43 {
44 }
45 
47 {
48  listbox& list = find_widget<listbox>(get_window(), "generators_list", false);
49  const int current = list.get_selected_row();
50 
51  if(current == -1 || static_cast<unsigned>(current) > map_generators_.size()) {
52  return; // shouldn't happen!
53  }
54 
55  button& settings = find_widget<button>(get_window(), "settings", false);
56  settings.set_active(map_generators_[current]->allow_user_config());
57 
58  current_map_generator_ = current;
59 }
60 
62 {
64 }
65 
67 {
68  assert(static_cast<std::size_t>(current_map_generator_)
69  < map_generators_.size());
71 }
72 
74 {
76 }
77 
79 {
80  assert(!map_generators_.empty());
81 
82  register_text("seed_textbox", false, random_seed_, false);
83 
84  listbox& list = find_widget<listbox>(&window, "generators_list", false);
85  window.keyboard_capture(&list);
86 
87  widget_data lrow;
88  for(const auto & gen : map_generators_)
89  {
90  assert(gen);
91  lrow["generator_name"]["label"] = gen->config_name();
92  // lrow["generator_id"]["label"] = gen->name();
93 
94  list.add_row(lrow);
95 
96  if(gen.get() == last_map_generator_) {
97  list.select_last_row();
98  }
99  }
100 
101  if (last_map_generator_ != nullptr) {
102  // We need to call this manually because it won't be called by
103  // list.select_row() even if we set the callback before
104  // calling it
105  this->do_generator_selected();
106  }
107 
110 
111  button& settings_button = find_widget<button>(&window, "settings", false);
113  settings_button,
114  std::bind(&editor_generate_map::do_settings,this));
115 }
116 
117 std::optional<uint32_t> editor_generate_map::get_seed()
118 {
119  try {
120  return lexical_cast<uint32_t>(random_seed_);
121  }
122  catch(const bad_lexical_cast& ) {
123  return std::nullopt;
124  }
125 }
126 
127 } // namespace dialogs
Simple push button.
Definition: button.hpp:36
The dialog for selecting which random generator to use in the editor.
std::vector< std::unique_ptr< map_generator > > & map_generators_
Available map generators.
int current_map_generator_
Current map generator index.
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
void do_generator_selected()
Callback for generator list selection changes.
void do_settings()
Callback for the generator settings button.
std::optional< uint32_t > get_seed()
map_generator * last_map_generator_
Last used map generator, must be in map_generators_.
std::string random_seed_
random seed integer input
void select_map_generator(map_generator *mg)
map_generator * get_selected_map_generator()
Abstract base class for all modal dialogs.
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.
window * get_window()
Returns a pointer to the dialog's window.
The listbox class.
Definition: listbox.hpp:43
bool select_last_row(const bool select=true)
Does exactly as advertised: selects the list's last row.
Definition: listbox.hpp:189
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:59
int get_selected_row() const
Returns the first selected row.
Definition: listbox.cpp:268
base class of top level items, the only item which needs to store the final canvases to draw on.
Definition: window.hpp:63
void keyboard_capture(widget *widget)
Definition: window.cpp:1215
virtual void user_config()
Display the interactive screen, which allows the user to modify how the generator behaves.
This file contains the window object, this object is a top level container which has the event manage...
New lexcical_cast header.
REGISTER_DIALOG(tod_new_schedule)
void connect_signal_notify_modified(dispatcher &dispatcher, const signal_notification &signal)
Connects a signal handler for getting a notification upon modification.
Definition: dispatcher.cpp:203
void connect_signal_mouse_left_click(dispatcher &dispatcher, const signal &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.cpp:177
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:34
Contains the general settings which have a default.
Thrown when a lexical_cast fails.