1
0
Fork 0

Codechange: Use parameterised GetString() for industry window. (#13685)

pull/13686/head
Peter Nelson 2025-03-01 19:59:43 +00:00 committed by GitHub
parent b92172e3d2
commit 170002ff7e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 54 additions and 57 deletions

View File

@ -8,6 +8,7 @@
/** @file industry_gui.cpp GUIs related to industries. */ /** @file industry_gui.cpp GUIs related to industries. */
#include "stdafx.h" #include "stdafx.h"
#include <ranges>
#include "error.h" #include "error.h"
#include "gui.h" #include "gui.h"
#include "settings_gui.h" #include "settings_gui.h"
@ -870,6 +871,18 @@ public:
DrawSprite(icon, PAL_NONE, CenterBounds(ir.left, ir.right, d.width), CenterBounds(ir.top, ir.bottom, this->cargo_icon_size.height)); DrawSprite(icon, PAL_NONE, CenterBounds(ir.left, ir.right, d.width), CenterBounds(ir.top, ir.bottom, this->cargo_icon_size.height));
} }
std::string GetAcceptedCargoString(const Industry::AcceptedCargo &ac, const CargoSuffix &suffix) const
{
auto params = MakeParameters(CargoSpec::Get(ac.cargo)->name, ac.cargo, ac.waiting, suffix.text);
switch (suffix.display) {
case CSD_CARGO_AMOUNT_TEXT: return GetStringWithArgs(STR_INDUSTRY_VIEW_ACCEPT_CARGO_AMOUNT_SUFFIX, params);
case CSD_CARGO_TEXT: return GetStringWithArgs(STR_INDUSTRY_VIEW_ACCEPT_CARGO_SUFFIX, params);
case CSD_CARGO_AMOUNT: return GetStringWithArgs(STR_INDUSTRY_VIEW_ACCEPT_CARGO_AMOUNT_NOSUFFIX, params);
case CSD_CARGO: return GetStringWithArgs(STR_INDUSTRY_VIEW_ACCEPT_CARGO_NOSUFFIX, params);
default: NOT_REACHED();
}
}
/** /**
* Draw the text in the #WID_IV_INFO panel. * Draw the text in the #WID_IV_INFO panel.
* @param r Rectangle of the panel. * @param r Rectangle of the panel.
@ -908,31 +921,11 @@ public:
CargoSuffix suffix; CargoSuffix suffix;
GetCargoSuffix(CARGOSUFFIX_IN, CST_VIEW, i, i->type, ind, a.cargo, &a - i->accepted.data(), suffix); GetCargoSuffix(CARGOSUFFIX_IN, CST_VIEW, i, i->type, ind, a.cargo, &a - i->accepted.data(), suffix);
/* if the industry is not stockpiling then don't show amount in the acceptance display. */
if (!stockpiling && suffix.display == CSD_CARGO_AMOUNT_TEXT) suffix.display = CSD_CARGO_TEXT;
if (!stockpiling && suffix.display == CSD_CARGO_AMOUNT) suffix.display = CSD_CARGO;
SetDParam(0, CargoSpec::Get(a.cargo)->name); DrawString(ir.Indent(label_indent, rtl), this->GetAcceptedCargoString(a, suffix));
SetDParam(1, a.cargo);
SetDParam(2, a.waiting);
SetDParamStr(3, "");
StringID str = STR_NULL;
switch (suffix.display) {
case CSD_CARGO_AMOUNT_TEXT:
SetDParamStr(3, suffix.text);
[[fallthrough]];
case CSD_CARGO_AMOUNT:
str = stockpiling ? STR_INDUSTRY_VIEW_ACCEPT_CARGO_AMOUNT : STR_INDUSTRY_VIEW_ACCEPT_CARGO;
break;
case CSD_CARGO_TEXT:
SetDParamStr(3, suffix.text);
[[fallthrough]];
case CSD_CARGO:
str = STR_INDUSTRY_VIEW_ACCEPT_CARGO;
break;
default:
NOT_REACHED();
}
DrawString(ir.Indent(label_indent, rtl), str);
ir.top += GetCharacterHeight(FS_NORMAL); ir.top += GetCharacterHeight(FS_NORMAL);
} }
@ -955,11 +948,8 @@ public:
CargoSuffix suffix; CargoSuffix suffix;
GetCargoSuffix(CARGOSUFFIX_OUT, CST_VIEW, i, i->type, ind, p.cargo, &p - i->produced.data(), suffix); GetCargoSuffix(CARGOSUFFIX_OUT, CST_VIEW, i, i->type, ind, p.cargo, &p - i->produced.data(), suffix);
SetDParam(0, p.cargo); DrawString(ir.Indent(label_indent + (this->editable == EA_RATE ? SETTING_BUTTON_WIDTH + WidgetDimensions::scaled.hsep_normal : 0), rtl).Translate(0, text_y_offset),
SetDParam(1, p.history[LAST_MONTH].production); GetString(STR_INDUSTRY_VIEW_TRANSPORTED, p.cargo, p.history[LAST_MONTH].production, suffix.text, ToPercent8(p.history[LAST_MONTH].PctTransported())));
SetDParamStr(2, suffix.text);
SetDParam(3, ToPercent8(p.history[LAST_MONTH].PctTransported()));
DrawString(ir.Indent(label_indent + (this->editable == EA_RATE ? SETTING_BUTTON_WIDTH + WidgetDimensions::scaled.hsep_normal : 0), rtl).Translate(0, text_y_offset), STR_INDUSTRY_VIEW_TRANSPORTED);
/* Let's put out those buttons.. */ /* Let's put out those buttons.. */
if (this->editable == EA_RATE) { if (this->editable == EA_RATE) {
DrawArrowButtons(ir.Indent(label_indent, rtl).WithWidth(SETTING_BUTTON_WIDTH, rtl).left, ir.top + button_y_offset, COLOUR_YELLOW, (this->clicked_line == IL_RATE1 + (&p - i->produced.data())) ? this->clicked_button : 0, DrawArrowButtons(ir.Indent(label_indent, rtl).WithWidth(SETTING_BUTTON_WIDTH, rtl).left, ir.top + button_y_offset, COLOUR_YELLOW, (this->clicked_line == IL_RATE1 + (&p - i->produced.data())) ? this->clicked_button : 0,
@ -975,8 +965,8 @@ public:
button_y_offset = (line_height - SETTING_BUTTON_HEIGHT) / 2; button_y_offset = (line_height - SETTING_BUTTON_HEIGHT) / 2;
ir.top += WidgetDimensions::scaled.vsep_wide; ir.top += WidgetDimensions::scaled.vsep_wide;
this->production_offset_y = ir.top; this->production_offset_y = ir.top;
SetDParam(0, RoundDivSU(i->prod_level * 100, PRODLEVEL_DEFAULT)); DrawString(ir.Indent(label_indent + SETTING_BUTTON_WIDTH + WidgetDimensions::scaled.hsep_normal, rtl).Translate(0, text_y_offset),
DrawString(ir.Indent(label_indent + SETTING_BUTTON_WIDTH + WidgetDimensions::scaled.hsep_normal, rtl).Translate(0, text_y_offset), STR_INDUSTRY_VIEW_PRODUCTION_LEVEL); GetString(STR_INDUSTRY_VIEW_PRODUCTION_LEVEL, RoundDivSU(i->prod_level * 100, PRODLEVEL_DEFAULT)));
DrawArrowButtons(ir.Indent(label_indent, rtl).WithWidth(SETTING_BUTTON_WIDTH, rtl).left, ir.top + button_y_offset, COLOUR_YELLOW, (this->clicked_line == IL_MULTIPLIER) ? this->clicked_button : 0, DrawArrowButtons(ir.Indent(label_indent, rtl).WithWidth(SETTING_BUTTON_WIDTH, rtl).left, ir.top + button_y_offset, COLOUR_YELLOW, (this->clicked_line == IL_MULTIPLIER) ? this->clicked_button : 0,
i->prod_level > PRODLEVEL_MINIMUM, i->prod_level < PRODLEVEL_MAXIMUM); i->prod_level > PRODLEVEL_MINIMUM, i->prod_level < PRODLEVEL_MAXIMUM);
ir.top += line_height; ir.top += line_height;
@ -999,9 +989,8 @@ public:
} }
if (!i->text.empty()) { if (!i->text.empty()) {
SetDParamStr(0, i->text);
ir.top += WidgetDimensions::scaled.vsep_wide; ir.top += WidgetDimensions::scaled.vsep_wide;
ir.top = DrawStringMultiLine(ir, STR_JUST_RAW_STRING, TC_BLACK); ir.top = DrawStringMultiLine(ir, GetString(STR_JUST_RAW_STRING, i->text), TC_BLACK);
} }
/* Return required bottom position, the last pixel row plus some padding. */ /* Return required bottom position, the last pixel row plus some padding. */
@ -1567,18 +1556,25 @@ protected:
return (r == 0) ? IndustryNameSorter(a, b, filter) : r < 0; return (r == 0) ? IndustryNameSorter(a, b, filter) : r < 0;
} }
StringID GetStringForNumCargo(size_t count) const
{
switch (count) {
case 0: return STR_INDUSTRY_DIRECTORY_ITEM_NOPROD;
case 1: return STR_INDUSTRY_DIRECTORY_ITEM_PROD1;
case 2: return STR_INDUSTRY_DIRECTORY_ITEM_PROD2;
case 3: return STR_INDUSTRY_DIRECTORY_ITEM_PROD3;
default: return STR_INDUSTRY_DIRECTORY_ITEM_PRODMORE;
}
}
/** /**
* Get the StringID to draw and set the appropriate DParams. * Get the StringID to draw and set the appropriate DParams.
* @param i the industry to get the StringID of. * @param i the industry to get the StringID of.
* @return the StringID. * @return the StringID.
*/ */
StringID GetIndustryString(const Industry *i) const std::string GetIndustryString(const Industry *i) const
{ {
const IndustrySpec *indsp = GetIndustrySpec(i->type); const IndustrySpec *indsp = GetIndustrySpec(i->type);
uint8_t p = 0;
/* Industry name */
SetDParam(p++, i->index);
/* Get industry productions (CargoType, production, suffix, transported) */ /* Get industry productions (CargoType, production, suffix, transported) */
struct CargoInfo { struct CargoInfo {
@ -1628,27 +1624,26 @@ protected:
} }
} }
/* Display first 3 cargos */ static constexpr size_t MAX_DISPLAYED_CARGOES = 3;
for (size_t j = 0; j < std::min<size_t>(3, cargos.size()); j++) { std::array<StringParameter, 2 + 5 * MAX_DISPLAYED_CARGOES> params{};
CargoInfo &ci = cargos[j]; auto it = params.begin();
SetDParam(p++, STR_INDUSTRY_DIRECTORY_ITEM_INFO);
SetDParam(p++, ci.cargo_type); /* Industry name */
SetDParam(p++, ci.production); *it++ = i->index;
SetDParamStr(p++, std::move(ci.suffix));
SetDParam(p++, ci.transported); /* Display first MAX_DISPLAYED_CARGOES cargoes */
for (CargoInfo &ci : cargos | std::views::take(MAX_DISPLAYED_CARGOES)) {
*it++ = STR_INDUSTRY_DIRECTORY_ITEM_INFO;
*it++ = ci.cargo_type;
*it++ = ci.production;
*it++ = std::move(ci.suffix);
*it++ = ci.transported;
} }
/* Undisplayed cargos if any */ /* Undisplayed cargos if any */
SetDParam(p++, cargos.size() - 3); if (std::size(cargos) > MAX_DISPLAYED_CARGOES) *it++ = std::size(cargos) - MAX_DISPLAYED_CARGOES;
/* Drawing the right string */ return GetStringWithArgs(GetStringForNumCargo(std::size(cargos)), {params.begin(), it});
switch (cargos.size()) {
case 0: return STR_INDUSTRY_DIRECTORY_ITEM_NOPROD;
case 1: return STR_INDUSTRY_DIRECTORY_ITEM_PROD1;
case 2: return STR_INDUSTRY_DIRECTORY_ITEM_PROD2;
case 3: return STR_INDUSTRY_DIRECTORY_ITEM_PROD3;
default: return STR_INDUSTRY_DIRECTORY_ITEM_PRODMORE;
}
} }
public: public:

