00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include <stdarg.h>
00013
00014 #ifdef ENABLE_NETWORK
00015
00016 #include "../stdafx.h"
00017 #include "../strings_func.h"
00018 #include "../blitter/factory.hpp"
00019 #include "../console_func.h"
00020 #include "../video/video_driver.hpp"
00021 #include "../querystring_gui.h"
00022 #include "../town.h"
00023 #include "../window_func.h"
00024 #include "../core/geometry_func.hpp"
00025 #include "network.h"
00026 #include "network_client.h"
00027 #include "network_base.h"
00028
00029 #include "../widgets/network_chat_widget.h"
00030
00031 #include "table/strings.h"
00032
00035 assert_compile((int)DRAW_STRING_BUFFER >= (int)NETWORK_CHAT_LENGTH + NETWORK_NAME_LENGTH + 40);
00036
00038 static const uint NETWORK_CHAT_LINE_SPACING = 3;
00039
00041 struct ChatMessage {
00042 char message[DRAW_STRING_BUFFER];
00043 TextColour colour;
00044 uint32 remove_time;
00045 };
00046
00047
00048 static ChatMessage *_chatmsg_list = NULL;
00049 static bool _chatmessage_dirty = false;
00050 static bool _chatmessage_visible = false;
00051 static bool _chat_tab_completion_active;
00052 static uint MAX_CHAT_MESSAGES = 0;
00053
00058 static PointDimension _chatmsg_box;
00059 static uint8 *_chatmessage_backup = NULL;
00060
00065 static inline uint GetChatMessageCount()
00066 {
00067 uint i = 0;
00068 for (; i < MAX_CHAT_MESSAGES; i++) {
00069 if (_chatmsg_list[i].message[0] == '\0') break;
00070 }
00071
00072 return i;
00073 }
00074
00081 void CDECL NetworkAddChatMessage(TextColour colour, uint duration, const char *message, ...)
00082 {
00083 char buf[DRAW_STRING_BUFFER];
00084 va_list va;
00085
00086 va_start(va, message);
00087 vsnprintf(buf, lengthof(buf), message, va);
00088 va_end(va);
00089
00090 Utf8TrimString(buf, DRAW_STRING_BUFFER);
00091
00092 uint msg_count = GetChatMessageCount();
00093 if (MAX_CHAT_MESSAGES == msg_count) {
00094 memmove(&_chatmsg_list[0], &_chatmsg_list[1], sizeof(_chatmsg_list[0]) * (msg_count - 1));
00095 msg_count = MAX_CHAT_MESSAGES - 1;
00096 }
00097
00098 ChatMessage *cmsg = &_chatmsg_list[msg_count++];
00099 strecpy(cmsg->message, buf, lastof(cmsg->message));
00100 cmsg->colour = (colour & TC_IS_PALETTE_COLOUR) ? colour : TC_WHITE;
00101 cmsg->remove_time = _realtime_tick + duration * 1000;
00102
00103 _chatmessage_dirty = true;
00104 }
00105
00107 void NetworkReInitChatBoxSize()
00108 {
00109 _chatmsg_box.y = 3 * FONT_HEIGHT_NORMAL;
00110 _chatmsg_box.height = MAX_CHAT_MESSAGES * (FONT_HEIGHT_NORMAL + NETWORK_CHAT_LINE_SPACING) + 2;
00111 _chatmessage_backup = ReallocT(_chatmessage_backup, _chatmsg_box.width * _chatmsg_box.height * BlitterFactoryBase::GetCurrentBlitter()->GetBytesPerPixel());
00112 }
00113
00115 void NetworkInitChatMessage()
00116 {
00117 MAX_CHAT_MESSAGES = _settings_client.gui.network_chat_box_height;
00118
00119 _chatmsg_list = ReallocT(_chatmsg_list, _settings_client.gui.network_chat_box_height);
00120 _chatmsg_box.x = 10;
00121 _chatmsg_box.width = _settings_client.gui.network_chat_box_width;
00122 NetworkReInitChatBoxSize();
00123 _chatmessage_visible = false;
00124
00125 for (uint i = 0; i < MAX_CHAT_MESSAGES; i++) {
00126 _chatmsg_list[i].message[0] = '\0';
00127 }
00128 }
00129
00131 void NetworkUndrawChatMessage()
00132 {
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143 if (_cursor.visible &&
00144 _cursor.draw_pos.x + _cursor.draw_size.x >= _chatmsg_box.x &&
00145 _cursor.draw_pos.x <= _chatmsg_box.x + _chatmsg_box.width &&
00146 _cursor.draw_pos.y + _cursor.draw_size.y >= _screen.height - _chatmsg_box.y - _chatmsg_box.height &&
00147 _cursor.draw_pos.y <= _screen.height - _chatmsg_box.y) {
00148 UndrawMouseCursor();
00149 }
00150
00151 if (_chatmessage_visible) {
00152 Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
00153 int x = _chatmsg_box.x;
00154 int y = _screen.height - _chatmsg_box.y - _chatmsg_box.height;
00155 int width = _chatmsg_box.width;
00156 int height = _chatmsg_box.height;
00157 if (y < 0) {
00158 height = max(height + y, min(_chatmsg_box.height, _screen.height));
00159 y = 0;
00160 }
00161 if (x + width >= _screen.width) {
00162 width = _screen.width - x;
00163 }
00164 if (width <= 0 || height <= 0) return;
00165
00166 _chatmessage_visible = false;
00167
00168 blitter->CopyFromBuffer(blitter->MoveTo(_screen.dst_ptr, x, y), _chatmessage_backup, width, height);
00169
00170 _video_driver->MakeDirty(x, y, width, height);
00171
00172 _chatmessage_dirty = true;
00173 }
00174 }
00175
00177 void NetworkChatMessageLoop()
00178 {
00179 for (uint i = 0; i < MAX_CHAT_MESSAGES; i++) {
00180 ChatMessage *cmsg = &_chatmsg_list[i];
00181 if (cmsg->message[0] == '\0') continue;
00182
00183
00184 if (cmsg->remove_time < _realtime_tick) {
00185
00186 if (i != MAX_CHAT_MESSAGES - 1) memmove(cmsg, cmsg + 1, sizeof(*cmsg) * (MAX_CHAT_MESSAGES - i - 1));
00187
00188
00189 _chatmsg_list[MAX_CHAT_MESSAGES - 1].message[0] = '\0';
00190 _chatmessage_dirty = true;
00191
00192
00193 i--;
00194 }
00195 }
00196 }
00197
00199 void NetworkDrawChatMessage()
00200 {
00201 Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
00202 if (!_chatmessage_dirty) return;
00203
00204
00205 NetworkUndrawChatMessage();
00206
00207 if (_iconsole_mode == ICONSOLE_FULL) return;
00208
00209
00210 uint count = GetChatMessageCount();
00211 if (count == 0) return;
00212
00213 int x = _chatmsg_box.x;
00214 int y = _screen.height - _chatmsg_box.y - _chatmsg_box.height;
00215 int width = _chatmsg_box.width;
00216 int height = _chatmsg_box.height;
00217 if (y < 0) {
00218 height = max(height + y, min(_chatmsg_box.height, _screen.height));
00219 y = 0;
00220 }
00221 if (x + width >= _screen.width) {
00222 width = _screen.width - x;
00223 }
00224 if (width <= 0 || height <= 0) return;
00225
00226 assert(blitter->BufferSize(width, height) <= (int)(_chatmsg_box.width * _chatmsg_box.height * blitter->GetBytesPerPixel()));
00227
00228
00229 blitter->CopyToBuffer(blitter->MoveTo(_screen.dst_ptr, x, y), _chatmessage_backup, width, height);
00230
00231 _cur_dpi = &_screen;
00232
00233 int string_height = 0;
00234 for (uint i = 0; i < count; i++) {
00235 SetDParamStr(0, _chatmsg_list[i].message);
00236 string_height += GetStringLineCount(STR_JUST_RAW_STRING, width - 1) * FONT_HEIGHT_NORMAL + NETWORK_CHAT_LINE_SPACING;
00237 }
00238
00239 string_height = min(string_height, MAX_CHAT_MESSAGES * (FONT_HEIGHT_NORMAL + NETWORK_CHAT_LINE_SPACING));
00240
00241 int top = _screen.height - _chatmsg_box.y - string_height - 2;
00242 int bottom = _screen.height - _chatmsg_box.y - 2;
00243
00244 GfxFillRect(_chatmsg_box.x, top - 2, _chatmsg_box.x + _chatmsg_box.width - 1, bottom,
00245 PALETTE_TO_TRANSPARENT, FILLRECT_RECOLOUR
00246 );
00247
00248
00249 int ypos = bottom - 2;
00250
00251 for (int i = count - 1; i >= 0; i--) {
00252 ypos = DrawStringMultiLine(_chatmsg_box.x + 3, _chatmsg_box.x + _chatmsg_box.width - 1, top, ypos, _chatmsg_list[i].message, _chatmsg_list[i].colour, SA_LEFT | SA_BOTTOM | SA_FORCE) - NETWORK_CHAT_LINE_SPACING;
00253 if (ypos < top) break;
00254 }
00255
00256
00257 _video_driver->MakeDirty(x, y, width, height);
00258
00259 _chatmessage_visible = true;
00260 _chatmessage_dirty = false;
00261 }
00262
00269 static void SendChat(const char *buf, DestType type, int dest)
00270 {
00271 if (StrEmpty(buf)) return;
00272 if (!_network_server) {
00273 MyClient::SendChat((NetworkAction)(NETWORK_ACTION_CHAT + type), type, dest, buf, 0);
00274 } else {
00275 NetworkServerSendChat((NetworkAction)(NETWORK_ACTION_CHAT + type), type, dest, buf, CLIENT_ID_SERVER);
00276 }
00277 }
00278
00280 struct NetworkChatWindow : public Window {
00281 DestType dtype;
00282 StringID dest_string;
00283 int dest;
00284 QueryString message_editbox;
00285
00292 NetworkChatWindow(const WindowDesc *desc, DestType type, int dest) : message_editbox(NETWORK_CHAT_LENGTH)
00293 {
00294 this->dtype = type;
00295 this->dest = dest;
00296 this->querystrings[WID_NC_TEXTBOX] = &this->message_editbox;
00297 this->message_editbox.cancel_button = WID_NC_CLOSE;
00298 this->message_editbox.ok_button = WID_NC_SENDBUTTON;
00299
00300 static const StringID chat_captions[] = {
00301 STR_NETWORK_CHAT_ALL_CAPTION,
00302 STR_NETWORK_CHAT_COMPANY_CAPTION,
00303 STR_NETWORK_CHAT_CLIENT_CAPTION
00304 };
00305 assert((uint)this->dtype < lengthof(chat_captions));
00306 this->dest_string = chat_captions[this->dtype];
00307
00308 this->InitNested(desc, type);
00309
00310 this->SetFocusedWidget(WID_NC_TEXTBOX);
00311 InvalidateWindowData(WC_NEWS_WINDOW, 0, this->height);
00312 _chat_tab_completion_active = false;
00313
00314 PositionNetworkChatWindow(this);
00315 }
00316
00317 ~NetworkChatWindow()
00318 {
00319 InvalidateWindowData(WC_NEWS_WINDOW, 0, 0);
00320 }
00321
00328 const char *ChatTabCompletionNextItem(uint *item)
00329 {
00330 static char chat_tab_temp_buffer[64];
00331
00332
00333 if (*item < MAX_CLIENT_SLOTS) {
00334
00335 NetworkClientInfo *ci;
00336 FOR_ALL_CLIENT_INFOS_FROM(ci, *item) {
00337 *item = ci->index;
00338 return ci->client_name;
00339 }
00340 *item = MAX_CLIENT_SLOTS;
00341 }
00342
00343
00344
00345
00346 if (*item < (uint)MAX_CLIENT_SLOTS + Town::GetPoolSize()) {
00347 const Town *t;
00348
00349 FOR_ALL_TOWNS_FROM(t, *item - MAX_CLIENT_SLOTS) {
00350
00351 SetDParam(0, t->index);
00352 GetString(chat_tab_temp_buffer, STR_TOWN_NAME, lastof(chat_tab_temp_buffer));
00353 return &chat_tab_temp_buffer[0];
00354 }
00355 }
00356
00357 return NULL;
00358 }
00359
00365 static char *ChatTabCompletionFindText(char *buf)
00366 {
00367 char *p = strrchr(buf, ' ');
00368 if (p == NULL) return buf;
00369
00370 *p = '\0';
00371 return p + 1;
00372 }
00373
00377 void ChatTabCompletion()
00378 {
00379 static char _chat_tab_completion_buf[NETWORK_CHAT_LENGTH];
00380 assert(this->message_editbox.text.max_bytes == lengthof(_chat_tab_completion_buf));
00381
00382 Textbuf *tb = &this->message_editbox.text;
00383 size_t len, tb_len;
00384 uint item;
00385 char *tb_buf, *pre_buf;
00386 const char *cur_name;
00387 bool second_scan = false;
00388
00389 item = 0;
00390
00391
00392 pre_buf = (_chat_tab_completion_active) ? strdup(_chat_tab_completion_buf) : strdup(tb->buf);
00393
00394 tb_buf = ChatTabCompletionFindText(pre_buf);
00395 tb_len = strlen(tb_buf);
00396
00397 while ((cur_name = ChatTabCompletionNextItem(&item)) != NULL) {
00398 item++;
00399
00400 if (_chat_tab_completion_active) {
00401
00402
00403 if (!second_scan) {
00404 size_t offset;
00405 size_t length;
00406
00407
00408 if (tb_buf == pre_buf) {
00409 offset = 0;
00410 length = (tb->bytes - 1) - 2;
00411 } else {
00412
00413 offset = strlen(pre_buf) + 1;
00414 length = (tb->bytes - 1) - offset;
00415 }
00416
00417
00418 if (strlen(cur_name) == length && strncmp(cur_name, tb->buf + offset, length) == 0) second_scan = true;
00419
00420 continue;
00421 }
00422
00423
00424 }
00425
00426 len = strlen(cur_name);
00427 if (tb_len < len && strncasecmp(cur_name, tb_buf, tb_len) == 0) {
00428
00429 if (!second_scan) snprintf(_chat_tab_completion_buf, lengthof(_chat_tab_completion_buf), "%s", tb->buf);
00430 _chat_tab_completion_active = true;
00431
00432
00433 if (pre_buf == tb_buf) {
00434 this->message_editbox.text.Print("%s: ", cur_name);
00435 } else {
00436 this->message_editbox.text.Print("%s %s", pre_buf, cur_name);
00437 }
00438
00439 this->SetDirty();
00440 free(pre_buf);
00441 return;
00442 }
00443 }
00444
00445 if (second_scan) {
00446
00447 this->message_editbox.text.Assign(_chat_tab_completion_buf);
00448 _chat_tab_completion_active = false;
00449
00450 this->SetDirty();
00451 }
00452 free(pre_buf);
00453 }
00454
00455 virtual Point OnInitialPosition(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
00456 {
00457 Point pt = { 0, _screen.height - sm_height - FindWindowById(WC_STATUS_BAR, 0)->height };
00458 return pt;
00459 }
00460
00461 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00462 {
00463 if (widget != WID_NC_DESTINATION) return;
00464
00465 if (this->dtype == DESTTYPE_CLIENT) {
00466 SetDParamStr(0, NetworkClientInfo::GetByClientID((ClientID)this->dest)->client_name);
00467 }
00468 Dimension d = GetStringBoundingBox(this->dest_string);
00469 d.width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
00470 d.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
00471 *size = maxdim(*size, d);
00472 }
00473
00474 virtual void DrawWidget(const Rect &r, int widget) const
00475 {
00476 if (widget != WID_NC_DESTINATION) return;
00477
00478 if (this->dtype == DESTTYPE_CLIENT) {
00479 SetDParamStr(0, NetworkClientInfo::GetByClientID((ClientID)this->dest)->client_name);
00480 }
00481 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, this->dest_string, TC_BLACK, SA_RIGHT);
00482 }
00483
00484 virtual void OnClick(Point pt, int widget, int click_count)
00485 {
00486 switch (widget) {
00487
00488 case WID_NC_SENDBUTTON: SendChat(this->message_editbox.text.buf, this->dtype, this->dest);
00489
00490 case WID_NC_CLOSE: delete this; break;
00491 }
00492 }
00493
00494 virtual EventState OnKeyPress(WChar key, uint16 keycode)
00495 {
00496 EventState state = ES_NOT_HANDLED;
00497 if (keycode == WKC_TAB) {
00498 ChatTabCompletion();
00499 state = ES_HANDLED;
00500 }
00501 return state;
00502 }
00503
00504 virtual void OnEditboxChanged(int wid)
00505 {
00506 _chat_tab_completion_active = false;
00507 }
00508
00514 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00515 {
00516 if (data == this->dest) delete this;
00517 }
00518 };
00519
00521 static const NWidgetPart _nested_chat_window_widgets[] = {
00522 NWidget(NWID_HORIZONTAL),
00523 NWidget(WWT_CLOSEBOX, COLOUR_GREY, WID_NC_CLOSE),
00524 NWidget(WWT_PANEL, COLOUR_GREY, WID_NC_BACKGROUND),
00525 NWidget(NWID_HORIZONTAL),
00526 NWidget(WWT_TEXT, COLOUR_GREY, WID_NC_DESTINATION), SetMinimalSize(62, 12), SetPadding(1, 0, 1, 0), SetDataTip(STR_NULL, STR_NULL),
00527 NWidget(WWT_EDITBOX, COLOUR_GREY, WID_NC_TEXTBOX), SetMinimalSize(100, 12), SetPadding(1, 0, 1, 0), SetResize(1, 0),
00528 SetDataTip(STR_NETWORK_CHAT_OSKTITLE, STR_NULL),
00529 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_NC_SENDBUTTON), SetMinimalSize(62, 12), SetPadding(1, 0, 1, 0), SetDataTip(STR_NETWORK_CHAT_SEND, STR_NULL),
00530 EndContainer(),
00531 EndContainer(),
00532 EndContainer(),
00533 };
00534
00536 static const WindowDesc _chat_window_desc(
00537 WDP_MANUAL, 640, 14,
00538 WC_SEND_NETWORK_MSG, WC_NONE,
00539 0,
00540 _nested_chat_window_widgets, lengthof(_nested_chat_window_widgets)
00541 );
00542
00543
00549 void ShowNetworkChatQueryWindow(DestType type, int dest)
00550 {
00551 DeleteWindowByClass(WC_SEND_NETWORK_MSG);
00552 new NetworkChatWindow(&_chat_window_desc, type, dest);
00553 }
00554
00555 #endif