The Battle for Wesnoth  1.15.0+dev
player_list_helper.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017-2018 by the Battle for Wesnoth Project https://www.wesnoth.org/
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY.
10 
11  See the COPYING file for more details.
12 */
13 
15 
17 #include "gui/widgets/listbox.hpp"
18 #include "gui/widgets/window.hpp"
20 
21 namespace gui2
22 {
24  : list_(find_widget<listbox>(window, "player_list", false))
25 {
26  // add ourselves as the host
27  std::map<std::string, string_map> data = {
28  { "player_type_icon", {{ "label", "misc/leader-crown.png~CROP(12, 1, 15, 15)"}}},
29  { "player_name", {{ "label", preferences::login()}}}
30  };
31  list_.add_row(data);
32  list_.select_row(0);
33 }
34 
36 {
37  list_.clear();
38  unsigned i = 0;
39 
40  for(const config& user : users) {
41  std::map<std::string, string_map> data;
43 
44  const std::string name = user["name"];
45  const bool is_you = name == preferences::login();
46 
47  std::string icon;
48  if(user["host"].to_bool()) {
49  icon = "misc/leader-crown.png~CROP(12, 1, 15, 15)";
50  } else if(user["observer"].to_bool()) {
51  icon = "misc/eye.png";
52  } else if(is_you) {
53  icon = "lobby/status-lobby-s.png";
54  } else {
55  icon = "lobby/status-lobby-n.png";
56  }
57 
58  item["label"] = icon;
59  data.emplace("player_type_icon", item);
60 
61  item["label"] = name;
62  data.emplace("player_name", item);
63 
64  list_.add_row(data);
65 
66  if(is_you) {
67  list_.select_row(i);
68  }
69 
70  ++i;
71  }
72 }
73 
74 } // 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:250
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:68
Generic file dialog.
Definition: field-fwd.hpp:22
The listbox class.
Definition: listbox.hpp:40
static const char * name(const std::vector< SDL_Joystick *> &joysticks, const std::size_t index)
Definition: joystick.cpp:48
void clear()
Removes all the rows in the listbox, clearing it.
Definition: listbox.cpp:125
player_list_helper(window *window)
boost::iterator_range< const_child_iterator > const_child_itors
Definition: config.hpp:238
std::string login()
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
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:92
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:63
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:371