From bd3d498c86685ec60313b0d35b0ff8e2d2d3f4e1 Mon Sep 17 00:00:00 2001 From: Nikolay Borodin Date: Sun, 21 Jul 2024 19:49:20 +0200 Subject: [PATCH] Create a shorthand for the formatted wzstring --- lib/framework/wzstring.h | 6 ++++++ src/design.cpp | 4 ++-- src/frontend.cpp | 8 ++++---- src/hci.cpp | 2 +- src/hci/build.cpp | 2 +- src/hci/groups.cpp | 10 +++++----- src/hci/manufacture.cpp | 6 +++--- src/hci/objects_stats.h | 2 +- src/hci/quickchat.cpp | 2 +- src/multiint.cpp | 4 ++-- src/seqdisp.cpp | 2 +- src/spectatorwidgets.cpp | 2 +- src/texture.cpp | 8 ++++---- src/wzscriptdebug.cpp | 6 +++--- 14 files changed, 35 insertions(+), 29 deletions(-) diff --git a/lib/framework/wzstring.h b/lib/framework/wzstring.h index f6a800a3e48..0c922b432a0 100644 --- a/lib/framework/wzstring.h +++ b/lib/framework/wzstring.h @@ -67,6 +67,12 @@ class WzString { static WzString fromUtf32(const std::vector& utf32); static WzString fromCodepoint(const WzUniCodepoint& codepoint); + template + static WzString format(char const *format, P &&... params) + { + return WzString::fromUtf8(astringf(format, params...)); + } + const std::string& toUtf8() const; // Same as `toUtf8()` diff --git a/src/design.cpp b/src/design.cpp index be86018b987..50faae90e71 100644 --- a/src/design.cpp +++ b/src/design.cpp @@ -366,7 +366,7 @@ class DesignStatsBar: public W_BARGRAPH auto x0 = xOffset + x() + PADDING; auto y0 = yOffset + y() + PADDING; - valueText.setText(WzString::fromUtf8(astringf("%.*f", precision, majorValue / (float)denominator)), font_regular); + valueText.setText(WzString::format("%.*f", precision, majorValue / (float)denominator), font_regular); /* indent to allow text value */ auto iX = x0 + maxValueTextWidth; @@ -392,7 +392,7 @@ class DesignStatsBar: public W_BARGRAPH auto delta = minorValue - majorValue; if (delta != 0) { - deltaText.setText(WzString::fromUtf8(astringf("%+.*f", precision, delta / (float)denominator)), font_small); + deltaText.setText(WzString::format("%+.*f", precision, delta / (float)denominator), font_small); auto xDeltaText = xOffset + x() + width() - deltaText.width() - PADDING; deltaText.renderOutlined(xDeltaText, iY - 1, (delta < 0) == lessIsBetter ? WZCOL_LGREEN : WZCOL_LRED, {0, 0, 0, 192}); } diff --git a/src/frontend.cpp b/src/frontend.cpp index 067fa9fd7fd..05da58d3eb4 100644 --- a/src/frontend.cpp +++ b/src/frontend.cpp @@ -808,7 +808,7 @@ static std::shared_ptr makeLODDistanceDropdown() } else { - dropDownChoices.push_back({WzString::fromUtf8(astringf("(%d)", currValue)), currValue}); + dropDownChoices.push_back({WzString::format("(%d)", currValue), currValue}); currentSettingIdx = dropDownChoices.size() - 1; } @@ -958,7 +958,7 @@ static std::shared_ptr makeShadowMapResolutionDropdown() } else { - dropDownChoices.push_back({WzString::fromUtf8(astringf("(Custom: %" PRIu32 ")", currValue)), currValue}); + dropDownChoices.push_back({WzString::format("(Custom: %" PRIu32 ")", currValue), currValue}); currentSettingIdx = dropDownChoices.size() - 1; } @@ -1019,7 +1019,7 @@ static std::shared_ptr makeShadowFilterSizeDropdown() } else { - dropDownChoices.push_back({WzString::fromUtf8(astringf("(Custom: %u)", currValue)), currValue}); + dropDownChoices.push_back({WzString::format("(Custom: %u)", currValue), currValue}); currentSettingIdx = dropDownChoices.size() - 1; } @@ -2400,7 +2400,7 @@ static std::shared_ptr makeCursorScaleDropdown() } else { - dropDownChoices.push_back({WzString::fromUtf8(astringf("(%u%%)", currValue)), currValue}); + dropDownChoices.push_back({WzString::format("(%u%%)", currValue), currValue}); currentSettingIdx = dropDownChoices.size() - 1; } diff --git a/src/hci.cpp b/src/hci.cpp index bef0b7548a9..08ab65dfaf1 100644 --- a/src/hci.cpp +++ b/src/hci.cpp @@ -2431,7 +2431,7 @@ static bool intAddDebugStatsForm(BASE_STATS **_ppsStatsList, UDWORD numStats) bar = widgAddBarGraph(psWScreen, &sBarInit); bar->setBackgroundColour(WZCOL_BLACK); } - WzString costString = WzString::fromUtf8(astringf(_("Cost: %u"), powerCost)); + WzString costString = WzString::format(_("Cost: %u"), powerCost); tipString.append("\n"); tipString.append(costString); button->setTip(tipString.toUtf8().c_str()); diff --git a/src/hci/build.cpp b/src/hci/build.cpp index 33b1aa8128a..4da3eb6f54b 100644 --- a/src/hci/build.cpp +++ b/src/hci/build.cpp @@ -379,7 +379,7 @@ class BuildStatsButton: public StatsButton if (remaining != -1) { - productionRunSizeLabel->setString(WzString::fromUtf8(astringf("%d", remaining))); + productionRunSizeLabel->setString(WzString::format("%d", remaining)); productionRunSizeLabel->show(); } else diff --git a/src/hci/groups.cpp b/src/hci/groups.cpp index 52c1a2cdb35..9dfb53e07a4 100644 --- a/src/hci/groups.cpp +++ b/src/hci/groups.cpp @@ -127,7 +127,7 @@ class GroupButton : public DynamicIntFancyButton { attach(groupNumberLabel = std::make_shared()); groupNumberLabel->setGeometry(OBJ_TEXTX, OBJ_B1TEXTY - 5, 16, 16); - groupNumberLabel->setString(WzString::fromUtf8(astringf("%u", groupNumber))); + groupNumberLabel->setString(WzString::format("%u", groupNumber)); groupNumberLabel->setTransparentToMouse(true); attach(groupCountLabel = std::make_shared()); @@ -141,9 +141,9 @@ class GroupButton : public DynamicIntFancyButton setTip(_("Select / Assign Group Number: ") + groupNumberStr); auto helpInfo = WidgetHelp() - .setTitle(WzString::fromUtf8(astringf(_("Group %u"), groupNumber))) - .addInteraction({WidgetHelp::InteractionTriggers::PrimaryClick}, WzString::fromUtf8(astringf(_("Select the Units in Group %u"), groupNumber))) - .addInteraction({WidgetHelp::InteractionTriggers::SecondaryClick, WidgetHelp::InteractionTriggers::ClickAndHold}, WzString::fromUtf8(astringf(_("Assign Selected Units to Group %u"), groupNumber))) + .setTitle(WzString::format(_("Group %u"), groupNumber)) + .addInteraction({WidgetHelp::InteractionTriggers::PrimaryClick}, WzString::format(_("Select the Units in Group %u"), groupNumber)) + .addInteraction({WidgetHelp::InteractionTriggers::SecondaryClick, WidgetHelp::InteractionTriggers::ClickAndHold}, WzString::format(_("Assign Selected Units to Group %u"), groupNumber)) .addInteraction({WidgetHelp::InteractionTriggers::Misc}, _("Center Camera on this Group by clicking or tapping twice")) .addRelatedKeybinding("SelectGrouping_" + groupNumberStr) .addRelatedKeybinding("AssignGrouping_" + groupNumberStr) @@ -201,7 +201,7 @@ class GroupButton : public DynamicIntFancyButton else { displayIMD(AtlasImage(), ImdObject::DroidTemplate(&(groupInfo->displayDroidTemplate)), xOffset, yOffset); - groupCountLabel->setString(WzString::fromUtf8(astringf("%u", groupInfo->numberInGroup))); + groupCountLabel->setString(WzString::format("%u", groupInfo->numberInGroup)); } if (groupInfo->currAttackGlowAlpha > 0) diff --git a/src/hci/manufacture.cpp b/src/hci/manufacture.cpp index 22fd363eda1..cddc01d9b8f 100644 --- a/src/hci/manufacture.cpp +++ b/src/hci/manufacture.cpp @@ -275,11 +275,11 @@ class ManufactureObjectButton : public ObjectButton } else { - factoryNumberLabel->setString(WzString::fromUtf8(astringf("%u", factory->psAssemblyPoint->factoryInc + 1))); + factoryNumberLabel->setString(WzString::format("%u", factory->psAssemblyPoint->factoryInc + 1)); } if (psStruct->productToGroup != UBYTE_MAX) { - factoryAssignGroupLabel->setString(WzString::fromUtf8(astringf("%u", psStruct->productToGroup))); + factoryAssignGroupLabel->setString(WzString::format("%u", psStruct->productToGroup)); } else { @@ -419,7 +419,7 @@ class ManufactureStatsButton: public StatsButton auto productionRemaining = getProduction(factory, droidTemplate).numRemaining(); if (productionRemaining > 0 && factory && StructureIsManufacturingPending(factory)) { - productionRunSizeLabel->setString(WzString::fromUtf8(astringf("%d", productionRemaining))); + productionRunSizeLabel->setString(WzString::format("%d", productionRemaining)); productionRunSizeLabel->show(); } else diff --git a/src/hci/objects_stats.h b/src/hci/objects_stats.h index bcddbfba5d7..974da364d22 100644 --- a/src/hci/objects_stats.h +++ b/src/hci/objects_stats.h @@ -178,7 +178,7 @@ class StatsFormButton : public StatsButton protected: std::string getTip() override { - WzString costString = WzString::fromUtf8(astringf(_("Cost: %u"), getCost())); + WzString costString = WzString::format(_("Cost: %u"), getCost()); auto stats = getStats(); WzString tipString = (stats == nullptr) ? "" : getLocalizedStatsName(stats); tipString.append("\n"); diff --git a/src/hci/quickchat.cpp b/src/hci/quickchat.cpp index 5627e81a7f9..569d54ed077 100644 --- a/src/hci/quickchat.cpp +++ b/src/hci/quickchat.cpp @@ -1059,7 +1059,7 @@ void WzQuickChatSendToSelector::updateSelectedPlayersString() { selectedPlayersString += " + "; } - selectedPlayersString += WzString::fromUtf8(astringf(_("%u players"), static_cast(additionalPlayers.size()))); + selectedPlayersString += WzString::format(_("%u players"), static_cast(additionalPlayers.size())); } if (selectedPlayersString.isEmpty()) diff --git a/src/multiint.cpp b/src/multiint.cpp index 86258b9b6ea..f0ffd531392 100644 --- a/src/multiint.cpp +++ b/src/multiint.cpp @@ -5309,7 +5309,7 @@ void ChatBoxWidget::displayMessage(RoomMessage const &message) paragraph->setShadeColour({0, 0, 0, 0}); bool specSender = (*message.sender)->isSpectator && !message.sender->isHost(); paragraph->setFontColour((!specSender) ? WZCOL_WHITE : WZCOL_TEXT_MEDIUM); - paragraph->addText(WzString::fromUtf8(astringf(" %s", message.text.c_str()))); + paragraph->addText(WzString::format(" %s", message.text.c_str())); break; } @@ -7753,7 +7753,7 @@ void WzMultiplayerOptionsTitleUI::start() } if (getHostLaunch() == HostLaunch::Autohost) { - changeTitleUI(std::make_shared(WzString(_("Failed to process autohost config:")), WzString::fromUtf8(astringf(_("Failed to load the autohost map or config from: %s"), wz_skirmish_test().c_str())), parent)); + changeTitleUI(std::make_shared(WzString(_("Failed to process autohost config:")), WzString::format(_("Failed to load the autohost map or config from: %s"), wz_skirmish_test().c_str()), parent)); setHostLaunch(HostLaunch::Normal); // Don't load the autohost file on subsequent hosts return; } diff --git a/src/seqdisp.cpp b/src/seqdisp.cpp index ffef87f44cf..8f21d5bd702 100644 --- a/src/seqdisp.cpp +++ b/src/seqdisp.cpp @@ -613,7 +613,7 @@ bool seq_UpdateFullScreenVideo() { wzCachedSeqText.resize(2); } - wzCachedSeqText[0].setText(WzString::fromUtf8(astringf("%s (%" PRIu32 "%%)...", _("Loading video"), onDemandVideoProvider.getVideoDataRequestProgress(currVideoName))), font_scaled); + wzCachedSeqText[0].setText(WzString::format("%s (%" PRIu32 "%%)...", _("Loading video"), onDemandVideoProvider.getVideoDataRequestProgress(currVideoName)), font_scaled); wzCachedSeqText[0].render((pie_GetVideoBufferWidth() - wzCachedSeqText[0].width()) / 2, (pie_GetVideoBufferHeight() - wzCachedSeqText[0].height()) / 2, WZCOL_WHITE); return true; } diff --git a/src/spectatorwidgets.cpp b/src/spectatorwidgets.cpp index 475350808c1..7c346a947e1 100644 --- a/src/spectatorwidgets.cpp +++ b/src/spectatorwidgets.cpp @@ -691,7 +691,7 @@ if ((!NetPlay.players[playerIdx].allocated && NetPlay.players[playerIdx].ai < 0) } else { - label.setString(WzString::fromUtf8(astringf("%.2f%%", ((float)sts.recentResearchPerformance/(float)sts.recentResearchPotential)*100))); + label.setString(WzString::format("%.2f%%", ((float)sts.recentResearchPerformance/(float)sts.recentResearchPotential)*100)); } ADJUST_LABEL_COLOR_FOR_PLAYERS(); }, INFO_UPDATE_INTERVAL_TICKS)); diff --git a/src/texture.cpp b/src/texture.cpp index 5638ce147bd..1f6952890cc 100644 --- a/src/texture.cpp +++ b/src/texture.cpp @@ -310,15 +310,15 @@ bool texLoad(const char *fileName) std::vector usedFilenames_tmp; for (k = 0; k <= maxTileNo; ++k) { - auto fullPath_base = gfx_api::imageLoadFilenameFromInputFilename(WzString::fromUtf8(astringf("%s/tile-%02d.png", partialPath, k))); + auto fullPath_base = gfx_api::imageLoadFilenameFromInputFilename(WzString::format("%s/tile-%02d.png", partialPath, k)); tile_base_filepaths.push_back(fullPath_base); usedFilenames_tmp.push_back(fullPath_base); if (has_auxillary_texture_info) { - fullPath_nm = gfx_api::imageLoadFilenameFromInputFilename(WzString::fromUtf8(astringf("%s/tile-%02d_nm.png", partialPath, k))); - fullPath_sm = gfx_api::imageLoadFilenameFromInputFilename(WzString::fromUtf8(astringf("%s/tile-%02d_sm.png", partialPath, k))); - fullPath_hm = gfx_api::imageLoadFilenameFromInputFilename(WzString::fromUtf8(astringf("%s/tile-%02d_hm.png", partialPath, k))); + fullPath_nm = gfx_api::imageLoadFilenameFromInputFilename(WzString::format("%s/tile-%02d_nm.png", partialPath, k)); + fullPath_sm = gfx_api::imageLoadFilenameFromInputFilename(WzString::format("%s/tile-%02d_sm.png", partialPath, k)); + fullPath_hm = gfx_api::imageLoadFilenameFromInputFilename(WzString::format("%s/tile-%02d_hm.png", partialPath, k)); if (has_nm) { diff --git a/src/wzscriptdebug.cpp b/src/wzscriptdebug.cpp index e3f42147802..4a0014c4418 100644 --- a/src/wzscriptdebug.cpp +++ b/src/wzscriptdebug.cpp @@ -1195,7 +1195,7 @@ class WzGraphicsPanel : public W_FORM } else { - dropDownChoices.push_back({WzString::fromUtf8(astringf("(Custom: %u)", currValue)), currValue}); + dropDownChoices.push_back({WzString::format("(Custom: %u)", currValue), currValue}); currentSettingIdx = dropDownChoices.size() - 1; } @@ -1270,7 +1270,7 @@ class WzGraphicsPanel : public W_FORM } else { - dropDownChoices.push_back({WzString::fromUtf8(astringf("(Custom: %u)", currValue)), currValue}); + dropDownChoices.push_back({WzString::format("(Custom: %u)", currValue), currValue}); currentSettingIdx = dropDownChoices.size() - 1; } @@ -1343,7 +1343,7 @@ class WzGraphicsPanel : public W_FORM } else { - dropDownChoices.push_back({WzString::fromUtf8(astringf("(Custom: %" PRIu32 ")", currValue)), currValue}); + dropDownChoices.push_back({WzString::format("(Custom: %" PRIu32 ")", currValue), currValue}); currentSettingIdx = dropDownChoices.size() - 1; }