The Battle for Wesnoth  1.19.0-dev
player_list_helper.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017 - 2024
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  widget_data data = {
29  { "player_type_icon", {{ "label", "misc/leader-crown.png~CROP(12, 1, 15, 15)"}}},
30  { "player_name", {{ "label", preferences::login()}}}
31  };
33  list_.select_row(0);
34 }
35 
37 {
38  list_.clear();
39  unsigned i = 0;
40 
41  for(const config& user : users) {
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 
66 
67  if(is_you) {
69  }
70 
71  ++i;
72  }
73 }
74 
75 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
boost::iterator_range< const_child_iterator > const_child_itors
Definition: config.hpp:283
The listbox class.
Definition: listbox.hpp:43
grid & add_row(const widget_item &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:59
bool select_row(const unsigned row, const bool select=true)
Selects a row.
Definition: listbox.cpp:243
void clear()
Removes all the rows in the listbox, clearing it.
Definition: listbox.cpp:118
void update_list(const config::const_child_itors &users)
player_list_helper(window *window)
base class of top level items, the only item which needs to store the final canvases to draw on.
Definition: window.hpp:63
std::size_t i
Definition: function.cpp:968
This file contains the window object, this object is a top level container which has the event manage...
Generic file dialog.
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:34
std::map< std::string, t_string > widget_item
Definition: widget.hpp:31
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
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:412
std::string login()
std::string_view data
Definition: picture.cpp:194