From 51fd2853cb897300b8d521360653bd273240e177 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Tue, 4 Mar 2025 23:15:50 +0000 Subject: [PATCH] Fix 6e10584b91: Keep custom news from game scripts in encoded form. (#13741) This allows the news message to translated as appropriate. --- src/gfx.cpp | 4 ++-- src/gfx_func.h | 2 +- src/industry_cmd.cpp | 26 ++++++++++++-------------- src/lang/english.txt | 1 - src/news_gui.cpp | 8 +++++--- 5 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/gfx.cpp b/src/gfx.cpp index 8d5b4fe090..21dfc75292 100644 --- a/src/gfx.cpp +++ b/src/gfx.cpp @@ -750,9 +750,9 @@ Dimension GetStringMultiLineBoundingBox(StringID str, const Dimension &suggestio * @param suggestion Suggested bounding box. * @return Bounding box for the multi-line string, may be bigger than \a suggestion. */ -Dimension GetStringMultiLineBoundingBox(std::string_view str, const Dimension &suggestion) +Dimension GetStringMultiLineBoundingBox(std::string_view str, const Dimension &suggestion, FontSize fontsize) { - Dimension box = {suggestion.width, (uint)GetStringHeight(str, suggestion.width)}; + Dimension box = {suggestion.width, (uint)GetStringHeight(str, suggestion.width, fontsize)}; return box; } diff --git a/src/gfx_func.h b/src/gfx_func.h index 770d536c7a..90e0ba15c8 100644 --- a/src/gfx_func.h +++ b/src/gfx_func.h @@ -140,7 +140,7 @@ int GetStringHeight(std::string_view str, int maxw, FontSize fontsize = FS_NORMA int GetStringHeight(StringID str, int maxw); int GetStringLineCount(std::string_view str, int maxw); Dimension GetStringMultiLineBoundingBox(StringID str, const Dimension &suggestion); -Dimension GetStringMultiLineBoundingBox(std::string_view str, const Dimension &suggestion); +Dimension GetStringMultiLineBoundingBox(std::string_view str, const Dimension &suggestion, FontSize fontsize = FS_NORMAL); void LoadStringWidthTable(bool monospace = false); void DrawDirtyBlocks(); diff --git a/src/industry_cmd.cpp b/src/industry_cmd.cpp index 3e1a569d36..914b9502d7 100644 --- a/src/industry_cmd.cpp +++ b/src/industry_cmd.cpp @@ -2170,20 +2170,12 @@ CommandCost CmdIndustrySetProduction(DoCommandFlags flags, IndustryID ind_id, ui if (ind == nullptr) return CMD_ERROR; if (flags.Test(DoCommandFlag::Execute)) { - StringID str = STR_NULL; - if (prod_level > ind->prod_level) { - str = GetIndustrySpec(ind->type)->production_up_text; - } else if (prod_level < ind->prod_level) { - str = GetIndustrySpec(ind->type)->production_down_text; - } - if (prod_level != ind->prod_level && !custom_news.empty()) str = STR_NEWS_CUSTOM_ITEM; - ind->ctlflags.Set(IndustryControlFlag::ExternalProdLevel); ind->prod_level = prod_level; ind->RecomputeProductionMultipliers(); /* Show news message if requested. */ - if (show_news && str != STR_NULL) { + if (show_news && prod_level != ind->prod_level) { NewsType nt; switch (WhoCanServiceIndustry(ind)) { case 0: nt = NewsType::IndustryNobody; break; @@ -2194,12 +2186,18 @@ CommandCost CmdIndustrySetProduction(DoCommandFlags flags, IndustryID ind_id, ui /* Set parameters of news string */ EncodedString headline; - if (str == STR_NEWS_CUSTOM_ITEM) { - headline = GetEncodedString(str, custom_news.GetDecodedString()); - } else if (str > STR_LAST_STRINGID) { - headline = GetEncodedString(str, STR_TOWN_NAME, ind->town->index, GetIndustrySpec(ind->type)->name); + if (!custom_news.empty()) { + headline = custom_news; } else { - headline = GetEncodedString(str, ind->index); + StringID str = (prod_level > ind->prod_level) + ? GetIndustrySpec(ind->type)->production_up_text + : GetIndustrySpec(ind->type)->production_down_text; + + if (str > STR_LAST_STRINGID) { + headline = GetEncodedString(str, STR_TOWN_NAME, ind->town->index, GetIndustrySpec(ind->type)->name); + } else { + headline = GetEncodedString(str, ind->index); + } } AddIndustryNewsItem(std::move(headline), nt, ind->index); diff --git a/src/lang/english.txt b/src/lang/english.txt index f01be923a2..9e0d926e2d 100644 --- a/src/lang/english.txt +++ b/src/lang/english.txt @@ -841,7 +841,6 @@ STR_MESSAGE_HISTORY_TOOLTIP :{BLACK}A list o STR_MESSAGE_NEWS_FORMAT :{STRING} - {RAW_STRING} STR_NEWS_MESSAGE_CAPTION :{WHITE}Message -STR_NEWS_CUSTOM_ITEM :{BIG_FONT}{BLACK}{RAW_STRING} STR_NEWS_FIRST_TRAIN_ARRIVAL :{BIG_FONT}{BLACK}Citizens celebrate . . .{}First train arrives at {STATION}! STR_NEWS_FIRST_BUS_ARRIVAL :{BIG_FONT}{BLACK}Citizens celebrate . . .{}First bus arrives at {STATION}! diff --git a/src/news_gui.cpp b/src/news_gui.cpp index 0356acbe61..3ebcf8a990 100644 --- a/src/news_gui.cpp +++ b/src/news_gui.cpp @@ -424,6 +424,7 @@ struct NewsWindow : Window { void UpdateWidgetSize(WidgetID widget, Dimension &size, [[maybe_unused]] const Dimension &padding, [[maybe_unused]] Dimension &fill, [[maybe_unused]] Dimension &resize) override { + FontSize fontsize = FS_NORMAL; std::string str; switch (widget) { case WID_N_CAPTION: { @@ -441,6 +442,7 @@ struct NewsWindow : Window { case WID_N_MESSAGE: case WID_N_COMPANY_MSG: + fontsize = FS_LARGE; str = this->ni->headline.GetDecodedString(); break; @@ -481,7 +483,7 @@ struct NewsWindow : Window { Dimension d = size; d.width = (d.width >= padding.width) ? d.width - padding.width : 0; d.height = (d.height >= padding.height) ? d.height - padding.height : 0; - d = GetStringMultiLineBoundingBox(str, d); + d = GetStringMultiLineBoundingBox(str, d, fontsize); d.width += padding.width; d.height += padding.height; size = maxdim(size, d); @@ -513,7 +515,7 @@ struct NewsWindow : Window { case WID_N_MESSAGE: case WID_N_COMPANY_MSG: - DrawStringMultiLine(r, this->ni->headline.GetDecodedString(), TC_FROMSTRING, SA_CENTER); + DrawStringMultiLine(r, this->ni->headline.GetDecodedString(), TC_BLACK, SA_CENTER, false, FS_LARGE); break; case WID_N_MGR_FACE: { @@ -957,7 +959,7 @@ CommandCost CmdCustomNewsItem(DoCommandFlags flags, NewsType type, CompanyID com if (company != INVALID_OWNER && company != _local_company) return CommandCost(); if (flags.Test(DoCommandFlag::Execute)) { - AddNewsItem(GetEncodedString(STR_NEWS_CUSTOM_ITEM, text.GetDecodedString()), type, NewsStyle::Normal, {}, reference, {}); + AddNewsItem(EncodedString{text}, type, NewsStyle::Normal, {}, reference, {}); } return CommandCost();