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