From ac33ff94827188d2623a042120ad51874ae7253b Mon Sep 17 00:00:00 2001 From: Andrei Kortunov Date: Wed, 4 Oct 2017 22:37:08 +0400 Subject: [PATCH] Move onDropItem() check to item models --- apps/openmw/mwgui/container.cpp | 24 ++-------------- apps/openmw/mwgui/containeritemmodel.cpp | 34 +++++++++++++++++++++-- apps/openmw/mwgui/containeritemmodel.hpp | 5 +++- apps/openmw/mwgui/inventoryitemmodel.cpp | 2 +- apps/openmw/mwgui/inventoryitemmodel.hpp | 3 +- apps/openmw/mwgui/itemmodel.cpp | 14 ++++++++-- apps/openmw/mwgui/itemmodel.hpp | 7 +++-- apps/openmw/mwgui/pickpocketitemmodel.cpp | 12 +++++++- apps/openmw/mwgui/pickpocketitemmodel.hpp | 1 + apps/openmw/mwgui/sortfilteritemmodel.cpp | 7 ++++- apps/openmw/mwgui/sortfilteritemmodel.hpp | 3 +- 11 files changed, 79 insertions(+), 33 deletions(-) diff --git a/apps/openmw/mwgui/container.cpp b/apps/openmw/mwgui/container.cpp index be3cb1522..3fa07b070 100644 --- a/apps/openmw/mwgui/container.cpp +++ b/apps/openmw/mwgui/container.cpp @@ -100,28 +100,10 @@ namespace MWGui void ContainerWindow::dropItem() { - if (mPtr.getTypeName() == typeid(ESM::Container).name()) - { - // check container organic flag - MWWorld::LiveCellRef* ref = mPtr.get(); - if (ref->mBase->mFlags & ESM::Container::Organic) - { - MWBase::Environment::get().getWindowManager()-> - messageBox("#{sContentsMessage2}"); - return; - } - - // check that we don't exceed container capacity - MWWorld::Ptr item = mDragAndDrop->mItem.mBase; - float weight = item.getClass().getWeight(item) * mDragAndDrop->mDraggedCount; - if (mPtr.getClass().getCapacity(mPtr) < mPtr.getClass().getEncumbrance(mPtr) + weight) - { - MWBase::Environment::get().getWindowManager()->messageBox("#{sContentsMessage3}"); - return; - } - } + bool success = mModel->onDropItem(mDragAndDrop->mItem.mBase, mDragAndDrop->mDraggedCount); - mDragAndDrop->drop(mModel, mItemView); + if (success) + mDragAndDrop->drop(mModel, mItemView); } void ContainerWindow::onBackgroundSelected() diff --git a/apps/openmw/mwgui/containeritemmodel.cpp b/apps/openmw/mwgui/containeritemmodel.cpp index ee89f1350..ef7aa8215 100644 --- a/apps/openmw/mwgui/containeritemmodel.cpp +++ b/apps/openmw/mwgui/containeritemmodel.cpp @@ -10,6 +10,7 @@ #include "../mwbase/environment.hpp" #include "../mwbase/mechanicsmanager.hpp" +#include "../mwbase/windowmanager.hpp" #include "../mwbase/world.hpp" #include "../mwmechanics/actorutil.hpp" @@ -185,8 +186,37 @@ void ContainerItemModel::update() } } } +bool ContainerItemModel::onDropItem(const MWWorld::Ptr &item, int count) const +{ + if (mItemSources.empty()) + return false; + + MWWorld::Ptr target = mItemSources[0]; -bool ContainerItemModel::onTakeItem(const MWWorld::Ptr &item, int count) + if (target.getTypeName() != typeid(ESM::Container).name()) + return true; + + // check container organic flag + MWWorld::LiveCellRef* ref = target.get(); + if (ref->mBase->mFlags & ESM::Container::Organic) + { + MWBase::Environment::get().getWindowManager()-> + messageBox("#{sContentsMessage2}"); + return false; + } + + // check that we don't exceed container capacity + float weight = item.getClass().getWeight(item) * count; + if (target.getClass().getCapacity(target) < target.getClass().getEncumbrance(target) + weight) + { + MWBase::Environment::get().getWindowManager()->messageBox("#{sContentsMessage3}"); + return false; + } + + return true; +} + +bool ContainerItemModel::onTakeItem(const MWWorld::Ptr &item, int count) const { if (mItemSources.empty()) return false; @@ -196,7 +226,7 @@ bool ContainerItemModel::onTakeItem(const MWWorld::Ptr &item, int count) // Looting a dead corpse is considered OK if (target.getClass().isActor() && target.getClass().getCreatureStats(target).isDead()) return true; - + MWWorld::Ptr player = MWMechanics::getPlayer(); MWBase::Environment::get().getMechanicsManager()->itemTaken(player, item, target, count); diff --git a/apps/openmw/mwgui/containeritemmodel.hpp b/apps/openmw/mwgui/containeritemmodel.hpp index a0dbc2cf9..fb57096ef 100644 --- a/apps/openmw/mwgui/containeritemmodel.hpp +++ b/apps/openmw/mwgui/containeritemmodel.hpp @@ -18,6 +18,10 @@ namespace MWGui ContainerItemModel (const MWWorld::Ptr& source); virtual bool allowedToUseItems() const; + + virtual bool onDropItem(const MWWorld::Ptr &item, int count) const; + virtual bool onTakeItem(const MWWorld::Ptr &item, int count) const; + virtual ItemStack getItem (ModelIndex index); virtual ModelIndex getIndex (ItemStack item); virtual size_t getItemCount(); @@ -26,7 +30,6 @@ namespace MWGui virtual void removeItem (const ItemStack& item, size_t count); virtual void update(); - virtual bool onTakeItem(const MWWorld::Ptr &item, int count); private: std::vector mItemSources; diff --git a/apps/openmw/mwgui/inventoryitemmodel.cpp b/apps/openmw/mwgui/inventoryitemmodel.cpp index b807a58f4..aed08feb1 100644 --- a/apps/openmw/mwgui/inventoryitemmodel.cpp +++ b/apps/openmw/mwgui/inventoryitemmodel.cpp @@ -124,7 +124,7 @@ bool InventoryItemModel::onTakeItem(const MWWorld::Ptr &item, int count) const // Looting a dead corpse is considered OK if (mActor.getClass().isActor() && mActor.getClass().getCreatureStats(mActor).isDead()) return true; - + MWWorld::Ptr player = MWMechanics::getPlayer(); MWBase::Environment::get().getMechanicsManager()->itemTaken(player, item, mActor, count); diff --git a/apps/openmw/mwgui/inventoryitemmodel.hpp b/apps/openmw/mwgui/inventoryitemmodel.hpp index 95afeb5d4..c5ca80eed 100644 --- a/apps/openmw/mwgui/inventoryitemmodel.hpp +++ b/apps/openmw/mwgui/inventoryitemmodel.hpp @@ -15,6 +15,8 @@ namespace MWGui virtual ModelIndex getIndex (ItemStack item); virtual size_t getItemCount(); + virtual bool onTakeItem(const MWWorld::Ptr &item, int count) const; + virtual MWWorld::Ptr copyItem (const ItemStack& item, size_t count, bool setNewOwner=false); virtual void removeItem (const ItemStack& item, size_t count); @@ -22,7 +24,6 @@ namespace MWGui virtual MWWorld::Ptr moveItem (const ItemStack& item, size_t count, ItemModel* otherModel); virtual void update(); - virtual bool onTakeItem(const MWWorld::Ptr &item, int count) const; protected: MWWorld::Ptr mActor; diff --git a/apps/openmw/mwgui/itemmodel.cpp b/apps/openmw/mwgui/itemmodel.cpp index 5a6afdf02..25dce728a 100644 --- a/apps/openmw/mwgui/itemmodel.cpp +++ b/apps/openmw/mwgui/itemmodel.cpp @@ -129,7 +129,12 @@ namespace MWGui return true; } - bool ItemModel::onTakeItem(const MWWorld::Ptr &item, int count) + bool ItemModel::onDropItem(const MWWorld::Ptr &item, int count) const + { + return true; + } + + bool ItemModel::onTakeItem(const MWWorld::Ptr &item, int count) const { return true; } @@ -203,7 +208,12 @@ namespace MWGui mSourceModel = sourceModel; } - bool ProxyItemModel::onTakeItem(const MWWorld::Ptr &item, int count) + bool ProxyItemModel::onDropItem(const MWWorld::Ptr &item, int count) const + { + return mSourceModel->onDropItem (item, count); + } + + bool ProxyItemModel::onTakeItem(const MWWorld::Ptr &item, int count) const { return mSourceModel->onTakeItem (item, count); } diff --git a/apps/openmw/mwgui/itemmodel.hpp b/apps/openmw/mwgui/itemmodel.hpp index c4a3a03e8..78c646e33 100644 --- a/apps/openmw/mwgui/itemmodel.hpp +++ b/apps/openmw/mwgui/itemmodel.hpp @@ -75,7 +75,8 @@ namespace MWGui /// Is the player allowed to insert items into this model? (default true) virtual bool allowedToInsertItems() const; - virtual bool onTakeItem(const MWWorld::Ptr &item, int count); + virtual bool onDropItem(const MWWorld::Ptr &item, int count) const; + virtual bool onTakeItem(const MWWorld::Ptr &item, int count) const; private: ItemModel(const ItemModel&); @@ -92,10 +93,12 @@ namespace MWGui bool allowedToUseItems() const; + virtual bool onDropItem(const MWWorld::Ptr &item, int count) const; + virtual bool onTakeItem(const MWWorld::Ptr &item, int count) const; + virtual MWWorld::Ptr copyItem (const ItemStack& item, size_t count, bool setNewOwner=false); virtual void removeItem (const ItemStack& item, size_t count); virtual ModelIndex getIndex (ItemStack item); - virtual bool onTakeItem(const MWWorld::Ptr &item, int count); /// @note Takes ownership of the passed pointer. void setSourceModel(ItemModel* sourceModel); diff --git a/apps/openmw/mwgui/pickpocketitemmodel.cpp b/apps/openmw/mwgui/pickpocketitemmodel.cpp index e610854c1..ba8fd7d75 100644 --- a/apps/openmw/mwgui/pickpocketitemmodel.cpp +++ b/apps/openmw/mwgui/pickpocketitemmodel.cpp @@ -4,6 +4,7 @@ #include #include "../mwmechanics/actorutil.hpp" +#include "../mwmechanics/creaturestats.hpp" #include "../mwmechanics/pickpocket.hpp" #include "../mwworld/class.hpp" @@ -80,12 +81,21 @@ namespace MWGui bool PickpocketItemModel::allowedToInsertItems() const { - // don't allow "reverse pickpocket" (yet) + // don't allow "reverse pickpocket" (it will be handled by scripts after 1.0) + return false; + } + + bool PickpocketItemModel::onDropItem(const MWWorld::Ptr &item, int count) + { + // don't allow "reverse pickpocket" (it will be handled by scripts after 1.0) return false; } bool PickpocketItemModel::onTakeItem(const MWWorld::Ptr &item, int count) const { + if (mActor.getClass().getCreatureStats(mActor).getKnockedDown()) + return mSourceModel->onTakeItem(item, count); + MWWorld::Ptr player = MWMechanics::getPlayer(); MWMechanics::Pickpocket pickpocket(player, mActor); if (pickpocket.pick(item, count)) diff --git a/apps/openmw/mwgui/pickpocketitemmodel.hpp b/apps/openmw/mwgui/pickpocketitemmodel.hpp index ad8788da7..a759cd993 100644 --- a/apps/openmw/mwgui/pickpocketitemmodel.hpp +++ b/apps/openmw/mwgui/pickpocketitemmodel.hpp @@ -18,6 +18,7 @@ namespace MWGui virtual void update(); virtual void removeItem (const ItemStack& item, size_t count); virtual bool allowedToInsertItems() const; + virtual bool onDropItem(const MWWorld::Ptr &item, int count) const; virtual bool onTakeItem(const MWWorld::Ptr &item, int count) const; protected: diff --git a/apps/openmw/mwgui/sortfilteritemmodel.cpp b/apps/openmw/mwgui/sortfilteritemmodel.cpp index 932f362fb..15c7fbfcb 100644 --- a/apps/openmw/mwgui/sortfilteritemmodel.cpp +++ b/apps/openmw/mwgui/sortfilteritemmodel.cpp @@ -311,7 +311,12 @@ namespace MWGui std::sort(mItems.begin(), mItems.end(), cmp); } - bool SortFilterItemModel::onTakeItem(const MWWorld::Ptr &item, int count) + bool SortFilterItemModel::onDropItem(const MWWorld::Ptr &item, int count) const + { + return mSourceModel->onDropItem (item, count); + } + + bool SortFilterItemModel::onTakeItem(const MWWorld::Ptr &item, int count) const { return mSourceModel->onTakeItem (item, count); } diff --git a/apps/openmw/mwgui/sortfilteritemmodel.hpp b/apps/openmw/mwgui/sortfilteritemmodel.hpp index e99726b0f..fa8d836b5 100644 --- a/apps/openmw/mwgui/sortfilteritemmodel.hpp +++ b/apps/openmw/mwgui/sortfilteritemmodel.hpp @@ -29,7 +29,8 @@ namespace MWGui /// Use ItemStack::Type for sorting? void setSortByType(bool sort) { mSortByType = sort; } - bool onTakeItem(const MWWorld::Ptr &item, int count); + bool onDropItem(const MWWorld::Ptr &item, int count) const; + bool onTakeItem(const MWWorld::Ptr &item, int count) const; static const int Category_Weapon = (1<<1); static const int Category_Apparel = (1<<2);