1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-12-02 20:04:31 +00:00
openmw/apps/openmw/mwgui/itemtransfer.hpp
elsid 463de2d791
Instantly transfer items if alt key is pressed
Support player's inventory, container and companion windows with
dropping and picking up items from the world.

Add ItemTransfer class to handle transfers similar to DragAndDrop. When
a container window is opened the view is added to the set of targets and
removed when closed. If ALT is pressed instead of starting dragging an
item perform a transfer. If there is 1 target which is not a source move
item there. If there is no target drop item into the world using
WorldItemModel.

Special case is picking up an item. Don't start dragging, just update
the player's inventory view because the item is already there.
2025-07-12 13:34:59 +02:00

78 lines
2.1 KiB
C++

#ifndef OPENMW_APPS_OPENMW_MWGUI_ITEMTRANSFER_H
#define OPENMW_APPS_OPENMW_MWGUI_ITEMTRANSFER_H
#include "inventorywindow.hpp"
#include "itemmodel.hpp"
#include "itemview.hpp"
#include "windowmanagerimp.hpp"
#include "worlditemmodel.hpp"
#include <apps/openmw/mwbase/windowmanager.hpp>
#include <apps/openmw/mwworld/class.hpp>
#include <components/misc/notnullptr.hpp>
#include <unordered_set>
namespace MWGui
{
class ItemTransfer
{
public:
explicit ItemTransfer(WindowManager& windowManager)
: mWindowManager(&windowManager)
{
}
void addTarget(ItemView& view) { mTargets.insert(&view); }
void removeTarget(ItemView& view) { mTargets.erase(&view); }
void apply(const ItemStack& item, std::size_t count, ItemView& sourceView)
{
if (item.mFlags & ItemStack::Flag_Bound)
{
mWindowManager->messageBox("#{sBarterDialog12}");
return;
}
ItemView* targetView = nullptr;
for (ItemView* const view : mTargets)
{
if (view == &sourceView)
continue;
if (targetView != nullptr)
{
mWindowManager->messageBox("#{sContentsMessage2}");
return;
}
targetView = view;
}
WorldItemModel worldItemModel(0.5f, 0.5f);
ItemModel* const targetModel = targetView == nullptr ? &worldItemModel : targetView->getModel();
if (!targetModel->onDropItem(item.mBase, count))
return;
sourceView.getModel()->moveItem(item, count, targetModel);
if (targetView != nullptr)
targetView->update();
sourceView.update();
mWindowManager->getInventoryWindow()->updateItemView();
mWindowManager->playSound(item.mBase.getClass().getDownSoundId(item.mBase));
}
private:
Misc::NotNullPtr<WindowManager> mWindowManager;
std::unordered_set<ItemView*> mTargets;
};
}
#endif