/
generate_map.cpp
157 lines (128 loc) · 4.02 KB
/
generate_map.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
Copyright (C) 2008 - 2017 by Mark de Wever <koraq@xs4all.nl>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#define GETTEXT_DOMAIN "wesnoth-editor"
#include "gui/dialogs/editor/generate_map.hpp"
#include "gui/auxiliary/find_widget.hpp"
#include "gui/dialogs/helper.hpp"
#include "gui/widgets/button.hpp"
#include "gui/widgets/label.hpp"
#ifdef GUI2_EXPERIMENTAL_LISTBOX
#include "gui/widgets/list.hpp"
#else
#include "gui/widgets/listbox.hpp"
#endif
#include "gui/widgets/settings.hpp"
#include "gui/widgets/text_box.hpp"
#include "generators/map_generator.hpp"
#include "lexical_cast.hpp"
#include "utils/functional.hpp"
#define ERR_ED LOG_STREAM_INDENT(err, editor)
namespace gui2
{
namespace dialogs
{
/*WIKI
* @page = GUIWindowDefinitionWML
* @order = 2_editor_generate_map
*
* == Editor generate map ==
*
* This shows the dialog in the editor to select which random generator
* should be used to generate a map.
*
* @begin{table}{dialog_widgets}
*
* generators_list & & listbox & m &
* Listbox displaying known map generators. $
*
* settings & & button & m &
* When clicked this button opens the generator settings dialog. $
*
* seed_textbox & & text_box & m &
* Allows entering a seed for the map generator. $
*
* @end{table}
*/
REGISTER_DIALOG(editor_generate_map)
editor_generate_map::editor_generate_map(std::vector<map_generator*>& mg)
: map_generators_(mg)
, last_map_generator_(nullptr)
, current_map_generator_(0)
, random_seed_()
{
}
void editor_generate_map::do_generator_selected(window& window)
{
listbox& list = find_widget<listbox>(&window, "generators_list", false);
const int current = list.get_selected_row();
if(current == -1 || unsigned(current) > map_generators_.size()) {
return; // shouldn't happen!
}
button& settings = find_widget<button>(&window, "settings", false);
settings.set_active(map_generators_[current]->allow_user_config());
current_map_generator_ = current;
}
void editor_generate_map::do_settings()
{
get_selected_map_generator()->user_config();
}
map_generator* editor_generate_map::get_selected_map_generator()
{
assert(static_cast<size_t>(current_map_generator_)
< map_generators_.size());
return map_generators_[current_map_generator_];
}
void editor_generate_map::select_map_generator(map_generator* mg)
{
last_map_generator_ = mg;
}
void editor_generate_map::pre_show(window& window)
{
assert(!map_generators_.empty());
register_text("seed_textbox", false, random_seed_, false);
listbox& list = find_widget<listbox>(&window, "generators_list", false);
window.keyboard_capture(&list);
std::map<std::string, string_map> lrow;
for(const auto & gen : map_generators_)
{
assert(gen);
lrow["generator_name"]["label"] = gen->config_name();
// lrow["generator_id"]["label"] = gen->name();
list.add_row(lrow);
if(gen == last_map_generator_) {
list.select_row(list.get_item_count() - 1);
}
}
if (last_map_generator_ != nullptr) {
// We need to call this manually because it won't be called by
// list.select_row() even if we set the callback before
// calling it
this->do_generator_selected(window);
}
list.set_callback_item_change(
std::bind(&editor_generate_map::do_generator_selected, this, std::ref(window)));
button& settings_button = find_widget<button>(&window, "settings", false);
connect_signal_mouse_left_click(
settings_button,
std::bind(&editor_generate_map::do_settings,this));
}
boost::optional<uint32_t> editor_generate_map::get_seed()
{
try {
return lexical_cast<uint32_t>(random_seed_);
}
catch(const bad_lexical_cast& ) {
return boost::none;
}
}
} // namespace dialogs
} // namespace gui2