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