View File

@ -4039,8 +4039,10 @@ STR_INDUSTRY_VIEW_PRODUCES_N_CARGO :{BLACK}Produces
STR_INDUSTRY_VIEW_CARGO_LIST_EXTENSION :, {STRING}{RAW_STRING} STR_INDUSTRY_VIEW_CARGO_LIST_EXTENSION :, {STRING}{RAW_STRING}
STR_INDUSTRY_VIEW_REQUIRES :{BLACK}Requires: STR_INDUSTRY_VIEW_REQUIRES :{BLACK}Requires:
STR_INDUSTRY_VIEW_ACCEPT_CARGO :{YELLOW}{0:STRING}{BLACK}{3:RAW_STRING} STR_INDUSTRY_VIEW_ACCEPT_CARGO_SUFFIX :{YELLOW}{0:STRING}{BLACK}{3:RAW_STRING}
STR_INDUSTRY_VIEW_ACCEPT_CARGO_AMOUNT :{YELLOW}{STRING}{BLACK}: {CARGO_SHORT} waiting{RAW_STRING} STR_INDUSTRY_VIEW_ACCEPT_CARGO_AMOUNT_SUFFIX :{YELLOW}{0:STRING}{BLACK}: {1:CARGO_SHORT} waiting{3:RAW_STRING}
STR_INDUSTRY_VIEW_ACCEPT_CARGO_NOSUFFIX :{YELLOW}{0:STRING}
STR_INDUSTRY_VIEW_ACCEPT_CARGO_AMOUNT_NOSUFFIX :{YELLOW}{0:STRING}{BLACK}: {1:CARGO_SHORT} waiting
STR_CONFIG_GAME_PRODUCTION :{WHITE}Change production (multiple of 8, up to 2040) STR_CONFIG_GAME_PRODUCTION :{WHITE}Change production (multiple of 8, up to 2040)
STR_CONFIG_GAME_PRODUCTION_LEVEL :{WHITE}Change production level (percentage, up to 800%) STR_CONFIG_GAME_PRODUCTION_LEVEL :{WHITE}Change production level (percentage, up to 800%)