2012-05-17 11:36:25 +00:00
|
|
|
#include "tradewindow.hpp"
|
|
|
|
|
2017-05-06 22:04:46 +00:00
|
|
|
#include <climits>
|
|
|
|
|
2015-01-10 01:50:43 +00:00
|
|
|
#include <MyGUI_Button.h>
|
|
|
|
#include <MyGUI_InputManager.h>
|
|
|
|
#include <MyGUI_ControllerManager.h>
|
2012-05-17 19:56:16 +00:00
|
|
|
|
2014-09-25 18:28:00 +00:00
|
|
|
#include <components/widgets/numericeditbox.hpp>
|
|
|
|
|
2012-05-17 17:53:04 +00:00
|
|
|
#include "../mwbase/environment.hpp"
|
2012-07-03 10:30:50 +00:00
|
|
|
#include "../mwbase/world.hpp"
|
2012-08-12 16:11:09 +00:00
|
|
|
#include "../mwbase/windowmanager.hpp"
|
2012-11-05 18:53:55 +00:00
|
|
|
#include "../mwbase/mechanicsmanager.hpp"
|
2012-11-09 23:38:45 +00:00
|
|
|
#include "../mwbase/dialoguemanager.hpp"
|
2012-07-03 10:30:50 +00:00
|
|
|
|
2013-05-11 16:38:27 +00:00
|
|
|
#include "../mwworld/class.hpp"
|
|
|
|
#include "../mwworld/containerstore.hpp"
|
2015-02-09 14:01:49 +00:00
|
|
|
#include "../mwworld/esmstore.hpp"
|
2012-05-17 17:53:04 +00:00
|
|
|
|
2015-08-21 09:12:39 +00:00
|
|
|
#include "../mwmechanics/actorutil.hpp"
|
2016-06-04 16:28:47 +00:00
|
|
|
#include "../mwmechanics/creaturestats.hpp"
|
2012-11-08 12:38:20 +00:00
|
|
|
|
2012-05-17 19:56:16 +00:00
|
|
|
#include "inventorywindow.hpp"
|
2013-05-11 16:38:27 +00:00
|
|
|
#include "itemview.hpp"
|
|
|
|
#include "sortfilteritemmodel.hpp"
|
|
|
|
#include "containeritemmodel.hpp"
|
|
|
|
#include "tradeitemmodel.hpp"
|
|
|
|
#include "countdialog.hpp"
|
2014-09-10 22:25:09 +00:00
|
|
|
#include "controllers.hpp"
|
2012-05-17 19:15:48 +00:00
|
|
|
|
2014-06-26 12:59:33 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
int getEffectiveValue (MWWorld::Ptr item, int count)
|
|
|
|
{
|
2015-03-08 00:22:56 +00:00
|
|
|
float price = static_cast<float>(item.getClass().getValue(item));
|
2014-06-26 12:59:33 +00:00
|
|
|
if (item.getClass().hasItemHealth(item))
|
2015-03-08 00:07:29 +00:00
|
|
|
{
|
|
|
|
price *= item.getClass().getItemHealth(item);
|
|
|
|
price /= item.getClass().getItemMaxHealth(item);
|
|
|
|
}
|
|
|
|
return static_cast<int>(price * count);
|
2014-06-26 12:59:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-05-17 11:36:25 +00:00
|
|
|
namespace MWGui
|
|
|
|
{
|
2015-03-08 00:07:29 +00:00
|
|
|
const float TradeWindow::sBalanceChangeInitialPause = 0.5f;
|
|
|
|
const float TradeWindow::sBalanceChangeInterval = 0.1f;
|
2013-02-22 17:33:23 +00:00
|
|
|
|
2013-05-11 16:38:27 +00:00
|
|
|
TradeWindow::TradeWindow()
|
|
|
|
: WindowBase("openmw_trade_window.layout")
|
2013-07-31 16:46:32 +00:00
|
|
|
, mSortModel(NULL)
|
2015-04-25 18:37:42 +00:00
|
|
|
, mTradeModel(NULL)
|
|
|
|
, mItemToSell(-1)
|
|
|
|
, mCurrentBalance(0)
|
2013-07-31 16:46:32 +00:00
|
|
|
, mCurrentMerchantOffer(0)
|
2012-05-17 11:36:25 +00:00
|
|
|
{
|
2012-05-17 19:15:48 +00:00
|
|
|
getWidget(mFilterAll, "AllButton");
|
|
|
|
getWidget(mFilterWeapon, "WeaponButton");
|
|
|
|
getWidget(mFilterApparel, "ApparelButton");
|
|
|
|
getWidget(mFilterMagic, "MagicButton");
|
|
|
|
getWidget(mFilterMisc, "MiscButton");
|
|
|
|
|
|
|
|
getWidget(mMaxSaleButton, "MaxSaleButton");
|
|
|
|
getWidget(mCancelButton, "CancelButton");
|
|
|
|
getWidget(mOfferButton, "OfferButton");
|
|
|
|
getWidget(mPlayerGold, "PlayerGold");
|
|
|
|
getWidget(mMerchantGold, "MerchantGold");
|
|
|
|
getWidget(mIncreaseButton, "IncreaseButton");
|
|
|
|
getWidget(mDecreaseButton, "DecreaseButton");
|
|
|
|
getWidget(mTotalBalance, "TotalBalance");
|
|
|
|
getWidget(mTotalBalanceLabel, "TotalBalanceLabel");
|
|
|
|
getWidget(mBottomPane, "BottomPane");
|
|
|
|
|
2013-05-11 16:38:27 +00:00
|
|
|
getWidget(mItemView, "ItemView");
|
|
|
|
mItemView->eventItemClicked += MyGUI::newDelegate(this, &TradeWindow::onItemSelected);
|
|
|
|
|
2012-05-17 19:15:48 +00:00
|
|
|
mFilterAll->setStateSelected(true);
|
|
|
|
|
2012-05-17 17:53:04 +00:00
|
|
|
mFilterAll->eventMouseButtonClick += MyGUI::newDelegate(this, &TradeWindow::onFilterChanged);
|
|
|
|
mFilterWeapon->eventMouseButtonClick += MyGUI::newDelegate(this, &TradeWindow::onFilterChanged);
|
|
|
|
mFilterApparel->eventMouseButtonClick += MyGUI::newDelegate(this, &TradeWindow::onFilterChanged);
|
|
|
|
mFilterMagic->eventMouseButtonClick += MyGUI::newDelegate(this, &TradeWindow::onFilterChanged);
|
|
|
|
mFilterMisc->eventMouseButtonClick += MyGUI::newDelegate(this, &TradeWindow::onFilterChanged);
|
|
|
|
|
2012-05-17 19:15:48 +00:00
|
|
|
mCancelButton->eventMouseButtonClick += MyGUI::newDelegate(this, &TradeWindow::onCancelButtonClicked);
|
|
|
|
mOfferButton->eventMouseButtonClick += MyGUI::newDelegate(this, &TradeWindow::onOfferButtonClicked);
|
2013-03-04 11:21:38 +00:00
|
|
|
mMaxSaleButton->eventMouseButtonClick += MyGUI::newDelegate(this, &TradeWindow::onMaxSaleButtonClicked);
|
2013-02-10 04:50:36 +00:00
|
|
|
mIncreaseButton->eventMouseButtonPressed += MyGUI::newDelegate(this, &TradeWindow::onIncreaseButtonPressed);
|
|
|
|
mIncreaseButton->eventMouseButtonReleased += MyGUI::newDelegate(this, &TradeWindow::onBalanceButtonReleased);
|
|
|
|
mDecreaseButton->eventMouseButtonPressed += MyGUI::newDelegate(this, &TradeWindow::onDecreaseButtonPressed);
|
|
|
|
mDecreaseButton->eventMouseButtonReleased += MyGUI::newDelegate(this, &TradeWindow::onBalanceButtonReleased);
|
2012-05-17 19:15:48 +00:00
|
|
|
|
2014-09-25 18:28:00 +00:00
|
|
|
mTotalBalance->eventValueChanged += MyGUI::newDelegate(this, &TradeWindow::onBalanceValueChanged);
|
2017-09-24 11:10:50 +00:00
|
|
|
mTotalBalance->eventEditSelectAccept += MyGUI::newDelegate(this, &TradeWindow::onAccept);
|
2014-11-06 02:51:18 +00:00
|
|
|
mTotalBalance->setMinValue(INT_MIN+1); // disallow INT_MIN since abs(INT_MIN) is undefined
|
2014-05-31 11:53:36 +00:00
|
|
|
|
2012-06-29 14:48:50 +00:00
|
|
|
setCoord(400, 0, 400, 300);
|
2012-05-17 11:36:25 +00:00
|
|
|
}
|
2012-05-17 15:15:44 +00:00
|
|
|
|
2014-12-20 19:17:14 +00:00
|
|
|
void TradeWindow::restock()
|
|
|
|
{
|
|
|
|
// Restock items on the actor inventory
|
|
|
|
mPtr.getClass().restock(mPtr);
|
|
|
|
|
|
|
|
// Also restock any containers owned by this merchant, which are also available to buy in the trade window
|
|
|
|
std::vector<MWWorld::Ptr> itemSources;
|
|
|
|
MWBase::Environment::get().getWorld()->getContainersOwnedBy(mPtr, itemSources);
|
|
|
|
for (std::vector<MWWorld::Ptr>::iterator it = itemSources.begin(); it != itemSources.end(); ++it)
|
|
|
|
{
|
|
|
|
it->getClass().restock(*it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-22 19:26:41 +00:00
|
|
|
void TradeWindow::setPtr(const MWWorld::Ptr& actor)
|
2014-04-27 17:03:33 +00:00
|
|
|
{
|
2013-05-11 16:38:27 +00:00
|
|
|
mPtr = actor;
|
2012-05-17 19:56:16 +00:00
|
|
|
|
|
|
|
mCurrentBalance = 0;
|
2012-11-08 12:38:20 +00:00
|
|
|
mCurrentMerchantOffer = 0;
|
2012-05-17 19:56:16 +00:00
|
|
|
|
2013-05-11 16:38:27 +00:00
|
|
|
std::vector<MWWorld::Ptr> itemSources;
|
|
|
|
MWBase::Environment::get().getWorld()->getContainersOwnedBy(actor, itemSources);
|
2014-03-27 05:23:56 +00:00
|
|
|
|
2013-05-11 16:38:27 +00:00
|
|
|
// Important: actor goes last, so that items purchased by the merchant go into his inventory
|
|
|
|
itemSources.push_back(actor);
|
2013-05-27 00:18:36 +00:00
|
|
|
std::vector<MWWorld::Ptr> worldItems;
|
|
|
|
MWBase::Environment::get().getWorld()->getItemsOwnedBy(actor, worldItems);
|
2012-05-18 15:27:55 +00:00
|
|
|
|
2013-05-27 00:18:36 +00:00
|
|
|
mTradeModel = new TradeItemModel(new ContainerItemModel(itemSources, worldItems), mPtr);
|
2013-05-11 16:38:27 +00:00
|
|
|
mSortModel = new SortFilterItemModel(mTradeModel);
|
|
|
|
mItemView->setModel (mSortModel);
|
2015-06-04 20:09:40 +00:00
|
|
|
mItemView->resetScrollBars();
|
2012-05-18 15:27:55 +00:00
|
|
|
|
2012-05-17 19:56:16 +00:00
|
|
|
updateLabels();
|
2013-11-16 21:29:40 +00:00
|
|
|
|
2014-05-22 18:37:22 +00:00
|
|
|
setTitle(actor.getClass().getName(actor));
|
2013-11-30 07:33:18 +00:00
|
|
|
|
|
|
|
onFilterChanged(mFilterAll);
|
2017-09-24 11:10:50 +00:00
|
|
|
|
|
|
|
MWBase::Environment::get().getWindowManager()->setKeyFocusWidget(mTotalBalance);
|
2012-05-17 17:53:04 +00:00
|
|
|
}
|
|
|
|
|
2017-09-23 20:00:15 +00:00
|
|
|
void TradeWindow::onFrame(float dt)
|
|
|
|
{
|
|
|
|
checkReferenceAvailable();
|
|
|
|
}
|
|
|
|
|
2012-05-17 17:53:04 +00:00
|
|
|
void TradeWindow::onFilterChanged(MyGUI::Widget* _sender)
|
|
|
|
{
|
|
|
|
if (_sender == mFilterAll)
|
2013-05-11 16:38:27 +00:00
|
|
|
mSortModel->setCategory(SortFilterItemModel::Category_All);
|
2012-05-17 17:53:04 +00:00
|
|
|
else if (_sender == mFilterWeapon)
|
2013-05-11 16:38:27 +00:00
|
|
|
mSortModel->setCategory(SortFilterItemModel::Category_Weapon);
|
2012-05-17 17:53:04 +00:00
|
|
|
else if (_sender == mFilterApparel)
|
2013-05-11 16:38:27 +00:00
|
|
|
mSortModel->setCategory(SortFilterItemModel::Category_Apparel);
|
2012-05-17 17:53:04 +00:00
|
|
|
else if (_sender == mFilterMagic)
|
2013-05-11 16:38:27 +00:00
|
|
|
mSortModel->setCategory(SortFilterItemModel::Category_Magic);
|
2012-05-17 17:53:04 +00:00
|
|
|
else if (_sender == mFilterMisc)
|
2013-05-11 16:38:27 +00:00
|
|
|
mSortModel->setCategory(SortFilterItemModel::Category_Misc);
|
2012-05-17 17:53:04 +00:00
|
|
|
|
|
|
|
mFilterAll->setStateSelected(false);
|
|
|
|
mFilterWeapon->setStateSelected(false);
|
|
|
|
mFilterApparel->setStateSelected(false);
|
|
|
|
mFilterMagic->setStateSelected(false);
|
|
|
|
mFilterMisc->setStateSelected(false);
|
|
|
|
|
2014-09-13 02:07:40 +00:00
|
|
|
_sender->castType<MyGUI::Button>()->setStateSelected(true);
|
2013-05-11 16:38:27 +00:00
|
|
|
|
|
|
|
mItemView->update();
|
2012-05-17 15:15:44 +00:00
|
|
|
}
|
2012-05-17 19:15:48 +00:00
|
|
|
|
2013-05-11 16:38:27 +00:00
|
|
|
int TradeWindow::getMerchantServices()
|
2012-05-17 19:15:48 +00:00
|
|
|
{
|
2014-05-22 18:37:22 +00:00
|
|
|
return mPtr.getClass().getServices(mPtr);
|
2013-05-11 16:38:27 +00:00
|
|
|
}
|
|
|
|
|
2017-09-23 10:18:39 +00:00
|
|
|
bool TradeWindow::exit()
|
2014-05-27 03:13:37 +00:00
|
|
|
{
|
|
|
|
mTradeModel->abort();
|
|
|
|
MWBase::Environment::get().getWindowManager()->getInventoryWindow()->getTradeModel()->abort();
|
2017-09-23 10:18:39 +00:00
|
|
|
return true;
|
2014-05-27 03:13:37 +00:00
|
|
|
}
|
|
|
|
|
2013-05-11 16:38:27 +00:00
|
|
|
void TradeWindow::onItemSelected (int index)
|
|
|
|
{
|
|
|
|
const ItemStack& item = mSortModel->getItem(index);
|
|
|
|
|
|
|
|
MWWorld::Ptr object = item.mBase;
|
|
|
|
int count = item.mCount;
|
|
|
|
bool shift = MyGUI::InputManager::getInstance().isShiftPressed();
|
|
|
|
if (MyGUI::InputManager::getInstance().isControlPressed())
|
|
|
|
count = 1;
|
|
|
|
|
|
|
|
if (count > 1 && !shift)
|
|
|
|
{
|
|
|
|
CountDialog* dialog = MWBase::Environment::get().getWindowManager()->getCountDialog();
|
|
|
|
std::string message = "#{sQuanityMenuMessage02}";
|
2015-06-04 18:31:28 +00:00
|
|
|
dialog->openCountDialog(object.getClass().getName(object), message, count);
|
2013-05-11 16:38:27 +00:00
|
|
|
dialog->eventOkClicked.clear();
|
|
|
|
dialog->eventOkClicked += MyGUI::newDelegate(this, &TradeWindow::sellItem);
|
|
|
|
mItemToSell = mSortModel->mapToSource(index);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mItemToSell = mSortModel->mapToSource(index);
|
|
|
|
sellItem (NULL, count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TradeWindow::sellItem(MyGUI::Widget* sender, int count)
|
|
|
|
{
|
|
|
|
const ItemStack& item = mTradeModel->getItem(mItemToSell);
|
2017-08-03 14:39:59 +00:00
|
|
|
std::string sound = item.mBase.getClass().getUpSoundId(item.mBase);
|
2017-07-10 11:48:00 +00:00
|
|
|
MWBase::Environment::get().getWindowManager()->playSound(sound);
|
2013-05-11 16:38:27 +00:00
|
|
|
|
|
|
|
TradeItemModel* playerTradeModel = MWBase::Environment::get().getWindowManager()->getInventoryWindow()->getTradeModel();
|
|
|
|
|
|
|
|
if (item.mType == ItemStack::Type_Barter)
|
|
|
|
{
|
|
|
|
// this was an item borrowed to us by the player
|
|
|
|
mTradeModel->returnItemBorrowedToUs(mItemToSell, count);
|
|
|
|
playerTradeModel->returnItemBorrowedFromUs(mItemToSell, mTradeModel, count);
|
|
|
|
buyFromNpc(item.mBase, count, true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// borrow item to player
|
|
|
|
playerTradeModel->borrowItemToUs(mItemToSell, mTradeModel, count);
|
|
|
|
mTradeModel->borrowItemFromUs(mItemToSell, count);
|
|
|
|
buyFromNpc(item.mBase, count, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
MWBase::Environment::get().getWindowManager()->getInventoryWindow()->updateItemView();
|
|
|
|
mItemView->update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TradeWindow::borrowItem (int index, size_t count)
|
|
|
|
{
|
|
|
|
TradeItemModel* playerTradeModel = MWBase::Environment::get().getWindowManager()->getInventoryWindow()->getTradeModel();
|
|
|
|
mTradeModel->borrowItemToUs(index, playerTradeModel, count);
|
|
|
|
mItemView->update();
|
|
|
|
sellToNpc(playerTradeModel->getItem(index).mBase, count, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TradeWindow::returnItem (int index, size_t count)
|
|
|
|
{
|
|
|
|
TradeItemModel* playerTradeModel = MWBase::Environment::get().getWindowManager()->getInventoryWindow()->getTradeModel();
|
|
|
|
const ItemStack& item = playerTradeModel->getItem(index);
|
|
|
|
mTradeModel->returnItemBorrowedFromUs(index, playerTradeModel, count);
|
|
|
|
mItemView->update();
|
|
|
|
sellToNpc(item.mBase, count, true);
|
2012-05-17 19:15:48 +00:00
|
|
|
}
|
|
|
|
|
2013-11-21 03:27:53 +00:00
|
|
|
void TradeWindow::addOrRemoveGold(int amount, const MWWorld::Ptr& actor)
|
2012-09-08 22:17:03 +00:00
|
|
|
{
|
2014-05-22 18:37:22 +00:00
|
|
|
MWWorld::ContainerStore& store = actor.getClass().getContainerStore(actor);
|
2012-11-06 07:29:18 +00:00
|
|
|
|
2013-08-12 23:19:33 +00:00
|
|
|
if (amount > 0)
|
2012-09-08 22:17:03 +00:00
|
|
|
{
|
2014-01-08 22:37:46 +00:00
|
|
|
store.add(MWWorld::ContainerStore::sGoldId, amount, actor);
|
2012-09-08 22:17:03 +00:00
|
|
|
}
|
2013-08-12 23:19:33 +00:00
|
|
|
else
|
|
|
|
{
|
2014-01-08 22:37:46 +00:00
|
|
|
store.remove(MWWorld::ContainerStore::sGoldId, - amount, actor);
|
2013-08-12 23:19:33 +00:00
|
|
|
}
|
2012-09-08 22:17:03 +00:00
|
|
|
}
|
|
|
|
|
2012-05-17 19:15:48 +00:00
|
|
|
void TradeWindow::onOfferButtonClicked(MyGUI::Widget* _sender)
|
|
|
|
{
|
2013-05-11 16:38:27 +00:00
|
|
|
TradeItemModel* playerItemModel = MWBase::Environment::get().getWindowManager()->getInventoryWindow()->getTradeModel();
|
|
|
|
|
2012-11-08 22:16:40 +00:00
|
|
|
const MWWorld::Store<ESM::GameSetting> &gmst =
|
|
|
|
MWBase::Environment::get().getWorld()->getStore().get<ESM::GameSetting>();
|
|
|
|
|
2012-05-18 20:06:25 +00:00
|
|
|
// were there any items traded at all?
|
2013-05-11 16:38:27 +00:00
|
|
|
std::vector<ItemStack> playerBought = playerItemModel->getItemsBorrowedToUs();
|
|
|
|
std::vector<ItemStack> merchantBought = mTradeModel->getItemsBorrowedToUs();
|
2014-04-27 17:03:33 +00:00
|
|
|
if (playerBought.empty() && merchantBought.empty())
|
2012-05-18 20:06:25 +00:00
|
|
|
{
|
|
|
|
// user notification
|
|
|
|
MWBase::Environment::get().getWindowManager()->
|
2013-03-30 11:56:37 +00:00
|
|
|
messageBox("#{sBarterDialog11}");
|
2012-05-18 20:06:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2012-05-18 20:06:25 +00:00
|
|
|
// check if the player can afford this
|
2014-01-08 22:37:46 +00:00
|
|
|
if (mCurrentBalance < 0 && playerGold < std::abs(mCurrentBalance))
|
2012-05-18 20:06:25 +00:00
|
|
|
{
|
|
|
|
// user notification
|
|
|
|
MWBase::Environment::get().getWindowManager()->
|
2013-03-30 11:56:37 +00:00
|
|
|
messageBox("#{sBarterDialog1}");
|
2012-05-18 20:06:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if the merchant can afford this
|
2013-03-04 11:21:38 +00:00
|
|
|
if (mCurrentBalance > 0 && getMerchantGold() < mCurrentBalance)
|
2012-05-18 20:06:25 +00:00
|
|
|
{
|
|
|
|
// user notification
|
|
|
|
MWBase::Environment::get().getWindowManager()->
|
2013-03-30 11:56:37 +00:00
|
|
|
messageBox("#{sBarterDialog2}");
|
2012-05-18 20:06:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-22 12:04:36 +00:00
|
|
|
// check if the player is attempting to sell back an item stolen from this actor
|
|
|
|
for (std::vector<ItemStack>::iterator it = merchantBought.begin(); it != merchantBought.end(); ++it)
|
|
|
|
{
|
2018-06-03 06:46:12 +00:00
|
|
|
if (MWBase::Environment::get().getMechanicsManager()->isItemStolenFrom(it->mBase.getCellRef().getRefId(), mPtr))
|
2014-01-22 12:04:36 +00:00
|
|
|
{
|
|
|
|
std::string msg = gmst.find("sNotifyMessage49")->getString();
|
|
|
|
if (msg.find("%s") != std::string::npos)
|
|
|
|
msg.replace(msg.find("%s"), 2, it->mBase.getClass().getName(it->mBase));
|
|
|
|
MWBase::Environment::get().getWindowManager()->messageBox(msg);
|
2017-08-14 06:41:37 +00:00
|
|
|
|
|
|
|
MWBase::Environment::get().getMechanicsManager()->confiscateStolenItemToOwner(player, it->mBase, mPtr, it->mCount);
|
|
|
|
|
2014-01-22 12:04:36 +00:00
|
|
|
onCancelButtonClicked(mCancelButton);
|
2017-09-26 21:27:53 +00:00
|
|
|
MWBase::Environment::get().getWindowManager()->exitCurrentGuiMode();
|
2014-01-22 12:04:36 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-04 16:28:47 +00:00
|
|
|
bool offerAccepted = mTrading.haggle(player, mPtr, mCurrentBalance, mCurrentMerchantOffer);
|
2015-02-04 20:18:43 +00:00
|
|
|
|
2016-06-04 16:28:47 +00:00
|
|
|
// apply disposition change if merchant is NPC
|
|
|
|
if ( mPtr.getClass().isNpc() ) {
|
|
|
|
int dispositionDelta = offerAccepted
|
|
|
|
? gmst.find("iBarterSuccessDisposition")->getInt()
|
|
|
|
: gmst.find("iBarterFailDisposition")->getInt();
|
2014-05-31 16:28:00 +00:00
|
|
|
|
2016-06-04 16:28:47 +00:00
|
|
|
MWBase::Environment::get().getDialogueManager()->applyDispositionChange(dispositionDelta);
|
2013-05-11 16:38:27 +00:00
|
|
|
}
|
2012-11-08 12:38:20 +00:00
|
|
|
|
2016-06-04 16:28:47 +00:00
|
|
|
// display message on haggle failure
|
|
|
|
if ( !offerAccepted ) {
|
|
|
|
MWBase::Environment::get().getWindowManager()->
|
|
|
|
messageBox("#{sNotifyMessage9}");
|
|
|
|
return;
|
|
|
|
}
|
2012-11-08 12:38:20 +00:00
|
|
|
|
2013-05-11 16:38:27 +00:00
|
|
|
// make the item transfer
|
2014-09-28 10:18:46 +00:00
|
|
|
mTradeModel->transferItems();
|
|
|
|
playerItemModel->transferItems();
|
2012-05-18 18:53:24 +00:00
|
|
|
|
2013-11-21 03:27:53 +00:00
|
|
|
// transfer the gold
|
2012-09-10 15:44:59 +00:00
|
|
|
if (mCurrentBalance != 0)
|
2013-11-21 03:27:53 +00:00
|
|
|
{
|
2014-01-08 22:37:46 +00:00
|
|
|
addOrRemoveGold(mCurrentBalance, player);
|
2014-03-28 16:01:56 +00:00
|
|
|
mPtr.getClass().getCreatureStats(mPtr).setGoldPool(
|
|
|
|
mPtr.getClass().getCreatureStats(mPtr).getGoldPool() - mCurrentBalance );
|
2013-11-21 03:27:53 +00:00
|
|
|
}
|
2012-05-18 20:21:44 +00:00
|
|
|
|
2017-09-25 16:07:49 +00:00
|
|
|
eventTradeDone();
|
2014-01-03 16:39:57 +00:00
|
|
|
|
2017-07-10 11:48:00 +00:00
|
|
|
MWBase::Environment::get().getWindowManager()->playSound("Item Gold Up");
|
2013-04-10 04:32:05 +00:00
|
|
|
MWBase::Environment::get().getWindowManager()->removeGuiMode(GM_Barter);
|
2017-08-03 14:41:13 +00:00
|
|
|
|
|
|
|
restock();
|
2012-05-17 19:15:48 +00:00
|
|
|
}
|
|
|
|
|
2017-09-24 11:10:50 +00:00
|
|
|
void TradeWindow::onAccept(MyGUI::EditBox *sender)
|
|
|
|
{
|
|
|
|
onOfferButtonClicked(sender);
|
|
|
|
}
|
|
|
|
|
2012-05-17 19:15:48 +00:00
|
|
|
void TradeWindow::onCancelButtonClicked(MyGUI::Widget* _sender)
|
|
|
|
{
|
2014-05-27 03:13:37 +00:00
|
|
|
exit();
|
2017-09-23 10:18:39 +00:00
|
|
|
MWBase::Environment::get().getWindowManager()->removeGuiMode(GM_Barter);
|
2012-05-17 19:15:48 +00:00
|
|
|
}
|
2012-05-17 19:56:16 +00:00
|
|
|
|
2013-03-04 11:21:38 +00:00
|
|
|
void TradeWindow::onMaxSaleButtonClicked(MyGUI::Widget* _sender)
|
|
|
|
{
|
|
|
|
mCurrentBalance = getMerchantGold();
|
|
|
|
updateLabels();
|
|
|
|
}
|
|
|
|
|
2014-09-10 22:25:09 +00:00
|
|
|
void TradeWindow::addRepeatController(MyGUI::Widget *widget)
|
|
|
|
{
|
2014-09-13 06:00:24 +00:00
|
|
|
MyGUI::ControllerItem* item = MyGUI::ControllerManager::getInstance().createItem(Controllers::ControllerRepeatEvent::getClassTypeName());
|
|
|
|
Controllers::ControllerRepeatEvent* controller = item->castType<Controllers::ControllerRepeatEvent>();
|
2014-09-10 22:25:09 +00:00
|
|
|
controller->eventRepeatClick += MyGUI::newDelegate(this, &TradeWindow::onRepeatClick);
|
|
|
|
controller->setRepeat(sBalanceChangeInitialPause, sBalanceChangeInterval);
|
|
|
|
MyGUI::ControllerManager::getInstance().addItem(widget, controller);
|
|
|
|
}
|
|
|
|
|
2013-02-10 04:50:36 +00:00
|
|
|
void TradeWindow::onIncreaseButtonPressed(MyGUI::Widget* _sender, int _left, int _top, MyGUI::MouseButton _id)
|
|
|
|
{
|
2014-09-10 22:25:09 +00:00
|
|
|
addRepeatController(_sender);
|
2013-02-10 04:50:36 +00:00
|
|
|
onIncreaseButtonTriggered();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TradeWindow::onDecreaseButtonPressed(MyGUI::Widget* _sender, int _left, int _top, MyGUI::MouseButton _id)
|
|
|
|
{
|
2014-09-10 22:25:09 +00:00
|
|
|
addRepeatController(_sender);
|
2013-02-10 04:50:36 +00:00
|
|
|
onDecreaseButtonTriggered();
|
|
|
|
}
|
|
|
|
|
2014-09-10 22:25:09 +00:00
|
|
|
void TradeWindow::onRepeatClick(MyGUI::Widget* widget, MyGUI::ControllerItem* controller)
|
|
|
|
{
|
|
|
|
if (widget == mIncreaseButton)
|
|
|
|
onIncreaseButtonTriggered();
|
|
|
|
else if (widget == mDecreaseButton)
|
|
|
|
onDecreaseButtonTriggered();
|
|
|
|
}
|
|
|
|
|
2013-02-10 04:50:36 +00:00
|
|
|
void TradeWindow::onBalanceButtonReleased(MyGUI::Widget *_sender, int _left, int _top, MyGUI::MouseButton _id)
|
|
|
|
{
|
2014-09-10 22:25:09 +00:00
|
|
|
MyGUI::ControllerManager::getInstance().removeItem(_sender);
|
2013-02-10 04:50:36 +00:00
|
|
|
}
|
|
|
|
|
2014-09-25 18:28:00 +00:00
|
|
|
void TradeWindow::onBalanceValueChanged(int value)
|
2014-05-31 11:53:36 +00:00
|
|
|
{
|
2014-09-25 18:28:00 +00:00
|
|
|
// Entering a "-" sign inverts the buying/selling state
|
|
|
|
mCurrentBalance = (mCurrentBalance >= 0 ? 1 : -1) * value;
|
|
|
|
updateLabels();
|
|
|
|
|
|
|
|
if (value != std::abs(value))
|
|
|
|
mTotalBalance->setValue(std::abs(value));
|
2014-05-31 11:53:36 +00:00
|
|
|
}
|
|
|
|
|
2013-02-10 04:50:36 +00:00
|
|
|
void TradeWindow::onIncreaseButtonTriggered()
|
2012-11-08 12:38:20 +00:00
|
|
|
{
|
2014-11-06 02:51:18 +00:00
|
|
|
// prevent overflows, and prevent entering INT_MIN since abs(INT_MIN) is undefined
|
|
|
|
if (mCurrentBalance == INT_MAX || mCurrentBalance == INT_MIN+1)
|
|
|
|
return;
|
2016-04-09 21:49:21 +00:00
|
|
|
if (mCurrentBalance < 0) mCurrentBalance -= 1;
|
|
|
|
else mCurrentBalance += 1;
|
2012-11-08 12:38:20 +00:00
|
|
|
updateLabels();
|
|
|
|
}
|
|
|
|
|
2013-02-10 04:50:36 +00:00
|
|
|
void TradeWindow::onDecreaseButtonTriggered()
|
2012-11-08 12:38:20 +00:00
|
|
|
{
|
2016-04-09 21:49:21 +00:00
|
|
|
if (mCurrentBalance < 0) mCurrentBalance += 1;
|
|
|
|
else mCurrentBalance -= 1;
|
2012-11-08 12:38:20 +00:00
|
|
|
updateLabels();
|
|
|
|
}
|
|
|
|
|
2012-05-17 19:56:16 +00:00
|
|
|
void TradeWindow::updateLabels()
|
|
|
|
{
|
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 01:50:43 +00:00
|
|
|
mPlayerGold->setCaptionWithReplacing("#{sYourGold} " + MyGUI::utility::toString(playerGold));
|
2012-05-17 19:56:16 +00:00
|
|
|
|
2016-04-09 21:49:21 +00:00
|
|
|
if (mCurrentBalance < 0)
|
2012-05-17 19:56:16 +00:00
|
|
|
{
|
2016-04-09 21:49:21 +00:00
|
|
|
mTotalBalanceLabel->setCaptionWithReplacing("#{sTotalCost}");
|
2012-05-17 19:56:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-04-09 21:49:21 +00:00
|
|
|
mTotalBalanceLabel->setCaptionWithReplacing("#{sTotalSold}");
|
2012-05-17 19:56:16 +00:00
|
|
|
}
|
2014-09-25 18:28:00 +00:00
|
|
|
|
|
|
|
mTotalBalance->setValue(std::abs(mCurrentBalance));
|
2012-05-17 19:56:16 +00:00
|
|
|
|
2015-01-10 01:50:43 +00:00
|
|
|
mMerchantGold->setCaptionWithReplacing("#{sSellerGold} " + MyGUI::utility::toString(getMerchantGold()));
|
2012-05-17 20:24:47 +00:00
|
|
|
}
|
2012-05-17 19:56:16 +00:00
|
|
|
|
2014-09-22 09:26:16 +00:00
|
|
|
void TradeWindow::updateOffer()
|
2012-05-18 20:06:25 +00:00
|
|
|
{
|
2014-09-22 09:26:16 +00:00
|
|
|
TradeItemModel* playerTradeModel = MWBase::Environment::get().getWindowManager()->getInventoryWindow()->getTradeModel();
|
2013-01-02 14:58:52 +00:00
|
|
|
|
2014-09-22 09:26:16 +00:00
|
|
|
int merchantOffer = 0;
|
|
|
|
|
|
|
|
std::vector<ItemStack> playerBorrowed = playerTradeModel->getItemsBorrowedToUs();
|
|
|
|
for (std::vector<ItemStack>::const_iterator it = playerBorrowed.begin(); it != playerBorrowed.end(); ++it)
|
|
|
|
{
|
|
|
|
merchantOffer -= MWBase::Environment::get().getMechanicsManager()->getBarterOffer(mPtr, getEffectiveValue(it->mBase, it->mCount), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<ItemStack> merchantBorrowed = mTradeModel->getItemsBorrowedToUs();
|
|
|
|
for (std::vector<ItemStack>::const_iterator it = merchantBorrowed.begin(); it != merchantBorrowed.end(); ++it)
|
|
|
|
{
|
|
|
|
merchantOffer += MWBase::Environment::get().getMechanicsManager()->getBarterOffer(mPtr, getEffectiveValue(it->mBase, it->mCount), false);
|
|
|
|
}
|
2012-05-18 20:06:25 +00:00
|
|
|
|
2014-09-22 09:26:16 +00:00
|
|
|
int diff = merchantOffer - mCurrentMerchantOffer;
|
|
|
|
mCurrentMerchantOffer = merchantOffer;
|
|
|
|
mCurrentBalance += diff;
|
2012-05-18 20:06:25 +00:00
|
|
|
updateLabels();
|
|
|
|
}
|
|
|
|
|
2014-09-22 09:26:16 +00:00
|
|
|
void TradeWindow::sellToNpc(const MWWorld::Ptr& item, int count, bool boughtItem)
|
2012-05-18 20:06:25 +00:00
|
|
|
{
|
2014-09-22 09:26:16 +00:00
|
|
|
updateOffer();
|
|
|
|
}
|
2012-05-18 20:06:25 +00:00
|
|
|
|
2014-09-22 09:26:16 +00:00
|
|
|
void TradeWindow::buyFromNpc(const MWWorld::Ptr& item, int count, bool soldItem)
|
|
|
|
{
|
|
|
|
updateOffer();
|
2012-05-18 20:06:25 +00:00
|
|
|
}
|
2012-05-26 23:14:33 +00:00
|
|
|
|
|
|
|
void TradeWindow::onReferenceUnavailable()
|
|
|
|
{
|
|
|
|
// remove both Trade 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_Barter);
|
2017-09-26 21:27:53 +00:00
|
|
|
MWBase::Environment::get().getWindowManager()->exitCurrentGuiMode();
|
2012-05-26 23:14:33 +00:00
|
|
|
}
|
2013-03-04 11:21:38 +00:00
|
|
|
|
|
|
|
int TradeWindow::getMerchantGold()
|
|
|
|
{
|
2014-03-28 16:01:56 +00:00
|
|
|
int merchantGold = mPtr.getClass().getCreatureStats(mPtr).getGoldPool();
|
2013-03-04 11:21:38 +00:00
|
|
|
return merchantGold;
|
|
|
|
}
|
2014-03-27 05:23:56 +00:00
|
|
|
|
2014-07-07 21:37:59 +00:00
|
|
|
void TradeWindow::resetReference()
|
|
|
|
{
|
|
|
|
ReferenceInterface::resetReference();
|
|
|
|
mItemView->setModel(NULL);
|
2014-07-14 14:53:58 +00:00
|
|
|
mTradeModel = NULL;
|
|
|
|
mSortModel = NULL;
|
2014-07-07 21:37:59 +00:00
|
|
|
}
|
2012-05-17 11:36:25 +00:00
|
|
|
}
|