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));
355 if (this->chat_tab_completion.AutoComplete()) {
391 if (keycode == WKC_TAB) {
401 this->chat_tab_completion.Reset();
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),
std::string_view query
Last token of the text. This is used to based the suggestions on.
std::string_view prefix
Prefix of the text before the last space.
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
How all blitters should look like.
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...
virtual void CopyToBuffer(const void *video, void *dst, int width, int height)=0
Copy from the screen to a buffer.
virtual void CopyFromBuffer(void *video, const void *src, int width, int height)=0
Copy from a buffer to the screen.
static NetworkRecvStatus SendChat(NetworkAction action, DestType type, int dest, const std::string &msg, int64_t data)
Send a chat-packet over the network.
An interval timer will fire every interval, and will continue to fire until it is deleted.
A reusable buffer that can be used for places that temporary allocate a bit of memory and do that ver...
const T * GetBuffer() const
Get the currently allocated buffer.
T * Allocate(size_t count)
Get buffer of at least count times T.
virtual void MakeDirty(int left, int top, int width, int height)=0
Mark a particular area dirty.
static VideoDriver * GetInstance()
Get the currently active instance of the video driver.
static const uint NETWORK_CHAT_LENGTH
The maximum length of a chat message, in bytes including '\0'.
@ ICONSOLE_FULL
In-game console is opened, whole screen.
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
int GetStringLineCount(StringID str, int maxw)
Calculates number of lines of string.
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.
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.
@ SA_LEFT
Left align the text.
@ SA_RIGHT
Right align the text (must be a single bit).
@ SA_FORCE
Force the alignment, i.e. don't swap for RTL languages.
@ SA_BOTTOM
Bottom align the text.
@ SA_VERT_CENTER
Vertically center the text.
@ FS_NORMAL
Index of the normal font in the font tables.
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
@ FILLRECT_RECOLOUR
Apply a recolour sprite to the screen content.
void SetDirty() const
Mark entire window as dirty (in need of re-paint)
bool _network_server
network-server is active
Basic functions/variables used all over the place.
Base core network types and some helper functions to access them.
static uint MAX_CHAT_MESSAGES
The limit of chat messages to show.
static void SendChat(const std::string &buf, DestType type, int dest)
Send an actual chat message.
void NetworkDrawChatMessage()
Draw the chat message-box.
static ReusableBuffer< uint8_t > _chatmessage_backup
Backup in case text is moved.
void ShowNetworkChatQueryWindow(DestType type, int dest)
Show the chat window.
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.
static bool HaveChatMessages(bool show_all)
Test if there are any chat messages to display.
void CDECL NetworkAddChatMessage(TextColour colour, uint duration, const std::string &message)
Add a text message to the 'chat window' to be shown.
static constexpr NWidgetPart _nested_chat_window_widgets[]
The widgets of the chat window.
static std::chrono::steady_clock::time_point _chatmessage_dirty_time
Time the chat history was marked dirty.
void NetworkReInitChatBoxSize()
Initialize all font-dependent chat box sizes.
static const uint NETWORK_CHAT_LINE_SPACING
Spacing between chat lines.
void NetworkInitChatMessage()
Initialize all buffers of the chat visualisation.
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 NetworkUndrawChatMessage()
Hide the chatbox.
static PointDimension _chatmsg_box
The chatbox grows from the bottom so the coordinates are pixels from the left and pixels from the bot...
static bool _chatmessage_dirty
Does the chat message need repainting?
static bool _chatmessage_visible
Is a chat message visible.
static std::deque< ChatMessage > _chatmsg_list
The actual chat message list.
Client part of the network protocol.
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.
DestType
Destination of our chat messages.
@ DESTTYPE_CLIENT
Send message/notice to only a certain client (Private)
NetworkAction
Actions that can be used for NetworkTextMessage.
ClientID
'Unique' identifier to be given to clients
@ CLIENT_ID_SERVER
Servers always have this ID.
ClientSettings _settings_client
The current settings for this game.
static const PaletteID PALETTE_TO_TRANSPARENT
This sets the sprite to transparent.
#define lengthof(array)
Return the length of an fixed size array.
void SetDParam(size_t n, uint64_t v)
Set a string parameter v at index n in the global string parameter array.
std::string GetString(StringID string)
Resolve the given StringID into a std::string with all the associated DParam lookups and formatting.
void SetDParamStr(size_t n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
TextColour colour
The colour of the message.
std::chrono::steady_clock::time_point remove_time
The time to remove the message.
std::string message
The action message.
GUISettings gui
settings related to the GUI
bool visible
cursor is visible
Point draw_size
position and size bounding-box for drawing
uint8_t network_chat_box_height
height of the chat box in lines
uint16_t network_chat_box_width_pct
width of the chat box in percent
Window to enter the chat message in.
void FindWindowPlacementAndResize(int def_width, int def_height) override
Resize window towards the default size.
NetworkChatAutoCompletion chat_tab_completion
Holds the state and logic of auto-completion of player names and towns on Tab press.
EventState OnKeyPress(char32_t key, uint16_t keycode) override
A key has been pressed.
NetworkChatWindow(WindowDesc &desc, DestType type, int dest)
Create a chat input window.
void OnEditboxChanged(WidgetID widget) override
The text in an editbox has been edited.
void SetStringParameters(WidgetID widget) const override
Initialize string parameters for a widget.
void OnClick(Point pt, WidgetID widget, int click_count) override
A click with the left mouse button has been made on the window.
void ChatTabCompletion()
See if we can auto-complete the current text of the user.
int dest
The identifier of the destination.
DestType dtype
The type of destination.
void OnInvalidateData(int data=0, bool gui_scope=true) override
Some data on this window has become invalid.
QueryString message_editbox
Message editbox.
Point OnInitialPosition(int16_t sm_width, int16_t sm_height, int window_number) override
Compute the initial position of the window.
void Close(int data=0) override
Hide the window and all its child windows, and mark them for a later deletion.
Container for all information known about a client.
static NetworkClientInfo * GetByClientID(ClientID client_id)
Return the CI given it's client-identifier.
Specification of a rectangle with an absolute top-left coordinate and a (relative) width/height.
Coordinates of a point in 2D.
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Data stored about a string that can be modified in the GUI.
int ok_button
Widget button of parent window to simulate when pressing OK in OSK.
int cancel_button
Widget button of parent window to simulate when pressing CANCEL in OSK.
void Assign(StringID string)
Render a string into the textbuffer.
char *const buf
buffer in which text is saved
High level window description.
Data structure for an opened window.
virtual void Close(int data=0)
Hide the window and all its child windows, and mark them for a later deletion.
void FinishInitNested(WindowNumber window_number=0)
Perform the second part of the initialization of a nested widget tree.
std::map< WidgetID, QueryString * > querystrings
QueryString associated to WWT_EDITBOX widgets.
void CreateNestedTree()
Perform the first part of the initialization of a nested widget tree.
bool SetFocusedWidget(WidgetID widget_index)
Set focus within this window to the given widget.
const NWID * GetWidget(WidgetID widnum) const
Get the nested widget with number widnum from the nested widget tree.
int height
Height of the window (number of pixels down in y direction)
virtual void FindWindowPlacementAndResize(int def_width, int def_height)
Resize window towards the default size.
WindowNumber window_number
Window number within the window class.
int PositionNetworkChatWindow(Window *w)
(Re)position network chat window at the screen.
Window * FindWindowByClass(WindowClass cls)
Find any window by its class.
void CloseWindowByClass(WindowClass cls, int data)
Close all windows of a given class.
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-...
Window * FindWindowById(WindowClass cls, WindowNumber number)
Find a window by its class and window number.
@ WDP_MANUAL
Manually align the window (so no automatic location finding)
EventState
State of handling an event.
@ ES_HANDLED
The passed event is handled.
@ ES_NOT_HANDLED
The passed event is not handled.
@ WC_NEWS_WINDOW
News window; Window numbers:
@ WC_STATUS_BAR
Statusbar (at the bottom of your screen); Window numbers:
@ WC_SEND_NETWORK_MSG
Chatbox; Window numbers:
@ WC_NONE
No window, redirects to WC_MAIN_WINDOW.