2012-09-09 18:10:07 +00:00
|
|
|
#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>
|
|
|
|
|
2017-04-24 16:46:12 +00:00
|
|
|
/*
|
|
|
|
Start of tes3mp addition
|
|
|
|
|
|
|
|
Include additional headers for multiplayer purposes
|
|
|
|
*/
|
|
|
|
#include "../mwmp/Main.hpp"
|
|
|
|
#include "../mwmp/LocalPlayer.hpp"
|
|
|
|
/*
|
|
|
|
End of tes3mp addition
|
|
|
|
*/
|
|
|
|
|
2012-09-08 22:17:03 +00:00
|
|
|
#include "../mwbase/environment.hpp"
|
|
|
|
#include "../mwbase/world.hpp"
|
|
|
|
#include "../mwbase/windowmanager.hpp"
|
2012-11-05 18:53:55 +00:00
|
|
|
#include "../mwbase/mechanicsmanager.hpp"
|
2012-09-08 22:17:03 +00:00
|
|
|
|
2013-05-11 16:38:27 +00:00
|
|
|
#include "../mwworld/class.hpp"
|
2013-11-21 03:27:53 +00:00
|
|
|
#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
|
|
|
|
{
|
2012-09-09 18:10:07 +00:00
|
|
|
const int SpellBuyingWindow::sLineHeight = 18;
|
2012-09-08 22:17:03 +00:00
|
|
|
|
2013-04-10 18:46:21 +00:00
|
|
|
SpellBuyingWindow::SpellBuyingWindow() :
|
|
|
|
WindowBase("openmw_spell_buying_window.layout")
|
2012-09-08 22:17:03 +00:00
|
|
|
, mLastPos(0)
|
2015-05-01 00:24:27 +00:00
|
|
|
, 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
|
|
|
|
2012-09-09 18:10:07 +00:00
|
|
|
mCancelButton->eventMouseButtonClick += MyGUI::newDelegate(this, &SpellBuyingWindow::onCancelButtonClicked);
|
2012-09-08 22:17:03 +00:00
|
|
|
}
|
|
|
|
|
2014-05-27 03:13:37 +00:00
|
|
|
void SpellBuyingWindow::exit()
|
|
|
|
{
|
|
|
|
MWBase::Environment::get().getWindowManager()->removeGuiMode(GM_SpellBuying);
|
|
|
|
}
|
|
|
|
|
2012-09-09 19:08:57 +00:00
|
|
|
void SpellBuyingWindow::addSpell(const std::string& spellId)
|
2012-09-08 22:17:03 +00:00
|
|
|
{
|
2012-11-05 20:34:11 +00:00
|
|
|
const MWWorld::ESMStore &store =
|
|
|
|
MWBase::Environment::get().getWorld()->getStore();
|
|
|
|
|
2012-11-08 22:16:40 +00:00
|
|
|
const ESM::Spell* spell = MWBase::Environment::get().getWorld()->getStore().get<ESM::Spell>().find(spellId);
|
2015-03-08 00:07:29 +00:00
|
|
|
int price = static_cast<int>(spell->mData.mCost*store.get<ESM::GameSetting>().find("fSpellValueMult")->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();
|
2014-01-08 22:37:46 +00:00
|
|
|
int playerGold = player.getClass().getContainerStore(player).count(MWWorld::ContainerStore::sGoldId);
|
|
|
|
|
2014-07-26 00:23:42 +00:00
|
|
|
// TODO: refactor to use MyGUI::ListBox
|
|
|
|
|
2012-09-17 07:37:50 +00:00
|
|
|
MyGUI::Button* toAdd =
|
|
|
|
mSpellsView->createWidget<MyGUI::Button>(
|
2014-10-17 16:58:20 +00:00
|
|
|
price <= playerGold ? "SandTextButton" : "SandTextButtonDisabled", // can't use setEnabled since that removes tooltip
|
2012-09-17 07:37:50 +00:00
|
|
|
0,
|
|
|
|
mCurrentY,
|
|
|
|
200,
|
|
|
|
sLineHeight,
|
|
|
|
MyGUI::Align::Default
|
|
|
|
);
|
|
|
|
|
2012-09-09 18:10:07 +00:00
|
|
|
mCurrentY += sLineHeight;
|
2012-09-17 07:37:50 +00:00
|
|
|
|
2012-09-09 18:10:07 +00:00
|
|
|
toAdd->setUserData(price);
|
2015-01-10 02:01:01 +00:00
|
|
|
toAdd->setCaptionWithReplacing(spell->mName+" - "+MyGUI::utility::toString(price)+"#{sgp}");
|
2017-04-15 11:54:42 +00:00
|
|
|
toAdd->setSize(mSpellsView->getWidth(),sLineHeight);
|
2012-09-09 18:10:07 +00:00
|
|
|
toAdd->eventMouseWheel += MyGUI::newDelegate(this, &SpellBuyingWindow::onMouseWheel);
|
2012-09-08 22:17:03 +00:00
|
|
|
toAdd->setUserString("ToolTipType", "Spell");
|
2012-09-09 19:03:39 +00:00
|
|
|
toAdd->setUserString("Spell", spellId);
|
2012-09-09 18:10:07 +00:00
|
|
|
toAdd->eventMouseButtonClick += MyGUI::newDelegate(this, &SpellBuyingWindow::onSpellButtonClick);
|
2012-09-09 19:03:39 +00:00
|
|
|
mSpellsWidgetMap.insert(std::make_pair (toAdd, spellId));
|
2012-09-08 22:17:03 +00:00
|
|
|
}
|
|
|
|
|
2012-09-09 18:10:07 +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();
|
|
|
|
}
|
|
|
|
|
2012-09-09 19:08:57 +00:00
|
|
|
void SpellBuyingWindow::startSpellBuying(const MWWorld::Ptr& actor)
|
2012-09-08 22:17:03 +00:00
|
|
|
{
|
2012-09-09 18:10:07 +00:00
|
|
|
center();
|
2012-09-22 22:36:20 +00:00
|
|
|
mPtr = actor;
|
2012-09-08 22:17:03 +00:00
|
|
|
clearSpells();
|
|
|
|
|
2014-05-22 18:37:22 +00:00
|
|
|
MWMechanics::Spells& merchantSpells = actor.getClass().getCreatureStats (actor).getSpells();
|
2013-01-12 12:10:20 +00:00
|
|
|
|
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
|
|
|
{
|
2015-11-27 00:02:29 +00:00
|
|
|
const ESM::Spell* spell = iter->first;
|
2013-01-12 12:10:20 +00:00
|
|
|
|
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
|
2013-01-12 12:10:20 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-11-27 00:02:29 +00:00
|
|
|
if (playerHasSpell(iter->first->mId))
|
2013-03-06 14:11:40 +00:00
|
|
|
continue;
|
2013-01-12 12:10:20 +00:00
|
|
|
|
2015-11-27 00:02:29 +00:00
|
|
|
addSpell (iter->first->mId);
|
2012-09-08 22:17:03 +00:00
|
|
|
}
|
2012-09-09 19:03:39 +00:00
|
|
|
|
2012-09-08 22:17:03 +00:00
|
|
|
updateLabels();
|
2012-09-10 12:10:01 +00:00
|
|
|
|
2014-07-26 00:23:42 +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)));
|
2014-07-26 00:23:42 +00:00
|
|
|
mSpellsView->setVisibleVScroll(true);
|
2012-09-08 22:17:03 +00:00
|
|
|
}
|
|
|
|
|
2013-03-06 14:11:40 +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);
|
2013-03-06 14:11:40 +00:00
|
|
|
}
|
|
|
|
|
2012-09-09 18:10:07 +00:00
|
|
|
void SpellBuyingWindow::onSpellButtonClick(MyGUI::Widget* _sender)
|
2012-09-08 22:17:03 +00:00
|
|
|
{
|
2012-09-09 18:10:07 +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();
|
2014-10-17 16:58:20 +00:00
|
|
|
if (price > player.getClass().getContainerStore(player).count(MWWorld::ContainerStore::sGoldId))
|
|
|
|
return;
|
|
|
|
|
2014-05-22 18:37:22 +00:00
|
|
|
MWMechanics::CreatureStats& stats = player.getClass().getCreatureStats(player);
|
2014-01-08 22:56:40 +00:00
|
|
|
MWMechanics::Spells& spells = stats.getSpells();
|
|
|
|
spells.add (mSpellsWidgetMap.find(_sender)->second);
|
2016-12-29 13:19:26 +00:00
|
|
|
|
2017-02-26 14:59:53 +00:00
|
|
|
/*
|
|
|
|
Start of tes3mp addition
|
|
|
|
|
|
|
|
Send an ID_PLAYER_SPELLBOOK packet every time a player buys a spell
|
|
|
|
*/
|
2016-12-29 13:19:26 +00:00
|
|
|
mwmp::Main::get().getLocalPlayer()->sendSpellAddition(mSpellsWidgetMap.find(_sender)->second);
|
2017-02-26 14:59:53 +00:00
|
|
|
/*
|
|
|
|
End of tes3mp addition
|
|
|
|
*/
|
2016-12-29 13:19:26 +00:00
|
|
|
|
2014-01-08 22:56:40 +00:00
|
|
|
player.getClass().getContainerStore(player).remove(MWWorld::ContainerStore::sGoldId, price, player);
|
2014-07-27 22:55:57 +00:00
|
|
|
|
|
|
|
// add gold to NPC trading gold pool
|
|
|
|
MWMechanics::CreatureStats& npcStats = mPtr.getClass().getCreatureStats(mPtr);
|
|
|
|
npcStats.setGoldPool(npcStats.getGoldPool() + price);
|
|
|
|
|
2014-01-08 22:56:40 +00:00
|
|
|
startSpellBuying(mPtr);
|
2014-01-08 22:37:46 +00:00
|
|
|
|
2017-07-10 11:48:00 +00:00
|
|
|
MWBase::Environment::get().getWindowManager()->playSound("Item Gold Up");
|
2012-09-08 22:17:03 +00:00
|
|
|
}
|
|
|
|
|
2012-09-09 18:10:07 +00:00
|
|
|
void SpellBuyingWindow::onCancelButtonClicked(MyGUI::Widget* _sender)
|
2012-09-08 22:17:03 +00:00
|
|
|
{
|
2014-05-27 03:13:37 +00:00
|
|
|
exit();
|
2012-09-08 22:17:03 +00:00
|
|
|
}
|
|
|
|
|
2012-09-09 18:10:07 +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();
|
2014-01-08 22:37:46 +00:00
|
|
|
int playerGold = player.getClass().getContainerStore(player).count(MWWorld::ContainerStore::sGoldId);
|
|
|
|
|
2015-01-10 02:01:01 +00:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2012-09-09 18:10:07 +00:00
|
|
|
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)
|
2013-04-10 04:32:05 +00:00
|
|
|
MWBase::Environment::get().getWindowManager()->removeGuiMode(GM_SpellBuying);
|
|
|
|
MWBase::Environment::get().getWindowManager()->removeGuiMode(GM_Dialogue);
|
2012-09-08 22:17:03 +00:00
|
|
|
}
|
|
|
|
|
2012-09-09 18:10:07 +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
|
2015-03-08 00:07:29 +00:00
|
|
|
mSpellsView->setViewOffset(MyGUI::IntPoint(0, static_cast<int>(mSpellsView->getViewOffset().top + _rel*0.3f)));
|
2012-09-08 22:17:03 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-09 18:10:07 +00:00
|
|
|
|