The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
multiplayer_ui.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2007 - 2016 by David White <[email protected]>
3  Part of the Battle for Wesnoth Project http://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 #ifndef MULTIPLAYER_UI_HPP_INCLUDED
15 #define MULTIPLAYER_UI_HPP_INCLUDED
16 
17 #include "chat_events.hpp"
18 #include "floating_label.hpp"
20 #include "preferences_display.hpp"
22 #include "widgets/combo.hpp"
23 #include "widgets/label.hpp"
24 #include "widgets/menu.hpp"
25 #include "widgets/textbox.hpp"
26 
27 #include <deque>
28 #include <boost/scoped_ptr.hpp>
29 
30 class display;
31 class game_display;
32 class config;
33 class plugins_context;
34 
36 namespace mp {
37 
40 
41 /** this class memorizes a chat session. */
42 class chat
43 {
44 public:
45  chat();
46 
47  void add_message(const time_t& time, const std::string& user,
48  const std::string& message);
49 
50  void init_textbox(gui::textbox& textbox);
51  void update_textbox(gui::textbox& textbox);
52 
53  void clear_history();
54 
55 private:
56  struct msg {
57  msg(const time_t& time, const std::string& user, const std::string& message)
58  : time(time), user(user), message(message) {}
59  time_t time;
62  };
63  typedef std::deque<msg> msg_hist;
64 
65  std::string format_message(const msg& message);
66  SDL_Color color_message(const msg& message);
67 
68  msg_hist message_history_;
69  msg_hist::size_type last_update_;
70 };
71 
72 /**
73  * a base class for the different multiplayer base dialogs: game list, create
74  * game, wait game, game setup.
75  */
77 {
78 public:
80  PLAY, QUIT };
81 
82  ui(CVideo& v, twesnothd_connection* wesnothd_connection, const std::string& title,
83  const config& cfg, chat& c, config& gamelist);
84 
85  /**
86  * Asks the multiplayer_ui to pump some data from the network, and then to
87  * process it. The actual processing will be left to the child classes,
88  * through process_network_data and process_network_error.
89  */
90  void process_network();
91 
92  /**
93  * Returns the result of the current widget. While the result is equal to
94  * continue, the widget should not be destroyed.
95  */
97 
98  /**
99  * Hides children, moves them (using layout_children), then shows them.
100  *
101  * The methodes hide_children and layout_children are supposed to be
102  * overridden by subclasses of this class which add new sub-widgets.
103  */
104  void set_location(const SDL_Rect& rect);
105  using widget::set_location;
106  const std::vector<std::string>& user_list() const { return user_list_; }
107  void send_to_server(const config& cfg) override;
109 protected:
110  int xscale(int x) const;
111  int yscale(int y) const;
112  static const int xscale_base;
113  static const int yscale_base;
114 
115  SDL_Rect client_area() const;
116 
119  CVideo& video() { return video_; }
120 
121  /**
122  * Returns the main game config, as defined by loading the preprocessed WML
123  * files. Children of this class may need this to obtain, for example, the
124  * list of available eras.
125  */
126  const config& game_config() const;
127 
128  virtual void draw_contents();
129 
130  virtual void process_event();
131 
132  virtual void handle_event(const SDL_Event& event);
133  virtual void handle_key_event(const SDL_KeyboardEvent& event);
134 
135  /** Override chat_handler. */
136  void add_chat_message(const time_t& time, const std::string& speaker,
137  int side, const std::string& message,
139  void send_chat_message(const std::string& message, bool allies_only=false);
140 
141  /** Process chat messages. */
142  void process_message(const config& msg, const bool whisper=false);
143 
144  /**
145  * Processes any pending network data. Called by the public
146  * process_network() method. Overridden by subclasses who add more
147  * behavior for network.
148  */
149  virtual void process_network_data(const config& data);
150 
151  /**
152  * Hides or shows all gui::widget children of this widget. Should be
153  * overridden by subclasses which add their own children.
154  */
155  virtual void hide_children(bool hide=true);
156 
157  /**
158  * Lays the children out. This method is to be overridden by the subclasses
159  * of the mp_ui class; it will be called.
160  */
161  virtual void layout_children(const SDL_Rect& rect);
162 
163  /** Sets the result of this dialog, to be checked by get_result(). */
165 
166  /**
167  * Sets the name of the selected game which is used to highlight the names
168  * of the players which have joined this game.
169  */
170  void set_selected_game(const std::string& game_name);
171 
172  /**
173  * Called each time the gamelist_ variable is updated. May be
174  * overridden by child classes to add custom gamelist behavior.
175  */
176  virtual void gamelist_updated(bool silent=true);
177 
178  /** Sets the user list */
179  void set_user_list(const std::vector<std::string>&, bool silent);
180  void set_user_menu_items(const std::vector<std::string>& list);
181 
182  /** Returns the current gamelist */
183  config& gamelist() { return gamelist_; }
184 
185  void append_to_title(const std::string& name);
186  const gui::label& title() const;
187 
190  void set_selected_user_changed(const bool& changed) { selected_user_changed_ = changed; }
191 
192 private:
193  /**
194  * Set to true when the widgets are initialized. Allows delayed
195  * initialization on first positioning.
196  */
199 
200  /**
201  * The main game configuration, as defined by loading the preprocessed WML
202  * files. Access using the game_config() method if necessary.
203  */
205 
207 
209 
213 
215 
216  std::vector<std::string> user_list_;
217 
219 
222 
224 
226 
227  Uint32 lobby_clock_;
228 
229  std::map<std::string, time_t> whisper_warnings_;
230 
231 public:
234 
235 private:
236  struct user_info
237  {
239  name(),
240  game_id(),
241  location(),
242  relation(ME),
243  state(LOBBY),
244  registered()
245  {
246  }
247 
253  /** True if this user is registered on the server. */
255  bool operator> (const user_info& b) const;
256  };
257 
258 protected:
259  boost::scoped_ptr<plugins_context> plugins_context_;
260 public:
262 };
263 
264 }
265 
266 #endif
gui::textbox entry_textbox_
chat & chat_
bool selected_user_changed() const
virtual void handle_key_event(const SDL_KeyboardEvent &event)
virtual void draw_contents()
std::vector< std::string > user_list_
const GLfloat * c
Definition: glew.h:12741
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1221
boost::scoped_ptr< plugins_context > plugins_context_
ui(CVideo &v, twesnothd_connection *wesnothd_connection, const std::string &title, const config &cfg, chat &c, config &gamelist)
Definition: video.hpp:58
std::map< std::string, time_t > whisper_warnings_
user_relation relation
virtual void layout_children(const SDL_Rect &rect)
Lays the children out.
result get_result()
Returns the result of the current widget.
std::string get_color_string(int id)
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1220
void add_message(const time_t &time, const std::string &user, const std::string &message)
gui::label title_
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
Definition: glew.h:1347
std::string user
std::string format_message(const msg &message)
msg_hist::size_type last_update_
void init_textbox(gui::textbox &textbox)
virtual void process_network_data(const config &data)
Processes any pending network data.
virtual void hide_children(bool hide=true)
Hides or shows all gui::widget children of this widget.
msg_hist message_history_
GLdouble GLdouble GLdouble b
Definition: glew.h:6966
This module controls the multiplayer lobby.
Uint32 lobby_clock_
std::string selected_user_
GLuint64EXT * result
Definition: glew.h:10727
A class that represents a TCP/IP connection to the wesnothd server.
msg(const time_t &time, const std::string &user, const std::string &message)
result set_result(result res)
Sets the result of this dialog, to be checked by get_result().
plugins_context * get_plugins_context()
std::deque< msg > msg_hist
static const int xscale_base
void set_selected_user_changed(const bool &changed)
const config & game_config() const
Returns the main game config, as defined by loading the preprocessed WML files.
const config & game_config_
The main game configuration, as defined by loading the preprocessed WML files.
std::string selected_game_
virtual void hide(bool value=true)
Definition: widget.cpp:162
void add_chat_message(const time_t &time, const std::string &speaker, int side, const std::string &message, events::chat_handler::MESSAGE_TYPE type=events::chat_handler::MESSAGE_PRIVATE)
Override chat_handler.
const GLdouble * v
Definition: glew.h:1359
GLenum GLenum dst
Definition: glew.h:2392
const gui::label & title() const
virtual void process_event()
void send_to_server(const config &cfg) override
int yscale(int y) const
std::string message
bool initialized_
Set to true when the widgets are initialized.
CVideo & video()
bool registered
True if this user is registered on the server.
void process_message(const config &msg, const bool whisper=false)
Process chat messages.
void append_to_title(const std::string &name)
bool gamelist_refresh_
Encapsulates the map of the game.
Definition: location.hpp:38
gui::textbox chat_textbox_
gui::menu users_menu_
virtual void handle_event(const SDL_Event &event)
GLuint res
Definition: glew.h:9258
void send_chat_message(const std::string &message, bool allies_only=false)
void clear_history()
SDL_Rect client_area() const
void update_textbox(gui::textbox &textbox)
SDL_Color color_message(const msg &message)
config & gamelist_
void set_user_list(const std::vector< std::string > &, bool silent)
Sets the user list.
structure which will hide all current floating labels, and cause floating labels instantiated after i...
config & gamelist()
Returns the current gamelist.
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:112
GLint GLint GLint GLint GLint x
Definition: glew.h:1220
bool gamelist_initialized_
int xscale(int x) const
const std::vector< std::string > & user_list() const
GLuint const GLchar * name
Definition: glew.h:1782
void set_user_menu_items(const std::vector< std::string > &list)
result result_
bool selected_user_changed_
cl_event event
Definition: glew.h:3070
void set_selected_game(const std::string &game_name)
Sets the name of the selected game which is used to highlight the names of the players which have joi...
void process_network()
Asks the multiplayer_ui to pump some data from the network, and then to process it.
void set_location(const SDL_Rect &rect)
Hides children, moves them (using layout_children), then shows them.
twesnothd_connection * wesnothd_connection_
GLsizei GLenum GLuint GLuint GLsizei char * message
Definition: glew.h:2499
CVideo & video_
this class memorizes a chat session.
static const int yscale_base
std::string get_selected_user_game()
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
bool receive_from_server(config &dst)
bool operator>(const user_info &b) const
GLsizei const GLcharARB ** string
Definition: glew.h:4503
a base class for the different multiplayer base dialogs: game list, create game, wait game...
virtual void gamelist_updated(bool silent=true)
Called each time the gamelist_ variable is updated.