The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
generate_map.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2008 - 2025
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
25#include "lexical_cast.hpp"
26
27#include <functional>
28
29#define ERR_ED LOG_STREAM_INDENT(err, editor)
30
31namespace gui2::dialogs
32{
33
34REGISTER_DIALOG(editor_generate_map)
35
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
64
71
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 for(const auto& gen : map_generators_) {
87 assert(gen);
88 list.add_row(widget_data{{ "generator_name", {{ "label", gen->config_name() }}}});
89
90 if(gen.get() == last_map_generator_) {
91 list.select_last_row();
92 }
93 }
94
95 if (last_map_generator_ != nullptr) {
96 // We need to call this manually because it won't be called by
97 // list.select_row() even if we set the callback before
98 // calling it
100 }
101
102 connect_signal_notify_modified(list,
104
105 button& settings_button = find_widget<button>("settings");
106 connect_signal_mouse_left_click(
107 settings_button,
108 std::bind(&editor_generate_map::do_settings,this));
109}
110
111utils::optional<uint32_t> editor_generate_map::get_seed()
112{
113 try {
114 return lexical_cast<uint32_t>(random_seed_);
115 }
116 catch(const bad_lexical_cast& ) {
117 return utils::nullopt;
118 }
119}
120
121} // namespace dialogs
Simple push button.
Definition button.hpp:36
virtual void pre_show() override
Actions to be taken before showing the window.
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.
const std::vector< std::unique_ptr< map_generator > > & map_generators_
Available map generators.
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:41
bool select_last_row(const bool select=true)
Does exactly as advertised: selects the list's last row.
Definition listbox.hpp:186
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:92
int get_selected_row() const
Returns the first selected row.
Definition listbox.cpp:290
void keyboard_capture(widget *widget)
Definition window.cpp:1197
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.
#define REGISTER_DIALOG(window_id)
Wrapper for REGISTER_DIALOG2.
std::map< std::string, widget_item > widget_data
Definition widget.hpp:36
Contains the general settings which have a default.
STL namespace.
Thrown when a lexical_cast fails.