OpenTTD Source
12.0-beta2
|
Go to the documentation of this file.
10 #include "../stdafx.h"
11 #include "../strings_func.h"
12 #include "../blitter/factory.hpp"
13 #include "../console_func.h"
14 #include "../video/video_driver.hpp"
15 #include "../querystring_gui.h"
17 #include "../window_func.h"
18 #include "../toolbar_gui.h"
19 #include "../core/geometry_func.hpp"
24 #include "../widgets/network_chat_widget.h"
26 #include "table/strings.h"
31 #include "../safeguards.h"
76 auto now = std::chrono::steady_clock::now();
78 if (cmsg.remove_time >= now)
return true;
99 cmsg->
remove_time = std::chrono::steady_clock::now() + std::chrono::seconds(duration);
142 _cursor.draw_pos.y <= _screen.height -
_chatmsg_box.y) {
153 height = std::max(height + y, std::min(
_chatmsg_box.height, _screen.height));
156 if (x + width >= _screen.width) {
157 width = _screen.width - x;
159 if (width <= 0 || height <= 0)
return;
175 auto now = std::chrono::steady_clock::now();
193 bool show_all = (w !=
nullptr);
208 height = std::max(height + y, std::min(
_chatmsg_box.height, _screen.height));
211 if (x + width >= _screen.width) {
212 width = _screen.width - x;
214 if (width <= 0 || height <= 0)
return;
223 auto now = std::chrono::steady_clock::now();
224 int string_height = 0;
226 if (!show_all && cmsg.remove_time < now)
continue;
233 int top = _screen.height -
_chatmsg_box.y - string_height - 2;
241 int ypos = bottom - 2;
244 if (!show_all && cmsg.remove_time < now)
continue;
246 if (ypos < top)
break;
264 if (buf.empty())
return;
292 static const StringID chat_captions[] = {
293 STR_NETWORK_CHAT_ALL_CAPTION,
294 STR_NETWORK_CHAT_COMPANY_CAPTION,
295 STR_NETWORK_CHAT_CLIENT_CAPTION
297 assert((uint)this->dtype <
lengthof(chat_captions));
305 _chat_tab_completion_active =
false;
329 static char chat_tab_temp_buffer[64];
336 return ci->client_name.c_str();
348 GetString(chat_tab_temp_buffer, STR_TOWN_NAME,
lastof(chat_tab_temp_buffer));
349 return &chat_tab_temp_buffer[0];
363 char *p = strrchr(buf,
' ');
364 if (p ==
nullptr)
return buf;
376 assert(this->message_editbox.text.
max_bytes ==
lengthof(_chat_tab_completion_buf));
378 Textbuf *tb = &this->message_editbox.text;
381 char *tb_buf, *pre_buf;
382 const char *cur_name;
383 bool second_scan =
false;
391 tb_len = strlen(tb_buf);
404 if (tb_buf == pre_buf) {
406 length = (tb->
bytes - 1) - 2;
409 offset = strlen(pre_buf) + 1;
410 length = (tb->
bytes - 1) - offset;
414 if (strlen(cur_name) == length && strncmp(cur_name, tb->
buf + offset, length) == 0) second_scan =
true;
422 len = strlen(cur_name);
423 if (tb_len < len && strncasecmp(cur_name, tb_buf, tb_len) == 0) {
425 if (!second_scan)
seprintf(_chat_tab_completion_buf,
lastof(_chat_tab_completion_buf),
"%s", tb->
buf);
429 if (pre_buf == tb_buf) {
430 this->message_editbox.text.
Print(
"%s: ", cur_name);
432 this->message_editbox.text.
Print(
"%s %s", pre_buf, cur_name);
443 this->message_editbox.text.
Assign(_chat_tab_completion_buf);
470 SendChat(this->message_editbox.text.
buf, this->dtype, this->dest);
482 if (keycode == WKC_TAB) {
501 if (data == this->dest) this->
Close();
511 NWidget(
WWT_TEXT, COLOUR_GREY,
WID_NC_DESTINATION),
SetMinimalSize(62, 12),
SetPadding(1, 0, 1, 0),
SetTextColour(TC_BLACK),
SetAlignment(
SA_TOP |
SA_RIGHT),
SetDataTip(STR_NULL, STR_NULL),
512 NWidget(
WWT_EDITBOX, COLOUR_GREY,
WID_NC_TEXTBOX),
SetMinimalSize(100, 12),
SetPadding(1, 0, 1, 0),
SetResize(1, 0),
513 SetDataTip(STR_NETWORK_CHAT_OSKTITLE, STR_NULL),
514 NWidget(
WWT_PUSHTXTBTN, COLOUR_GREY,
WID_NC_SENDBUTTON),
SetMinimalSize(62, 12),
SetPadding(1, 0, 1, 0),
SetDataTip(STR_NETWORK_CHAT_SEND, STR_NULL),
@ ES_HANDLED
The passed event is handled.
DestType
Destination of our chat messages.
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-...
int ok_button
Widget button of parent window to simulate when pressing OK in OSK.
char32_t WChar
Type for wide characters, i.e.
Window to enter the chat message in.
uint16 max_bytes
the maximum size of the buffer in bytes (including terminating '\0')
void CDECL Print(const char *format,...) WARN_FORMAT(2
Print a formatted string into the textbuffer.
int dest
The identifier of the destination.
void NetworkServerSendChat(NetworkAction action, DestType type, int dest, const std::string &msg, ClientID from_id, int64 data=0, bool from_admin=false)
Send an actual chat message.
static const uint NETWORK_NAME_LENGTH
The maximum length of the server name and map name, in bytes including '\0'.
How all blitters should look like.
bool visible
cursor is visible
void Assign(StringID string)
Render a string into the textbuffer.
bool _network_server
network-server is active
NetworkAction
Actions that can be used for NetworkTextMessage.
virtual void CopyToBuffer(const void *video, void *dst, int width, int height)=0
Copy from the screen to a buffer.
@ FILLRECT_RECOLOUR
Apply a recolour sprite to the screen content.
static uint8 * _chatmessage_backup
Backup in case text is moved.
void CreateNestedTree(bool fill_nested=true)
Perform the first part of the initialization of a nested widget tree.
virtual void MakeDirty(int left, int top, int width, int height)=0
Mark a particular area dirty.
void NetworkInitChatMessage()
Initialize all buffers of the chat visualisation.
static const uint NETWORK_CHAT_LENGTH
The maximum length of a chat message, in bytes including '\0'.
Window * FindWindowById(WindowClass cls, WindowNumber number)
Find a window by its class and window number.
static const PaletteID PALETTE_TO_TRANSPARENT
This sets the sprite to transparent.
DestType dtype
The type of destination.
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
@ SA_BOTTOM
Bottom align the text.
ClientSettings _settings_client
The current settings for this game.
NetworkChatWindow(WindowDesc *desc, DestType type, int dest)
Create a chat input window.
@ SA_RIGHT
Right align the text (must be a single bit).
Point draw_size
position and size bounding-box for drawing
static void SetDParam(uint n, uint64 v)
Set a string parameter v at index n in the global string parameter array.
Data stored about a string that can be modified in the GUI.
int GetStringLineCount(StringID str, int maxw)
Calculates number of lines of string.
char *const buf
buffer in which text is saved
SmallMap< int, QueryString * > querystrings
QueryString associated to WWT_EDITBOX widgets.
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.
void NetworkReInitChatBoxSize()
Initialize all font-dependent chat box sizes.
static NetworkRecvStatus SendChat(NetworkAction action, DestType type, int dest, const std::string &msg, int64 data)
Send a chat-packet over the network.
@ ICONSOLE_FULL
In-game console is closed.
EventState OnKeyPress(WChar key, uint16 keycode) override
A key has been pressed.
High level window description.
std::string message
The action message.
static NetworkClientInfo * GetByClientID(ClientID client_id)
Return the CI given it's client-identifier.
static size_t GetPoolSize()
Returns first unused index.
TextColour colour
The colour of the message.
static bool _chatmessage_visible
Is a chat message visible.
static const int DRAW_STRING_BUFFER
Size of the buffer used for drawing strings.
void SetStringParameters(int widget) const override
Initialize string parameters for a widget.
@ SA_TOP
Top align the text.
int height
Height of the window (number of pixels down in y direction)
Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number) override
Compute the initial position of the window.
void SetDirty() const
Mark entire window as dirty (in need of re-paint)
ClientID
'Unique' identifier to be given to clients
@ ES_NOT_HANDLED
The passed event is not handled.
void NetworkDrawChatMessage()
Draw the chat message-box.
@ TC_IS_PALETTE_COLOUR
Colour value is already a real palette colour index, not an index of a StringColour.
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
@ SA_FORCE
Force the alignment, i.e. don't swap for RTL languages.
static bool _chat_tab_completion_active
Whether tab completion is active.
@ CLIENT_ID_SERVER
Servers always have this ID.
static void SendChat(const std::string &buf, DestType type, int dest)
Send an actual chat message.
Coordinates of a point in 2D.
void NetworkUndrawChatMessage()
Hide the chatbox.
bool SetFocusedWidget(int widget_index)
Set focus within this window to the given widget.
WindowNumber window_number
Window number within the window class.
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.
static VideoDriver * GetInstance()
Get the currently active instance of the video driver.
@ WC_NONE
No window, redirects to WC_MAIN_WINDOW.
uint32 StringID
Numeric value that represents a string, independent of the selected language.
uint16 bytes
the current size of the string in bytes (including terminating '\0')
int cancel_button
Widget button of parent window to simulate when pressing CANCEL in OSK.
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
static const uint NETWORK_CHAT_LINE_SPACING
The draw buffer must be able to contain the chat message, client name and the "[All]" message,...
static bool _chatmessage_dirty
Does the chat message need repainting?
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...
void OnInvalidateData(int data=0, bool gui_scope=true) override
Some data on this window has become invalid.
void CDECL NetworkAddChatMessage(TextColour colour, uint duration, const std::string &message)
Add a text message to the 'chat window' to be shown.
#define FONT_HEIGHT_NORMAL
Height of characters in the normal (FS_NORMAL) font.
const char * ChatTabCompletionNextItem(uint *item)
Find the next item of the list of things that can be auto-completed.
void CloseWindowByClass(WindowClass cls)
Close all windows of a given class.
static std::chrono::steady_clock::time_point _chatmessage_dirty_time
Time the chat history was marked dirty.
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.
void OnEditboxChanged(int wid) override
The text in an editbox has been edited.
void FindWindowPlacementAndResize(int def_width, int def_height) override
Resize window towards the default size.
void Close() override
Hide the window and all its child windows, and mark them for a later deletion.
EventState
State of handling an event.
static uint MAX_CHAT_MESSAGES
The limit of chat messages to show.
Window * FindWindowByClass(WindowClass cls)
Find any window by its class.
void NetworkChatMessageLoop()
Check if a message is expired.
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
void FinishInitNested(WindowNumber window_number=0)
Perform the second part of the initialization of a nested widget tree.
static bool HaveChatMessages(bool show_all)
Test if there are any chat messages to display.
void ChatTabCompletion()
See if we can auto-complete the current text of the user.
static T * ReallocT(T *t_ptr, size_t num_elements)
Simplified reallocation function that allocates the specified number of elements of the given type.
void OnClick(Point pt, int widget, int click_count) override
A click with the left mouse button has been made on the window.
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
void ShowNetworkChatQueryWindow(DestType type, int dest)
Show the chat window.
@ SA_LEFT
Left align the text.
uint16 network_chat_box_width_pct
width of the chat box in percent
@ WC_SEND_NETWORK_MSG
Chatbox; Window numbers:
virtual void CopyFromBuffer(void *video, const void *src, int width, int height)=0
Copy from a buffer to the screen.
static char * ChatTabCompletionFindText(char *buf)
Find what text to complete.
#define lengthof(x)
Return the length of an fixed size array.
@ WDP_MANUAL
Manually align the window (so no automatic location finding)
static std::deque< ChatMessage > _chatmsg_list
The actual chat message list.
QueryString message_editbox
Message editbox.
Specification of a rectangle with an absolute top-left coordinate and a (relative) width/height.
static const NWidgetPart _nested_chat_window_widgets[]
The widgets of the chat window.
int PositionNetworkChatWindow(Window *w)
(Re)position network chat window at the screen.
@ DESTTYPE_CLIENT
Send message/notice to only a certain client (Private)
Data structure for an opened window.
@ WC_STATUS_BAR
Statusbar (at the bottom of your screen); Window numbers:
@ WC_NEWS_WINDOW
News window; Window numbers:
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
static const uint MAX_CLIENT_SLOTS
The number of slots; must be at least 1 more than MAX_CLIENTS.
static PointDimension _chatmsg_box
The chatbox grows from the bottom so the coordinates are pixels from the left and pixels from the bot...
#define lastof(x)
Get the last element of an fixed size array.
uint8 network_chat_box_height
height of the chat box in lines
virtual int GetBytesPerPixel()=0
Get how many bytes are needed to store a pixel.
Container for all information known about a client.
void SetDParamStr(uint n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
std::chrono::steady_clock::time_point remove_time
The time to remove the message.
GUISettings gui
settings related to the GUI
virtual void FindWindowPlacementAndResize(int def_width, int def_height)
Resize window towards the default size.
Helper/buffer for input fields.
virtual void Close()
Hide the window and all its child windows, and mark them for a later deletion.
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.