1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-27 21:35:24 +00:00
OpenMW/apps/openmw/mwgui/itemmodel.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

151 lines
4.2 KiB
C++
Raw Normal View History

2013-05-11 18:38:27 +02:00
#include "itemmodel.hpp"
#include "../mwworld/class.hpp"
#include "../mwworld/containerstore.hpp"
#include "../mwbase/environment.hpp"
2017-11-10 09:43:22 +04:00
#include "../mwbase/mechanicsmanager.hpp"
2013-05-11 18:38:27 +02:00
namespace MWGui
{
ItemStack::ItemStack(const MWWorld::Ptr& base, ItemModel* creator, size_t count)
: mType(Type_Normal)
2013-05-11 18:38:27 +02:00
, mFlags(0)
, mCreator(creator)
, mCount(count)
2013-05-11 18:38:27 +02:00
, mBase(base)
{
2022-08-22 16:55:53 +02:00
if (!base.getClass().getEnchantment(base).empty())
2013-05-11 18:38:27 +02:00
mFlags |= Flag_Enchanted;
2017-11-10 09:43:22 +04:00
if (MWBase::Environment::get().getMechanicsManager()->isBoundItem(base))
mFlags |= Flag_Bound;
2013-05-11 18:38:27 +02:00
}
ItemStack::ItemStack()
: mType(Type_Normal)
2013-05-11 18:38:27 +02:00
, mFlags(0)
2018-10-09 10:21:12 +04:00
, mCreator(nullptr)
, mCount(0)
2013-05-11 18:38:27 +02:00
{
}
bool operator==(const ItemStack& left, const ItemStack& right)
{
if (left.mType != right.mType)
return false;
2013-05-27 02:18:36 +02:00
2013-05-11 18:38:27 +02:00
if (left.mBase == right.mBase)
return true;
2013-05-27 02:18:36 +02:00
// If one of the items is in an inventory and currently equipped, we need to check stacking both ways to be sure
if (left.mBase.getContainerStore() && right.mBase.getContainerStore())
return left.mBase.getContainerStore()->stacks(left.mBase, right.mBase)
&& right.mBase.getContainerStore()->stacks(left.mBase, right.mBase);
if (left.mBase.getContainerStore())
return left.mBase.getContainerStore()->stacks(left.mBase, right.mBase);
if (right.mBase.getContainerStore())
return right.mBase.getContainerStore()->stacks(left.mBase, right.mBase);
MWWorld::ContainerStore store;
return store.stacks(left.mBase, right.mBase);
2013-05-11 18:38:27 +02:00
}
ItemModel::ItemModel() {}
MWWorld::Ptr ItemModel::moveItem(const ItemStack& item, size_t count, ItemModel* otherModel)
{
// TODO(#6148): moving an item should preserve RefNum and Lua scripts (unless the item stack is merged with
// already existing stack).
MWWorld::Ptr ret = otherModel->copyItem(item, count);
removeItem(item, count);
return ret;
}
bool ItemModel::allowedToUseItems() const
{
return true;
}
2017-10-04 23:26:06 +04:00
bool ItemModel::onDropItem(const MWWorld::Ptr& item, int count)
2017-10-04 22:37:08 +04:00
{
return true;
}
2017-10-04 23:26:06 +04:00
bool ItemModel::onTakeItem(const MWWorld::Ptr& item, int count)
2017-10-04 21:25:22 +04:00
{
return true;
}
bool ProxyItemModel::allowedToUseItems() const
{
return mSourceModel->allowedToUseItems();
}
MWWorld::Ptr ProxyItemModel::copyItem(const ItemStack& item, size_t count, bool allowAutoEquip)
2013-05-11 18:38:27 +02:00
{
return mSourceModel->copyItem(item, count, allowAutoEquip);
2013-05-11 18:38:27 +02:00
}
void ProxyItemModel::removeItem(const ItemStack& item, size_t count)
{
mSourceModel->removeItem(item, count);
}
ItemModel::ModelIndex ProxyItemModel::mapToSource(ModelIndex index)
{
const ItemStack& itemToSearch = getItem(index);
for (size_t i = 0; i < mSourceModel->getItemCount(); ++i)
{
const ItemStack& item = mSourceModel->getItem(i);
if (item.mBase == itemToSearch.mBase)
2013-05-11 18:38:27 +02:00
return i;
}
return -1;
}
ItemModel::ModelIndex ProxyItemModel::mapFromSource(ModelIndex index)
{
const ItemStack& itemToSearch = mSourceModel->getItem(index);
for (size_t i = 0; i < getItemCount(); ++i)
{
const ItemStack& item = getItem(i);
if (item.mBase == itemToSearch.mBase)
2013-05-11 18:38:27 +02:00
return i;
}
return -1;
}
2021-08-15 19:50:28 +02:00
ItemModel::ModelIndex ProxyItemModel::getIndex(const ItemStack& item)
2013-05-11 18:38:27 +02:00
{
return mSourceModel->getIndex(item);
}
2022-08-31 18:49:50 +02:00
void ProxyItemModel::setSourceModel(std::unique_ptr<ItemModel> sourceModel)
{
2022-08-31 18:49:50 +02:00
mSourceModel = std::move(sourceModel);
}
2017-10-04 23:26:06 +04:00
void ProxyItemModel::onClose()
2017-10-04 22:37:08 +04:00
{
2017-10-04 23:26:06 +04:00
mSourceModel->onClose();
2017-10-04 22:37:08 +04:00
}
2017-10-04 23:26:06 +04:00
bool ProxyItemModel::onDropItem(const MWWorld::Ptr& item, int count)
2017-10-04 21:25:22 +04:00
{
2017-10-04 23:26:06 +04:00
return mSourceModel->onDropItem(item, count);
}
bool ProxyItemModel::onTakeItem(const MWWorld::Ptr& item, int count)
{
return mSourceModel->onTakeItem(item, count);
2017-10-04 21:25:22 +04:00
}
bool ProxyItemModel::usesContainer(const MWWorld::Ptr& container)
{
return mSourceModel->usesContainer(container);
}
2013-05-11 18:38:27 +02:00
}