The Battle for Wesnoth  1.17.0-dev
display_chat_manager.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2021
3  by Chris Beck <render787@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 
16 #include "display_chat_manager.hpp"
17 
19 #include "display.hpp"
20 #include "floating_label.hpp"
21 #include "game_board.hpp" // <-- only needed for is_observer()
22 #include "preferences/game.hpp"
23 #include "log.hpp"
24 #include "font/sdl_ttf_compat.hpp"
25 #include "mp_ui_alerts.hpp"
27 #include "color.hpp"
30 
31 #include <SDL2/SDL_timer.h>
32 
33 static lg::log_domain log_engine("engine");
34 #define ERR_NG LOG_STREAM(err, log_engine)
35 
36 namespace {
37  const int chat_message_border = 5;
38  const int chat_message_x = 10;
39  const color_t chat_message_color {255,255,255,SDL_ALPHA_OPAQUE};
40  const color_t chat_message_bg {0,0,0,140};
41 }
42 
44  : speaker_handle(speaker), handle(h), created_at(SDL_GetTicks())
45 {}
46 
47 
48 void display_chat_manager::add_chat_message(const std::time_t& time, const std::string& speaker,
49  int side, const std::string& message, events::chat_handler::MESSAGE_TYPE type,
50  bool bell)
51 {
52  const bool whisper = speaker.find("whisper: ") == 0;
53  std::string sender = speaker;
54  if (whisper) {
55  sender.assign(speaker, 9, speaker.size());
56  add_whisperer( sender );
57  }
58  //remove disconnected user from whisperer
59  std::string::size_type pos = message.find(" has disconnected");
60  if (pos != std::string::npos){
61  for(std::set<std::string>::const_iterator w = whisperers().begin(); w != whisperers().end(); ++w){
62  if (*w == message.substr(0,pos)) remove_whisperer(*w);
63  }
64  }
65 
66  if (!preferences::parse_should_show_lobby_join(sender, message)) return;
67  if (preferences::is_ignored(sender)) return;
68 
69  //preferences::parse_admin_authentication(sender, message); TODO: replace
70 
71  bool is_observer = false;
72  { //TODO: Clean this block up somehow
73 
74  const game_board * board = dynamic_cast<const game_board*>(&my_disp_.get_disp_context());
75 
76  if (board) {
77  is_observer = board->is_observer();
78  }
79  }
80 
81  if (bell) {
82  if ((type == events::chat_handler::MESSAGE_PRIVATE && (!is_observer || whisper))
83  || utils::word_match(message, preferences::login())) {
84  mp_ui_alerts::private_message(false, sender, message);
85  } else if (preferences::is_friend(sender)) {
86  mp_ui_alerts::friend_message(false, sender, message);
87  } else if (sender == "server") {
88  mp_ui_alerts::server_message(false, sender, message);
89  } else {
90  mp_ui_alerts::public_message(false, sender, message);
91  }
92  }
93 
94  bool action = false;
95 
96  std::string msg;
97 
98  if (message.compare(0,4,"/me ") == 0) {
99  msg.assign(message, 4, message.size());
100  action = true;
101  } else {
102  msg = message;
103  }
104 
105  try {
106  // We've had a joker who send an invalid utf-8 message to crash clients
107  // so now catch the exception and ignore the message.
109  } catch (utf8::invalid_utf8_exception&) {
110  ERR_NG << "Invalid utf-8 found, chat message is ignored." << std::endl;
111  return;
112  }
113 
114  int ypos = chat_message_x;
115  for(std::vector<chat_message>::const_iterator m = chat_messages_.begin(); m != chat_messages_.end(); ++m) {
116  ypos += std::max(font::get_floating_label_rect(m->handle).h,
117  font::get_floating_label_rect(m->speaker_handle).h);
118  }
119  color_t speaker_color {255,255,255,SDL_ALPHA_OPAQUE};
120  if(side >= 1) {
121  speaker_color = team::get_side_color_range(side).mid();
122  }
123 
124  color_t message_color = chat_message_color;
125  std::stringstream str;
126  std::stringstream message_str;
127 
129  if(action) {
130  str << "<" << speaker << " " << msg << ">";
131  message_color = speaker_color;
132  message_str << " ";
133  } else {
134  if (!speaker.empty())
135  str << "<" << speaker << ">";
136  message_str << msg;
137  }
138  } else {
139  if(action) {
140  str << "*" << speaker << " " << msg << "*";
141  message_color = speaker_color;
142  message_str << " ";
143  } else {
144  if (!speaker.empty())
145  str << "*" << speaker << "*";
146  message_str << msg;
147  }
148  }
149 
150  // Prepend message with timestamp.
151  std::stringstream message_complete;
152  message_complete << preferences::get_chat_timestamp(time) << str.str();
153 
154  const SDL_Rect rect = my_disp_.map_outside_area();
155 
156  font::floating_label spk_flabel(message_complete.str());
157  spk_flabel.set_font_size(font::SIZE_15);
158  spk_flabel.set_color(speaker_color);
159  spk_flabel.set_position(rect.x + chat_message_x, rect.y + ypos);
160  spk_flabel.set_clip_rect(rect);
161  spk_flabel.set_alignment(font::LEFT_ALIGN);
162  spk_flabel.set_bg_color(chat_message_bg);
163  spk_flabel.set_border_size(chat_message_border);
164  spk_flabel.use_markup(false);
165 
166  int speaker_handle = font::add_floating_label(spk_flabel);
167 
168  font::floating_label msg_flabel(message_str.str());
169  msg_flabel.set_font_size(font::SIZE_15);
170  msg_flabel.set_color(message_color);
171  msg_flabel.set_position(rect.x + chat_message_x + font::get_floating_label_rect(speaker_handle).w,
172  rect.y + ypos);
173  msg_flabel.set_clip_rect(rect);
174  msg_flabel.set_alignment(font::LEFT_ALIGN);
175  msg_flabel.set_bg_color(chat_message_bg);
176  msg_flabel.set_border_size(chat_message_border);
177  msg_flabel.use_markup(false);
178 
179  int message_handle = font::add_floating_label(msg_flabel);
180 
181  chat_messages_.emplace_back(speaker_handle,message_handle);
182 
184 }
185 
186 static unsigned int safe_subtract(unsigned int a, unsigned int b)
187 {
188  return (a > b) ? a - b : 0;
189 }
190 
192 {
193  //NOTE: prune_chat_messages(false) seems to be only called when a new message is added, which in
194  // particular means the aging feature won't work unless new messages are addded regularly
195  const unsigned message_aging = preferences::chat_message_aging();
196  const unsigned max_chat_messages = preferences::chat_lines();
197  const bool enable_aging = message_aging != 0;
198 
199  const unsigned remove_before = enable_aging ? safe_subtract(SDL_GetTicks(), message_aging * 60 * 1000) : 0;
200  int movement = 0;
201 
202  if(enable_aging || remove_all || chat_messages_.size() > max_chat_messages) {
203  while (!chat_messages_.empty() &&
204  (remove_all ||
205  chat_messages_.front().created_at < remove_before ||
206  chat_messages_.size() > max_chat_messages))
207  {
208  const chat_message &old = chat_messages_.front();
209  movement += font::get_floating_label_rect(old.handle).h;
212  chat_messages_.erase(chat_messages_.begin());
213  }
214  }
215 
216  for(const chat_message &cm : chat_messages_) {
217  font::move_floating_label(cm.speaker_handle, 0, - movement);
218  font::move_floating_label(cm.handle, 0, - movement);
219  }
220 }
Game board class.
Definition: game_board.hpp:51
static unsigned int safe_subtract(unsigned int a, unsigned int b)
void remove_floating_label(int handle, int fadeout)
removes the floating label given by &#39;handle&#39; from the screen
const std::set< std::string > & whisperers() const
#define a
static const color_range get_side_color_range(int side)
Definition: team.cpp:949
int chat_lines()
Definition: game.cpp:900
#define h
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:110
void set_font_size(int font_size)
std::vector< chat_message > chat_messages_
void add_chat_message(const std::time_t &time, const std::string &speaker, int side, const std::string &msg, events::chat_handler::MESSAGE_TYPE type, bool bell)
void move_floating_label(int handle, double xmove, double ymove)
moves the floating label given by &#39;handle&#39; by (xmove,ymove)
#define b
void friend_message(bool is_lobby, const std::string &sender, const std::string &message)
bool is_friend(const std::string &nick)
Definition: game.cpp:278
void add_whisperer(const std::string &nick)
const int SIZE_15
Definition: constants.cpp:28
void private_message(bool is_lobby, const std::string &sender, const std::string &message)
#define ERR_NG
std::string pango_word_wrap(const std::string &unwrapped_text, int font_size, int max_width, int max_height, int max_lines, bool)
Uses Pango to word wrap text.
map_display and display: classes which take care of displaying the map and game-data on the screen...
void prune_chat_messages(bool remove_all=false)
void remove_whisperer(const std::string &nick)
bool is_ignored(const std::string &nick)
Definition: game.cpp:290
std::string login()
bool faked() const
Definition: video.hpp:66
static lg::log_domain log_engine("engine")
Thrown by operations encountering invalid UTF-8 data.
bool is_observer() const
Check if we are an observer in this game.
void public_message(bool is_lobby, const std::string &sender, const std::string &message)
const SDL_Rect & map_outside_area() const
Returns the available area for a map, this may differ from the above.
Definition: display.hpp:242
const display_context & get_disp_context() const
Definition: display.hpp:172
int add_floating_label(const floating_label &flabel)
add a label floating on the screen above everything else.
SDL_Rect get_floating_label_rect(int handle)
void server_message(bool is_lobby, const std::string &sender, const std::string &message)
int w
std::string get_chat_timestamp(const std::time_t &t)
Definition: game.cpp:877
int chat_message_aging()
Definition: game.cpp:915
Standard logging facilities (interface).
CVideo & video()
Gets the underlying screen object.
Definition: display.hpp:201
bool parse_should_show_lobby_join(const std::string &sender, const std::string &message)
Definition: game.cpp:318
std::shared_ptr< halo_record > handle
Definition: halo.hpp:30
Transitional API for porting SDL_ttf-based code to Pango.
color_t mid() const
Average color shade.
Definition: color_range.hpp:86
bool word_match(const std::string &message, const std::string &word)
Check if a message contains a word.