The Battle for Wesnoth  1.19.5+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 
20 
21 #include "gui/widgets/button.hpp"
22 #include "gui/widgets/listbox.hpp"
23 #include "gui/widgets/window.hpp"
25 #include "lexical_cast.hpp"
26 
27 #include <functional>
28 
29 #define ERR_ED LOG_STREAM_INDENT(err, editor)
30 
31 namespace gui2::dialogs
32 {
33 
34 REGISTER_DIALOG(editor_generate_map)
35 
36 editor_generate_map::editor_generate_map(std::vector<std::unique_ptr<map_generator>>& mg)
37  : modal_dialog(window_id())
38  , map_generators_(mg)
39  , last_map_generator_(nullptr)
40  , current_map_generator_(0)
41  , random_seed_()
42 {
43 }
44 
46 {
47  listbox& list = find_widget<listbox>("generators_list");
48  const int current = list.get_selected_row();
49 
50  if(current == -1 || static_cast<unsigned>(current) > map_generators_.size()) {
51  return; // shouldn't happen!
52  }
53 
54  button& settings = find_widget<button>("settings");
55  settings.set_active(map_generators_[current]->allow_user_config());
56 
57  current_map_generator_ = current;
58 }
59 
61 {
63 }
64 
66 {
67  assert(static_cast<std::size_t>(current_map_generator_)
68  < map_generators_.size());
70 }
71 
73 {
75 }
76 
78 {
79  assert(!map_generators_.empty());
80 
81  register_text("seed_textbox", false, random_seed_, false);
82 
83  listbox& list = find_widget<listbox>("generators_list");
84  keyboard_capture(&list);
85 
86  widget_data lrow;
87  for(const auto & gen : map_generators_)
88  {
89  assert(gen);
90  lrow["generator_name"]["label"] = gen->config_name();
91  // lrow["generator_id"]["label"] = gen->name();
92 
93  list.add_row(lrow);
94 
95  if(gen.get() == last_map_generator_) {
96  list.select_last_row();
97  }
98  }
99 
100  if (last_map_generator_ != nullptr) {
101  // We need to call this manually because it won't be called by
102  // list.select_row() even if we set the callback before
103  // calling it
104  this->do_generator_selected();
105  }
106 
109 
110  button& settings_button = find_widget<button>("settings");
112  settings_button,
113  std::bind(&editor_generate_map::do_settings,this));
114 }
115 
116 utils::optional<uint32_t> editor_generate_map::get_seed()
117 {
118  try {
119  return lexical_cast<uint32_t>(random_seed_);
120  }
121  catch(const bad_lexical_cast& ) {
122  return utils::nullopt;
123  }
124 }
125 
126 } // namespace dialogs
Simple push button.
Definition: button.hpp:36
virtual void pre_show() override
Actions to be taken before showing the window.
std::vector< std::unique_ptr< map_generator > > & map_generators_
Available map generators.
int current_map_generator_
Current map generator index.
utils::optional< uint32_t > get_seed()
void do_generator_selected()
Callback for generator list selection changes.
void do_settings()
Callback for the generator settings button.
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.
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:58
int get_selected_row() const
Returns the first selected row.
Definition: listbox.cpp:267
void keyboard_capture(widget *widget)
Definition: window.cpp:1207
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(editor_edit_unit)
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:36
Contains the general settings which have a default.
Thrown when a lexical_cast fails.