From 6ab36c05399ebcefceb5bec1bffbcc56c8a71697 Mon Sep 17 00:00:00 2001 From: Andrei Kortunov Date: Thu, 27 Jul 2017 13:20:18 +0400 Subject: [PATCH] Spellbuying menu improvements --- apps/openmw/mwgui/spellbuyingwindow.cpp | 38 ++++++++++++++++++------- apps/openmw/mwgui/spellbuyingwindow.hpp | 9 ++++-- apps/openmw/mwgui/windowmanagerimp.cpp | 2 +- 3 files changed, 36 insertions(+), 13 deletions(-) diff --git a/apps/openmw/mwgui/spellbuyingwindow.cpp b/apps/openmw/mwgui/spellbuyingwindow.cpp index 806b17a90..6a7376aa0 100644 --- a/apps/openmw/mwgui/spellbuyingwindow.cpp +++ b/apps/openmw/mwgui/spellbuyingwindow.cpp @@ -22,7 +22,6 @@ namespace MWGui SpellBuyingWindow::SpellBuyingWindow() : WindowBase("openmw_spell_buying_window.layout") - , mLastPos(0) , mCurrentY(0) { getWidget(mCancelButton, "CancelButton"); @@ -37,13 +36,20 @@ namespace MWGui MWBase::Environment::get().getWindowManager()->removeGuiMode(GM_SpellBuying); } - void SpellBuyingWindow::addSpell(const std::string& spellId) + bool SpellBuyingWindow::sortSpells (const ESM::Spell* left, const ESM::Spell* right) + { + std::string leftName = Misc::StringUtils::lowerCase(left->mName); + std::string rightName = Misc::StringUtils::lowerCase(right->mName); + + return leftName.compare(rightName) < 0; + } + + void SpellBuyingWindow::addSpell(const ESM::Spell& spell) { const MWWorld::ESMStore &store = MWBase::Environment::get().getWorld()->getStore(); - const ESM::Spell* spell = MWBase::Environment::get().getWorld()->getStore().get().find(spellId); - int price = static_cast(spell->mData.mCost*store.get().find("fSpellValueMult")->getFloat()); + int price = static_cast(spell.mData.mCost*store.get().find("fSpellValueMult")->getFloat()); price = MWBase::Environment::get().getMechanicsManager()->getBarterOffer(mPtr,price,true); MWWorld::Ptr player = MWMechanics::getPlayer(); @@ -64,13 +70,13 @@ namespace MWGui mCurrentY += sLineHeight; toAdd->setUserData(price); - toAdd->setCaptionWithReplacing(spell->mName+" - "+MyGUI::utility::toString(price)+"#{sgp}"); + toAdd->setCaptionWithReplacing(spell.mName+" - "+MyGUI::utility::toString(price)+"#{sgp}"); toAdd->setSize(mSpellsView->getWidth(),sLineHeight); toAdd->eventMouseWheel += MyGUI::newDelegate(this, &SpellBuyingWindow::onMouseWheel); toAdd->setUserString("ToolTipType", "Spell"); - toAdd->setUserString("Spell", spellId); + toAdd->setUserString("Spell", spell.mId); toAdd->eventMouseButtonClick += MyGUI::newDelegate(this, &SpellBuyingWindow::onSpellButtonClick); - mSpellsWidgetMap.insert(std::make_pair (toAdd, spellId)); + mSpellsWidgetMap.insert(std::make_pair (toAdd, spell.mId)); } void SpellBuyingWindow::clearSpells() @@ -82,7 +88,7 @@ namespace MWGui mSpellsWidgetMap.clear(); } - void SpellBuyingWindow::startSpellBuying(const MWWorld::Ptr& actor) + void SpellBuyingWindow::startSpellBuying(const MWWorld::Ptr& actor, int startOffset) { center(); mPtr = actor; @@ -90,6 +96,8 @@ namespace MWGui MWMechanics::Spells& merchantSpells = actor.getClass().getCreatureStats (actor).getSpells(); + std::vector spellsToSort; + for (MWMechanics::Spells::TIterator iter = merchantSpells.begin(); iter!=merchantSpells.end(); ++iter) { const ESM::Spell* spell = iter->first; @@ -109,15 +117,25 @@ namespace MWGui if (playerHasSpell(iter->first->mId)) continue; - addSpell (iter->first->mId); + spellsToSort.push_back(iter->first); } + std::stable_sort(spellsToSort.begin(), spellsToSort.end(), sortSpells); + + for (std::vector::iterator it = spellsToSort.begin() ; it != spellsToSort.end(); ++it) + { + addSpell(**it); + } + + spellsToSort.clear(); + updateLabels(); // Canvas size must be expressed with VScroll disabled, otherwise MyGUI would expand the scroll area when the scrollbar is hidden mSpellsView->setVisibleVScroll(false); mSpellsView->setCanvasSize (MyGUI::IntSize(mSpellsView->getWidth(), std::max(mSpellsView->getHeight(), mCurrentY))); mSpellsView->setVisibleVScroll(true); + mSpellsView->setViewOffset(MyGUI::IntPoint(0, startOffset)); } bool SpellBuyingWindow::playerHasSpell(const std::string &id) @@ -143,7 +161,7 @@ namespace MWGui MWMechanics::CreatureStats& npcStats = mPtr.getClass().getCreatureStats(mPtr); npcStats.setGoldPool(npcStats.getGoldPool() + price); - startSpellBuying(mPtr); + startSpellBuying(mPtr, mSpellsView->getViewOffset().top); MWBase::Environment::get().getWindowManager()->playSound("Item Gold Up"); } diff --git a/apps/openmw/mwgui/spellbuyingwindow.hpp b/apps/openmw/mwgui/spellbuyingwindow.hpp index 2a6dcfdcc..37210819f 100644 --- a/apps/openmw/mwgui/spellbuyingwindow.hpp +++ b/apps/openmw/mwgui/spellbuyingwindow.hpp @@ -4,6 +4,8 @@ #include "windowbase.hpp" #include "referenceinterface.hpp" +#include "../mwworld/esmstore.hpp" + namespace MyGUI { class Gui; @@ -23,7 +25,7 @@ namespace MWGui public: SpellBuyingWindow(); - void startSpellBuying(const MWWorld::Ptr& actor); + void startSpellBuying(const MWWorld::Ptr& actor, int startOffset); virtual void exit(); @@ -38,7 +40,7 @@ namespace MWGui void onCancelButtonClicked(MyGUI::Widget* _sender); void onSpellButtonClick(MyGUI::Widget* _sender); void onMouseWheel(MyGUI::Widget* _sender, int _rel); - void addSpell(const std::string& spellID); + void addSpell(const ESM::Spell& spell); void clearSpells(); int mLastPos,mCurrentY; @@ -49,6 +51,9 @@ namespace MWGui virtual void onReferenceUnavailable(); bool playerHasSpell (const std::string& id); + + private: + static bool sortSpells (const ESM::Spell* left, const ESM::Spell* right); }; } diff --git a/apps/openmw/mwgui/windowmanagerimp.cpp b/apps/openmw/mwgui/windowmanagerimp.cpp index bc77eb069..21dde4e0a 100644 --- a/apps/openmw/mwgui/windowmanagerimp.cpp +++ b/apps/openmw/mwgui/windowmanagerimp.cpp @@ -2001,7 +2001,7 @@ namespace MWGui void WindowManager::startSpellBuying(const MWWorld::Ptr &actor) { pushGuiMode(GM_SpellBuying); - mSpellBuyingWindow->startSpellBuying(actor); + mSpellBuyingWindow->startSpellBuying(actor, 0); } void WindowManager::startTrade(const MWWorld::Ptr &actor)