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 "../date_func.h"
00018 #include "../gfx_func.h"
00019 #include "../strings_func.h"
00020 #include "../blitter/factory.hpp"
00021 #include "../console_func.h"
00022 #include "../video/video_driver.hpp"
00023 #include "../table/sprites.h"
00024 #include "../querystring_gui.h"
00025 #include "../town.h"
00026 #include "../window_func.h"
00027 #include "network_internal.h"
00028 #include "network_client.h"
00029
00030 #include "table/strings.h"
00031
00032
00033
00034 assert_compile((int)DRAW_STRING_BUFFER >= (int)NETWORK_CHAT_LENGTH + NETWORK_NAME_LENGTH + 40);
00035
00036 enum {
00037 NETWORK_CHAT_LINE_SPACING = 3,
00038 };
00039
00040 struct ChatMessage {
00041 char message[DRAW_STRING_BUFFER];
00042 TextColour colour;
00043 Date end_date;
00044 };
00045
00046
00047 static ChatMessage *_chatmsg_list = NULL;
00048 static bool _chatmessage_dirty = false;
00049 static bool _chatmessage_visible = false;
00050 static bool _chat_tab_completion_active;
00051 static uint MAX_CHAT_MESSAGES = 0;
00052
00053
00054
00055 static PointDimension _chatmsg_box;
00056 static uint8 *_chatmessage_backup = NULL;
00057
00058 static inline uint GetChatMessageCount()
00059 {
00060 uint i = 0;
00061 for (; i < MAX_CHAT_MESSAGES; i++) {
00062 if (_chatmsg_list[i].message[0] == '\0') break;
00063 }
00064
00065 return i;
00066 }
00067
00074 void CDECL NetworkAddChatMessage(TextColour colour, uint8 duration, const char *message, ...)
00075 {
00076 char buf[DRAW_STRING_BUFFER];
00077 const char *bufp;
00078 va_list va;
00079 uint msg_count;
00080 uint16 lines;
00081
00082 va_start(va, message);
00083 vsnprintf(buf, lengthof(buf), message, va);
00084 va_end(va);
00085
00086 Utf8TrimString(buf, DRAW_STRING_BUFFER);
00087
00088
00089 lines = GB(FormatStringLinebreaks(buf, lastof(buf), _chatmsg_box.width - 8), 0, 16) + 1;
00090 if (lines >= MAX_CHAT_MESSAGES) return;
00091
00092 msg_count = GetChatMessageCount();
00093
00094 if (lines > MAX_CHAT_MESSAGES - msg_count) {
00095 int i = lines - (MAX_CHAT_MESSAGES - msg_count);
00096 memmove(&_chatmsg_list[0], &_chatmsg_list[i], sizeof(_chatmsg_list[0]) * (msg_count - i));
00097 msg_count = MAX_CHAT_MESSAGES - lines;
00098 }
00099
00100 for (bufp = buf; lines != 0; lines--) {
00101 ChatMessage *cmsg = &_chatmsg_list[msg_count++];
00102 strecpy(cmsg->message, bufp, lastof(cmsg->message));
00103
00104
00105
00106 cmsg->colour = (bufp == buf && (colour & IS_PALETTE_COLOUR)) ? colour : TC_WHITE;
00107 cmsg->end_date = _date + duration;
00108
00109 bufp += strlen(bufp) + 1;
00110 }
00111
00112 _chatmessage_dirty = true;
00113 }
00114
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.y = 3 * FONT_HEIGHT_NORMAL;
00122 _chatmsg_box.width = _settings_client.gui.network_chat_box_width;
00123 _chatmsg_box.height = _settings_client.gui.network_chat_box_height * (FONT_HEIGHT_NORMAL + NETWORK_CHAT_LINE_SPACING) + 2;
00124 _chatmessage_backup = ReallocT(_chatmessage_backup, _chatmsg_box.width * _chatmsg_box.height * BlitterFactoryBase::GetCurrentBlitter()->GetBytesPerPixel());
00125
00126 for (uint i = 0; i < MAX_CHAT_MESSAGES; i++) {
00127 _chatmsg_list[i].message[0] = '\0';
00128 }
00129 }
00130
00132 void NetworkUndrawChatMessage()
00133 {
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144 if (_cursor.visible &&
00145 _cursor.draw_pos.x + _cursor.draw_size.x >= _chatmsg_box.x &&
00146 _cursor.draw_pos.x <= _chatmsg_box.x + _chatmsg_box.width &&
00147 _cursor.draw_pos.y + _cursor.draw_size.y >= _screen.height - _chatmsg_box.y - _chatmsg_box.height &&
00148 _cursor.draw_pos.y <= _screen.height - _chatmsg_box.y) {
00149 UndrawMouseCursor();
00150 }
00151
00152 if (_chatmessage_visible) {
00153 Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
00154 int x = _chatmsg_box.x;
00155 int y = _screen.height - _chatmsg_box.y - _chatmsg_box.height;
00156 int width = _chatmsg_box.width;
00157 int height = _chatmsg_box.height;
00158 if (y < 0) {
00159 height = max(height + y, min(_chatmsg_box.height, _screen.height));
00160 y = 0;
00161 }
00162 if (x + width >= _screen.width) {
00163 width = _screen.width - x;
00164 }
00165 if (width <= 0 || height <= 0) return;
00166
00167 _chatmessage_visible = false;
00168
00169 blitter->CopyFromBuffer(blitter->MoveTo(_screen.dst_ptr, x, y), _chatmessage_backup, width, height);
00170
00171 _video_driver->MakeDirty(x, y, width, height);
00172
00173 _chatmessage_dirty = true;
00174 }
00175 }
00176
00178 void NetworkChatMessageDailyLoop()
00179 {
00180 for (uint i = 0; i < MAX_CHAT_MESSAGES; i++) {
00181 ChatMessage *cmsg = &_chatmsg_list[i];
00182 if (cmsg->message[0] == '\0') continue;
00183
00184
00185 if (cmsg->end_date < _date) {
00186
00187 if (i != MAX_CHAT_MESSAGES - 1) memmove(cmsg, cmsg + 1, sizeof(*cmsg) * (MAX_CHAT_MESSAGES - i - 1));
00188
00189
00190 _chatmsg_list[MAX_CHAT_MESSAGES - 1].message[0] = '\0';
00191 _chatmessage_dirty = true;
00192
00193
00194 i--;
00195 }
00196 }
00197 }
00198
00200 void NetworkDrawChatMessage()
00201 {
00202 Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
00203 if (!_chatmessage_dirty) return;
00204
00205
00206 NetworkUndrawChatMessage();
00207
00208 if (_iconsole_mode == ICONSOLE_FULL) return;
00209
00210
00211 uint count = GetChatMessageCount();
00212 if (count == 0) return;
00213
00214 int x = _chatmsg_box.x;
00215 int y = _screen.height - _chatmsg_box.y - _chatmsg_box.height;
00216 int width = _chatmsg_box.width;
00217 int height = _chatmsg_box.height;
00218 if (y < 0) {
00219 height = max(height + y, min(_chatmsg_box.height, _screen.height));
00220 y = 0;
00221 }
00222 if (x + width >= _screen.width) {
00223 width = _screen.width - x;
00224 }
00225 if (width <= 0 || height <= 0) return;
00226
00227 assert(blitter->BufferSize(width, height) <= (int)(_chatmsg_box.width * _chatmsg_box.height * blitter->GetBytesPerPixel()));
00228
00229
00230 blitter->CopyToBuffer(blitter->MoveTo(_screen.dst_ptr, x, y), _chatmessage_backup, width, height);
00231
00232 _cur_dpi = &_screen;
00233
00234
00235 GfxFillRect(
00236 _chatmsg_box.x,
00237 _screen.height - _chatmsg_box.y - count * (FONT_HEIGHT_NORMAL + NETWORK_CHAT_LINE_SPACING) - 2,
00238 _chatmsg_box.x + _chatmsg_box.width - 1,
00239 _screen.height - _chatmsg_box.y - 2,
00240 PALETTE_TO_TRANSPARENT, FILLRECT_RECOLOUR
00241 );
00242
00243
00244 for (uint y = FONT_HEIGHT_NORMAL + NETWORK_CHAT_LINE_SPACING; count-- != 0; y += (FONT_HEIGHT_NORMAL + NETWORK_CHAT_LINE_SPACING)) {
00245 DrawString(_chatmsg_box.x + 3, _chatmsg_box.x + _chatmsg_box.width - 1, _screen.height - _chatmsg_box.y - y + 1, _chatmsg_list[count].message, _chatmsg_list[count].colour);
00246 }
00247
00248
00249 _video_driver->MakeDirty(x, y, width, height);
00250
00251 _chatmessage_visible = true;
00252 _chatmessage_dirty = false;
00253 }
00254
00255
00256 static void SendChat(const char *buf, DestType type, int dest)
00257 {
00258 if (StrEmpty(buf)) return;
00259 if (!_network_server) {
00260 SEND_COMMAND(PACKET_CLIENT_CHAT)((NetworkAction)(NETWORK_ACTION_CHAT + type), type, dest, buf, 0);
00261 } else {
00262 NetworkServerSendChat((NetworkAction)(NETWORK_ACTION_CHAT + type), type, dest, buf, CLIENT_ID_SERVER);
00263 }
00264 }
00265
00267 enum NetWorkChatWidgets {
00268 NWCW_CLOSE,
00269 NWCW_BACKGROUND,
00270 NWCW_DESTINATION,
00271 NWCW_TEXTBOX,
00272 NWCW_SENDBUTTON,
00273 };
00274
00275 struct NetworkChatWindow : public QueryStringBaseWindow {
00276 DestType dtype;
00277 StringID dest_string;
00278 int dest;
00279
00280 NetworkChatWindow(const WindowDesc *desc, DestType type, int dest) : QueryStringBaseWindow(NETWORK_CHAT_LENGTH)
00281 {
00282 this->dtype = type;
00283 this->dest = dest;
00284 this->afilter = CS_ALPHANUMERAL;
00285 InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, 0);
00286
00287 static const StringID chat_captions[] = {
00288 STR_NETWORK_CHAT_ALL_CAPTION,
00289 STR_NETWORK_CHAT_COMPANY_CAPTION,
00290 STR_NETWORK_CHAT_CLIENT_CAPTION
00291 };
00292 assert((uint)this->dtype < lengthof(chat_captions));
00293 this->dest_string = chat_captions[this->dtype];
00294
00295 this->InitNested(desc, type);
00296
00297 this->SetFocusedWidget(NWCW_TEXTBOX);
00298 InvalidateWindowData(WC_NEWS_WINDOW, 0, this->height);
00299 _chat_tab_completion_active = false;
00300 }
00301
00302 ~NetworkChatWindow()
00303 {
00304 InvalidateWindowData(WC_NEWS_WINDOW, 0, 0);
00305 }
00306
00313 const char *ChatTabCompletionNextItem(uint *item)
00314 {
00315 static char chat_tab_temp_buffer[64];
00316
00317
00318 if (*item < MAX_CLIENT_SLOTS) {
00319
00320 NetworkClientInfo *ci;
00321 FOR_ALL_CLIENT_INFOS_FROM(ci, *item) {
00322 *item = ci->index;
00323 return ci->client_name;
00324 }
00325 *item = MAX_CLIENT_SLOTS;
00326 }
00327
00328
00329
00330
00331 if (*item < (uint)MAX_CLIENT_SLOTS + Town::GetPoolSize()) {
00332 const Town *t;
00333
00334 FOR_ALL_TOWNS_FROM(t, *item - MAX_CLIENT_SLOTS) {
00335
00336 SetDParam(0, t->index);
00337 GetString(chat_tab_temp_buffer, STR_TOWN_NAME, lastof(chat_tab_temp_buffer));
00338 return &chat_tab_temp_buffer[0];
00339 }
00340 }
00341
00342 return NULL;
00343 }
00344
00350 static char *ChatTabCompletionFindText(char *buf)
00351 {
00352 char *p = strrchr(buf, ' ');
00353 if (p == NULL) return buf;
00354
00355 *p = '\0';
00356 return p + 1;
00357 }
00358
00362 void ChatTabCompletion()
00363 {
00364 static char _chat_tab_completion_buf[NETWORK_CHAT_LENGTH];
00365 assert(this->edit_str_size == lengthof(_chat_tab_completion_buf));
00366
00367 Textbuf *tb = &this->text;
00368 size_t len, tb_len;
00369 uint item;
00370 char *tb_buf, *pre_buf;
00371 const char *cur_name;
00372 bool second_scan = false;
00373
00374 item = 0;
00375
00376
00377 pre_buf = (_chat_tab_completion_active) ? strdup(_chat_tab_completion_buf) : strdup(tb->buf);
00378
00379 tb_buf = ChatTabCompletionFindText(pre_buf);
00380 tb_len = strlen(tb_buf);
00381
00382 while ((cur_name = ChatTabCompletionNextItem(&item)) != NULL) {
00383 item++;
00384
00385 if (_chat_tab_completion_active) {
00386
00387
00388 if (!second_scan) {
00389 size_t offset;
00390 size_t length;
00391
00392
00393 if (tb_buf == pre_buf) {
00394 offset = 0;
00395 length = (tb->size - 1) - 2;
00396 } else {
00397
00398 offset = strlen(pre_buf) + 1;
00399 length = (tb->size - 1) - offset;
00400 }
00401
00402
00403 if (strlen(cur_name) == length && strncmp(cur_name, tb->buf + offset, length) == 0) second_scan = true;
00404
00405 continue;
00406 }
00407
00408
00409 }
00410
00411 len = strlen(cur_name);
00412 if (tb_len < len && strncasecmp(cur_name, tb_buf, tb_len) == 0) {
00413
00414 if (!second_scan) snprintf(_chat_tab_completion_buf, lengthof(_chat_tab_completion_buf), "%s", tb->buf);
00415 _chat_tab_completion_active = true;
00416
00417
00418 if (pre_buf == tb_buf) {
00419 snprintf(tb->buf, this->edit_str_size, "%s: ", cur_name);
00420 } else {
00421 snprintf(tb->buf, this->edit_str_size, "%s %s", pre_buf, cur_name);
00422 }
00423
00424
00425 UpdateTextBufferSize(&this->text);
00426
00427 this->SetDirty();
00428 free(pre_buf);
00429 return;
00430 }
00431 }
00432
00433 if (second_scan) {
00434
00435 strcpy(tb->buf, _chat_tab_completion_buf);
00436 _chat_tab_completion_active = false;
00437
00438
00439 UpdateTextBufferSize(&this->text);
00440
00441 this->SetDirty();
00442 }
00443 free(pre_buf);
00444 }
00445
00446 virtual void OnPaint()
00447 {
00448 this->DrawWidgets();
00449 this->DrawEditBox(NWCW_TEXTBOX);
00450 }
00451
00452 virtual Point OnInitialPosition(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
00453 {
00454 Point pt = { (_screen.width - max(sm_width, desc->default_width)) / 2, _screen.height - sm_height - FindWindowById(WC_STATUS_BAR, 0)->height };
00455 return pt;
00456 }
00457
00458 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00459 {
00460 if (widget != NWCW_DESTINATION) return;
00461
00462 if (this->dtype == DESTTYPE_CLIENT) {
00463 SetDParamStr(0, NetworkFindClientInfoFromClientID((ClientID)this->dest)->client_name);
00464 }
00465 Dimension d = GetStringBoundingBox(this->dest_string);
00466 d.width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
00467 d.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
00468 *size = maxdim(*size, d);
00469 }
00470
00471 virtual void DrawWidget(const Rect &r, int widget) const
00472 {
00473 if (widget != NWCW_DESTINATION) return;
00474
00475 if (this->dtype == DESTTYPE_CLIENT) {
00476 SetDParamStr(0, NetworkFindClientInfoFromClientID((ClientID)this->dest)->client_name);
00477 }
00478 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, this->dest_string, TC_BLACK, SA_RIGHT);
00479 }
00480
00481 virtual void OnClick(Point pt, int widget)
00482 {
00483 switch (widget) {
00484
00485 case NWCW_SENDBUTTON: SendChat(this->text.buf, this->dtype, this->dest);
00486
00487 case NWCW_CLOSE: delete this; break;
00488 }
00489 }
00490
00491 virtual void OnMouseLoop()
00492 {
00493 this->HandleEditBox(NWCW_TEXTBOX);
00494 }
00495
00496 virtual EventState OnKeyPress(uint16 key, uint16 keycode)
00497 {
00498 EventState state = ES_NOT_HANDLED;
00499 if (keycode == WKC_TAB) {
00500 ChatTabCompletion();
00501 state = ES_HANDLED;
00502 } else {
00503 _chat_tab_completion_active = false;
00504 switch (this->HandleEditBoxKey(NWCW_TEXTBOX, key, keycode, state)) {
00505 default: NOT_REACHED();
00506 case HEBR_EDITING: {
00507 Window *osk = FindWindowById(WC_OSK, 0);
00508 if (osk != NULL && osk->parent == this) osk->InvalidateData();
00509 } break;
00510 case HEBR_CONFIRM:
00511 SendChat(this->text.buf, this->dtype, this->dest);
00512
00513 case HEBR_CANCEL: delete this; break;
00514 case HEBR_NOT_FOCUSED: break;
00515 }
00516 }
00517 return state;
00518 }
00519
00520 virtual void OnOpenOSKWindow(int wid)
00521 {
00522 ShowOnScreenKeyboard(this, wid, NWCW_CLOSE, NWCW_SENDBUTTON);
00523 }
00524
00525 virtual void OnInvalidateData(int data)
00526 {
00527 if (data == this->dest) delete this;
00528 }
00529 };
00530
00531 static const NWidgetPart _nested_chat_window_widgets[] = {
00532 NWidget(NWID_HORIZONTAL),
00533 NWidget(WWT_CLOSEBOX, COLOUR_GREY, NWCW_CLOSE),
00534 NWidget(WWT_PANEL, COLOUR_GREY, NWCW_BACKGROUND),
00535 NWidget(NWID_HORIZONTAL),
00536 NWidget(WWT_TEXT, COLOUR_GREY, NWCW_DESTINATION), SetMinimalSize(62, 12), SetPadding(1, 0, 1, 0), SetDataTip(STR_NULL, STR_NULL),
00537 NWidget(WWT_EDITBOX, COLOUR_GREY, NWCW_TEXTBOX), SetMinimalSize(100, 12), SetPadding(1, 0, 1, 0), SetResize(1, 0),
00538 SetDataTip(STR_NETWORK_CHAT_OSKTITLE, STR_NULL),
00539 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, NWCW_SENDBUTTON), SetMinimalSize(62, 12), SetPadding(1, 0, 1, 0), SetDataTip(STR_NETWORK_CHAT_SEND, STR_NULL),
00540 EndContainer(),
00541 EndContainer(),
00542 EndContainer(),
00543 };
00544
00545 static const WindowDesc _chat_window_desc(
00546 WDP_MANUAL, 640, 14,
00547 WC_SEND_NETWORK_MSG, WC_NONE,
00548 0,
00549 _nested_chat_window_widgets, lengthof(_nested_chat_window_widgets)
00550 );
00551
00552 void ShowNetworkChatQueryWindow(DestType type, int dest)
00553 {
00554 DeleteWindowByClass(WC_SEND_NETWORK_MSG);
00555 new NetworkChatWindow(&_chat_window_desc, type, dest);
00556 }
00557
00558 #endif