Merge branch 'formattedtooltips' into 'master'

Use std::string_view and std::format in MWGui::ToolTips

See merge request OpenMW/openmw!4855
This commit is contained in:
psi29a 2025-08-18 19:26:16 +00:00
commit 54b130c5aa
5 changed files with 63 additions and 42 deletions

View File

@ -300,9 +300,9 @@ namespace MWGui
ESM::Class::Specialization specialization
= static_cast<ESM::Class::Specialization>(currentClass->mData.mSpecialization);
std::string specName{ MWBase::Environment::get().getWindowManager()->getGameSettingString(
ESM::Class::sGmstSpecializationIds[specialization], ESM::Class::sGmstSpecializationIds[specialization]) };
mSpecializationName->setCaption(specName);
std::string_view specName = MWBase::Environment::get().getWindowManager()->getGameSettingString(
ESM::Class::sGmstSpecializationIds[specialization], ESM::Class::sGmstSpecializationIds[specialization]);
mSpecializationName->setCaption(MyGUI::UString(specName));
ToolTips::createSpecializationToolTip(mSpecializationName, specName, specialization);
mFavoriteAttribute[0]->setAttributeId(ESM::Attribute::indexToRefId(currentClass->mData.mAttribute[0]));

View File

@ -1,5 +1,6 @@
#include "tooltips.hpp"
#include <format>
#include <iomanip>
#include <MyGUI_Gui.h>
@ -52,7 +53,7 @@ namespace MWGui
mDynamicToolTipBox->setNeedMouseFocus(false);
mMainWidget->setNeedMouseFocus(false);
for (unsigned int i = 0; i < mMainWidget->getChildCount(); ++i)
for (size_t i = 0; i < mMainWidget->getChildCount(); ++i)
{
mMainWidget->getChildAt(i)->setVisible(false);
}
@ -77,7 +78,7 @@ namespace MWGui
}
// start by hiding everything
for (unsigned int i = 0; i < mMainWidget->getChildCount(); ++i)
for (size_t i = 0; i < mMainWidget->getChildCount(); ++i)
{
mMainWidget->getChildAt(i)->setVisible(false);
}
@ -343,7 +344,7 @@ namespace MWGui
MyGUI::Gui::getInstance().destroyWidget(mDynamicToolTipBox->getChildAt(0));
}
for (unsigned int i = 0; i < mMainWidget->getChildCount(); ++i)
for (size_t i = 0; i < mMainWidget->getChildCount(); ++i)
{
mMainWidget->getChildAt(i)->setVisible(false);
}
@ -642,13 +643,13 @@ namespace MWGui
std::string ToolTips::toString(const float value)
{
std::ostringstream stream;
if (value != int(value))
stream << std::setprecision(3);
stream << value;
return stream.str();
std::string s = std::format("{:.2f}", value);
// Trim result so 1.00 turns into 1
while (!s.empty() && s.back() == '0')
s.pop_back();
if (!s.empty() && s.back() == '.')
s.pop_back();
return s;
}
std::string ToolTips::toString(const int value)
@ -656,44 +657,39 @@ namespace MWGui
return std::to_string(value);
}
std::string ToolTips::getWeightString(const float weight, const std::string& prefix)
std::string ToolTips::getWeightString(const float weight, std::string_view prefix)
{
if (weight == 0)
return {};
else
return "\n" + prefix + ": " + toString(weight);
return std::format("\n{}: {}", prefix, toString(weight));
}
std::string ToolTips::getPercentString(const float value, const std::string& prefix)
std::string ToolTips::getPercentString(const float value, std::string_view prefix)
{
if (value == 0)
return {};
else
return "\n" + prefix + ": " + toString(value * 100) + "%";
return std::format("\n{}: {}%", prefix, toString(value * 100));
}
std::string ToolTips::getValueString(const int value, const std::string& prefix)
std::string ToolTips::getValueString(const int value, std::string_view prefix)
{
if (value == 0)
return {};
else
return "\n" + prefix + ": " + toString(value);
return std::format("\n{}: {}", prefix, value);
}
std::string ToolTips::getMiscString(const std::string& text, const std::string& prefix)
std::string ToolTips::getMiscString(std::string_view text, std::string_view prefix)
{
if (text.empty())
return {};
else
return "\n" + prefix + ": " + text;
return std::format("\n{}: {}", prefix, text);
}
std::string ToolTips::getCountString(const int value)
{
if (value == 1)
return {};
else
return " (" + MyGUI::utility::toString(value) + ")";
return std::format(" ({})", value);
}
std::string ToolTips::getSoulString(const MWWorld::CellRef& cellref)
@ -706,8 +702,8 @@ namespace MWGui
if (!creature)
return {};
if (creature->mName.empty())
return " (" + creature->mId.toDebugString() + ")";
return " (" + creature->mName + ")";
return std::format(" ({})", creature->mId.toDebugString());
return std::format(" ({})", creature->mName);
}
std::string ToolTips::getCellRefString(const MWWorld::CellRef& cellref)
@ -740,22 +736,21 @@ namespace MWGui
for (std::pair<ESM::RefId, int>& owner : itemOwners)
{
if (owner.second == std::numeric_limits<int>::max())
ret += std::string("\nStolen from ") + owner.first.toDebugString(); // for legacy (ESS) savegames
ret += std::format("\nStolen from {}", owner.first.toDebugString()); // for legacy (ESS) savegames
else
ret += std::string("\nStolen ") + MyGUI::utility::toString(owner.second) + " from "
+ owner.first.toDebugString();
ret += std::format("\nStolen {} from {}", owner.second, owner.first.toDebugString());
}
ret += getMiscString(cellref.getGlobalVariable(), "Global");
return ret;
}
std::string ToolTips::getDurationString(float duration, const std::string& prefix)
std::string ToolTips::getDurationString(float duration, std::string_view prefix)
{
auto l10n = MWBase::Environment::get().getL10nManager()->getContext("Interface");
std::string ret;
ret = prefix + ": ";
std::string ret(prefix);
ret += ": ";
if (duration < 1.f)
{
@ -858,7 +853,7 @@ namespace MWGui
widget->setUserString("ImageTexture_AttributeImage", attribute->mIcon);
}
void ToolTips::createSpecializationToolTip(MyGUI::Widget* widget, const std::string& name, int specId)
void ToolTips::createSpecializationToolTip(MyGUI::Widget* widget, std::string_view name, int specId)
{
widget->setUserString("Caption_Caption", name);
std::string specText;

View File

@ -67,12 +67,12 @@ namespace MWGui
void setFocusObjectScreenCoords(float x, float y);
///< set the screen-space position of the tooltip for focused object
static std::string getWeightString(const float weight, const std::string& prefix);
static std::string getPercentString(const float value, const std::string& prefix);
static std::string getValueString(const int value, const std::string& prefix);
static std::string getWeightString(const float weight, std::string_view prefix);
static std::string getPercentString(const float value, std::string_view prefix);
static std::string getValueString(const int value, std::string_view prefix);
///< @return "prefix: value" or "" if value is 0
static std::string getMiscString(const std::string& text, const std::string& prefix);
static std::string getMiscString(std::string_view text, std::string_view prefix);
///< @return "prefix: text" or "" if text is empty
static std::string toString(const float value);
@ -87,14 +87,14 @@ namespace MWGui
static std::string getCellRefString(const MWWorld::CellRef& cellref);
///< Returns a string containing debug tooltip information about the given cellref.
static std::string getDurationString(float duration, const std::string& prefix);
static std::string getDurationString(float duration, std::string_view prefix);
///< Returns duration as two largest time units, rounded down. Note: not localized; no line break.
// these do not create an actual tooltip, but they fill in the data that is required so the tooltip
// system knows what to show in case this widget is hovered
static void createSkillToolTip(MyGUI::Widget* widget, ESM::RefId skillId);
static void createAttributeToolTip(MyGUI::Widget* widget, ESM::RefId attributeId);
static void createSpecializationToolTip(MyGUI::Widget* widget, const std::string& name, int specId);
static void createSpecializationToolTip(MyGUI::Widget* widget, std::string_view name, int specId);
static void createBirthsignToolTip(MyGUI::Widget* widget, const ESM::RefId& birthsignId);
static void createRaceToolTip(MyGUI::Widget* widget, const ESM::Race* playerRace);
static void createClassToolTip(MyGUI::Widget* widget, const ESM::Class& playerClass);

View File

@ -14,6 +14,8 @@ file(GLOB UNITTEST_SRC_FILES
mwdialogue/testkeywordsearch.cpp
mwgui/tooltips.cpp
mwscript/testscripts.cpp
)

View File

@ -0,0 +1,24 @@
#include <gtest/gtest.h>
#include "apps/openmw/mwgui/tooltips.hpp"
#include <limits>
namespace MWGui
{
namespace
{
TEST(MWGuiToolTipsTest, floatsShouldBeFormattedCorrectly)
{
EXPECT_EQ(ToolTips::toString(1.f), "1");
EXPECT_EQ(ToolTips::toString(1.1f), "1.1");
EXPECT_EQ(ToolTips::toString(1.12f), "1.12");
EXPECT_EQ(ToolTips::toString(1234567.12f), "1234567.12");
EXPECT_EQ(ToolTips::toString(0.001f), "0");
EXPECT_EQ(ToolTips::toString(0.01f), "0.01");
EXPECT_EQ(ToolTips::toString(0.012f), "0.01");
EXPECT_EQ(ToolTips::toString(std::numeric_limits<float>::infinity()), "inf");
EXPECT_EQ(ToolTips::toString(std::numeric_limits<float>::quiet_NaN()), "nan");
}
}
}