Making is possible to color messages sent by server moderators via /query

This commit is contained in:
Thomas Baumhauer 2008-12-23 18:11:58 +00:00
parent cb22b1895f
commit 0d7a5d6e75
4 changed files with 41 additions and 26 deletions

View file

@ -17,6 +17,7 @@
#include "construct_dialog.hpp"
#include "gettext.hpp"
#include "log.hpp"
#include "marked-up_text.hpp"
#include "multiplayer_ui.hpp"
#include "sound.hpp"
#include "replay.hpp"
@ -186,43 +187,56 @@ void chat::add_message(const time_t& time, const std::string& user,
void chat::init_textbox(gui::textbox& textbox)
{
std::string s;
for(msg_hist::const_iterator itor = message_history_.begin();
itor != message_history_.end(); ++itor) {
s.append(format_message(*itor));
textbox.append_text(format_message(*itor), true, itor->user == "server" ?
color_message(*itor): font::NORMAL_COLOUR);
}
textbox.set_text(s);
last_update_ = message_history_.size();
textbox.scroll_to_bottom();
}
void chat::update_textbox(gui::textbox& textbox)
{
std::string s;
for(msg_hist::const_iterator itor = message_history_.begin() + last_update_;
itor != message_history_.end(); ++itor) {
s.append(format_message(*itor));
textbox.append_text(format_message(*itor), true, itor->user == "server" ?
color_message(*itor) : font::NORMAL_COLOUR);
}
textbox.append_text(s,true);
last_update_ = message_history_.size();
}
std::string chat::format_message(const msg& message)
{
std::string msg_text = message.message;
if(message.user == "server") {
SDL_Color c;
int unused;
std::string::const_iterator after_markup =
font::parse_markup(message.message.begin(), message.message.end(), &unused, &c, &unused);
msg_text = std::string(after_markup,message.message.end());
}
if(message.message.substr(0,3) == "/me") {
return preferences::get_chat_timestamp(message.time) + "<" + message.user
+ message.message.substr(3) + ">\n";
+ msg_text.substr(3) + ">\n";
} else {
return preferences::get_chat_timestamp(message.time) + "<" + message.user
+ "> " + message.message + "\n";
+ "> " + msg_text + "\n";
}
}
SDL_Color chat::color_message(const msg& message) {
SDL_Color c = font::NORMAL_COLOUR;
// Normal users are not allowed to color their messages
if(message.user == "server") {
int unused;
font::parse_markup(message.message.begin(), message.message.end(), &unused, &c, &unused);
}
return c;
};
ui::ui(game_display& disp, const std::string& title, const config& cfg, chat& c, config& gamelist) :
gui::widget(disp.video()),
disp_(disp),

View file

@ -62,6 +62,7 @@ private:
typedef std::deque<msg> msg_hist;
std::string format_message(const msg& message);
SDL_Color color_message(const msg& message);
msg_hist message_history_;
msg_hist::size_type last_update_;

View file

@ -16,7 +16,6 @@
#include "widgets/textbox.hpp"
#include "clipboard.hpp"
#include "font.hpp"
#include "log.hpp"
#include "video.hpp"
@ -64,7 +63,7 @@ const std::string textbox::text() const
}
// set_text does not respect max_size_
void textbox::set_text(const std::string& text)
void textbox::set_text(const std::string& text, const SDL_Color& color)
{
text_ = utils::string_to_wstring(text);
cursor_ = text_.size();
@ -72,14 +71,14 @@ void textbox::set_text(const std::string& text)
selstart_ = -1;
selend_ = -1;
set_dirty(true);
update_text_cache(true);
update_text_cache(true, color);
handle_text_changed(text_);
}
void textbox::append_text(const std::string& text, bool auto_scroll)
void textbox::append_text(const std::string& text, bool auto_scroll, const SDL_Color& color)
{
if(text_image_.get() == NULL) {
set_text(text);
set_text(text, color);
return;
}
@ -90,7 +89,7 @@ void textbox::append_text(const std::string& text, bool auto_scroll)
const bool is_at_bottom = get_position() == get_max_position();
const wide_string& wtext = utils::string_to_wstring(text);
const surface new_text = add_text_line(wtext);
const surface new_text = add_text_line(wtext, color);
const surface new_surface = create_compatible_surface(text_image_,std::max<size_t>(text_image_->w,new_text->w),text_image_->h+new_text->h);
SDL_SetAlpha(new_text.get(),0,0);
@ -248,7 +247,7 @@ void textbox::scroll(unsigned int pos)
set_dirty(true);
}
surface textbox::add_text_line(const wide_string& text)
surface textbox::add_text_line(const wide_string& text, const SDL_Color& color)
{
line_height_ = font::get_max_height(font_size);
@ -308,19 +307,19 @@ surface textbox::add_text_line(const wide_string& text)
}
const std::string s = utils::wstring_to_string(wrapped_text);
const surface res(font::get_rendered_text(s, font_size, font::NORMAL_COLOUR));
const surface res(font::get_rendered_text(s, font_size, color));
return res;
}
void textbox::update_text_cache(bool changed)
void textbox::update_text_cache(bool changed, const SDL_Color& color)
{
if(changed) {
char_x_.clear();
char_y_.clear();
text_image_.assign(add_text_line(text_));
text_image_.assign(add_text_line(text_, color));
}
int cursor_x = char_x_[cursor_];

View file

@ -17,6 +17,7 @@
#include "../serialization/string_utils.hpp"
#include "../sdl_utils.hpp"
#include "font.hpp"
#include "scrollarea.hpp"
@ -31,8 +32,8 @@ public:
virtual ~textbox();
const std::string text() const;
void set_text(const std::string& text);
void append_text(const std::string& text,bool auto_scroll = false);
void set_text(const std::string& text, const SDL_Color& color =font::NORMAL_COLOUR);
void append_text(const std::string& text,bool auto_scroll = false, const SDL_Color& color =font::NORMAL_COLOUR);
void clear();
void process();
@ -91,8 +92,8 @@ private:
void handle_event(const SDL_Event& event);
void draw_cursor(int pos, CVideo &video) const;
void update_text_cache(bool reset = false);
surface add_text_line(const wide_string& text);
void update_text_cache(bool reset = false, const SDL_Color& color =font::NORMAL_COLOUR);
surface add_text_line(const wide_string& text, const SDL_Color& color =font::NORMAL_COLOUR);
bool is_selection();
void erase_selection();