The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
set_starting_position.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2016 by Ignacio Riquelme Morelle <[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 
15 #define GETTEXT_DOMAIN "wesnoth-editor"
16 
18 
19 #include "formatter.hpp"
20 #include "formula/string_utils.hpp"
21 #include "gettext.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/window.hpp"
30 #include "map/location.hpp"
31 
32 namespace gui2
33 {
34 
35 /*WIKI
36  * @page = GUIWindowDefinitionWML
37  * @order = 2_editor_set_starting_position
38  *
39  * == Editor set starting position ==
40  *
41  * Map editor dialog for setting player starting positions.
42  *
43  * @begin{table}{dialog_widgets}
44  *
45  * listbox & & listbox & m &
46  * Listbox displaying player choices. $
47  *
48  * -player & & control & m &
49  * Widget which shows a player item label. $
50  *
51  * -location & & control & m &
52  * Widget which shows the coordinates to the current
53  * starting position for a player if it exists. $
54  *
55  * ok & & button & m &
56  * OK button. $
57  *
58  * cancel & & button & m &
59  * Cancel button. $
60  *
61  * @end{table}
62  */
63 
64 REGISTER_DIALOG(editor_set_starting_position)
65 
67  unsigned current_player,
68  unsigned maximum_players,
69  const std::vector<map_location>& starting_positions)
70  : selection_(std::min(current_player, maximum_players))
71  , starting_positions_(starting_positions)
72 {
73  if(starting_positions_.size() != maximum_players) {
74  starting_positions_.resize(maximum_players);
75  }
76 }
77 
79 {
80  tlistbox& list = find_widget<tlistbox>(&window, "listbox", false);
81  window.keyboard_capture(&list);
82 
83  std::map<std::string, string_map> data;
85 
86  column["label"] = _("player^None");
87  data.insert(std::make_pair("player", column));
88  list.add_row(data);
89 
90  for(unsigned i = 0; i < starting_positions_.size(); ++i) {
91  const map_location& player_pos = starting_positions_[i];
92 
93  data.clear();
94 
95  utils::string_map symbols;
96  symbols["player_number"] = std::to_string(i + 1);
97 
99  _("Player $player_number"), &symbols);
100  data.insert(std::make_pair("player", column));
101 
102  if(player_pos.valid()) {
103  column["label"] = (formatter() << "(" << player_pos.x + 1 << ", "
104  << player_pos.y + 1 << ")").str();
105  data.insert(std::make_pair("location", column));
106  }
107 
108  list.add_row(data);
109  }
110 
111  list.select_row(selection_);
112 }
113 
115 {
116  if(get_retval() != twindow::OK) {
117  return;
118  }
119 
120  tlistbox& list = find_widget<tlistbox>(&window, "listbox", false);
121  selection_ = list.get_selected_row();
122 }
123 }
std::string interpolate_variables_into_string(const std::string &str, const string_map *const symbols)
Function which will interpolate variables, starting with '$' in the string 'str' with the equivalent ...
int get_retval() const
Definition: dialog.hpp:161
void pre_show(twindow &window)
Inherited from tdialog.
This file contains the window object, this object is a top level container which has the event manage...
bool select_row(const unsigned row, const bool select=true)
Selectes a row.
Definition: listbox.cpp:228
REGISTER_DIALOG(label_settings)
STL namespace.
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
Definition: glew.h:1347
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:62
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:82
Dialog is closed with ok button.
Definition: window.hpp:125
std::map< std::string, t_string > string_map
This file contains the settings handling of the widget library.
bool valid() const
Definition: location.hpp:69
std::ostringstream wrapper.
Definition: formatter.hpp:32
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 post_show(twindow &window)
Inherited from tdialog.
std::vector< map_location > starting_positions_
std::map< std::string, t_string > string_map
Definition: generator.hpp:23
Encapsulates the map of the game.
Definition: location.hpp:38
GLenum GLenum GLvoid GLvoid * column
Definition: glew.h:3805
The listbox class.
Definition: listbox.hpp:39
size_t i
Definition: function.cpp:1057
int get_selected_row() const
Returns the first selected row.
Definition: listbox.cpp:237