From 6f03694d505f2ec15ee845056d8db8a7b3309050 Mon Sep 17 00:00:00 2001 From: scrawl Date: Mon, 19 May 2014 08:03:55 +0200 Subject: [PATCH] Fixes #1334: Only unequip item if the sell/drag action is actually successful --- apps/openmw/mwgui/inventorywindow.cpp | 62 ++++++++++++++------------- apps/openmw/mwgui/inventorywindow.hpp | 3 ++ 2 files changed, 36 insertions(+), 29 deletions(-) diff --git a/apps/openmw/mwgui/inventorywindow.cpp b/apps/openmw/mwgui/inventorywindow.cpp index 4d52d59bb..e9a4a18e5 100644 --- a/apps/openmw/mwgui/inventorywindow.cpp +++ b/apps/openmw/mwgui/inventorywindow.cpp @@ -176,35 +176,6 @@ namespace MWGui return; } - if (item.mType == ItemStack::Type_Equipped) - { - MWWorld::InventoryStore& invStore = MWWorld::Class::get(mPtr).getInventoryStore(mPtr); - MWWorld::Ptr newStack = *invStore.unequipItem(item.mBase, mPtr); - - // The unequipped item was re-stacked. We have to update the index - // since the item pointed does not exist anymore. - if (item.mBase != newStack) - { - // newIndex will store the index of the ItemStack the item was stacked on - int newIndex = -1; - for (size_t i=0; i < mTradeModel->getItemCount(); ++i) - { - if (mTradeModel->getItem(i).mBase == newStack) - { - newIndex = i; - break; - } - } - - if (newIndex == -1) - throw std::runtime_error("Can't find restacked item"); - - index = newIndex; - object = mTradeModel->getItem(index).mBase; - } - - } - bool shift = MyGUI::InputManager::getInstance().isShiftPressed(); if (MyGUI::InputManager::getInstance().isControlPressed()) count = 1; @@ -247,13 +218,46 @@ namespace MWGui notifyContentChanged(); } + void InventoryWindow::ensureSelectedItemUnequipped() + { + const ItemStack& item = mTradeModel->getItem(mSelectedItem); + if (item.mType == ItemStack::Type_Equipped) + { + MWWorld::InventoryStore& invStore = MWWorld::Class::get(mPtr).getInventoryStore(mPtr); + MWWorld::Ptr newStack = *invStore.unequipItem(item.mBase, mPtr); + + // The unequipped item was re-stacked. We have to update the index + // since the item pointed does not exist anymore. + if (item.mBase != newStack) + { + // newIndex will store the index of the ItemStack the item was stacked on + int newIndex = -1; + for (size_t i=0; i < mTradeModel->getItemCount(); ++i) + { + if (mTradeModel->getItem(i).mBase == newStack) + { + newIndex = i; + break; + } + } + + if (newIndex == -1) + throw std::runtime_error("Can't find restacked item"); + + mSelectedItem = newIndex; + } + } + } + void InventoryWindow::dragItem(MyGUI::Widget* sender, int count) { + ensureSelectedItemUnequipped(); mDragAndDrop->startDrag(mSelectedItem, mSortModel, mTradeModel, mItemView, count); } void InventoryWindow::sellItem(MyGUI::Widget* sender, int count) { + ensureSelectedItemUnequipped(); const ItemStack& item = mTradeModel->getItem(mSelectedItem); std::string sound = MWWorld::Class::get(item.mBase).getDownSoundId(item.mBase); MWBase::Environment::get().getSoundManager()->playSound (sound, 1.0, 1.0); diff --git a/apps/openmw/mwgui/inventorywindow.hpp b/apps/openmw/mwgui/inventorywindow.hpp index 17a25e502..df563b3d4 100644 --- a/apps/openmw/mwgui/inventorywindow.hpp +++ b/apps/openmw/mwgui/inventorywindow.hpp @@ -102,6 +102,9 @@ namespace MWGui void notifyContentChanged(); void adjustPanes(); + + /// Unequips mSelectedItem, if it is equipped, and then updates mSelectedItem in case it was re-stacked + void ensureSelectedItemUnequipped(); }; }