OpenTTD Source
20240917-master-g9ab0a47812
|
Go to the documentation of this file.
10 #include "../stdafx.h"
11 #include "../strings_func.h"
12 #include "../autocompletion.h"
13 #include "../blitter/factory.hpp"
14 #include "../console_func.h"
15 #include "../video/video_driver.hpp"
16 #include "../querystring_gui.h"
18 #include "../window_func.h"
19 #include "../toolbar_gui.h"
20 #include "../core/geometry_func.hpp"
21 #include "../zoom_func.h"
22 #include "../timer/timer.h"
23 #include "../timer/timer_window.h"
28 #include "../widgets/network_chat_widget.h"
30 #include "table/strings.h"
32 #include "../safeguards.h"
72 auto now = std::chrono::steady_clock::now();
74 if (cmsg.remove_time >= now)
return true;
95 cmsg->
remove_time = std::chrono::steady_clock::now() + std::chrono::seconds(duration);
137 _cursor.draw_pos.y <= _screen.height -
_chatmsg_box.y) {
148 height = std::max(height + y, std::min(
_chatmsg_box.height, _screen.height));
151 if (x + width >= _screen.width) {
152 width = _screen.width - x;
154 if (width <= 0 || height <= 0)
return;
169 auto now = std::chrono::steady_clock::now();
187 bool show_all = (w !=
nullptr);
202 height = std::max(height + y, std::min(
_chatmsg_box.height, _screen.height));
205 if (x + width >= _screen.width) {
206 width = _screen.width - x;
208 if (width <= 0 || height <= 0)
return;
216 auto now = std::chrono::steady_clock::now();
217 int string_height = 0;
219 if (!show_all && cmsg.remove_time < now)
continue;
226 int top = _screen.height -
_chatmsg_box.y - string_height - 2;
234 int ypos = bottom - 2;
237 if (!show_all && cmsg.remove_time < now)
continue;
239 if (ypos < top)
break;
257 if (buf.empty())
return;
267 using AutoCompletion::AutoCompletion;
270 std::vector<std::string> GetSuggestions([[maybe_unused]] std::string_view
prefix, std::string_view
query)
override
272 std::vector<std::string> suggestions;
274 if (ci->client_name.starts_with(
query)) {
275 suggestions.push_back(ci->client_name);
281 std::string town_name =
GetString(STR_TOWN_NAME);
282 if (town_name.starts_with(
query)) {
283 suggestions.push_back(std::move(town_name));
289 void ApplySuggestion(std::string_view
prefix, std::string_view suggestion)
override
293 this->textbuf->
Assign(fmt::format(
"{}: ", suggestion));
295 this->textbuf->
Assign(fmt::format(
"{}{} ",
prefix, suggestion));
322 static const StringID chat_captions[] = {
323 STR_NETWORK_CHAT_ALL_CAPTION,
324 STR_NETWORK_CHAT_COMPANY_CAPTION,
325 STR_NETWORK_CHAT_CLIENT_CAPTION
327 assert((uint)this->dtype <
lengthof(chat_captions));
339 void Close([[maybe_unused]]
int data = 0)
override
345 void FindWindowPlacementAndResize([[maybe_unused]]
int def_width, [[maybe_unused]]
int def_height)
override
355 if (this->chat_tab_completion.AutoComplete()) {
360 Point OnInitialPosition([[maybe_unused]] int16_t sm_width, [[maybe_unused]] int16_t sm_height, [[maybe_unused]]
int window_number)
override
366 void SetStringParameters(
WidgetID widget)
const override
375 void OnClick([[maybe_unused]]
Point pt,
WidgetID widget, [[maybe_unused]]
int click_count)
override
379 SendChat(this->message_editbox.text.
buf, this->dtype, this->dest);
388 EventState OnKeyPress([[maybe_unused]] char32_t key, uint16_t keycode)
override
391 if (keycode == WKC_TAB) {
398 void OnEditboxChanged(
WidgetID widget)
override
401 this->chat_tab_completion.Reset();
410 void OnInvalidateData([[maybe_unused]]
int data = 0, [[maybe_unused]]
bool gui_scope =
true)
override
412 if (data == this->dest) this->Close();
422 NWidget(
WWT_TEXT, COLOUR_GREY,
WID_NC_DESTINATION),
SetMinimalSize(62, 12),
SetPadding(1, 0, 1, 0),
SetAlignment(
SA_VERT_CENTER |
SA_RIGHT),
SetDataTip(STR_NULL, STR_NULL),
423 NWidget(
WWT_EDITBOX, COLOUR_GREY,
WID_NC_TEXTBOX),
SetMinimalSize(100, 0),
SetPadding(1, 0, 1, 0),
SetResize(1, 0),
424 SetDataTip(STR_NETWORK_CHAT_OSKTITLE, STR_NULL),
425 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.
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-...
void CloseWindowByClass(WindowClass cls, int data)
Close all windows of a given class.
int ok_button
Widget button of parent window to simulate when pressing OK in OSK.
Window to enter the chat message in.
int dest
The identifier of the destination.
How all blitters should look like.
bool visible
cursor is visible
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
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.
virtual void MakeDirty(int left, int top, int width, int height)=0
Mark a particular area dirty.
static WindowDesc _chat_window_desc(WDP_MANUAL, nullptr, 0, 0, WC_SEND_NETWORK_MSG, WC_NONE, 0, _nested_chat_window_widgets)
The description of the chat window.
void NetworkInitChatMessage()
Initialize all buffers of the chat visualisation.
virtual void Close(int data=0)
Hide the window and all its child windows, and mark them for a later deletion.
static const uint NETWORK_CHAT_LENGTH
The maximum length of a chat message, in bytes including '\0'.
static NetworkRecvStatus SendChat(NetworkAction action, DestType type, int dest, const std::string &msg, int64_t data)
Send a chat-packet over the network.
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...
DestType
Destination of our chat messages.
@ SA_BOTTOM
Bottom align the text.
ClientSettings _settings_client
The current settings for this game.
T * Allocate(size_t count)
Get buffer of at least count times T.
std::string_view prefix
Prefix of the text before the last space.
@ SA_RIGHT
Right align the text (must be a single bit).
@ SA_VERT_CENTER
Vertically center the text.
void NetworkServerSendChat(NetworkAction action, DestType type, int dest, const std::string &msg, ClientID from_id, int64_t data=0, bool from_admin=false)
Send an actual chat message.
Point draw_size
position and size bounding-box for drawing
void OnInvalidateData([[maybe_unused]] int data=0, [[maybe_unused]] bool gui_scope=true) override
Some data on this window has become invalid.
Data stored about a string that can be modified in the GUI.
@ DESTTYPE_CLIENT
Send message/notice to only a certain client (Private)
int GetStringLineCount(StringID str, int maxw)
Calculates number of lines of string.
char *const buf
buffer in which text is saved
void NetworkReInitChatBoxSize()
Initialize all font-dependent chat box sizes.
@ ICONSOLE_FULL
In-game console is opened, whole screen.
High level window description.
std::string message
The action message.
int ScaleGUITrad(int value)
Scale traditional pixel dimensions to GUI zoom level.
static NetworkClientInfo * GetByClientID(ClientID client_id)
Return the CI given it's client-identifier.
std::string_view query
Last token of the text. This is used to based the suggestions on.
TextColour colour
The colour of the message.
static bool _chatmessage_visible
Is a chat message visible.
@ FS_NORMAL
Index of the normal font in the font tables.
int height
Height of the window (number of pixels down in y direction)
void SetDirty() const
Mark entire window as dirty (in need of re-paint)
@ ES_NOT_HANDLED
The passed event is not handled.
static ReusableBuffer< uint8_t > _chatmessage_backup
Backup in case text is moved.
void NetworkDrawChatMessage()
Draw the chat message-box.
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.
NetworkChatWindow(WindowDesc &desc, DestType type, int dest)
Create a chat input window.
#define lengthof(array)
Return the length of an fixed size array.
uint8_t network_chat_box_height
height of the chat box in lines
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.
static IntervalTimer< TimerWindow > network_message_expired_interval(std::chrono::seconds(1), [](auto) { auto now=std::chrono::steady_clock::now();for(auto &cmsg :_chatmsg_list) { if(now > cmsg.remove_time &&_chatmessage_dirty_time< cmsg.remove_time) { _chatmessage_dirty_time=now;_chatmessage_dirty=true;break;} } })
Check if a message is expired on a regular interval.
WindowNumber window_number
Window number within the window class.
bool SetFocusedWidget(WidgetID widget_index)
Set focus within this window to the given widget.
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.
std::map< WidgetID, QueryString * > querystrings
QueryString associated to WWT_EDITBOX widgets.
int cancel_button
Widget button of parent window to simulate when pressing CANCEL in OSK.
int DrawStringMultiLine(int left, int right, int top, int bottom, std::string_view str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly over multiple lines.
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
void CreateNestedTree()
Perform the first part of the initialization of a nested widget tree.
static const uint NETWORK_CHAT_LINE_SPACING
Spacing between chat lines.
static bool _chatmessage_dirty
Does the chat message need repainting?
ClientID
'Unique' identifier to be given to clients
const T * GetBuffer() const
Get the currently allocated buffer.
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 CDECL NetworkAddChatMessage(TextColour colour, uint duration, const std::string &message)
Add a text message to the 'chat window' to be shown.
void SetDParam(size_t n, uint64_t v)
Set a string parameter v at index n in the global string parameter array.
static std::chrono::steady_clock::time_point _chatmessage_dirty_time
Time the chat history was marked dirty.
static constexpr NWidgetPart _nested_chat_window_widgets[]
The widgets of the chat window.
std::string GetString(StringID string)
Resolve the given StringID into a std::string with all the associated DParam lookups and formatting.
EventState
State of handling an event.
NetworkChatAutoCompletion chat_tab_completion
Holds the state and logic of auto-completion of player names and towns on Tab press.
static uint MAX_CHAT_MESSAGES
The limit of chat messages to show.
Window * FindWindowByClass(WindowClass cls)
Find any window by its class.
void SetDParamStr(size_t n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
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.
void ShowNetworkChatQueryWindow(DestType type, int dest)
Show the chat window.
@ SA_LEFT
Left align the text.
@ 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.
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
@ 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.
int PositionNetworkChatWindow(Window *w)
(Re)position network chat window at the screen.
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 PointDimension _chatmsg_box
The chatbox grows from the bottom so the coordinates are pixels from the left and pixels from the bot...
@ CLIENT_ID_SERVER
Servers always have this ID.
Container for all information known about a client.
std::chrono::steady_clock::time_point remove_time
The time to remove the message.
GUISettings gui
settings related to the GUI
uint16_t network_chat_box_width_pct
width of the chat box in percent
virtual void FindWindowPlacementAndResize(int def_width, int def_height)
Resize window towards the default size.