From db49e2c04668744a9d3372a7b7c4c67972a009b2 Mon Sep 17 00:00:00 2001 From: scrawl Date: Fri, 18 May 2012 20:53:24 +0200 Subject: [PATCH] bought/sold items now use a separate ContainerStore, this fixes a stacking issue among other things --- apps/openmw/mwgui/container.cpp | 76 +++++++++++++-------------- apps/openmw/mwgui/container.hpp | 14 +++-- apps/openmw/mwgui/inventorywindow.cpp | 1 - apps/openmw/mwgui/tradewindow.cpp | 23 ++++---- 4 files changed, 56 insertions(+), 58 deletions(-) diff --git a/apps/openmw/mwgui/container.cpp b/apps/openmw/mwgui/container.cpp index d0fd06a702..14a500b690 100644 --- a/apps/openmw/mwgui/container.cpp +++ b/apps/openmw/mwgui/container.cpp @@ -182,19 +182,15 @@ void ContainerBase::onSelectedItem(MyGUI::Widget* _sender) void ContainerBase::sellAlreadyBoughtItem(MyGUI::Widget* _sender, int count) { MWWorld::Ptr object = *mSelectedItem->getUserData(); - assert( std::find(mBoughtItems.begin(), mBoughtItems.end(), object) != mBoughtItems.end() ); - - if (count == object.getRefData().getCount()) - mBoughtItems.erase( std::find(mBoughtItems.begin(), mBoughtItems.end(), object) ); if (isInventory()) { - MWBase::Environment::get().getWindowManager()->getTradeWindow()->readdBarteredItem(*mSelectedItem->getUserData(), count); + MWBase::Environment::get().getWindowManager()->getTradeWindow()->addItem(object, count); MWBase::Environment::get().getWindowManager()->getTradeWindow()->drawItems(); } else { - MWBase::Environment::get().getWindowManager()->getInventoryWindow()->readdBarteredItem(*mSelectedItem->getUserData(), count); + MWBase::Environment::get().getWindowManager()->getInventoryWindow()->addItem(object, count); MWBase::Environment::get().getWindowManager()->getInventoryWindow()->drawItems(); } @@ -203,19 +199,14 @@ void ContainerBase::sellAlreadyBoughtItem(MyGUI::Widget* _sender, int count) void ContainerBase::sellItem(MyGUI::Widget* _sender, int count) { - MWWorld::Ptr newPtr; if (isInventory()) { - newPtr = MWBase::Environment::get().getWindowManager()->getTradeWindow()->addBarteredItem(*mSelectedItem->getUserData(), count); - if (std::find(mSoldItems.begin(), mSoldItems.end(), newPtr) == mSoldItems.end()) - mSoldItems.push_back(newPtr); + MWBase::Environment::get().getWindowManager()->getTradeWindow()->addBarteredItem(*mSelectedItem->getUserData(), count); MWBase::Environment::get().getWindowManager()->getTradeWindow()->drawItems(); } else { - newPtr = MWBase::Environment::get().getWindowManager()->getInventoryWindow()->addBarteredItem(*mSelectedItem->getUserData(), count); - if (std::find(mSoldItems.begin(), mSoldItems.end(), newPtr) == mSoldItems.end()) - mSoldItems.push_back(newPtr); + MWBase::Environment::get().getWindowManager()->getInventoryWindow()->addBarteredItem(*mSelectedItem->getUserData(), count); MWBase::Environment::get().getWindowManager()->getInventoryWindow()->drawItems(); } @@ -385,10 +376,16 @@ void ContainerBase::drawItems() std::vector equippedItems = getEquippedItems(); - // add bartered items (always at the beginning) - std::sort(mBoughtItems.begin(), mBoughtItems.end(), sortItems); - for (std::vector::iterator it=mBoughtItems.begin(); - it != mBoughtItems.end(); ++it) + // add bought items (always at the beginning) + std::vector boughtItems; + for (MWWorld::ContainerStoreIterator it (mBoughtItems.begin()); it!=mBoughtItems.end(); ++it) + { + boughtItems.push_back(*it); + } + std::sort(boughtItems.begin(), boughtItems.end(), sortItems); + + for (std::vector::iterator it=boughtItems.begin(); + it != boughtItems.end(); ++it) { items.push_back( std::make_pair(*it, ItemState_Barter) ); } @@ -529,49 +526,50 @@ std::string ContainerBase::getCountString(const int count) return boost::lexical_cast(count); } -MWWorld::Ptr ContainerBase::readdBarteredItem(MWWorld::Ptr item, int count) +void ContainerBase::addBarteredItem(MWWorld::Ptr item, int count) { - MWWorld::ContainerStore& containerStore = MWWorld::Class::get(mContainer).getContainerStore(mContainer); - int origCount = item.getRefData().getCount(); item.getRefData().setCount(count); - MWWorld::ContainerStoreIterator it = containerStore.add(item); + MWWorld::ContainerStoreIterator it = mBoughtItems.add(item); item.getRefData().setCount(origCount - count); - - if (origCount - count == 0) - mSoldItems.erase( std::find(mSoldItems.begin(), mSoldItems.end(), item) ); - - return *it; } -MWWorld::Ptr ContainerBase::addBarteredItem(MWWorld::Ptr item, int count) +void ContainerBase::addItem(MWWorld::Ptr item, int count) { MWWorld::ContainerStore& containerStore = MWWorld::Class::get(mContainer).getContainerStore(mContainer); - int origCount = item.getRefData().getCount(); + int origCount = item.getRefData().getCount(); + item.getRefData().setCount(count); MWWorld::ContainerStoreIterator it = containerStore.add(item); + item.getRefData().setCount(origCount - count); - - if (std::find(mBoughtItems.begin(), mBoughtItems.end(), *it) == mBoughtItems.end()) - mBoughtItems.push_back(*it); - - return *it; } -void ContainerBase::removeBarteredItem(MWWorld::Ptr item, int count) +void ContainerBase::transferBoughtItems() { MWWorld::ContainerStore& containerStore = MWWorld::Class::get(mContainer).getContainerStore(mContainer); - for (MWWorld::ContainerStoreIterator it(containerStore.begin()); it != containerStore.end(); ++it) + for (MWWorld::ContainerStoreIterator it(mBoughtItems.begin()); it != mBoughtItems.end(); ++it) { - if (*it == item) - { - item.getRefData().setCount(item.getRefData().getCount() - count); - } + containerStore.add(*it); } } +void ContainerBase::returnBoughtItems(MWWorld::ContainerStore& store) +{ + for (MWWorld::ContainerStoreIterator it(mBoughtItems.begin()); it != mBoughtItems.end(); ++it) + { + store.add(*it); + } +} + +MWWorld::ContainerStore& ContainerBase::getContainerStore() +{ + MWWorld::ContainerStore& store = MWWorld::Class::get(mContainer).getContainerStore(mContainer); + return store; +} + // ------------------------------------------------------------------------------------------------ ContainerWindow::ContainerWindow(WindowManager& parWindowManager,DragAndDrop* dragAndDrop) diff --git a/apps/openmw/mwgui/container.hpp b/apps/openmw/mwgui/container.hpp index 58d4548ee3..d300f4497c 100644 --- a/apps/openmw/mwgui/container.hpp +++ b/apps/openmw/mwgui/container.hpp @@ -67,9 +67,13 @@ namespace MWGui void setWidgets(MyGUI::Widget* containerWidget, MyGUI::ScrollView* itemView); ///< only call once - MWWorld::Ptr addBarteredItem(MWWorld::Ptr item, int count); - MWWorld::Ptr readdBarteredItem(MWWorld::Ptr item, int count); - void removeBarteredItem(MWWorld::Ptr item, int count); + void addBarteredItem(MWWorld::Ptr item, int count); + void addItem(MWWorld::Ptr item, int count); + + void transferBoughtItems(); ///< transfer bought items into the inventory + void returnBoughtItems(MWWorld::ContainerStore& store); ///< return bought items into the specified ContainerStore + + MWWorld::ContainerStore& getContainerStore(); void openContainer(MWWorld::Ptr container); void setFilter(Filter filter); ///< set category filter @@ -86,8 +90,8 @@ namespace MWGui Filter mFilter; - std::vector mBoughtItems; - std::vector mSoldItems; + // bought items are put in a separate ContainerStore so that they don't stack with other (not bought) items. + MWWorld::ContainerStore mBoughtItems; void onSelectedItem(MyGUI::Widget* _sender); void onContainerClicked(MyGUI::Widget* _sender); diff --git a/apps/openmw/mwgui/inventorywindow.cpp b/apps/openmw/mwgui/inventorywindow.cpp index ca629d51d0..8bc9661e49 100644 --- a/apps/openmw/mwgui/inventorywindow.cpp +++ b/apps/openmw/mwgui/inventorywindow.cpp @@ -97,7 +97,6 @@ namespace MWGui mTrading = false; mBoughtItems.clear(); - mSoldItems.clear(); onWindowResize(static_cast(mMainWidget)); } diff --git a/apps/openmw/mwgui/tradewindow.cpp b/apps/openmw/mwgui/tradewindow.cpp index 9f491242bd..fb70e00457 100644 --- a/apps/openmw/mwgui/tradewindow.cpp +++ b/apps/openmw/mwgui/tradewindow.cpp @@ -110,7 +110,6 @@ namespace MWGui mWindowManager.getInventoryWindow()->startTrade(); mBoughtItems.clear(); - mSoldItems.clear(); ContainerBase::openContainer(actor); @@ -146,10 +145,19 @@ namespace MWGui void TradeWindow::onOfferButtonClicked(MyGUI::Widget* _sender) { + transferBoughtItems(); + mWindowManager.getInventoryWindow()->transferBoughtItems(); + + mWindowManager.setGuiMode(GM_Game); } void TradeWindow::onCancelButtonClicked(MyGUI::Widget* _sender) { + // i give you back your stuff! + returnBoughtItems(mWindowManager.getInventoryWindow()->getContainerStore()); + // now gimme back my stuff! + mWindowManager.getInventoryWindow()->returnBoughtItems(MWWorld::Class::get(mContainer).getContainerStore(mContainer)); + mWindowManager.setGuiMode(GM_Game); } @@ -169,16 +177,6 @@ namespace MWGui mTotalBalance->setCaption(boost::lexical_cast(-mCurrentBalance)); } - // retrieve merchant gold - int gold = 0; - MWWorld::InventoryStore& invStore = static_cast(MWWorld::Class::get(mContainer).getContainerStore(mContainer)); - for (MWWorld::ContainerStoreIterator it = invStore.begin(); - it != invStore.end(); ++it) - { - if (MWWorld::Class::get(*it).getName(*it) == MWBase::Environment::get().getWorld()->getStore().gameSettings.search("sGold")->str) - gold = it->getRefData().getCount(); - } - int merchantgold; if (mContainer.getTypeName() == typeid(ESM::NPC).name()) { @@ -193,10 +191,9 @@ namespace MWGui ESMS::LiveCellRef* ref = mContainer.get(); merchantgold = ref->base->data.gold; } - gold += merchantgold; mMerchantGold->setCaption(MWBase::Environment::get().getWorld()->getStore().gameSettings.search("sSellerGold")->str - + " " + boost::lexical_cast(gold)); + + " " + boost::lexical_cast(merchantgold)); } std::vector TradeWindow::getEquippedItems()