From ea2cedb5ea9377d0db60aa01fed42c5074b42dff Mon Sep 17 00:00:00 2001 From: Evil Eye Date: Sun, 18 Jun 2023 19:10:29 +0200 Subject: [PATCH] Build the attribute list in the stats window dynamically --- apps/openmw/mwgui/charactercreation.cpp | 18 +-- apps/openmw/mwgui/charactercreation.hpp | 2 +- apps/openmw/mwgui/statswatcher.cpp | 20 ++-- apps/openmw/mwgui/statswatcher.hpp | 4 +- apps/openmw/mwgui/statswindow.cpp | 69 ++++++------ apps/openmw/mwgui/statswindow.hpp | 4 +- files/data/mygui/openmw_stats_window.layout | 116 +------------------- 7 files changed, 54 insertions(+), 179 deletions(-) diff --git a/apps/openmw/mwgui/charactercreation.cpp b/apps/openmw/mwgui/charactercreation.cpp index dc1608b9c7..114238482d 100644 --- a/apps/openmw/mwgui/charactercreation.cpp +++ b/apps/openmw/mwgui/charactercreation.cpp @@ -102,21 +102,11 @@ namespace MWGui mPlayerSkillValues.emplace(skill.mId, MWMechanics::SkillValue()); } - void CharacterCreation::setValue(std::string_view id, const MWMechanics::AttributeValue& value) + void CharacterCreation::setValue(ESM::Attribute::AttributeID id, const MWMechanics::AttributeValue& value) { - std::string_view prefix = "AttribVal"; - if (id.starts_with(prefix) && id.size() == prefix.size() + 1) - { - char index = id[prefix.size()]; - if (index >= '1' && index <= '8') - { - // Match [AttribVal1-AttribVal8] to the corresponding AttributeID values [0-7] - auto attribute = static_cast(index - '0' - 1); - mPlayerAttributes[attribute] = value; - if (mReviewDialog) - mReviewDialog->setAttribute(attribute, value); - } - } + mPlayerAttributes[id] = value; + if (mReviewDialog) + mReviewDialog->setAttribute(id, value); } void CharacterCreation::setValue(std::string_view id, const MWMechanics::DynamicStat& value) diff --git a/apps/openmw/mwgui/charactercreation.hpp b/apps/openmw/mwgui/charactercreation.hpp index f9e76d1fc6..d590c0cfd6 100644 --- a/apps/openmw/mwgui/charactercreation.hpp +++ b/apps/openmw/mwgui/charactercreation.hpp @@ -44,7 +44,7 @@ namespace MWGui // Show a dialog void spawnDialog(const char id); - void setValue(std::string_view id, const MWMechanics::AttributeValue& value) override; + void setValue(ESM::Attribute::AttributeID id, const MWMechanics::AttributeValue& value) override; void setValue(std::string_view id, const MWMechanics::DynamicStat& value) override; void setValue(ESM::RefId id, const MWMechanics::SkillValue& value) override; void configureSkills(const std::vector& major, const std::vector& minor) override; diff --git a/apps/openmw/mwgui/statswatcher.cpp b/apps/openmw/mwgui/statswatcher.cpp index dab6b9bf82..b18073f7ad 100644 --- a/apps/openmw/mwgui/statswatcher.cpp +++ b/apps/openmw/mwgui/statswatcher.cpp @@ -35,14 +35,16 @@ namespace MWGui if (mWatched.isEmpty()) return; + const auto& store = MWBase::Environment::get().getESMStore(); MWBase::WindowManager* winMgr = MWBase::Environment::get().getWindowManager(); const MWMechanics::NpcStats& stats = mWatched.getClass().getNpcStats(mWatched); - for (int i = 0; i < ESM::Attribute::Length; ++i) + for (const ESM::Attribute& attribute : store->get()) { - if (stats.getAttribute(i) != mWatchedAttributes[i] || mWatchedStatsEmpty) + const auto& value = stats.getAttribute(attribute.mId); + if (value != mWatchedAttributes[attribute.mId] || mWatchedStatsEmpty) { - mWatchedAttributes[i] = stats.getAttribute(i); - setValue("AttribVal" + std::to_string(i + 1), stats.getAttribute(i)); + mWatchedAttributes[attribute.mId] = value; + setValue(attribute.mId, value); } } @@ -83,7 +85,7 @@ namespace MWGui } } - for (const ESM::Skill& skill : MWBase::Environment::get().getESMStore()->get()) + for (const ESM::Skill& skill : store->get()) { const auto& value = stats.getSkill(skill.mId); if (value != mWatchedSkills[skill.mId] || mWatchedStatsEmpty) @@ -112,16 +114,14 @@ namespace MWGui if (watchedRecord->mRace != mWatchedRace || mWatchedStatsEmpty) { mWatchedRace = watchedRecord->mRace; - const ESM::Race* race - = MWBase::Environment::get().getESMStore()->get().find(watchedRecord->mRace); + const ESM::Race* race = store->get().find(watchedRecord->mRace); setValue("race", race->mName); } if (watchedRecord->mClass != mWatchedClass || mWatchedStatsEmpty) { mWatchedClass = watchedRecord->mClass; - const ESM::Class* cls - = MWBase::Environment::get().getESMStore()->get().find(watchedRecord->mClass); + const ESM::Class* cls = store->get().find(watchedRecord->mClass); setValue("class", cls->mName); size_t size = cls->mData.mSkills.size(); @@ -151,7 +151,7 @@ namespace MWGui mListeners.erase(listener); } - void StatsWatcher::setValue(std::string_view id, const MWMechanics::AttributeValue& value) + void StatsWatcher::setValue(ESM::Attribute::AttributeID id, const MWMechanics::AttributeValue& value) { for (StatsListener* listener : mListeners) listener->setValue(id, value); diff --git a/apps/openmw/mwgui/statswatcher.hpp b/apps/openmw/mwgui/statswatcher.hpp index e7339294da..e88d72c543 100644 --- a/apps/openmw/mwgui/statswatcher.hpp +++ b/apps/openmw/mwgui/statswatcher.hpp @@ -19,7 +19,7 @@ namespace MWGui virtual ~StatsListener() = default; /// Set value for the given ID. - virtual void setValue(std::string_view id, const MWMechanics::AttributeValue& value) {} + virtual void setValue(ESM::Attribute::AttributeID id, const MWMechanics::AttributeValue& value) {} virtual void setValue(std::string_view id, const MWMechanics::DynamicStat& value) {} virtual void setValue(std::string_view, const std::string& value) {} virtual void setValue(std::string_view, int value) {} @@ -50,7 +50,7 @@ namespace MWGui std::set mListeners; - void setValue(std::string_view id, const MWMechanics::AttributeValue& value); + void setValue(ESM::Attribute::AttributeID id, const MWMechanics::AttributeValue& value); void setValue(std::string_view id, const MWMechanics::DynamicStat& value); void setValue(std::string_view id, const std::string& value); void setValue(std::string_view id, int value); diff --git a/apps/openmw/mwgui/statswindow.cpp b/apps/openmw/mwgui/statswindow.cpp index 4bd82faa70..b095ef67f8 100644 --- a/apps/openmw/mwgui/statswindow.cpp +++ b/apps/openmw/mwgui/statswindow.cpp @@ -1,5 +1,6 @@ #include "statswindow.hpp" +#include #include #include #include @@ -43,15 +44,26 @@ namespace MWGui , mMinFullWidth(mMainWidget->getSize().width) { - const char* names[][2] = { { "Attrib1", "sAttributeStrength" }, { "Attrib2", "sAttributeIntelligence" }, - { "Attrib3", "sAttributeWillpower" }, { "Attrib4", "sAttributeAgility" }, { "Attrib5", "sAttributeSpeed" }, - { "Attrib6", "sAttributeEndurance" }, { "Attrib7", "sAttributePersonality" }, - { "Attrib8", "sAttributeLuck" }, { 0, 0 } }; - const MWWorld::ESMStore& store = *MWBase::Environment::get().getESMStore(); - for (int i = 0; names[i][0]; ++i) + MyGUI::Widget* attributeView = getWidget("AttributeView"); + MyGUI::IntCoord coord{ 0, 0, 204, 144 }; + const MyGUI::Align align = MyGUI::Align::Left | MyGUI::Align::Top | MyGUI::Align::HStretch; + for (const ESM::Attribute& attribute : store.get()) { - setText(names[i][0], store.get().find(names[i][1])->mValue.getString()); + auto* box = attributeView->createWidget({}, coord, align); + box->setUserString("ToolTipType", "Layout"); + box->setUserString("ToolTipLayout", "AttributeToolTip"); + box->setUserString("Caption_AttributeName", attribute.mName); + box->setUserString("Caption_AttributeDescription", attribute.mDescription); + box->setUserString("ImageTexture_AttributeImage", attribute.mIcon); + coord.top += 18; + auto* name = box->createWidget("SandText", { 0, 0, 160, 18 }, align); + name->setNeedMouseFocus(false); + name->setCaption(attribute.mName); + auto* value = box->createWidget( + "SandTextRight", { 160, 0, 44, 18 }, MyGUI::Align::Right | MyGUI::Align::Top); + value->setNeedMouseFocus(false); + mAttributeWidgets.emplace(attribute.mId, value); } getWidget(mSkillView, "SkillView"); @@ -143,37 +155,20 @@ namespace MWGui mMainWidget->castType()->setCaption(playerName); } - void StatsWindow::setValue(std::string_view id, const MWMechanics::AttributeValue& value) + void StatsWindow::setValue(ESM::Attribute::AttributeID id, const MWMechanics::AttributeValue& value) { - static const char* ids[] = { - "AttribVal1", - "AttribVal2", - "AttribVal3", - "AttribVal4", - "AttribVal5", - "AttribVal6", - "AttribVal7", - "AttribVal8", - nullptr, - }; - - for (int i = 0; ids[i]; ++i) - if (ids[i] == id) - { - setText(id, std::to_string(static_cast(value.getModified()))); - - MyGUI::TextBox* box; - getWidget(box, id); - - if (value.getModified() > value.getBase()) - box->_setWidgetState("increased"); - else if (value.getModified() < value.getBase()) - box->_setWidgetState("decreased"); - else - box->_setWidgetState("normal"); - - break; - } + auto it = mAttributeWidgets.find(id); + if (it != mAttributeWidgets.end()) + { + MyGUI::TextBox* box = it->second; + box->setCaption(std::to_string(static_cast(value.getModified()))); + if (value.getModified() > value.getBase()) + box->_setWidgetState("increased"); + else if (value.getModified() < value.getBase()) + box->_setWidgetState("decreased"); + else + box->_setWidgetState("normal"); + } } void StatsWindow::setValue(std::string_view id, const MWMechanics::DynamicStat& value) diff --git a/apps/openmw/mwgui/statswindow.hpp b/apps/openmw/mwgui/statswindow.hpp index ca7a5a7e07..92926c1180 100644 --- a/apps/openmw/mwgui/statswindow.hpp +++ b/apps/openmw/mwgui/statswindow.hpp @@ -3,6 +3,7 @@ #include "statswatcher.hpp" #include "windowpinnablebase.hpp" +#include #include namespace MWGui @@ -21,7 +22,7 @@ namespace MWGui void setPlayerName(const std::string& playerName); /// Set value for the given ID. - void setValue(std::string_view id, const MWMechanics::AttributeValue& value) override; + void setValue(ESM::Attribute::AttributeID id, const MWMechanics::AttributeValue& value) override; void setValue(std::string_view id, const MWMechanics::DynamicStat& value) override; void setValue(std::string_view id, const std::string& value) override; void setValue(std::string_view id, int value) override; @@ -67,6 +68,7 @@ namespace MWGui std::vector mMajorSkills, mMinorSkills, mMiscSkills; std::map mSkillValues; + std::map mAttributeWidgets; std::map> mSkillWidgetMap; std::map mFactionWidgetMap; FactionList mFactions; ///< Stores a list of factions and the current rank diff --git a/files/data/mygui/openmw_stats_window.layout b/files/data/mygui/openmw_stats_window.layout index 77de3ac753..93a648f0e0 100644 --- a/files/data/mygui/openmw_stats_window.layout +++ b/files/data/mygui/openmw_stats_window.layout @@ -113,120 +113,8 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + +