The Battle for Wesnoth  1.17.0-dev
player_list_helper.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017 - 2021
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 
16 
18 #include "gui/widgets/listbox.hpp"
19 #include "gui/widgets/window.hpp"
21 
22 namespace gui2
23 {
25  : list_(find_widget<listbox>(window, "player_list", false))
26 {
27  // add ourselves as the host
28  std::map<std::string, string_map> data = {
29  { "player_type_icon", {{ "label", "misc/leader-crown.png~CROP(12, 1, 15, 15)"}}},
30  { "player_name", {{ "label", preferences::login()}}}
31  };
32  list_.add_row(data);
33  list_.select_row(0);
34 }
35 
37 {
38  list_.clear();
39  unsigned i = 0;
40 
41  for(const config& user : users) {
42  std::map<std::string, string_map> data;
44 
45  const std::string name = user["name"];
46  const bool is_you = name == preferences::login();
47 
48  std::string icon;
49  if(user["host"].to_bool()) {
50  icon = "misc/leader-crown.png~CROP(12, 1, 15, 15)";
51  } else if(user["observer"].to_bool()) {
52  icon = "misc/eye.png";
53  } else if(is_you) {
54  icon = "lobby/status-lobby-s.png";
55  } else {
56  icon = "lobby/status-lobby-n.png";
57  }
58 
59  item["label"] = icon;
60  data.emplace("player_type_icon", item);
61 
62  item["label"] = name;
63  data.emplace("player_name", item);
64 
65  list_.add_row(data);
66 
67  if(is_you) {
68  list_.select_row(i);
69  }
70 
71  ++i;
72  }
73 }
74 
75 } // namespace gui2
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)
Selects a row.
Definition: listbox.cpp:252
T * find_widget(utils::const_clone_ptr< widget, T > widget, const std::string &id, const bool must_be_active, const bool must_exist)
Gets a widget with the wanted id.
Definition: find_widget.hpp:69
Generic file dialog.
Definition: field-fwd.hpp:23
The listbox class.
Definition: listbox.hpp:43
void clear()
Removes all the rows in the listbox, clearing it.
Definition: listbox.cpp:127
player_list_helper(window *window)
boost::iterator_range< const_child_iterator > const_child_itors
Definition: config.hpp:206
std::string login()
std::size_t i
Definition: function.cpp:967
std::map< std::string, t_string > string_map
Definition: widget.hpp:26
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:68
void update_list(const config::const_child_itors &users)
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:65
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:410