1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-16 01:49:54 +00:00
openmw-tes3mp/apps/openmw/mwgui/spellbuyingwindow.cpp

203 lines
7.6 KiB
C++
Raw Normal View History

#include "spellbuyingwindow.hpp"
2012-09-08 22:17:03 +00:00
2015-01-10 01:50:43 +00:00
#include <MyGUI_Gui.h>
#include <MyGUI_Button.h>
#include <MyGUI_ScrollView.h>
2012-09-08 22:17:03 +00:00
#include "../mwbase/environment.hpp"
#include "../mwbase/world.hpp"
#include "../mwbase/windowmanager.hpp"
#include "../mwbase/mechanicsmanager.hpp"
2012-09-08 22:17:03 +00:00
2013-05-11 16:38:27 +00:00
#include "../mwworld/class.hpp"
#include "../mwworld/containerstore.hpp"
2014-02-23 19:11:05 +00:00
#include "../mwworld/esmstore.hpp"
2012-09-08 22:17:03 +00:00
#include "../mwmechanics/creaturestats.hpp"
2015-08-21 09:12:39 +00:00
#include "../mwmechanics/actorutil.hpp"
2012-09-08 22:17:03 +00:00
namespace MWGui
{
SpellBuyingWindow::SpellBuyingWindow() :
WindowBase("openmw_spell_buying_window.layout")
, mCurrentY(0)
2012-09-08 22:17:03 +00:00
{
getWidget(mCancelButton, "CancelButton");
getWidget(mPlayerGold, "PlayerGold");
2012-09-10 12:10:01 +00:00
getWidget(mSpellsView, "SpellsView");
2012-09-08 22:17:03 +00:00
mCancelButton->eventMouseButtonClick += MyGUI::newDelegate(this, &SpellBuyingWindow::onCancelButtonClicked);
2012-09-08 22:17:03 +00:00
}
2017-07-27 09:20:18 +00:00
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)
2012-09-08 22:17:03 +00:00
{
const MWWorld::ESMStore &store =
MWBase::Environment::get().getWorld()->getStore();
int price = std::max(1, static_cast<int>(spell.mData.mCost*store.get<ESM::GameSetting>().find("fSpellValueMult")->mValue.getFloat()));
2012-11-09 13:42:09 +00:00
price = MWBase::Environment::get().getMechanicsManager()->getBarterOffer(mPtr,price,true);
2012-09-17 07:37:50 +00:00
2015-08-21 09:12:39 +00:00
MWWorld::Ptr player = MWMechanics::getPlayer();
int playerGold = player.getClass().getContainerStore(player).count(MWWorld::ContainerStore::sGoldId);
// TODO: refactor to use MyGUI::ListBox
2018-06-18 09:43:39 +00:00
int lineHeight = MWBase::Environment::get().getWindowManager()->getFontHeight() + 2;
2012-09-17 07:37:50 +00:00
MyGUI::Button* toAdd =
mSpellsView->createWidget<MyGUI::Button>(
price <= playerGold ? "SandTextButton" : "SandTextButtonDisabled", // can't use setEnabled since that removes tooltip
2012-09-17 07:37:50 +00:00
0,
mCurrentY,
200,
2018-06-18 09:43:39 +00:00
lineHeight,
2012-09-17 07:37:50 +00:00
MyGUI::Align::Default
);
2018-06-18 09:43:39 +00:00
mCurrentY += lineHeight;
2012-09-17 07:37:50 +00:00
toAdd->setUserData(price);
2017-07-27 09:20:18 +00:00
toAdd->setCaptionWithReplacing(spell.mName+" - "+MyGUI::utility::toString(price)+"#{sgp}");
2018-06-18 09:43:39 +00:00
toAdd->setSize(mSpellsView->getWidth(), lineHeight);
toAdd->eventMouseWheel += MyGUI::newDelegate(this, &SpellBuyingWindow::onMouseWheel);
2012-09-08 22:17:03 +00:00
toAdd->setUserString("ToolTipType", "Spell");
2017-07-27 09:20:18 +00:00
toAdd->setUserString("Spell", spell.mId);
toAdd->setUserString("SpellCost", std::to_string(spell.mData.mCost));
toAdd->eventMouseButtonClick += MyGUI::newDelegate(this, &SpellBuyingWindow::onSpellButtonClick);
2017-07-27 09:20:18 +00:00
mSpellsWidgetMap.insert(std::make_pair (toAdd, spell.mId));
2012-09-08 22:17:03 +00:00
}
void SpellBuyingWindow::clearSpells()
2012-09-08 22:17:03 +00:00
{
2012-09-10 12:10:01 +00:00
mSpellsView->setViewOffset(MyGUI::IntPoint(0,0));
2012-09-08 22:17:03 +00:00
mCurrentY = 0;
2012-09-10 12:10:01 +00:00
while (mSpellsView->getChildCount())
MyGUI::Gui::getInstance().destroyWidget(mSpellsView->getChildAt(0));
2012-09-08 22:17:03 +00:00
mSpellsWidgetMap.clear();
}
void SpellBuyingWindow::setPtr(const MWWorld::Ptr &actor)
{
setPtr(actor, 0);
}
void SpellBuyingWindow::setPtr(const MWWorld::Ptr& actor, int startOffset)
2012-09-08 22:17:03 +00:00
{
center();
2012-09-22 22:36:20 +00:00
mPtr = actor;
2012-09-08 22:17:03 +00:00
clearSpells();
MWMechanics::Spells& merchantSpells = actor.getClass().getCreatureStats (actor).getSpells();
2017-07-27 09:20:18 +00:00
std::vector<const ESM::Spell*> spellsToSort;
2012-09-10 11:15:55 +00:00
for (MWMechanics::Spells::TIterator iter = merchantSpells.begin(); iter!=merchantSpells.end(); ++iter)
2012-09-08 22:17:03 +00:00
{
const ESM::Spell* spell = iter->first;
2012-09-17 07:37:50 +00:00
if (spell->mData.mType!=ESM::Spell::ST_Spell)
2012-09-10 11:15:55 +00:00
continue; // don't try to sell diseases, curses or powers
2014-10-02 12:45:57 +00:00
if (actor.getClass().isNpc())
{
const ESM::Race* race =
MWBase::Environment::get().getWorld()->getStore().get<ESM::Race>().find(
actor.get<ESM::NPC>()->mBase->mRace);
if (race->mPowers.exists(spell->mId))
continue;
}
if (playerHasSpell(iter->first->mId))
continue;
2017-07-27 09:20:18 +00:00
spellsToSort.push_back(iter->first);
2012-09-08 22:17:03 +00:00
}
2017-07-27 09:20:18 +00:00
std::stable_sort(spellsToSort.begin(), spellsToSort.end(), sortSpells);
for (const ESM::Spell* spell : spellsToSort)
2017-07-27 09:20:18 +00:00
{
addSpell(*spell);
2017-07-27 09:20:18 +00:00
}
spellsToSort.clear();
2012-09-08 22:17:03 +00:00
updateLabels();
2012-09-10 12:10:01 +00:00
// Canvas size must be expressed with VScroll disabled, otherwise MyGUI would expand the scroll area when the scrollbar is hidden
mSpellsView->setVisibleVScroll(false);
2012-09-10 12:10:01 +00:00
mSpellsView->setCanvasSize (MyGUI::IntSize(mSpellsView->getWidth(), std::max(mSpellsView->getHeight(), mCurrentY)));
mSpellsView->setVisibleVScroll(true);
2017-07-27 09:20:18 +00:00
mSpellsView->setViewOffset(MyGUI::IntPoint(0, startOffset));
2012-09-08 22:17:03 +00:00
}
bool SpellBuyingWindow::playerHasSpell(const std::string &id)
{
2015-08-21 09:12:39 +00:00
MWWorld::Ptr player = MWMechanics::getPlayer();
2015-05-10 11:09:22 +00:00
return player.getClass().getCreatureStats(player).getSpells().hasSpell(id);
}
void SpellBuyingWindow::onSpellButtonClick(MyGUI::Widget* _sender)
2012-09-08 22:17:03 +00:00
{
int price = *_sender->getUserData<int>();
2012-09-08 22:17:03 +00:00
2015-08-21 09:12:39 +00:00
MWWorld::Ptr player = MWMechanics::getPlayer();
if (price > player.getClass().getContainerStore(player).count(MWWorld::ContainerStore::sGoldId))
return;
MWMechanics::CreatureStats& stats = player.getClass().getCreatureStats(player);
MWMechanics::Spells& spells = stats.getSpells();
spells.add (mSpellsWidgetMap.find(_sender)->second);
player.getClass().getContainerStore(player).remove(MWWorld::ContainerStore::sGoldId, price, player);
// add gold to NPC trading gold pool
MWMechanics::CreatureStats& npcStats = mPtr.getClass().getCreatureStats(mPtr);
npcStats.setGoldPool(npcStats.getGoldPool() + price);
setPtr(mPtr, mSpellsView->getViewOffset().top);
MWBase::Environment::get().getWindowManager()->playSound("Item Gold Up");
2012-09-08 22:17:03 +00:00
}
void SpellBuyingWindow::onCancelButtonClicked(MyGUI::Widget* _sender)
2012-09-08 22:17:03 +00:00
{
2017-09-23 10:18:39 +00:00
MWBase::Environment::get().getWindowManager()->removeGuiMode (MWGui::GM_SpellBuying);
2012-09-08 22:17:03 +00:00
}
void SpellBuyingWindow::updateLabels()
2012-09-08 22:17:03 +00:00
{
2015-08-21 09:12:39 +00:00
MWWorld::Ptr player = MWMechanics::getPlayer();
int playerGold = player.getClass().getContainerStore(player).count(MWWorld::ContainerStore::sGoldId);
mPlayerGold->setCaptionWithReplacing("#{sGold}: " + MyGUI::utility::toString(playerGold));
2012-09-08 22:17:03 +00:00
mPlayerGold->setCoord(8,
mPlayerGold->getTop(),
mPlayerGold->getTextSize().width,
mPlayerGold->getHeight());
}
void SpellBuyingWindow::onReferenceUnavailable()
2012-09-08 22:17:03 +00:00
{
// remove both Spells and Dialogue (since you always trade with the NPC/creature that you have previously talked to)
MWBase::Environment::get().getWindowManager()->removeGuiMode(GM_SpellBuying);
MWBase::Environment::get().getWindowManager()->exitCurrentGuiMode();
2012-09-08 22:17:03 +00:00
}
void SpellBuyingWindow::onMouseWheel(MyGUI::Widget* _sender, int _rel)
2012-09-08 22:17:03 +00:00
{
2012-09-10 12:10:01 +00:00
if (mSpellsView->getViewOffset().top + _rel*0.3 > 0)
mSpellsView->setViewOffset(MyGUI::IntPoint(0, 0));
2012-09-08 22:17:03 +00:00
else
mSpellsView->setViewOffset(MyGUI::IntPoint(0, static_cast<int>(mSpellsView->getViewOffset().top + _rel*0.3f)));
2012-09-08 22:17:03 +00:00
}
}