Add: CommandCost supports an optional second error string

This commit is contained in:
Tyler Trahan
2018-11-19 18:59:25 +00:00
parent e3169c73bd
commit 44848f4edf
6 changed files with 70 additions and 21 deletions

View File

@@ -72,7 +72,7 @@ static WindowDesc _errmsg_face_desc(
*/
ErrorMessageData::ErrorMessageData(const ErrorMessageData &data) :
display_timer(data.display_timer), textref_stack_grffile(data.textref_stack_grffile), textref_stack_size(data.textref_stack_size),
summary_msg(data.summary_msg), detailed_msg(data.detailed_msg), position(data.position), face(data.face)
summary_msg(data.summary_msg), detailed_msg(data.detailed_msg), extra_msg(data.extra_msg), position(data.position), face(data.face)
{
memcpy(this->textref_stack, data.textref_stack, sizeof(this->textref_stack));
memcpy(this->decode_params, data.decode_params, sizeof(this->decode_params));
@@ -101,12 +101,14 @@ ErrorMessageData::~ErrorMessageData()
* @param textref_stack_grffile NewGRF that provides the #TextRefStack for the error message.
* @param textref_stack_size Number of uint32 values to put on the #TextRefStack for the error message; 0 if the #TextRefStack shall not be used.
* @param textref_stack Values to put on the #TextRefStack.
* @param extra_msg Extra error message showed in third line. Can be INVALID_STRING_ID.
*/
ErrorMessageData::ErrorMessageData(StringID summary_msg, StringID detailed_msg, uint duration, int x, int y, const GRFFile *textref_stack_grffile, uint textref_stack_size, const uint32 *textref_stack) :
ErrorMessageData::ErrorMessageData(StringID summary_msg, StringID detailed_msg, uint duration, int x, int y, const GRFFile *textref_stack_grffile, uint textref_stack_size, const uint32 *textref_stack, StringID extra_msg) :
textref_stack_grffile(textref_stack_grffile),
textref_stack_size(textref_stack_size),
summary_msg(summary_msg),
detailed_msg(detailed_msg),
extra_msg(extra_msg),
face(INVALID_COMPANY)
{
this->position.x = x;
@@ -184,6 +186,7 @@ struct ErrmsgWindow : public Window, ErrorMessageData {
private:
uint height_summary; ///< Height of the #summary_msg string in pixels in the #WID_EM_MESSAGE widget.
uint height_detailed; ///< Height of the #detailed_msg string in pixels in the #WID_EM_MESSAGE widget.
uint height_extra; ///< Height of the #extra_msg string in pixels in the #WID_EM_MESSAGE widget.
public:
ErrmsgWindow(const ErrorMessageData &data) : Window(data.HasFace() ? &_errmsg_face_desc : &_errmsg_desc), ErrorMessageData(data)
@@ -200,11 +203,13 @@ public:
this->height_summary = GetStringHeight(this->summary_msg, size->width);
this->height_detailed = (this->detailed_msg == INVALID_STRING_ID) ? 0 : GetStringHeight(this->detailed_msg, size->width);
this->height_extra = (this->extra_msg == INVALID_STRING_ID) ? 0 : GetStringHeight(this->extra_msg, size->width);
if (this->textref_stack_size > 0) StopTextRefStackUsage();
uint panel_height = this->height_summary;
if (this->detailed_msg != INVALID_STRING_ID) panel_height += this->height_detailed + WidgetDimensions::scaled.vsep_wide;
if (this->extra_msg != INVALID_STRING_ID) panel_height += this->height_extra + WidgetDimensions::scaled.vsep_wide;
size->height = std::max(size->height, panel_height);
break;
@@ -280,13 +285,24 @@ public:
if (this->detailed_msg == INVALID_STRING_ID) {
DrawStringMultiLine(r, this->summary_msg, TC_FROMSTRING, SA_CENTER);
} else {
} else if (this->extra_msg == INVALID_STRING_ID) {
/* Extra space when message is shorter than company face window */
int extra = (r.Height() - this->height_summary - this->height_detailed - WidgetDimensions::scaled.vsep_wide) / 2;
/* Note: NewGRF supplied error message often do not start with a colour code, so default to white. */
DrawStringMultiLine(r.WithHeight(this->height_summary + extra, false), this->summary_msg, TC_WHITE, SA_CENTER);
DrawStringMultiLine(r.WithHeight(this->height_detailed + extra, true), this->detailed_msg, TC_WHITE, SA_CENTER);
} else {
/* Extra space when message is shorter than company face window */
int extra = (r.Height() - this->height_summary - this->height_detailed - this->height_extra - (WidgetDimensions::scaled.vsep_wide * 2)) / 3;
/* Note: NewGRF supplied error message often do not start with a colour code, so default to white. */
Rect top_section = r.WithHeight(this->height_summary + extra, false);
Rect bottom_section = r.WithHeight(this->height_extra + extra, true);
Rect middle_section = { top_section.left, top_section.bottom, top_section.right, bottom_section.top };
DrawStringMultiLine(top_section, this->summary_msg, TC_WHITE, SA_CENTER);
DrawStringMultiLine(middle_section, this->detailed_msg, TC_WHITE, SA_CENTER);
DrawStringMultiLine(bottom_section, this->extra_msg, TC_WHITE, SA_CENTER);
}
if (this->textref_stack_size > 0) StopTextRefStackUsage();
@@ -361,6 +377,19 @@ void UnshowCriticalError()
}
}
/**
* Display an error message in a window.
* Note: CommandCost errors are always severity level WL_INFO.
* @param summary_msg General error message showed in first line. Must be valid.
* @param x World X position (TileVirtX) of the error location. Set both x and y to 0 to just center the message when there is no related error tile.
* @param y World Y position (TileVirtY) of the error location. Set both x and y to 0 to just center the message when there is no related error tile.
* @param cc CommandCost containing the optional detailed and extra error messages shown in the second and third lines (can be INVALID_STRING_ID) and TextRefStack info.
*/
void ShowErrorMessage(StringID summary_msg, int x, int y, CommandCost cc)
{
ShowErrorMessage(summary_msg, cc.GetErrorMessage(), WL_INFO, x, y, cc.GetTextRefStackGRF(), cc.GetTextRefStackSize(), cc.GetTextRefStack(), cc.GetExtraErrorMessage());
}
/**
* Display an error message in a window.
* @param summary_msg General error message showed in first line. Must be valid.
@@ -371,8 +400,9 @@ void UnshowCriticalError()
* @param textref_stack_grffile NewGRF providing the #TextRefStack for the error message.
* @param textref_stack_size Number of uint32 values to put on the #TextRefStack for the error message; 0 if the #TextRefStack shall not be used.
* @param textref_stack Values to put on the #TextRefStack.
* @param extra_msg Extra error message shown in third line. Can be INVALID_STRING_ID.
*/
void ShowErrorMessage(StringID summary_msg, StringID detailed_msg, WarningLevel wl, int x, int y, const GRFFile *textref_stack_grffile, uint textref_stack_size, const uint32 *textref_stack)
void ShowErrorMessage(StringID summary_msg, StringID detailed_msg, WarningLevel wl, int x, int y, const GRFFile *textref_stack_grffile, uint textref_stack_size, const uint32 *textref_stack, StringID extra_msg)
{
assert(textref_stack_size == 0 || (textref_stack_grffile != nullptr && textref_stack != nullptr));
if (summary_msg == STR_NULL) summary_msg = STR_EMPTY;
@@ -388,6 +418,10 @@ void ShowErrorMessage(StringID summary_msg, StringID detailed_msg, WarningLevel
b += seprintf(b, lastof(buf), " ");
GetString(b, detailed_msg, lastof(buf));
}
if (extra_msg != INVALID_STRING_ID) {
b += seprintf(b, lastof(buf), " ");
GetString(b, extra_msg, lastof(buf));
}
if (textref_stack_size > 0) StopTextRefStackUsage();
@@ -399,7 +433,7 @@ void ShowErrorMessage(StringID summary_msg, StringID detailed_msg, WarningLevel
if (_game_mode == GM_BOOTSTRAP) return;
if (_settings_client.gui.errmsg_duration == 0 && !no_timeout) return;
ErrorMessageData data(summary_msg, detailed_msg, no_timeout ? 0 : _settings_client.gui.errmsg_duration, x, y, textref_stack_grffile, textref_stack_size, textref_stack);
ErrorMessageData data(summary_msg, detailed_msg, no_timeout ? 0 : _settings_client.gui.errmsg_duration, x, y, textref_stack_grffile, textref_stack_size, textref_stack, extra_msg);
data.CopyOutDParams();
ErrmsgWindow *w = (ErrmsgWindow*)FindWindowById(WC_ERRMSG, 0);