The Battle for Wesnoth  1.19.0+dev
lobby_player_list_helper.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2024
3  by Tomasz Sniatowski <kailoran@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
17 
18 #include "font/text_formatting.hpp"
20 #include "gettext.hpp"
22 #include "gui/widgets/label.hpp"
25 #include "gui/widgets/window.hpp"
26 
27 static lg::log_domain log_lobby("lobby");
28 #define ERR_LB LOG_STREAM(err, log_lobby)
29 
30 namespace gui2
31 {
32 lobby_player_list_helper::sub_list::sub_list(tree_view* parent_tree, const std::string& lbl, const bool unfolded)
33 {
34  widget_data tree_group_item;
35  tree_group_item["tree_view_node_label"]["label"] = lbl;
36 
37  root = &parent_tree->add_node("player_group", tree_group_item);
38 
39  if(unfolded) {
40  root->unfold();
41  }
42 
43  label_player_count = find_widget<label>(root, "player_count", false, true);
44  assert(label_player_count);
45 }
46 
48 {
49  label_player_count->set_label(std::to_string(root->count_children()));
50 }
51 
52 namespace
53 {
54 struct update_pod
55 {
56  /** The raw data used to mass-construct player tree nodes. */
57  std::vector<widget_data> node_data;
58 
59  /** The associated user data for each node, index-to-index. */
60  std::vector<const mp::user_info*> user_data;
61 };
62 } // namespace
63 
64 void lobby_player_list_helper::update(const std::vector<mp::user_info>& user_info, int focused_game)
65 {
66  const unsigned scrollbar_position = tree->get_vertical_scrollbar_item_position();
67  std::array<update_pod, std::tuple_size<decltype(player_lists)>::value> inputs{};
68 
69  for(const auto& user : user_info) {
70  std::string name = user.name;
71 
72  std::stringstream icon_ss;
73  icon_ss << "lobby/status";
74 
75  switch(user.get_state(focused_game)) {
77  icon_ss << "-lobby";
78  break;
80  name = font::span_color({0, 255, 255}, name);
81  icon_ss << (user.observing ? "-obs" : "-playing");
82  break;
84  name = font::span_color(font::GRAY_COLOR, name);
85  icon_ss << (user.observing ? "-obs" : "-playing");
86  break;
87  }
88 
89  switch(user.get_relation()) {
91  icon_ss << "-s";
92  break;
94  icon_ss << "-n";
95  break;
97  icon_ss << "-f";
98  break;
100  icon_ss << "-i";
101  break;
102  }
103 
104  icon_ss << ".png";
105 
106  widget_item tree_group_field;
107  widget_data tree_group_item;
108 
109  /*** Add tree item ***/
110  tree_group_field["label"] = icon_ss.str();
111  tree_group_item["icon"] = tree_group_field;
112 
113  tree_group_field["label"] = name;
114  tree_group_field["use_markup"] = "true";
115  tree_group_item["name"] = tree_group_field;
116 
117  // Indices here must match the order of the lists in the player_lists array (see `init`)
118  switch(user.get_state(focused_game)) {
120  inputs[0].node_data.push_back(std::move(tree_group_item));
121  inputs[0].user_data.push_back(&user);
122 
123  break;
125  inputs[1].node_data.push_back(std::move(tree_group_item));
126  inputs[1].user_data.push_back(&user);
127 
128  break;
130  inputs[2].node_data.push_back(std::move(tree_group_item));
131  inputs[2].user_data.push_back(&user);
132 
133  break;
134  }
135  }
136 
137  info_map.clear();
138 
139  for(std::size_t i = 0; i < player_lists.size(); ++i) {
140  assert(inputs[i].node_data.size() == inputs[i].user_data.size());
141 
142  // Add the player nodes
143  const auto new_nodes = player_lists[i].root->replace_children("player", inputs[i].node_data);
144 
145  for(std::size_t k = 0; k < new_nodes.size(); ++k) {
146  auto* node = new_nodes[k].get();
147  auto* info = inputs[i].user_data[k];
148 
149  // Note the user_info associated with this node
150  info_map.try_emplace(node, info);
151 
153  find_widget<toggle_panel>(node, "tree_view_node_label", false),
154  std::bind(user_callback, info)
155  );
156  }
157 
158  player_lists[i].update_player_count_label();
159  }
160 
161  // Don't attempt to restore the scroll position if the window hasn't been laid out yet
162  if(tree->get_origin() != point{-1, -1}) {
163  tree->set_vertical_scrollbar_item_position(scrollbar_position);
164  }
165 }
166 
168 {
169  tree = find_widget<tree_view>(&w, "player_tree", false, true);
170 
171  player_lists = {
172  sub_list{tree, _("Selected Game"), true},
173  sub_list{tree, _("Lobby"), true},
174  sub_list{tree, _("Other Games"), false}
175  };
176 }
177 
179 {
180  return info_map.at(tree->selected_item());
181 }
182 
183 } // namespace gui2
std::map< const tree_view_node *, const mp::user_info * > info_map
Node-to-info mappings for easy access.
const mp::user_info * get_selected_info() const
std::array< sub_list, 3 > player_lists
Main subsections of the player tree.
void update(const std::vector< mp::user_info > &user_info, int focused_game)
Updates the tree contents based on the given user data.
std::function< void(const mp::user_info *)> user_callback
The double click callback bound to each player's tree node.
unsigned get_vertical_scrollbar_item_position() const
Returns current position of the vertical scrollbar.
void set_vertical_scrollbar_item_position(const unsigned position)
Move the vertical scrollbar to a position.
void unfold(const bool recursive=false)
tree_view_node & add_node(const std::string &id, const widget_data &data, const int index=-1)
Definition: tree_view.cpp:56
tree_view_node * selected_item()
Definition: tree_view.hpp:88
point get_origin() const
Returns the screen origin of the widget.
Definition: widget.cpp:301
base class of top level items, the only item which needs to store the final canvases to draw on.
Definition: window.hpp:61
std::size_t i
Definition: function.cpp:968
int w
static std::string _(const char *str)
Definition: gettext.hpp:93
This file contains the window object, this object is a top level container which has the event manage...
static lg::log_domain log_lobby("lobby")
std::vector< widget_data > node_data
The raw data used to mass-construct player tree nodes.
std::vector< const mp::user_info * > user_data
The associated user data for each node, index-to-index.
const color_t GRAY_COLOR
std::string span_color(const color_t &color)
Returns a Pango formatting string using the provided color_t object.
void connect_signal_mouse_left_double_click(dispatcher &dispatcher, const signal &signal)
Connects a signal handler for a left mouse button double click.
Definition: dispatcher.cpp:198
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
logger & info()
Definition: log.cpp:314
This class represents the information a client has about another player.
Definition: lobby_data.hpp:30
Holds a 2D point.
Definition: point.hpp:25