The Battle for Wesnoth  1.15.1+dev
set_starting_position.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2018 by Iris Morelle <shadowm2006@gmail.com>
3  Part of the Battle for Wesnoth Project https://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 #include "gui/widgets/listbox.hpp"
24 #include "gui/widgets/settings.hpp"
25 #include "gui/widgets/window.hpp"
26 #include "map/location.hpp"
27 
28 namespace gui2
29 {
30 namespace dialogs
31 {
32 
33 /*WIKI
34  * @page = GUIWindowDefinitionWML
35  * @order = 2_editor_set_starting_position
36  *
37  * == Editor set starting position ==
38  *
39  * Map editor dialog for setting player starting positions.
40  *
41  * @begin{table}{dialog_widgets}
42  *
43  * listbox & & listbox & m &
44  * Listbox displaying player choices. $
45  *
46  * -player & & styled_widget & m &
47  * Widget which shows a player item label. $
48  *
49  * -location & & styled_widget & m &
50  * Widget which shows the coordinates to the current
51  * starting position for a player if it exists. $
52  *
53  * ok & & button & m &
54  * OK button. $
55  *
56  * cancel & & button & m &
57  * Cancel button. $
58  *
59  * @end{table}
60  */
61 
62 REGISTER_DIALOG(editor_set_starting_position)
63 
65  unsigned current_player,
66  unsigned maximum_players,
67  const std::vector<map_location>& starting_positions)
68  : selection_(std::min(current_player, maximum_players))
69  , starting_positions_(starting_positions)
70 {
71  if(starting_positions_.size() != maximum_players) {
72  starting_positions_.resize(maximum_players);
73  }
74 }
75 
77 {
78  listbox& list = find_widget<listbox>(&window, "listbox", false);
79  window.keyboard_capture(&list);
80 
81  std::map<std::string, string_map> data;
82  string_map column;
83 
84  column["label"] = _("player^None");
85  data.emplace("player", column);
86  list.add_row(data);
87 
88  for(unsigned i = 0; i < starting_positions_.size(); ++i) {
89  const map_location& player_pos = starting_positions_[i];
90 
91  data.clear();
92 
93  utils::string_map symbols;
94  symbols["player_number"] = std::to_string(i + 1);
95 
97  _("Player $player_number"), &symbols);
98  data.emplace("player", column);
99 
100  if(player_pos.valid()) {
101  column["label"] = (formatter() << "(" << player_pos.wml_x() << ", "
102  << player_pos.wml_y() << ")").str();
103  data.emplace("location", column);
104  }
105 
106  list.add_row(data);
107  }
108 
109  list.select_row(selection_);
110 }
111 
113 {
114  if(get_retval() != retval::OK) {
115  return;
116  }
117 
118  listbox& list = find_widget<listbox>(&window, "listbox", false);
119  selection_ = list.get_selected_row();
120 }
121 } // namespace dialogs
122 } // namespace gui2
std::string interpolate_variables_into_string(const std::string &str, const string_map *const symbols)
Function which will interpolate variables, starting with &#39;$&#39; in the string &#39;str&#39; with the equivalent ...
std::map< std::string, t_string > string_map
boost::bimaps::bimap< boost::bimaps::set_of< std::string >, boost::bimaps::multiset_of< coordinate > > starting_positions
This file contains the window object, this object is a top level container which has the event manage...
int wml_x() const
Definition: location.hpp:157
STL namespace.
int get_selected_row() const
Returns the first selected row.
Definition: listbox.cpp:272
bool select_row(const unsigned row, const bool select=true)
Selects a row.
Definition: listbox.cpp:250
Generic file dialog.
Definition: field-fwd.hpp:22
The listbox class.
Definition: listbox.hpp:40
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:91
This file contains the settings handling of the widget library.
std::ostringstream wrapper.
Definition: formatter.hpp:38
int wml_y() const
Definition: location.hpp:158
bool valid() const
Definition: location.hpp:93
Various uncategorised dialogs.
Encapsulates the map of the game.
Definition: location.hpp:42
std::size_t i
Definition: function.cpp:933
std::map< std::string, t_string > string_map
Definition: widget.hpp:24
grid & 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:66
virtual void post_show(window &window) override
Inherited from modal_dialog.
virtual void pre_show(window &window) override
Inherited from modal_dialog.
Dialog was closed with the OK button.
Definition: retval.hpp:34
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:63