|
OpenTTD Source
14.0-beta3
|
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"
20 #include "../zoom_func.h"
21 #include "../timer/timer.h"
22 #include "../timer/timer_window.h"
27 #include "../widgets/network_chat_widget.h"
29 #include "table/strings.h"
31 #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;
285 static const StringID chat_captions[] = {
286 STR_NETWORK_CHAT_ALL_CAPTION,
287 STR_NETWORK_CHAT_COMPANY_CAPTION,
288 STR_NETWORK_CHAT_CLIENT_CAPTION
290 assert((uint)this->dtype <
lengthof(chat_captions));
303 void Close([[maybe_unused]]
int data = 0)
override
309 void FindWindowPlacementAndResize([[maybe_unused]]
int def_width, [[maybe_unused]]
int def_height)
override
327 return ci->client_name;
353 auto it = buf.find_last_of(
' ');
354 if (it == std::string_view::npos)
return buf;
356 std::string_view res = buf.substr(it + 1);
357 buf.remove_suffix(res.size() + 1);
366 static std::string _chat_tab_completion_buf;
368 Textbuf *tb = &this->message_editbox.text;
370 bool second_scan =
false;
380 bool begin_of_line = tb_buf.data() == pre_buf.data();
382 std::optional<std::string> cur_item;
384 std::string_view cur_name = cur_item.value();
391 std::string_view view;
395 view = std::string_view(tb->
buf, (tb->
bytes - 1) - 2);
398 size_t offset = pre_buf.size() + 1;
399 view = std::string_view(tb->
buf + offset, (tb->
bytes - 1) - offset);
403 if (cur_name == view) second_scan =
true;
411 if (tb_buf.size() < cur_name.size() && cur_name.starts_with(tb_buf)) {
413 if (!second_scan) _chat_tab_completion_buf = tb->
buf;
418 this->message_editbox.text.
Assign(fmt::format(
"{}: ", cur_name));
420 this->message_editbox.text.
Assign(fmt::format(
"{} {}", pre_buf, cur_name));
430 this->message_editbox.text.
Assign(_chat_tab_completion_buf);
437 Point OnInitialPosition([[maybe_unused]] int16_t sm_width, [[maybe_unused]] int16_t sm_height, [[maybe_unused]]
int window_number)
override
443 void SetStringParameters(
WidgetID widget)
const override
452 void OnClick([[maybe_unused]]
Point pt,
WidgetID widget, [[maybe_unused]]
int click_count)
override
456 SendChat(this->message_editbox.text.
buf, this->dtype, this->dest);
465 EventState OnKeyPress([[maybe_unused]] char32_t key, uint16_t keycode)
override
468 if (keycode == WKC_TAB) {
475 void OnEditboxChanged(
WidgetID widget)
override
487 void OnInvalidateData([[maybe_unused]]
int data = 0, [[maybe_unused]]
bool gui_scope =
true)
override
489 if (data == this->dest) this->Close();
499 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),
500 NWidget(
WWT_EDITBOX, COLOUR_GREY,
WID_NC_TEXTBOX),
SetMinimalSize(100, 12),
SetPadding(1, 0, 1, 0),
SetResize(1, 0),
501 SetDataTip(STR_NETWORK_CHAT_OSKTITLE, STR_NULL),
502 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.
A reusable buffer that can be used for places that temporary allocate a bit of memory and do that ver...
int dest
The identifier of the destination.
static std::string_view ChatTabCompletionFindText(std::string_view &buf)
Find what text to complete.
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.
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.
std::optional< std::string > ChatTabCompletionNextItem(uint *item)
Find the next item of the list of things that can be auto-completed.
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).
@ 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.
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.
static size_t GetPoolSize()
Returns first unused index.
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)
static WindowDesc _chat_window_desc(__FILE__, __LINE__, WDP_MANUAL, nullptr, 0, 0, WC_SEND_NETWORK_MSG, WC_NONE, 0, std::begin(_nested_chat_window_widgets), std::end(_nested_chat_window_widgets))
The description of the chat window.
void SetDirty() const
Mark entire window as dirty (in need of re-paint)
uint16_t bytes
the current size of the string in bytes (including terminating '\0')
@ 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.
static bool _chat_tab_completion_active
Whether tab completion is active.
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
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.
DestType
Destination of our chat messages.
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.
static uint MAX_CHAT_MESSAGES
The limit of chat messages to show.
Window * FindWindowByClass(WindowClass cls)
Find any window by its class.
@ DESTTYPE_CLIENT
Send message/notice to only a certain client (Private)
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.
#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.
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 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...
@ 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.
Helper/buffer for input fields.