OpenTTD Source: src/network/network_chat_gui.cpp Source File

OpenTTD Source 1.11.0-RC1

network_chat_gui.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10 #include <stdarg.h> /* va_list */
11
12 #include "../stdafx.h"
13 #include "../strings_func.h"
14 #include "../blitter/factory.hpp"
15 #include "../console_func.h"
16 #include "../video/video_driver.hpp"
17 #include "../querystring_gui.h"
18 #include "../town.h"
19 #include "../window_func.h"
20 #include "../toolbar_gui.h"
21 #include "../core/geometry_func.hpp"
22 #include "network.h"
23 #include "network_client.h"
24 #include "network_base.h"
25
26 #include "../widgets/network_chat_widget.h"
27
28 #include "table/strings.h"
29
30 #include "../safeguards.h"
31
34 static_assert((int)DRAW_STRING_BUFFER >= (int)NETWORK_CHAT_LENGTH + NETWORK_NAME_LENGTH + 40);
35
37 static const uint NETWORK_CHAT_LINE_SPACING = 3;
38
40 struct ChatMessage {
41 char message[DRAW_STRING_BUFFER];
42 TextColour colour;
43 std::chrono::steady_clock::time_point remove_time;
44 };
45
46 /* used for chat window */
47 static ChatMessage *_chatmsg_list = nullptr;
48 static bool _chatmessage_dirty = false;
49 static bool _chatmessage_visible = false;
50 static bool _chat_tab_completion_active;
51 static uint MAX_CHAT_MESSAGES = 0;
52
57 static PointDimension _chatmsg_box;
58 static uint8 *_chatmessage_backup = nullptr;
59
64 static inline uint GetChatMessageCount()
65 {
66 uint i = 0;
67 for (; i < MAX_CHAT_MESSAGES; i++) {
68 if (_chatmsg_list[i].message[0] == '\0') break;
69 }
70
71 return i;
72 }
73
80 void CDECL NetworkAddChatMessage(TextColour colour, uint duration, const char *message, ...)
81 {
82 char buf[DRAW_STRING_BUFFER];
83 va_list va;
84
85 va_start(va, message);
86 vseprintf(buf, lastof(buf), message, va);
87 va_end(va);
88
89 Utf8TrimString(buf, DRAW_STRING_BUFFER);
90
91 uint msg_count = GetChatMessageCount();
92 if (MAX_CHAT_MESSAGES == msg_count) {
93 memmove(&_chatmsg_list[0], &_chatmsg_list[1], sizeof(_chatmsg_list[0]) * (msg_count - 1));
94 msg_count = MAX_CHAT_MESSAGES - 1;
95 }
96
97 ChatMessage *cmsg = &_chatmsg_list[msg_count++];
98 strecpy(cmsg->message, buf, lastof(cmsg->message));
99 cmsg->colour = (colour & TC_IS_PALETTE_COLOUR) ? colour : TC_WHITE;
100 cmsg->remove_time = std::chrono::steady_clock::now() + std::chrono::seconds(duration);
101
102 _chatmessage_dirty = true;
103 }
104
106 void NetworkReInitChatBoxSize()
107 {
108 _chatmsg_box.y = 3 * FONT_HEIGHT_NORMAL;
109 _chatmsg_box.height = MAX_CHAT_MESSAGES * (FONT_HEIGHT_NORMAL + NETWORK_CHAT_LINE_SPACING) + 4;
110 _chatmessage_backup = ReallocT(_chatmessage_backup, _chatmsg_box.width * _chatmsg_box.height * BlitterFactory::GetCurrentBlitter()->GetBytesPerPixel());
111 }
112
114 void NetworkInitChatMessage()
115 {
116 MAX_CHAT_MESSAGES = _settings_client.gui.network_chat_box_height;
117
118 _chatmsg_list = ReallocT(_chatmsg_list, _settings_client.gui.network_chat_box_height);
119 _chatmsg_box.x = 10;
120 _chatmsg_box.width = _settings_client.gui.network_chat_box_width_pct * _screen.width / 100;
121 NetworkReInitChatBoxSize();
122 _chatmessage_visible = false;
123
124 for (uint i = 0; i < MAX_CHAT_MESSAGES; i++) {
125 _chatmsg_list[i].message[0] = '\0';
126 }
127 }
128
130 void NetworkUndrawChatMessage()
131 {
132 /* Sometimes we also need to hide the cursor
133 * This is because both textmessage and the cursor take a shot of the
134 * screen before drawing.
135 * Now the textmessage takes his shot and paints his data before the cursor
136 * does, so in the shot of the cursor is the screen-data of the textmessage
137 * included when the cursor hangs somewhere over the textmessage. To
138 * avoid wrong repaints, we undraw the cursor in that case, and everything
139 * looks nicely ;)
140 * (and now hope this story above makes sense to you ;))
141 */
142 if (_cursor.visible &&
143 _cursor.draw_pos.x + _cursor.draw_size.x >= _chatmsg_box.x &&
144 _cursor.draw_pos.x <= _chatmsg_box.x + _chatmsg_box.width &&
145 _cursor.draw_pos.y + _cursor.draw_size.y >= _screen.height - _chatmsg_box.y - _chatmsg_box.height &&
146 _cursor.draw_pos.y <= _screen.height - _chatmsg_box.y) {
147 UndrawMouseCursor();
148 }
149
150 if (_chatmessage_visible) {
151 Blitter *blitter = BlitterFactory::GetCurrentBlitter();
152 int x = _chatmsg_box.x;
153 int y = _screen.height - _chatmsg_box.y - _chatmsg_box.height;
154 int width = _chatmsg_box.width;
155 int height = _chatmsg_box.height;
156 if (y < 0) {
157 height = std::max(height + y, std::min(_chatmsg_box.height, _screen.height));
158 y = 0;
159 }
160 if (x + width >= _screen.width) {
161 width = _screen.width - x;
162 }
163 if (width <= 0 || height <= 0) return;
164
165 _chatmessage_visible = false;
166 /* Put our 'shot' back to the screen */
167 blitter->CopyFromBuffer(blitter->MoveTo(_screen.dst_ptr, x, y), _chatmessage_backup, width, height);
168 /* And make sure it is updated next time */
169 VideoDriver::GetInstance()->MakeDirty(x, y, width, height);
170
171 _chatmessage_dirty = true;
172 }
173 }
174
176 void NetworkChatMessageLoop()
177 {
178 for (uint i = 0; i < MAX_CHAT_MESSAGES; i++) {
179 ChatMessage *cmsg = &_chatmsg_list[i];
180 if (cmsg->message[0] == '\0') continue;
181
182 /* Message has expired, remove from the list */
183 if (std::chrono::steady_clock::now() > cmsg->remove_time) {
184 /* Move the remaining messages over the current message */
185 if (i != MAX_CHAT_MESSAGES - 1) memmove(cmsg, cmsg + 1, sizeof(*cmsg) * (MAX_CHAT_MESSAGES - i - 1));
186
187 /* Mark the last item as empty */
188 _chatmsg_list[MAX_CHAT_MESSAGES - 1].message[0] = '\0';
189 _chatmessage_dirty = true;
190
191 /* Go one item back, because we moved the array 1 to the left */
192 i--;
193 }
194 }
195 }
196
198 void NetworkDrawChatMessage()
199 {
200 Blitter *blitter = BlitterFactory::GetCurrentBlitter();
201 if (!_chatmessage_dirty) return;
202
203 /* First undraw if needed */
204 NetworkUndrawChatMessage();
205
206 if (_iconsole_mode == ICONSOLE_FULL) return;
207
208 /* Check if we have anything to draw at all */
209 uint count = GetChatMessageCount();
210 if (count == 0) return;
211
212 int x = _chatmsg_box.x;
213 int y = _screen.height - _chatmsg_box.y - _chatmsg_box.height;
214 int width = _chatmsg_box.width;
215 int height = _chatmsg_box.height;
216 if (y < 0) {
217 height = std::max(height + y, std::min(_chatmsg_box.height, _screen.height));
218 y = 0;
219 }
220 if (x + width >= _screen.width) {
221 width = _screen.width - x;
222 }
223 if (width <= 0 || height <= 0) return;
224
225 assert(blitter->BufferSize(width, height) <= (int)(_chatmsg_box.width * _chatmsg_box.height * blitter->GetBytesPerPixel()));
226
227 /* Make a copy of the screen as it is before painting (for undraw) */
228 blitter->CopyToBuffer(blitter->MoveTo(_screen.dst_ptr, x, y), _chatmessage_backup, width, height);
229
230 _cur_dpi = &_screen; // switch to _screen painting
231
232 int string_height = 0;
233 for (uint i = 0; i < count; i++) {
234 SetDParamStr(0, _chatmsg_list[i].message);
235 string_height += GetStringLineCount(STR_JUST_RAW_STRING, width - 1) * FONT_HEIGHT_NORMAL + NETWORK_CHAT_LINE_SPACING;
236 }
237
238 string_height = std::min<uint>(string_height, MAX_CHAT_MESSAGES * (FONT_HEIGHT_NORMAL + NETWORK_CHAT_LINE_SPACING));
239
240 int top = _screen.height - _chatmsg_box.y - string_height - 2;
241 int bottom = _screen.height - _chatmsg_box.y - 2;
242 /* Paint a half-transparent box behind the chat messages */
243 GfxFillRect(_chatmsg_box.x, top - 2, _chatmsg_box.x + _chatmsg_box.width - 1, bottom,
244 PALETTE_TO_TRANSPARENT, FILLRECT_RECOLOUR // black, but with some alpha for background
245 );
246
247 /* Paint the chat messages starting with the lowest at the bottom */
248 int ypos = bottom - 2;
249
250 for (int i = count - 1; i >= 0; i--) {
251 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;
252 if (ypos < top) break;
253 }
254
255 /* Make sure the data is updated next flush */
256 VideoDriver::GetInstance()->MakeDirty(x, y, width, height);
257
258 _chatmessage_visible = true;
259 _chatmessage_dirty = false;
260 }
261
268 static void SendChat(const char *buf, DestType type, int dest)
269 {
270 if (StrEmpty(buf)) return;
271 if (!_network_server) {
272 MyClient::SendChat((NetworkAction)(NETWORK_ACTION_CHAT + type), type, dest, buf, 0);
273 } else {
274 NetworkServerSendChat((NetworkAction)(NETWORK_ACTION_CHAT + type), type, dest, buf, CLIENT_ID_SERVER);
275 }
276 }
277
279 struct NetworkChatWindow : public Window {
280 DestType dtype;
281 StringID dest_string;
282 int dest;
283 QueryString message_editbox;
284
291 NetworkChatWindow(WindowDesc *desc, DestType type, int dest) : Window(desc), message_editbox(NETWORK_CHAT_LENGTH)
292 {
293 this->dtype = type;
294 this->dest = dest;
295 this->querystrings[WID_NC_TEXTBOX] = &this->message_editbox;
296 this->message_editbox.cancel_button = WID_NC_CLOSE;
297 this->message_editbox.ok_button = WID_NC_SENDBUTTON;
298
299 static const StringID chat_captions[] = {
300 STR_NETWORK_CHAT_ALL_CAPTION,
301 STR_NETWORK_CHAT_COMPANY_CAPTION,
302 STR_NETWORK_CHAT_CLIENT_CAPTION
303 };
304 assert((uint)this->dtype < lengthof(chat_captions));
305 this->dest_string = chat_captions[this->dtype];
306
307 this->InitNested(type);
308
309 this->SetFocusedWidget(WID_NC_TEXTBOX);
310 InvalidateWindowData(WC_NEWS_WINDOW, 0, this->height);
311 _chat_tab_completion_active = false;
312
313 PositionNetworkChatWindow(this);
314 }
315
316 ~NetworkChatWindow()
317 {
318 InvalidateWindowData(WC_NEWS_WINDOW, 0, 0);
319 }
320
321 void FindWindowPlacementAndResize(int def_width, int def_height) override
322 {
323 Window::FindWindowPlacementAndResize(_toolbar_width, def_height);
324 }
325
332 const char *ChatTabCompletionNextItem(uint *item)
333 {
334 static char chat_tab_temp_buffer[64];
335
336 /* First, try clients */
337 if (*item < MAX_CLIENT_SLOTS) {
338 /* Skip inactive clients */
339 for (NetworkClientInfo *ci : NetworkClientInfo::Iterate(*item)) {
340 *item = ci->index;
341 return ci->client_name;
342 }
343 *item = MAX_CLIENT_SLOTS;
344 }
345
346 /* Then, try townnames
347 * Not that the following assumes all town indices are adjacent, ie no
348 * towns have been deleted. */
349 if (*item < (uint)MAX_CLIENT_SLOTS + Town::GetPoolSize()) {
350 for (const Town *t : Town::Iterate(*item - MAX_CLIENT_SLOTS)) {
351 /* Get the town-name via the string-system */
352 SetDParam(0, t->index);
353 GetString(chat_tab_temp_buffer, STR_TOWN_NAME, lastof(chat_tab_temp_buffer));
354 return &chat_tab_temp_buffer[0];
355 }
356 }
357
358 return nullptr;
359 }
360
366 static char *ChatTabCompletionFindText(char *buf)
367 {
368 char *p = strrchr(buf, ' ');
369 if (p == nullptr) return buf;
370
371 *p = '\0';
372 return p + 1;
373 }
374
378 void ChatTabCompletion()
379 {
380 static char _chat_tab_completion_buf[NETWORK_CHAT_LENGTH];
381 assert(this->message_editbox.text.max_bytes == lengthof(_chat_tab_completion_buf));
382
383 Textbuf *tb = &this->message_editbox.text;
384 size_t len, tb_len;
385 uint item;
386 char *tb_buf, *pre_buf;
387 const char *cur_name;
388 bool second_scan = false;
389
390 item = 0;
391
392 /* Copy the buffer so we can modify it without damaging the real data */
393 pre_buf = (_chat_tab_completion_active) ? stredup(_chat_tab_completion_buf) : stredup(tb->buf);
394
395 tb_buf = ChatTabCompletionFindText(pre_buf);
396 tb_len = strlen(tb_buf);
397
398 while ((cur_name = ChatTabCompletionNextItem(&item)) != nullptr) {
399 item++;
400
401 if (_chat_tab_completion_active) {
402 /* We are pressing TAB again on the same name, is there another name
403 * that starts with this? */
404 if (!second_scan) {
405 size_t offset;
406 size_t length;
407
408 /* If we are completing at the begin of the line, skip the ': ' we added */
409 if (tb_buf == pre_buf) {
410 offset = 0;
411 length = (tb->bytes - 1) - 2;
412 } else {
413 /* Else, find the place we are completing at */
414 offset = strlen(pre_buf) + 1;
415 length = (tb->bytes - 1) - offset;
416 }
417
418 /* Compare if we have a match */
419 if (strlen(cur_name) == length && strncmp(cur_name, tb->buf + offset, length) == 0) second_scan = true;
420
421 continue;
422 }
423
424 /* Now any match we make on _chat_tab_completion_buf after this, is perfect */
425 }
426
427 len = strlen(cur_name);
428 if (tb_len < len && strncasecmp(cur_name, tb_buf, tb_len) == 0) {
429 /* Save the data it was before completion */
430 if (!second_scan) seprintf(_chat_tab_completion_buf, lastof(_chat_tab_completion_buf), "%s", tb->buf);
431 _chat_tab_completion_active = true;
432
433 /* Change to the found name. Add ': ' if we are at the start of the line (pretty) */
434 if (pre_buf == tb_buf) {
435 this->message_editbox.text.Print("%s: ", cur_name);
436 } else {
437 this->message_editbox.text.Print("%s %s", pre_buf, cur_name);
438 }
439
440 this->SetDirty();
441 free(pre_buf);
442 return;
443 }
444 }
445
446 if (second_scan) {
447 /* We walked all possibilities, and the user presses tab again.. revert to original text */
448 this->message_editbox.text.Assign(_chat_tab_completion_buf);
449 _chat_tab_completion_active = false;
450
451 this->SetDirty();
452 }
453 free(pre_buf);
454 }
455
456 Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number) override
457 {
458 Point pt = { 0, _screen.height - sm_height - FindWindowById(WC_STATUS_BAR, 0)->height };
459 return pt;
460 }
461
462 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
463 {
464 if (widget != WID_NC_DESTINATION) return;
465
466 if (this->dtype == DESTTYPE_CLIENT) {
467 SetDParamStr(0, NetworkClientInfo::GetByClientID((ClientID)this->dest)->client_name);
468 }
469 Dimension d = GetStringBoundingBox(this->dest_string);
470 d.width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
471 d.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
472 *size = maxdim(*size, d);
473 }
474
475 void DrawWidget(const Rect &r, int widget) const override
476 {
477 if (widget != WID_NC_DESTINATION) return;
478
479 if (this->dtype == DESTTYPE_CLIENT) {
480 SetDParamStr(0, NetworkClientInfo::GetByClientID((ClientID)this->dest)->client_name);
481 }
482 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, this->dest_string, TC_BLACK, SA_RIGHT);
483 }
484
485 void OnClick(Point pt, int widget, int click_count) override
486 {
487 switch (widget) {
488 case WID_NC_SENDBUTTON: /* Send */
489 SendChat(this->message_editbox.text.buf, this->dtype, this->dest);
490 FALLTHROUGH;
491
492 case WID_NC_CLOSE: /* Cancel */
493 delete this;
494 break;
495 }
496 }
497
498 EventState OnKeyPress(WChar key, uint16 keycode) override
499 {
500 EventState state = ES_NOT_HANDLED;
501 if (keycode == WKC_TAB) {
502 ChatTabCompletion();
503 state = ES_HANDLED;
504 }
505 return state;
506 }
507
508 void OnEditboxChanged(int wid) override
509 {
510 _chat_tab_completion_active = false;
511 }
512
518 void OnInvalidateData(int data = 0, bool gui_scope = true) override
519 {
520 if (data == this->dest) delete this;
521 }
522 };
523
525 static const NWidgetPart _nested_chat_window_widgets[] = {
526 NWidget(NWID_HORIZONTAL),
527 NWidget(WWT_CLOSEBOX, COLOUR_GREY, WID_NC_CLOSE),
528 NWidget(WWT_PANEL, COLOUR_GREY, WID_NC_BACKGROUND),
529 NWidget(NWID_HORIZONTAL),
530 NWidget(WWT_TEXT, COLOUR_GREY, WID_NC_DESTINATION), SetMinimalSize(62, 12), SetPadding(1, 0, 1, 0), SetDataTip(STR_NULL, STR_NULL),
531 NWidget(WWT_EDITBOX, COLOUR_GREY, WID_NC_TEXTBOX), SetMinimalSize(100, 12), SetPadding(1, 0, 1, 0), SetResize(1, 0),
532 SetDataTip(STR_NETWORK_CHAT_OSKTITLE, STR_NULL),
533 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_NC_SENDBUTTON), SetMinimalSize(62, 12), SetPadding(1, 0, 1, 0), SetDataTip(STR_NETWORK_CHAT_SEND, STR_NULL),
534 EndContainer(),
535 EndContainer(),
536 EndContainer(),
537 };
538
540 static WindowDesc _chat_window_desc(
541 WDP_MANUAL, nullptr, 0, 0,
542 WC_SEND_NETWORK_MSG, WC_NONE,
543 0,
544 _nested_chat_window_widgets, lengthof(_nested_chat_window_widgets)
545 );
546
547
553 void ShowNetworkChatQueryWindow(DestType type, int dest)
554 {
555 DeleteWindowByClass(WC_SEND_NETWORK_MSG);
556 new NetworkChatWindow(&_chat_window_desc, type, dest);
557 }
ES_HANDLED
@ ES_HANDLED
The passed event is handled.
Definition: window_type.h:718
WD_FRAMERECT_TOP
@ WD_FRAMERECT_TOP
Offset at top to draw the frame rectangular area.
Definition: window_gui.h:62
WID_NC_SENDBUTTON
@ WID_NC_SENDBUTTON
Send button.
Definition: network_chat_widget.h:19
DestType
DestType
Destination of our chat messages.
Definition: network_type.h:81
InvalidateWindowData
void InvalidateWindowData(WindowClass cls, WindowNumber number, int data, bool gui_scope)
Mark window data of the window of a given class and specific window number as invalid (in need of re-...
Definition: window.cpp:3321
QueryString::ok_button
int ok_button
Widget button of parent window to simulate when pressing OK in OSK.
Definition: querystring_gui.h:27
WChar
char32_t WChar
Type for wide characters, i.e.
Definition: string_type.h:35
NetworkChatWindow
Window to enter the chat message in.
Definition: network_chat_gui.cpp:279
Textbuf::max_bytes
uint16 max_bytes
the maximum size of the buffer in bytes (including terminating '\0')
Definition: textbuf_type.h:33
Textbuf::Print
void CDECL Print(const char *format,...) WARN_FORMAT(2
Print a formatted string into the textbuffer.
Definition: textbuf.cpp:415
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition: geometry_type.hpp:27
WID_NC_DESTINATION
@ WID_NC_DESTINATION
Destination.
Definition: network_chat_widget.h:17
NetworkChatWindow::dest
int dest
The identifier of the destination.
Definition: network_chat_gui.cpp:282
SetPadding
static NWidgetPart SetPadding(uint8 top, uint8 right, uint8 bottom, uint8 left)
Widget part function for setting additional space around a widget.
Definition: widget_type.h:1045
ClientNetworkGameSocketHandler::SendChat
static NetworkRecvStatus SendChat(NetworkAction action, DestType type, int dest, const char *msg, int64 data)
Send a chat-packet over the network.
Definition: network_client.cpp:447
NETWORK_NAME_LENGTH
static const uint NETWORK_NAME_LENGTH
The maximum length of the server name and map name, in bytes including '\0'.
Definition: config.h:40
Blitter
How all blitters should look like.
Definition: base.hpp:28
WID_NC_CLOSE
@ WID_NC_CLOSE
Close button.
Definition: network_chat_widget.h:15
SA_RIGHT
@ SA_RIGHT
Right align the text (must be a single bit).
Definition: gfx_func.h:98
CursorVars::visible
bool visible
cursor is visible
Definition: gfx_type.h:139
Textbuf::Assign
void Assign(StringID string)
Render a string into the textbuffer.
Definition: textbuf.cpp:396
_network_server
bool _network_server
network-server is active
Definition: network.cpp:53
NetworkAction
NetworkAction
Actions that can be used for NetworkTextMessage.
Definition: network_type.h:91
Blitter::CopyToBuffer
virtual void CopyToBuffer(const void *video, void *dst, int width, int height)=0
Copy from the screen to a buffer.
FILLRECT_RECOLOUR
@ FILLRECT_RECOLOUR
Apply a recolour sprite to the screen content.
Definition: gfx_type.h:289
_chatmessage_backup
static uint8 * _chatmessage_backup
Backup in case text is moved.
Definition: network_chat_gui.cpp:58
VideoDriver::MakeDirty
virtual void MakeDirty(int left, int top, int width, int height)=0
Mark a particular area dirty.
SA_LEFT
@ SA_LEFT
Left align the text.
Definition: gfx_func.h:96
NWID_HORIZONTAL
@ NWID_HORIZONTAL
Horizontal container.
Definition: widget_type.h:73
NetworkInitChatMessage
void NetworkInitChatMessage()
Initialize all buffers of the chat visualisation.
Definition: network_chat_gui.cpp:114
maxdim
Dimension maxdim(const Dimension &d1, const Dimension &d2)
Compute bounding box of both dimensions.
Definition: geometry_func.cpp:22
NETWORK_CHAT_LENGTH
static const uint NETWORK_CHAT_LENGTH
The maximum length of a chat message, in bytes including '\0'.
Definition: config.h:50
FindWindowById
Window * FindWindowById(WindowClass cls, WindowNumber number)
Find a window by its class and window number.
Definition: window.cpp:1133
PALETTE_TO_TRANSPARENT
static const PaletteID PALETTE_TO_TRANSPARENT
This sets the sprite to transparent.
Definition: sprites.h:1591
NetworkChatWindow::dtype
DestType dtype
The type of destination.
Definition: network_chat_gui.cpp:280
TextColour
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition: gfx_type.h:250
SetResize
static NWidgetPart SetResize(int16 dx, int16 dy)
Widget part function for setting the resize step.
Definition: widget_type.h:929
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:79
ChatMessage
Container for a message.
Definition: network_chat_gui.cpp:40
DrawString
int DrawString(int left, int right, int top, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly truncated to make it fit in its allocated space.
Definition: gfx.cpp:640
DeleteWindowByClass
void DeleteWindowByClass(WindowClass cls)
Delete all windows of a given class.
Definition: window.cpp:1178
NetworkChatWindow::NetworkChatWindow
NetworkChatWindow(WindowDesc *desc, DestType type, int dest)
Create a chat input window.
Definition: network_chat_gui.cpp:291
SA_BOTTOM
@ SA_BOTTOM
Bottom align the text.
Definition: gfx_func.h:103
SendChat
static void SendChat(const char *buf, DestType type, int dest)
Send an actual chat message.
Definition: network_chat_gui.cpp:268
SA_FORCE
@ SA_FORCE
Force the alignment, i.e. don't swap for RTL languages.
Definition: gfx_func.h:108
CursorVars::draw_size
Point draw_size
position and size bounding-box for drawing
Definition: gfx_type.h:133
SetDParam
static void SetDParam(uint n, uint64 v)
Set a string parameter v at index n in the global string parameter array.
Definition: strings_func.h:199
NWidgetPart
Partial widget specification to allow NWidgets to be written nested.
Definition: widget_type.h:909
SetDataTip
static NWidgetPart SetDataTip(uint32 data, StringID tip)
Widget part function for setting the data and tooltip.
Definition: widget_type.h:1013
QueryString
Data stored about a string that can be modified in the GUI.
Definition: querystring_gui.h:20
GetStringBoundingBox
Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize)
Return the string dimension in pixels.
Definition: gfx.cpp:842
GetStringLineCount
int GetStringLineCount(StringID str, int maxw)
Calculates number of lines of string.
Definition: gfx.cpp:714
Textbuf::buf
char *const buf
buffer in which text is saved
Definition: textbuf_type.h:32
Window::querystrings
SmallMap< int, QueryString * > querystrings
QueryString associated to WWT_EDITBOX widgets.
Definition: window_gui.h:328
network_base.h
DrawStringMultiLine
int DrawStringMultiLine(int left, int right, int top, int bottom, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly over multiple lines.
Definition: gfx.cpp:763
NetworkReInitChatBoxSize
void NetworkReInitChatBoxSize()
Initialize all font-dependent chat box sizes.
Definition: network_chat_gui.cpp:106
ICONSOLE_FULL
@ ICONSOLE_FULL
In-game console is closed.
Definition: console_type.h:17
NetworkChatWindow::OnKeyPress
EventState OnKeyPress(WChar key, uint16 keycode) override
A key has been pressed.
Definition: network_chat_gui.cpp:498
WindowDesc
High level window description.
Definition: window_gui.h:166
NetworkClientInfo::GetByClientID
static NetworkClientInfo * GetByClientID(ClientID client_id)
Return the CI given it's client-identifier.
Definition: network.cpp:119
Pool::PoolItem<&_town_pool >::GetPoolSize
static size_t GetPoolSize()
Returns first unused index.
Definition: pool_type.hpp:350
ChatMessage::colour
TextColour colour
The colour of the message.
Definition: network_chat_gui.cpp:42
_chatmessage_visible
static bool _chatmessage_visible
Is a chat message visible.
Definition: network_chat_gui.cpp:49
DRAW_STRING_BUFFER
static const int DRAW_STRING_BUFFER
Size of the buffer used for drawing strings.
Definition: gfx_func.h:85
Window::resize
ResizeInfo resize
Resize information.
Definition: window_gui.h:322
Window::InitNested
void InitNested(WindowNumber number=0)
Perform complete initialization of the Window with nested widgets, to allow use.
Definition: window.cpp:1861
WWT_EDITBOX
@ WWT_EDITBOX
a textbox for typing
Definition: widget_type.h:69
Window::height
int height
Height of the window (number of pixels down in y direction)
Definition: window_gui.h:320
_toolbar_width
uint _toolbar_width
Width of the toolbar, shared by statusbar.
Definition: toolbar_gui.cpp:63
NetworkChatWindow::OnInitialPosition
Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number) override
Compute the initial position of the window.
Definition: network_chat_gui.cpp:456
Window::SetDirty
void SetDirty() const
Mark entire window as dirty (in need of re-paint)
Definition: window.cpp:984
WD_FRAMERECT_LEFT
@ WD_FRAMERECT_LEFT
Offset at left to draw the frame rectangular area.
Definition: window_gui.h:60
ClientID
ClientID
'Unique' identifier to be given to clients
Definition: network_type.h:39
WD_FRAMERECT_RIGHT
@ WD_FRAMERECT_RIGHT
Offset at right to draw the frame rectangular area.
Definition: window_gui.h:61
ES_NOT_HANDLED
@ ES_NOT_HANDLED
The passed event is not handled.
Definition: window_type.h:719
WD_FRAMERECT_BOTTOM
@ WD_FRAMERECT_BOTTOM
Offset at bottom to draw the frame rectangular area.
Definition: window_gui.h:63
WWT_PUSHTXTBTN
@ WWT_PUSHTXTBTN
Normal push-button (no toggle button) with text caption.
Definition: widget_type.h:102
NetworkDrawChatMessage
void NetworkDrawChatMessage()
Draw the chat message-box.
Definition: network_chat_gui.cpp:198
TC_IS_PALETTE_COLOUR
@ TC_IS_PALETTE_COLOUR
Colour value is already a real palette colour index, not an index of a StringColour.
Definition: gfx_type.h:273
BlitterFactory::GetCurrentBlitter
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition: factory.hpp:140
_chat_tab_completion_active
static bool _chat_tab_completion_active
Whether tab completion is active.
Definition: network_chat_gui.cpp:50
CLIENT_ID_SERVER
@ CLIENT_ID_SERVER
Servers always have this ID.
Definition: network_type.h:41
StrEmpty
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:60
network_client.h
Point
Coordinates of a point in 2D.
Definition: geometry_type.hpp:21
vseprintf
int CDECL vseprintf(char *str, const char *last, const char *format, va_list ap)
Safer implementation of vsnprintf; same as vsnprintf except:
Definition: string.cpp:61
NetworkUndrawChatMessage
void NetworkUndrawChatMessage()
Hide the chatbox.
Definition: network_chat_gui.cpp:130
Window::SetFocusedWidget
bool SetFocusedWidget(int widget_index)
Set focus within this window to the given widget.
Definition: window.cpp:495
Window::window_number
WindowNumber window_number
Window number within the window class.
Definition: window_gui.h:312
GfxFillRect
void GfxFillRect(int left, int top, int right, int bottom, int colour, FillRectMode mode)
Applies a certain FillRectMode-operation to a rectangle [left, right] x [top, bottom] on the screen.
Definition: gfx.cpp:114
VideoDriver::GetInstance
static VideoDriver * GetInstance()
Get the currently active instance of the video driver.
Definition: video_driver.hpp:187
WC_NONE
@ WC_NONE
No window, redirects to WC_MAIN_WINDOW.
Definition: window_type.h:38
WWT_CLOSEBOX
@ WWT_CLOSEBOX
Close box (at top-left of a window)
Definition: widget_type.h:67
_chatmsg_list
static ChatMessage * _chatmsg_list
The actual chat message list.
Definition: network_chat_gui.cpp:47
StringID
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
Textbuf::bytes
uint16 bytes
the current size of the string in bytes (including terminating '\0')
Definition: textbuf_type.h:35
QueryString::cancel_button
int cancel_button
Widget button of parent window to simulate when pressing CANCEL in OSK.
Definition: querystring_gui.h:28
EndContainer
static NWidgetPart EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME,...
Definition: widget_type.h:998
Pool::PoolItem<&_networkclientinfo_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:378
NETWORK_CHAT_LINE_SPACING
static const uint NETWORK_CHAT_LINE_SPACING
The draw buffer must be able to contain the chat message, client name and the "[All]" message,...
Definition: network_chat_gui.cpp:37
_chatmessage_dirty
static bool _chatmessage_dirty
Does the chat message need repainting?
Definition: network_chat_gui.cpp:48
NetworkChatWindow::UpdateWidgetSize
void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
Update size and resize step of a widget in the window.
Definition: network_chat_gui.cpp:462
Blitter::MoveTo
virtual void * MoveTo(void *video, int x, int y)=0
Move the destination pointer the requested amount x and y, keeping in mind any pitch and bpp of the r...
GetChatMessageCount
static uint GetChatMessageCount()
Count the chat messages.
Definition: network_chat_gui.cpp:64
NetworkChatWindow::OnInvalidateData
void OnInvalidateData(int data=0, bool gui_scope=true) override
Some data on this window has become invalid.
Definition: network_chat_gui.cpp:518
WWT_TEXT
@ WWT_TEXT
Pure simple text.
Definition: widget_type.h:56
WID_NC_TEXTBOX
@ WID_NC_TEXTBOX
Textbox.
Definition: network_chat_widget.h:18
FONT_HEIGHT_NORMAL
#define FONT_HEIGHT_NORMAL
Height of characters in the normal (FS_NORMAL) font.
Definition: gfx_func.h:179
NWidget
static NWidgetPart NWidget(WidgetType tp, Colours col, int16 idx=-1)
Widget part function for starting a new 'real' widget.
Definition: widget_type.h:1113
NetworkChatWindow::ChatTabCompletionNextItem
const char * ChatTabCompletionNextItem(uint *item)
Find the next item of the list of things that can be auto-completed.
Definition: network_chat_gui.cpp:332
SetMinimalSize
static NWidgetPart SetMinimalSize(int16 x, int16 y)
Widget part function for setting the minimal size.
Definition: widget_type.h:946
_chat_window_desc
static WindowDesc _chat_window_desc(WDP_MANUAL, nullptr, 0, 0, WC_SEND_NETWORK_MSG, WC_NONE, 0, _nested_chat_window_widgets, lengthof(_nested_chat_window_widgets))
The description of the chat window.
NetworkServerSendChat
void NetworkServerSendChat(NetworkAction action, DestType type, int dest, const char *msg, ClientID from_id, int64 data=0, bool from_admin=false)
Send an actual chat message.
Definition: network_server.cpp:1273
NetworkChatWindow::OnEditboxChanged
void OnEditboxChanged(int wid) override
The text in an editbox has been edited.
Definition: network_chat_gui.cpp:508
NetworkChatWindow::FindWindowPlacementAndResize
void FindWindowPlacementAndResize(int def_width, int def_height) override
Resize window towards the default size.
Definition: network_chat_gui.cpp:321
WWT_PANEL
@ WWT_PANEL
Simple depressed panel.
Definition: widget_type.h:48
EventState
EventState
State of handling an event.
Definition: window_type.h:717
MAX_CHAT_MESSAGES
static uint MAX_CHAT_MESSAGES
The limit of chat messages to show.
Definition: network_chat_gui.cpp:51
NetworkChatMessageLoop
void NetworkChatMessageLoop()
Check if a message is expired.
Definition: network_chat_gui.cpp:176
seprintf
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:442
NetworkChatWindow::ChatTabCompletion
void ChatTabCompletion()
See if we can auto-complete the current text of the user.
Definition: network_chat_gui.cpp:378
ReallocT
static T * ReallocT(T *t_ptr, size_t num_elements)
Simplified reallocation function that allocates the specified number of elements of the given type.
Definition: alloc_func.hpp:111
NetworkChatWindow::OnClick
void OnClick(Point pt, int widget, int click_count) override
A click with the left mouse button has been made on the window.
Definition: network_chat_gui.cpp:485
stredup
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:137
ShowNetworkChatQueryWindow
void ShowNetworkChatQueryWindow(DestType type, int dest)
Show the chat window.
Definition: network_chat_gui.cpp:553
network.h
GUISettings::network_chat_box_width_pct
uint16 network_chat_box_width_pct
width of the chat box in percent
Definition: settings_type.h:169
WC_SEND_NETWORK_MSG
@ WC_SEND_NETWORK_MSG
Chatbox; Window numbers:
Definition: window_type.h:491
Blitter::CopyFromBuffer
virtual void CopyFromBuffer(void *video, const void *src, int width, int height)=0
Copy from a buffer to the screen.
Utf8TrimString
size_t Utf8TrimString(char *s, size_t maxlen)
Properly terminate an UTF8 string to some maximum length.
Definition: string.cpp:567
NetworkChatWindow::ChatTabCompletionFindText
static char * ChatTabCompletionFindText(char *buf)
Find what text to complete.
Definition: network_chat_gui.cpp:366
NetworkChatWindow::DrawWidget
void DrawWidget(const Rect &r, int widget) const override
Draw the contents of a nested widget.
Definition: network_chat_gui.cpp:475
Town
Town data structure.
Definition: town.h:50
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:367
WDP_MANUAL
@ WDP_MANUAL
Manually align the window (so no automatic location finding)
Definition: window_gui.h:153
NetworkChatWindow::message_editbox
QueryString message_editbox
Message editbox.
Definition: network_chat_gui.cpp:283
PointDimension
Specification of a rectangle with an absolute top-left coordinate and a (relative) width/height.
Definition: geometry_type.hpp:58
_nested_chat_window_widgets
static const NWidgetPart _nested_chat_window_widgets[]
The widgets of the chat window.
Definition: network_chat_gui.cpp:525
PositionNetworkChatWindow
int PositionNetworkChatWindow(Window *w)
(Re)position network chat window at the screen.
Definition: window.cpp:3540
DESTTYPE_CLIENT
@ DESTTYPE_CLIENT
Send message/notice to only a certain client (Private)
Definition: network_type.h:84
Window
Data structure for an opened window.
Definition: window_gui.h:276
WC_STATUS_BAR
@ WC_STATUS_BAR
Statusbar (at the bottom of your screen); Window numbers:
Definition: window_type.h:57
WC_NEWS_WINDOW
@ WC_NEWS_WINDOW
News window; Window numbers:
Definition: window_type.h:241
NetworkChatWindow::dest_string
StringID dest_string
String representation of the destination.
Definition: network_chat_gui.cpp:281
ChatMessage::message
char message[DRAW_STRING_BUFFER]
The action message.
Definition: network_chat_gui.cpp:41
strecpy
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: string.cpp:112
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:454
MAX_CLIENT_SLOTS
static const uint MAX_CLIENT_SLOTS
The number of slots; must be at least 1 more than MAX_CLIENTS.
Definition: network_type.h:23
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition: geometry_type.hpp:47
WID_NC_BACKGROUND
@ WID_NC_BACKGROUND
Background of the window.
Definition: network_chat_widget.h:16
_chatmsg_box
static PointDimension _chatmsg_box
The chatbox grows from the bottom so the coordinates are pixels from the left and pixels from the bot...
Definition: network_chat_gui.cpp:57
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:383
GUISettings::network_chat_box_height
uint8 network_chat_box_height
height of the chat box in lines
Definition: settings_type.h:170
Blitter::GetBytesPerPixel
virtual int GetBytesPerPixel()=0
Get how many bytes are needed to store a pixel.
NetworkClientInfo
Container for all information known about a client.
Definition: network_base.h:23
SetDParamStr
void SetDParamStr(uint n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
Definition: strings.cpp:286
ChatMessage::remove_time
std::chrono::steady_clock::time_point remove_time
The time to remove the message.
Definition: network_chat_gui.cpp:43
ClientSettings::gui
GUISettings gui
settings related to the GUI
Definition: settings_type.h:577
NetworkAddChatMessage
void CDECL NetworkAddChatMessage(TextColour colour, uint duration, const char *message,...)
Add a text message to the 'chat window' to be shown.
Definition: network_chat_gui.cpp:80
Window::FindWindowPlacementAndResize
virtual void FindWindowPlacementAndResize(int def_width, int def_height)
Resize window towards the default size.
Definition: window.cpp:1526
Textbuf
Helper/buffer for input fields.
Definition: textbuf_type.h:30
Blitter::BufferSize
virtual int BufferSize(int width, int height)=0
Calculate how much memory there is needed for an image of this size in the video-buffer.