Skip to content

Commit

Permalink
Create a shorthand for the formatted wzstring
Browse files Browse the repository at this point in the history
  • Loading branch information
Monsterovich committed Jul 21, 2024
1 parent b90c249 commit bd3d498
Show file tree
Hide file tree
Showing 14 changed files with 35 additions and 29 deletions.
6 changes: 6 additions & 0 deletions lib/framework/wzstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ class WzString {
static WzString fromUtf32(const std::vector<uint32_t>& utf32);
static WzString fromCodepoint(const WzUniCodepoint& codepoint);

template <typename... P>
static WzString format(char const *format, P &&... params)
{
return WzString::fromUtf8(astringf(format, params...));
}

const std::string& toUtf8() const;

// Same as `toUtf8()`
Expand Down
4 changes: 2 additions & 2 deletions src/design.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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});
}
Expand Down
8 changes: 4 additions & 4 deletions src/frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ static std::shared_ptr<WIDGET> makeLODDistanceDropdown()
}
else
{
dropDownChoices.push_back({WzString::fromUtf8(astringf("(%d)", currValue)), currValue});
dropDownChoices.push_back({WzString::format("(%d)", currValue), currValue});
currentSettingIdx = dropDownChoices.size() - 1;
}

Expand Down Expand Up @@ -958,7 +958,7 @@ static std::shared_ptr<WIDGET> 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;
}

Expand Down Expand Up @@ -1019,7 +1019,7 @@ static std::shared_ptr<WIDGET> 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;
}

Expand Down Expand Up @@ -2400,7 +2400,7 @@ static std::shared_ptr<WIDGET> makeCursorScaleDropdown()
}
else
{
dropDownChoices.push_back({WzString::fromUtf8(astringf("(%u%%)", currValue)), currValue});
dropDownChoices.push_back({WzString::format("(%u%%)", currValue), currValue});
currentSettingIdx = dropDownChoices.size() - 1;
}

Expand Down
2 changes: 1 addition & 1 deletion src/hci.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion src/hci/build.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/hci/groups.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class GroupButton : public DynamicIntFancyButton
{
attach(groupNumberLabel = std::make_shared<W_LABEL>());
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<W_LABEL>());
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions src/hci/manufacture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/hci/objects_stats.h
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion src/hci/quickchat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,7 @@ void WzQuickChatSendToSelector::updateSelectedPlayersString()
{
selectedPlayersString += " + ";
}
selectedPlayersString += WzString::fromUtf8(astringf(_("%u players"), static_cast<unsigned>(additionalPlayers.size())));
selectedPlayersString += WzString::format(_("%u players"), static_cast<unsigned>(additionalPlayers.size()));
}

if (selectedPlayersString.isEmpty())
Expand Down
4 changes: 2 additions & 2 deletions src/multiint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -7753,7 +7753,7 @@ void WzMultiplayerOptionsTitleUI::start()
}
if (getHostLaunch() == HostLaunch::Autohost)
{
changeTitleUI(std::make_shared<WzMsgBoxTitleUI>(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<WzMsgBoxTitleUI>(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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/seqdisp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/spectatorwidgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
8 changes: 4 additions & 4 deletions src/texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,15 +310,15 @@ bool texLoad(const char *fileName)
std::vector<WzString> 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)
{
Expand Down
6 changes: 3 additions & 3 deletions src/wzscriptdebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit bd3d498

Please sign in to comment.