OpenTTD Source 20260512-master-g20b387b91f
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "stdafx.h"
12#include "landscape.h"
13#include "newgrf_text.h"
14#include "error.h"
15#include "viewport_func.h"
16#include "gfx_func.h"
17#include "string_func.h"
18#include "company_base.h"
19#include "company_func.h"
21#include "strings_func.h"
22#include "zoom_func.h"
23#include "window_func.h"
24#include "console_func.h"
25#include "window_gui.h"
26#include "timer/timer.h"
27#include "timer/timer_window.h"
28
30
31#include "table/sprites.h"
32#include "table/strings.h"
33
34#include "safeguards.h"
35
36static constexpr std::initializer_list<NWidgetPart> _nested_errmsg_widgets = {
39 NWidget(WWT_CAPTION, Colours::Red, WID_EM_CAPTION), SetStringTip(STR_ERROR_MESSAGE_CAPTION),
44};
45
48 WindowPosition::Manual, {}, 0, 0,
50 {},
51 _nested_errmsg_widgets
52);
53
54static constexpr std::initializer_list<NWidgetPart> _nested_errmsg_face_widgets = {
65};
66
69 WindowPosition::Manual, {}, 0, 0,
71 {},
72 _nested_errmsg_face_widgets
73);
74
87 summary_msg(std::move(summary_msg)),
88 detailed_msg(std::move(detailed_msg)),
89 extra_msg(std::move(extra_msg)),
90 position(x, y),
92{
93 assert(!this->summary_msg.empty());
94}
95
100
102struct ErrmsgWindow : public Window, ErrorMessageData {
103private:
104 uint height_summary = 0;
106 uint height_extra = 0;
107
108 TimeoutTimer<TimerWindow> display_timeout = {std::chrono::seconds(_settings_client.gui.errmsg_duration), [this]() {
109 this->Close();
110 }};
111
112public:
113 ErrmsgWindow(const ErrorMessageData &data) :
115 ErrorMessageData(data)
116 {
117 this->InitNested();
118 }
119
120 void UpdateWidgetSize(WidgetID widget, Dimension &size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension &fill, [[maybe_unused]] Dimension &resize) override
121 {
122 switch (widget) {
123 case WID_EM_MESSAGE: {
124 this->height_summary = GetStringHeight(this->summary_msg.GetDecodedString(), size.width);
125 this->height_detailed = (this->detailed_msg.empty()) ? 0 : GetStringHeight(this->detailed_msg.GetDecodedString(), size.width);
126 this->height_extra = (this->extra_msg.empty()) ? 0 : GetStringHeight(this->extra_msg.GetDecodedString(), size.width);
127
128 uint panel_height = this->height_summary;
129 if (!this->detailed_msg.empty()) panel_height += this->height_detailed + WidgetDimensions::scaled.vsep_wide;
130 if (!this->extra_msg.empty()) panel_height += this->height_extra + WidgetDimensions::scaled.vsep_wide;
131
132 size.height = std::max(size.height, panel_height);
133 break;
134 }
135 case WID_EM_FACE:
136 size = maxdim(size, GetScaledSpriteSize(SPR_GRADIENT));
137 break;
138 }
139 }
140
141 Point OnInitialPosition([[maybe_unused]] int16_t sm_width, [[maybe_unused]] int16_t sm_height, [[maybe_unused]] int window_number) override
142 {
143 /* Position (0, 0) given, center the window. */
144 if (this->position.x == 0 && this->position.y == 0) {
145 Point pt = {(_screen.width - sm_width) >> 1, (_screen.height - sm_height) >> 1};
146 return pt;
147 }
148
149 constexpr int distance_to_cursor = 200;
150
151 /* Position the error window just above the cursor. This makes the
152 * error window clearly visible, without being in the way of what
153 * the user is doing. */
154 Point pt;
155 pt.x = _cursor.pos.x - sm_width / 2;
156 pt.y = _cursor.pos.y - (distance_to_cursor + sm_height);
157
158 if (pt.y < GetMainViewTop()) {
159 /* Window didn't fit above cursor, so place it below. */
160 pt.y = _cursor.pos.y + distance_to_cursor;
161 }
162
163 return pt;
164 }
165
171 void OnInvalidateData([[maybe_unused]] int data = 0, [[maybe_unused]] bool gui_scope = true) override
172 {
173 /* If company gets shut down, while displaying an error about it, remove the error message. */
174 if (this->company != CompanyID::Invalid() && !Company::IsValidID(this->company)) this->Close();
175 }
176
177 std::string GetWidgetString(WidgetID widget, StringID stringid) const override
178 {
179 if (widget == WID_EM_CAPTION && this->company != CompanyID::Invalid()) return GetString(STR_ERROR_MESSAGE_CAPTION_OTHER_COMPANY, this->company);
180
181 return this->Window::GetWidgetString(widget, stringid);
182 }
183
184 void DrawWidget(const Rect &r, WidgetID widget) const override
185 {
186 switch (widget) {
187 case WID_EM_FACE: {
188 const Company *c = Company::Get(this->company);
190 break;
191 }
192
193 case WID_EM_MESSAGE:
194 if (this->detailed_msg.empty()) {
195 DrawStringMultiLineWithClipping(r, this->summary_msg.GetDecodedString(), TC_WHITE, SA_CENTER);
196 } else if (this->extra_msg.empty()) {
197 /* Extra space when message is shorter than company face window */
198 int extra = (r.Height() - this->height_summary - this->height_detailed - WidgetDimensions::scaled.vsep_wide) / 2;
199
200 /* Note: NewGRF supplied error message often do not start with a colour code, so default to white. */
201 DrawStringMultiLineWithClipping(r.WithHeight(this->height_summary + extra, false), this->summary_msg.GetDecodedString(), TC_WHITE, SA_CENTER);
202 DrawStringMultiLineWithClipping(r.WithHeight(this->height_detailed + extra, true), this->detailed_msg.GetDecodedString(), TC_WHITE, SA_CENTER);
203 } else {
204 /* Extra space when message is shorter than company face window */
205 int extra = (r.Height() - this->height_summary - this->height_detailed - this->height_extra - (WidgetDimensions::scaled.vsep_wide * 2)) / 3;
206
207 /* Note: NewGRF supplied error message often do not start with a colour code, so default to white. */
208 Rect top_section = r.WithHeight(this->height_summary + extra, false);
209 Rect bottom_section = r.WithHeight(this->height_extra + extra, true);
210 Rect middle_section = top_section.WithY(top_section.bottom, bottom_section.top);
211 DrawStringMultiLineWithClipping(top_section, this->summary_msg.GetDecodedString(), TC_WHITE, SA_CENTER);
212 DrawStringMultiLineWithClipping(middle_section, this->detailed_msg.GetDecodedString(), TC_WHITE, SA_CENTER);
213 DrawStringMultiLineWithClipping(bottom_section, this->extra_msg.GetDecodedString(), TC_WHITE, SA_CENTER);
214 }
215
216 break;
217
218 default:
219 break;
220 }
221 }
222
223 void OnPaint() override
224 {
225 /* Start the timeout if not already started and the message is not critical. This is handled during OnPaint so that any delay between
226 * creating the window and displaying it does not affect how long the message is visible. */
227 if (!this->is_critical && this->display_timeout.HasFired()) {
228 this->display_timeout.Reset();
229 }
230
231 this->Window::OnPaint();
232 }
233
234 void OnMouseLoop() override
235 {
236 /* Disallow closing the window too easily, if timeout is disabled */
237 if (_right_button_down && !this->is_critical) this->Close();
238 }
239
240 void Close([[maybe_unused]] int data = 0) override
241 {
244 this->Window::Close();
245 }
246
252 {
253 return this->is_critical;
254 }
255};
256
261{
263 _error_list.clear();
264}
265
268{
270 if (!_error_list.empty()) {
271 new ErrmsgWindow(_error_list.front());
272 _error_list.pop_front();
273 }
274}
275
282{
283 ErrmsgWindow *w = dynamic_cast<ErrmsgWindow *>(FindWindowById(WC_ERRMSG, 0));
284 if (_window_system_initialized && w != nullptr) {
285 if (w->IsCritical()) _error_list.push_front(*w);
287 w->Close();
288 }
289}
290
299void ShowErrorMessage(EncodedString &&summary_msg, int x, int y, CommandCost &cc)
300{
301 EncodedString error = std::move(cc.GetEncodedMessage());
302 if (error.empty()) error = GetEncodedStringIfValid(cc.GetErrorMessage());
303
304 ShowErrorMessage(std::move(summary_msg), std::move(error), WL_INFO, x, y,
306}
307
318void ShowErrorMessage(EncodedString &&summary_msg, EncodedString &&detailed_msg, WarningLevel wl, int x, int y, EncodedString &&extra_msg, CompanyID company)
319{
320 if (wl != WL_INFO) {
321 /* Print message to console */
322
323 std::string message = summary_msg.GetDecodedString();
324 if (!detailed_msg.empty()) {
325 message += " ";
326 message += detailed_msg.GetDecodedString();
327 }
328 if (!extra_msg.empty()) {
329 message += " ";
330 message += extra_msg.GetDecodedString();
331 }
332
333 IConsolePrint(wl == WL_WARNING ? CC_WARNING : CC_ERROR, message);
334 }
335
336 bool is_critical = wl == WL_CRITICAL;
337
338 if (_game_mode == GM_BOOTSTRAP) return;
339 if (_settings_client.gui.errmsg_duration == 0 && !is_critical) return;
340
341 ErrorMessageData data(std::move(summary_msg), std::move(detailed_msg), is_critical, x, y, std::move(extra_msg), company);
342
343 ErrmsgWindow *w = dynamic_cast<ErrmsgWindow *>(FindWindowById(WC_ERRMSG, 0));
344 if (w != nullptr) {
345 if (w->IsCritical()) {
346 /* A critical error is currently shown. */
347 if (wl == WL_CRITICAL) {
348 /* Push another critical error in the queue of errors,
349 * but do not put other errors in the queue. */
350 _error_list.push_back(std::move(data));
351 }
352 return;
353 }
354 /* A non-critical error was shown. */
355 w->Close();
356 }
357 new ErrmsgWindow(data);
358}
359
360
366{
367 ErrmsgWindow *w = dynamic_cast<ErrmsgWindow *>(FindWindowById(WC_ERRMSG, 0));
368 if (w == nullptr) return false;
369 w->Close();
370 return true;
371}
372
379{
380 _error_list.splice(_error_list.end(), datas);
381}
382
389{
390 _error_list.push_back(data);
391}
Common return value for all commands.
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.
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:85
bool HasFace() const
Check whether error window shall display a company manager face.
Definition error.h:47
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
bool HasFired() const
Check whether the timeout occurred.
Definition timer.h:171
static WidgetDimensions scaled
Widget dimensions scaled for current zoom level.
Definition window_gui.h:30
static const WidgetDimensions unscaled
Unscaled widget dimensions.
Definition window_gui.h:93
Definition of stuff that is very close to a company, like the company struct itself.
Functions related to companies.
void DrawCompanyManagerFace(const 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:90
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:51
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:97
void UnshowCriticalError()
Unshow the critical error.
void ClearErrorMessages()
Clear all errors from the queue.
void ScheduleErrorMessage(ErrorList &datas)
Schedule a list of errors.
static WindowDesc _errmsg_face_desc(WindowPosition::Manual, {}, 0, 0, WC_ERRMSG, WC_NONE, {}, _nested_errmsg_face_widgets)
Window definition for the error message with company president face window.
bool _window_system_initialized
Whether the window system is initialized or not.
Definition error_gui.cpp:99
static WindowDesc _errmsg_desc(WindowPosition::Manual, {}, 0, 0, WC_ERRMSG, WC_NONE, {}, _nested_errmsg_widgets)
Window definition for the error message window.
bool HideActiveErrorMessage()
Close active error message window.
void ShowErrorMessage(EncodedString &&summary_msg, int x, int y, CommandCost &cc)
Display an error message in a window.
void ShowFirstError()
Show the first error of the queue.
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:717
bool DrawStringMultiLineWithClipping(int left, int right, int top, int bottom, std::string_view str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw a multiline string, possibly over multiple lines, if the region is within the current display cl...
Definition gfx.cpp:873
bool _right_button_down
Is right mouse button pressed?
Definition gfx.cpp:44
Functions related to the gfx engine.
Dimension GetScaledSpriteSize(SpriteID sprid)
Scale sprite size for GUI.
Definition widget.cpp:70
@ SA_CENTER
Center both horizontally and vertically.
Definition gfx_type.h:398
@ Invalid
Invalid marker.
Definition gfx_type.h:302
@ Red
Red.
Definition gfx_type.h:289
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 EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME,...
constexpr NWidgetPart NWidget(WidgetType tp, Colours col, WidgetID idx=INVALID_WIDGET)
Widget part function for starting a new 'real' widget.
Functions related to OTTD's landscape.
#define Point
Macro that prevents name conflicts between included headers.
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:60
This file contains all sprite-related enums and defines.
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:424
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.
Colours colour
Company colour.
CompanyManagerFace face
Face description of the president.
T y
Y coordinate.
T x
X coordinate.
Dimensions (a width and height) of a rectangle in 2D.
Window class for displaying an error message window.
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.
void OnPaint() override
The window must be repainted.
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.
static Company * Get(auto index)
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.
Rect WithY(int new_top, int new_bottom) const
Create a new Rect, replacing the top and bottom coordinates.
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:1117
virtual std::string GetWidgetString(WidgetID widget, StringID stringid) const
Get the raw string for a widget.
Definition window.cpp:518
ResizeInfo resize
Resize information.
Definition window_gui.h:315
virtual void OnPaint()
The window must be repainted.
Definition window_gui.h:599
Window(WindowDesc &desc)
Empty constructor, initialization has been moved to InitNested() called from the constructor of the d...
Definition window.cpp:1846
void InitNested(WindowNumber number=0)
Perform complete initialization of the Window with nested widgets, to allow use.
Definition window.cpp:1836
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:100
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:66
@ WWT_PANEL
Simple depressed panel.
Definition widget_type.h:39
@ WWT_CAPTION
Window caption (window title between closebox and stickybox).
Definition widget_type.h:52
@ WWT_CLOSEBOX
Close box (at top-left of a window).
Definition widget_type.h:60
@ WWT_EMPTY
Empty widget, place holder to reserve space in widget tree.
Definition widget_type.h:37
int GetMainViewTop()
Return the top of the main view available for general use.
Definition window.cpp:2153
Window * FindWindowById(WindowClass cls, WindowNumber number)
Find a window by its class and window number.
Definition window.cpp:1166
Window functions not directly related to making/drawing windows.
Functions, definitions and such used only by the GUI.
@ Manual
Manually align the window (so no automatic location finding).
Definition window_gui.h:143
int WidgetID
Widget ID.
Definition window_type.h:21
@ WC_ERRMSG
Error message; Window numbers:
@ WC_NONE
No window, redirects to WC_MAIN_WINDOW.
Definition window_type.h:51
Functions related to zooming.