The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
generate_map.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2016 by Mark de Wever <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 #define GETTEXT_DOMAIN "wesnoth-editor"
15 
17 
19 #include "gui/dialogs/helper.hpp"
20 
21 #include "gui/widgets/button.hpp"
22 #include "gui/widgets/label.hpp"
23 #ifdef GUI2_EXPERIMENTAL_LISTBOX
24 #include "gui/widgets/list.hpp"
25 #else
26 #include "gui/widgets/listbox.hpp"
27 #endif
28 #include "gui/widgets/settings.hpp"
29 #include "gui/widgets/text_box.hpp"
31 
32 #include "utils/functional.hpp"
33 
34 #define ERR_ED LOG_STREAM_INDENT(err, editor)
35 
36 namespace gui2
37 {
38 
39 /*WIKI
40  * @page = GUIWindowDefinitionWML
41  * @order = 2_editor_generate_map
42  *
43  * == Editor generate map ==
44  *
45  * This shows the dialog in the editor to select which random generator
46  * should be used to generate a map.
47  *
48  * @begin{table}{dialog_widgets}
49  *
50  * generators_list & & listbox & m &
51  * Listbox displaying known map generators. $
52  *
53  * settings & & button & m &
54  * When clicked this button opens the generator settings dialog. $
55  *
56  * seed_textbox & & text_box & m &
57  * Allows entering a seed for the map generator. $
58  *
59  * @end{table}
60  */
61 
62 REGISTER_DIALOG(editor_generate_map)
63 
65  : map_generators_()
66  , last_map_generator_(nullptr)
67  , current_map_generator_(0)
68  , random_seed_()
69 {
70 }
71 
73 {
74  tlistbox& list = find_widget<tlistbox>(&window, "generators_list", false);
75  const int current = list.get_selected_row();
76 
77  if(current == -1 || unsigned(current) > map_generators_.size()) {
78  return; // shouldn't happen!
79  }
80 
81  tbutton& settings = find_widget<tbutton>(&window, "settings", false);
82  settings.set_active(map_generators_[current]->allow_user_config());
83 
84  current_map_generator_ = current;
85 }
86 
88 {
90 }
91 
93 {
94  assert(static_cast<size_t>(current_map_generator_)
95  < map_generators_.size());
97 }
98 
100 {
101  last_map_generator_ = mg;
102 }
103 
105 {
106  assert(!map_generators_.empty());
107 
108  register_text("seed_textbox", false, random_seed_, false);
109 
110  tlistbox& list = find_widget<tlistbox>(&window, "generators_list", false);
111  window.keyboard_capture(&list);
112 
113  std::map<std::string, string_map> lrow;
114  for(const auto & gen : map_generators_)
115  {
116  assert(gen);
117  lrow["generator_name"]["label"] = gen->config_name();
118  // lrow["generator_id"]["label"] = gen->name();
119 
120  list.add_row(lrow);
121 
122  if(gen == last_map_generator_) {
123  list.select_row(list.get_item_count() - 1);
124  }
125  }
126 
127  if (last_map_generator_ != nullptr) {
128  // We need to call this manually because it won't be called by
129  // list.select_row() even if we set the callback before
130  // calling it
131  this->do_generator_selected(window);
132  }
133 
135  std::bind(&teditor_generate_map::do_generator_selected, this, std::ref(window)));
136 
137  tbutton& settings_button = find_widget<tbutton>(&window, "settings", false);
139  settings_button,
141  this,
142  std::ref(window)));
143 }
144 
145 boost::optional<boost::uint32_t> teditor_generate_map::get_seed()
146 {
147  try {
149  }
150  catch(const bad_lexical_cast& ) {
151  return boost::none;
152  }
153 }
154 
155 } // namespace gui2
virtual void set_active(const bool active) override
See tcontrol::set_active.
Definition: button.cpp:58
map_generator * get_selected_map_generator()
CVideo & video()
Definition: window.hpp:411
std::string random_seed_
random seed integer input
boost::uint32_t uint32_t
Definition: xbrz.hpp:45
bool select_row(const unsigned row, const bool select=true)
Selectes a row.
Definition: listbox.cpp:228
REGISTER_DIALOG(label_settings)
void do_settings(twindow &window)
Callback for the generator settings button.
void connect_signal_mouse_left_click(tdispatcher &dispatcher, const tsignal_function &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.hpp:710
To lexical_cast(From value)
Lexical cast converts one type to another.
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:62
void do_generator_selected(twindow &window)
Callback for generator list selection changes.
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
Simple push button.
Definition: button.hpp:32
This file contains the settings handling of the widget library.
unsigned get_item_count() const
Returns the number of items in the listbox.
Definition: listbox.cpp:138
void add_row(const string_map &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:74
void select_map_generator(map_generator *mg)
The dialog for selecting which random generator to use in the editor.
std::vector< map_generator * > map_generators_
Available map generators.
Contains the general settings which have a default.
Definition: settings.cpp:27
The listbox class.
Definition: listbox.hpp:39
void pre_show(twindow &window)
Inherited from tdialog.
GLenum GLint ref
Definition: glew.h:1813
boost::optional< boost::uint32_t > get_seed()
void set_callback_item_change(const std::function< void(size_t)> &callback)
Definition: listbox.hpp:219
map_generator * last_map_generator_
Last used map generator, must be in map_generators_.
virtual void user_config(CVideo &v)
Display the interactive screen, which allows the user to modify how the generator behaves...
int get_selected_row() const
Returns the first selected row.
Definition: listbox.cpp:237
Thrown when a lexical_cast fails.
tfield_text * register_text(const std::string &id, const bool mandatory, const std::function< std::string()> &callback_load_value=std::function< std::string()>(), const std::function< void(const std::string &)> &callback_save_value=std::function< void(const std::string &)>(), const bool capture_focus=false)
Creates a new text field.
Definition: dialog.cpp:127
int current_map_generator_
Current map generator index.