OpenTTD Source 20250312-master-gcdcc6b491d
error_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 "stdafx.h"
12#include "core/mem_func.hpp"
13#include "landscape.h"
14#include "newgrf_text.h"
15#include "error.h"
16#include "viewport_func.h"
17#include "gfx_func.h"
18#include "string_func.h"
19#include "company_base.h"
20#include "company_func.h"
22#include "strings_func.h"
23#include "zoom_func.h"
24#include "window_func.h"
25#include "console_func.h"
26#include "window_gui.h"
27#include "timer/timer.h"
28#include "timer/timer_window.h"
29
31
32#include "table/strings.h"
33
34#include "safeguards.h"
35
36static constexpr NWidgetPart _nested_errmsg_widgets[] = {
38 NWidget(WWT_CLOSEBOX, COLOUR_RED),
39 NWidget(WWT_CAPTION, COLOUR_RED, WID_EM_CAPTION), SetStringTip(STR_ERROR_MESSAGE_CAPTION),
41 NWidget(WWT_PANEL, COLOUR_RED),
42 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_EM_MESSAGE), SetPadding(WidgetDimensions::unscaled.modalpopup), SetFill(1, 0), SetMinimalSize(236, 0),
44};
45
46static WindowDesc _errmsg_desc(
47 WDP_MANUAL, nullptr, 0, 0,
49 {},
50 _nested_errmsg_widgets
51);
52
53static constexpr NWidgetPart _nested_errmsg_face_widgets[] = {
55 NWidget(WWT_CLOSEBOX, COLOUR_RED),
58 NWidget(WWT_PANEL, COLOUR_RED),
60 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_EM_FACE), SetPadding(2, 0, 2, 2), SetFill(0, 1), SetMinimalSize(92, 119),
61 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_EM_MESSAGE), SetPadding(WidgetDimensions::unscaled.modalpopup), SetFill(1, 1), SetMinimalSize(236, 0),
64};
65
66static WindowDesc _errmsg_face_desc(
67 WDP_MANUAL, nullptr, 0, 0,
69 {},
70 _nested_errmsg_face_widgets
71);
72
82ErrorMessageData::ErrorMessageData(EncodedString &&summary_msg, EncodedString &&detailed_msg, bool is_critical, int x, int y, EncodedString &&extra_msg, CompanyID company) :
83 is_critical(is_critical),
84 summary_msg(std::move(summary_msg)),
85 detailed_msg(std::move(detailed_msg)),
86 extra_msg(std::move(extra_msg)),
87 position(x, y),
88 company(company)
89{
90 assert(!this->summary_msg.empty());
91}
92
97
100private:
101 uint height_summary = 0;
103 uint height_extra = 0;
104 TimeoutTimer<TimerWindow> display_timeout;
105
106public:
107 ErrmsgWindow(const ErrorMessageData &data) :
108 Window(data.HasFace() ? _errmsg_face_desc : _errmsg_desc),
109 ErrorMessageData(data),
110 display_timeout(std::chrono::seconds(3 * _settings_client.gui.errmsg_duration), [this]() {
111 this->Close();
112 })
113 {
114 this->InitNested();
115
116 /* Only start the timeout if the message is not critical. */
117 if (!this->is_critical) {
118 this->display_timeout.Reset();
119 }
120 }
121
123 {
124 switch (widget) {
125 case WID_EM_MESSAGE: {
126 this->height_summary = GetStringHeight(this->summary_msg.GetDecodedString(), size.width);
127 this->height_detailed = (this->detailed_msg.empty()) ? 0 : GetStringHeight(this->detailed_msg.GetDecodedString(), size.width);
128 this->height_extra = (this->extra_msg.empty()) ? 0 : GetStringHeight(this->extra_msg.GetDecodedString(), size.width);
129
130 uint panel_height = this->height_summary;
131 if (!this->detailed_msg.empty()) panel_height += this->height_detailed + WidgetDimensions::scaled.vsep_wide;
132 if (!this->extra_msg.empty()) panel_height += this->height_extra + WidgetDimensions::scaled.vsep_wide;
133
134 size.height = std::max(size.height, panel_height);
135 break;
136 }
137 case WID_EM_FACE:
138 size = maxdim(size, GetScaledSpriteSize(SPR_GRADIENT));
139 break;
140 }
141 }
142
144 {
145 /* Position (0, 0) given, center the window. */
146 if (this->position.x == 0 && this->position.y == 0) {
147 Point pt = {(_screen.width - sm_width) >> 1, (_screen.height - sm_height) >> 1};
148 return pt;
149 }
150
151 constexpr int distance_to_cursor = 200;
152
153 /* Position the error window just above the cursor. This makes the
154 * error window clearly visible, without being in the way of what
155 * the user is doing. */
156 Point pt;
157 pt.x = _cursor.pos.x - sm_width / 2;
158 pt.y = _cursor.pos.y - (distance_to_cursor + sm_height);
159
160 if (pt.y < GetMainViewTop()) {
161 /* Window didn't fit above cursor, so place it below. */
162 pt.y = _cursor.pos.y + distance_to_cursor;
163 }
164
165 return pt;
166 }
167
173 void OnInvalidateData([[maybe_unused]] int data = 0, [[maybe_unused]] bool gui_scope = true) override
174 {
175 /* If company gets shut down, while displaying an error about it, remove the error message. */
176 if (this->company != CompanyID::Invalid() && !Company::IsValidID(this->company)) this->Close();
177 }
178
179 std::string GetWidgetString(WidgetID widget, StringID stringid) const override
180 {
181 if (widget == WID_EM_CAPTION && this->company != CompanyID::Invalid()) return GetString(STR_ERROR_MESSAGE_CAPTION_OTHER_COMPANY, this->company);
182
183 return this->Window::GetWidgetString(widget, stringid);
184 }
185
186 void DrawWidget(const Rect &r, WidgetID widget) const override
187 {
188 switch (widget) {
189 case WID_EM_FACE: {
190 const Company *c = Company::Get(this->company);
192 break;
193 }
194
195 case WID_EM_MESSAGE:
196 if (this->detailed_msg.empty()) {
197 DrawStringMultiLine(r, this->summary_msg.GetDecodedString(), TC_FROMSTRING, SA_CENTER);
198 } else if (this->extra_msg.empty()) {
199 /* Extra space when message is shorter than company face window */
200 int extra = (r.Height() - this->height_summary - this->height_detailed - WidgetDimensions::scaled.vsep_wide) / 2;
201
202 /* Note: NewGRF supplied error message often do not start with a colour code, so default to white. */
203 DrawStringMultiLine(r.WithHeight(this->height_summary + extra, false), this->summary_msg.GetDecodedString(), TC_WHITE, SA_CENTER);
204 DrawStringMultiLine(r.WithHeight(this->height_detailed + extra, true), this->detailed_msg.GetDecodedString(), TC_WHITE, SA_CENTER);
205 } else {
206 /* Extra space when message is shorter than company face window */
207 int extra = (r.Height() - this->height_summary - this->height_detailed - this->height_extra - (WidgetDimensions::scaled.vsep_wide * 2)) / 3;
208
209 /* Note: NewGRF supplied error message often do not start with a colour code, so default to white. */
210 Rect top_section = r.WithHeight(this->height_summary + extra, false);
211 Rect bottom_section = r.WithHeight(this->height_extra + extra, true);
216 }
217
218 break;
219
220 default:
221 break;
222 }
223 }
224
225 void OnMouseLoop() override
226 {
227 /* Disallow closing the window too easily, if timeout is disabled */
228 if (_right_button_down && !this->is_critical) this->Close();
229 }
230
231 void Close([[maybe_unused]] int data = 0) override
232 {
235 this->Window::Close();
236 }
237
243 {
244 return this->is_critical;
245 }
246};
247
252{
254 _error_list.clear();
255}
256
259{
261 if (!_error_list.empty()) {
262 new ErrmsgWindow(_error_list.front());
263 _error_list.pop_front();
264 }
265}
266
273{
274 ErrmsgWindow *w = dynamic_cast<ErrmsgWindow *>(FindWindowById(WC_ERRMSG, 0));
275 if (_window_system_initialized && w != nullptr) {
276 if (w->IsCritical()) _error_list.push_front(*w);
278 w->Close();
279 }
280}
281
290void ShowErrorMessage(EncodedString &&summary_msg, int x, int y, const CommandCost &cc)
291{
292 EncodedString error = std::move(cc.GetEncodedMessage());
293 if (error.empty()) error = GetEncodedStringIfValid(cc.GetErrorMessage());
294
295 ShowErrorMessage(std::move(summary_msg), std::move(error), WL_INFO, x, y,
297}
298
308void ShowErrorMessage(EncodedString &&summary_msg, EncodedString &&detailed_msg, WarningLevel wl, int x, int y, EncodedString &&extra_msg, CompanyID company)
309{
310 if (wl != WL_INFO) {
311 /* Print message to console */
312
313 std::string message = summary_msg.GetDecodedString();
314 if (!detailed_msg.empty()) {
315 message += " ";
316 message += detailed_msg.GetDecodedString();
317 }
318 if (!extra_msg.empty()) {
319 message += " ";
320 message += extra_msg.GetDecodedString();
321 }
322
323 IConsolePrint(wl == WL_WARNING ? CC_WARNING : CC_ERROR, message);
324 }
325
326 bool is_critical = wl == WL_CRITICAL;
327
328 if (_game_mode == GM_BOOTSTRAP) return;
329 if (_settings_client.gui.errmsg_duration == 0 && !is_critical) return;
330
331 ErrorMessageData data(std::move(summary_msg), std::move(detailed_msg), is_critical, x, y, std::move(extra_msg), company);
332
333 ErrmsgWindow *w = dynamic_cast<ErrmsgWindow *>(FindWindowById(WC_ERRMSG, 0));
334 if (w != nullptr) {
335 if (w->IsCritical()) {
336 /* A critical error is currently shown. */
337 if (wl == WL_CRITICAL) {
338 /* Push another critical error in the queue of errors,
339 * but do not put other errors in the queue. */
340 _error_list.push_back(data);
341 }
342 return;
343 }
344 /* A non-critical error was shown. */
345 w->Close();
346 }
347 new ErrmsgWindow(data);
348}
349
350
356{
357 ErrmsgWindow *w = dynamic_cast<ErrmsgWindow *>(FindWindowById(WC_ERRMSG, 0));
358 if (w == nullptr) return false;
359 w->Close();
360 return true;
361}
362
369{
370 _error_list.splice(_error_list.end(), datas);
371}
372
379{
380 _error_list.push_back(data);
381}
Common return value for all commands.
static EncodedString & GetEncodedMessage()
Get the last encoded error message.
StringID GetErrorMessage() const
Returns the error message of a command.
CompanyID GetErrorOwner() const
Get the originator owner for this error.
StringID GetExtraErrorMessage() const
Returns the extra error message of a command.
Container for an encoded string, created by GetEncodedString.
std::string GetDecodedString() const
Decode the encoded string.
Definition strings.cpp:219
The data of the error message.
Definition error.h:31
ErrorMessageData(EncodedString &&summary_msg, EncodedString &&detailed_msg, bool is_critical=false, int x=0, int y=0, EncodedString &&extra_msg={}, CompanyID company=CompanyID::Invalid())
Display an error message in a window.
Definition error_gui.cpp:82
bool HasFace() const
Check whether error window shall display a company manager face.
Definition error.h:44
CompanyID company
Company belonging to the face being shown. CompanyID::Invalid() if no face present.
Definition error.h:38
Point position
Position of the error message window.
Definition error.h:37
EncodedString detailed_msg
Detailed error message showed in second line. Can be INVALID_STRING_ID.
Definition error.h:35
EncodedString extra_msg
Extra error message shown in third line. Can be INVALID_STRING_ID.
Definition error.h:36
EncodedString summary_msg
General error message showed in first line. Must be valid.
Definition error.h:34
bool is_critical
Whether the error message is critical.
Definition error.h:33
A timeout timer will fire once after the interval.
Definition timer.h:116
void Reset()
Reset the timer, so it will fire again after the timeout.
Definition timer.h:140
static WidgetDimensions scaled
Widget dimensions scaled for current zoom level.
Definition window_gui.h:29
int vsep_wide
Wide vertical spacing.
Definition window_gui.h:60
static const WidgetDimensions unscaled
Unscaled widget dimensions.
Definition window_gui.h:94
Definition of stuff that is very close to a company, like the company struct itself.
Functions related to companies.
void DrawCompanyManagerFace(CompanyManagerFace cmf, Colours colour, const Rect &r)
Draws the face of a company manager's face.
Functionality related to the company manager's face.
void IConsolePrint(TextColour colour_code, const std::string &string)
Handle the printing of text entered into the console or redirected there by any other means.
Definition console.cpp:89
Console functions used outside of the console code.
static const TextColour CC_WARNING
Colour for warning lines.
static const TextColour CC_ERROR
Colour for error lines.
Functions related to errors.
std::list< ErrorMessageData > ErrorList
Define a queue with errors.
Definition error.h:48
WarningLevel
Message severity/type.
Definition error.h:23
@ WL_WARNING
Other information.
Definition error.h:25
@ WL_CRITICAL
Critical errors, the MessageBox is shown in all cases.
Definition error.h:27
@ WL_INFO
Used for DoCommand-like (and some non-fatal AI GUI) errors/information.
Definition error.h:24
static ErrorList _error_list
The actual queue with errors.
Definition error_gui.cpp:94
void UnshowCriticalError()
Unshow the critical error.
void ClearErrorMessages()
Clear all errors from the queue.
void ScheduleErrorMessage(ErrorList &datas)
Schedule a list of errors.
bool _window_system_initialized
Whether the window system is initialized or not.
Definition error_gui.cpp:96
bool HideActiveErrorMessage()
Close active error message window.
void ShowFirstError()
Show the first error of the queue.
void ShowErrorMessage(EncodedString &&summary_msg, int x, int y, const CommandCost &cc)
Display an error message in a window.
Types related to the error widgets.
@ WID_EM_CAPTION
Caption of the window.
@ WID_EM_MESSAGE
Error message.
@ WID_EM_FACE
Error title.
Dimension maxdim(const Dimension &d1, const Dimension &d2)
Compute bounding box of both dimensions.
Geometry functions.
int GetStringHeight(std::string_view str, int maxw, FontSize fontsize)
Calculates height of string (in pixels).
Definition gfx.cpp:705
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.
Definition gfx.cpp:775
bool _right_button_down
Is right mouse button pressed?
Definition gfx.cpp:43
Functions related to the gfx engine.
Dimension GetScaledSpriteSize(SpriteID sprid)
Scale sprite size for GUI.
Definition widget.cpp:67
@ SA_CENTER
Center both horizontally and vertically.
Definition gfx_type.h:385
constexpr NWidgetPart SetFill(uint16_t fill_x, uint16_t fill_y)
Widget part function for setting filling.
constexpr NWidgetPart SetPadding(uint8_t top, uint8_t right, uint8_t bottom, uint8_t left)
Widget part function for setting additional space around a widget.
constexpr NWidgetPart SetStringTip(StringID string, StringID tip={})
Widget part function for setting the string and tooltip.
constexpr NWidgetPart SetMinimalSize(int16_t x, int16_t y)
Widget part function for setting the minimal size.
constexpr NWidgetPart NWidget(WidgetType tp, Colours col, WidgetID idx=-1)
Widget part function for starting a new 'real' widget.
constexpr NWidgetPart EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME,...
Functions related to OTTD's landscape.
Functions related to memory operations.
Header of Action 04 "universal holder" structure and functions.
A number of safeguards to prevent using unsafe methods.
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:57
Definition of base types and functions in a cross-platform compatible way.
Functions related to low-level strings.
std::string GetString(StringID string)
Resolve the given StringID into a std::string with formatting but no parameters.
Definition strings.cpp:426
Functions related to OTTD's strings.
static EncodedString GetEncodedStringIfValid(StringID str)
Encode a string with no parameters into an encoded string, if the string id is valid.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
GUISettings gui
settings related to the GUI
Colours colour
Company colour.
CompanyManagerFace face
Face description of the president.
Point pos
logical mouse position
Definition gfx_type.h:124
Dimensions (a width and height) of a rectangle in 2D.
Window class for displaying an error message window.
Definition error_gui.cpp:99
std::string GetWidgetString(WidgetID widget, StringID stringid) const override
Get the raw string for a widget.
bool IsCritical()
Check whether the currently shown error message was critical or not.
void OnInvalidateData(int data=0, bool gui_scope=true) override
Some data on this window has become invalid.
void Close(int data=0) override
Hide the window and all its child windows, and mark them for a later deletion.
uint height_extra
Height of the extra_msg string in pixels in the WID_EM_MESSAGE widget.
void OnMouseLoop() override
Called for every mouse loop run, which is at least once per (game) tick.
Point OnInitialPosition(int16_t sm_width, int16_t sm_height, int window_number) override
Compute the initial position of the window.
uint height_detailed
Height of the detailed_msg string in pixels in the WID_EM_MESSAGE widget.
void DrawWidget(const Rect &r, WidgetID widget) const override
Draw the contents of a nested widget.
uint height_summary
Height of the summary_msg string in pixels in the WID_EM_MESSAGE widget.
void UpdateWidgetSize(WidgetID widget, Dimension &size, const Dimension &padding, Dimension &fill, Dimension &resize) override
Update size and resize step of a widget in the window.
uint8_t errmsg_duration
duration of error message
Partial widget specification to allow NWidgets to be written nested.
Coordinates of a point in 2D.
static Titem * Get(auto index)
Returns Titem with given index.
static bool IsValidID(auto index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Specification of a rectangle with absolute coordinates of all edges.
Rect WithHeight(int height, bool end=false) const
Copy Rect and set its height.
int Height() const
Get height of Rect.
High level window description.
Definition window_gui.h:168
Data structure for an opened window.
Definition window_gui.h:274
virtual void Close(int data=0)
Hide the window and all its child windows, and mark them for a later deletion.
Definition window.cpp:1050
virtual std::string GetWidgetString(WidgetID widget, StringID stringid) const
Get the raw string for a widget.
Definition window.cpp:502
ResizeInfo resize
Resize information.
Definition window_gui.h:315
const NWID * GetWidget(WidgetID widnum) const
Get the nested widget with number widnum from the nested widget tree.
Definition window_gui.h:973
void InitNested(WindowNumber number=0)
Perform complete initialization of the Window with nested widgets, to allow use.
Definition window.cpp:1749
WindowNumber window_number
Window number within the window class.
Definition window_gui.h:303
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:95
Definition of Interval and OneShot timers.
Definition of the Window system.
void SetRedErrorSquare(TileIndex tile)
Set a tile to display a red error square.
Functions related to (drawing on) viewports.
@ NWID_HORIZONTAL
Horizontal container.
Definition widget_type.h:65
@ WWT_PANEL
Simple depressed panel.
Definition widget_type.h:40
@ WWT_CAPTION
Window caption (window title between closebox and stickybox)
Definition widget_type.h:51
@ WWT_CLOSEBOX
Close box (at top-left of a window)
Definition widget_type.h:59
@ WWT_EMPTY
Empty widget, place holder to reserve space in widget tree.
Definition widget_type.h:38
int GetMainViewTop()
Return the top of the main view available for general use.
Definition window.cpp:2065
Window * FindWindowById(WindowClass cls, WindowNumber number)
Find a window by its class and window number.
Definition window.cpp:1101
Window functions not directly related to making/drawing windows.
Functions, definitions and such used only by the GUI.
@ WDP_MANUAL
Manually align the window (so no automatic location finding)
Definition window_gui.h:144
int WidgetID
Widget ID.
Definition window_type.h:20
@ WC_ERRMSG
Error message; Window numbers:
@ WC_NONE
No window, redirects to WC_MAIN_WINDOW.
Definition window_type.h:47
Functions related to zooming.