change the drag&drop to not use a seperate containerstore

This commit is contained in:
scrawl 2012-05-15 21:44:57 +02:00
parent ca4fa21d64
commit 175623bf22
4 changed files with 45 additions and 42 deletions

View file

@ -83,10 +83,9 @@ void ContainerBase::onSelectedItemImpl(MyGUI::Widget* _sender, int count)
MWWorld::Ptr object = *mSelectedItem->getUserData<MWWorld::Ptr>();
_unequipItem(object);
int originalCount = object.getRefData().getCount();
object.getRefData().setCount(count);
mDragAndDrop->mStore.add(object);
object.getRefData().setCount(originalCount - count);
mDragAndDrop->mDraggedCount = count;
mDragAndDrop->mDraggedFrom = this;
std::string sound = MWWorld::Class::get(object).getUpSoundId(object);
MWBase::Environment::get().getSoundManager()->playSound (sound, 1.0, 1.0);
@ -94,7 +93,7 @@ void ContainerBase::onSelectedItemImpl(MyGUI::Widget* _sender, int count)
mDragAndDrop->mDraggedWidget = mSelectedItem;
static_cast<MyGUI::ImageBox*>(mSelectedItem)->setImageTexture(""); // remove the background texture (not visible during drag)
static_cast<MyGUI::TextBox*>(mSelectedItem->getChildAt(0)->getChildAt(0))->setCaption(
getCountString((*mDragAndDrop->mStore.begin()).getRefData().getCount()));
getCountString(mDragAndDrop->mDraggedCount));
mDragAndDrop->mWasInInventory = isInventory();
@ -108,19 +107,27 @@ void ContainerBase::onContainerClicked(MyGUI::Widget* _sender)
if(mDragAndDrop->mIsOnDragAndDrop) //drop widget here
{
MWWorld::Ptr object = *mDragAndDrop->mDraggedWidget->getUserData<MWWorld::Ptr>();
if (mDragAndDrop->mDraggedFrom != this)
{
assert(object.getContainerStore() && "Item is not in a container!");
std::string sound = MWWorld::Class::get(object).getDownSoundId(object);
MWBase::Environment::get().getSoundManager()->playSound (sound, 1.0, 1.0);
MWWorld::ContainerStore& containerStore = MWWorld::Class::get(mContainer).getContainerStore(mContainer);
containerStore.add(*mDragAndDrop->mStore.begin());
mDragAndDrop->mStore.clear();
int origCount = object.getRefData().getCount();
object.getRefData().setCount (mDragAndDrop->mDraggedCount);
containerStore.add(object);
object.getRefData().setCount (origCount - mDragAndDrop->mDraggedCount);
}
mDragAndDrop->mIsOnDragAndDrop = false;
MyGUI::Gui::getInstance().destroyWidget(mDragAndDrop->mDraggedWidget);
drawItems();
mDragAndDrop->mDraggedFrom->drawItems();
MWBase::Environment::get().getWindowManager()->setDragDrop(false);
std::string sound = MWWorld::Class::get(object).getDownSoundId(object);
MWBase::Environment::get().getSoundManager()->playSound (sound, 1.0, 1.0);
}
}
@ -224,7 +231,14 @@ void ContainerBase::drawItems()
{
index++;
const MWWorld::Ptr* iter = &((*it).first);
if(iter->getRefData().getCount() > 0 && !(onlyMagic && MWWorld::Class::get(*iter).getEnchantment(*iter) == "" && iter->getTypeName() != typeid(ESM::Potion).name()))
int displayCount = iter->getRefData().getCount();
if (mDragAndDrop->mIsOnDragAndDrop && *iter == *mDragAndDrop->mDraggedWidget->getUserData<MWWorld::Ptr>())
{
std::cout << "beep" << std::endl;
displayCount -= mDragAndDrop->mDraggedCount;
}
if(displayCount > 0 && !(onlyMagic && MWWorld::Class::get(*iter).getEnchantment(*iter) == "" && iter->getTypeName() != typeid(ESM::Potion).name()))
{
std::string path = std::string("icons\\");
path+=MWWorld::Class::get(*iter).getInventoryIcon(*iter);
@ -276,7 +290,7 @@ void ContainerBase::drawItems()
y = 0;
}
text->setCaption(getCountString(iter->getRefData().getCount()));
text->setCaption(getCountString(displayCount));
}
}

View file

@ -44,7 +44,8 @@ namespace MWGui
bool mIsOnDragAndDrop;
MyGUI::Widget* mDraggedWidget;
MyGUI::Widget* mDragAndDropWidget;
MWWorld::ContainerStore mStore;
ContainerBase* mDraggedFrom;
int mDraggedCount;
bool mWasInInventory; // was the item in inventory before it was dragged
};
@ -77,6 +78,8 @@ namespace MWGui
void openContainer(MWWorld::Ptr container);
void setFilter(Filter filter); ///< set category filter
void Update();
void drawItems();
protected:
MyGUI::ScrollView* mItemView;
@ -100,8 +103,6 @@ namespace MWGui
virtual bool isInventory() { return false; }
virtual std::vector<MWWorld::Ptr> getEquippedItems() { return std::vector<MWWorld::Ptr>(); }
virtual void _unequipItem(MWWorld::Ptr item) { ; }
void drawItems();
};
class ContainerWindow : public ContainerBase, public WindowBase

View file

@ -260,7 +260,7 @@ void HUD::onWorldClicked(MyGUI::Widget* _sender)
if (mDragAndDrop->mIsOnDragAndDrop)
{
// drop item into the gameworld
MWWorld::Ptr object = *mDragAndDrop->mStore.begin();
MWWorld::Ptr object = *mDragAndDrop->mDraggedWidget->getUserData<MWWorld::Ptr>();
MWWorld::World* world = MWBase::Environment::get().getWorld();
@ -279,7 +279,9 @@ void HUD::onWorldClicked(MyGUI::Widget* _sender)
std::string sound = MWWorld::Class::get(object).getDownSoundId(object);
MWBase::Environment::get().getSoundManager()->playSound (sound, 1.0, 1.0);
mDragAndDrop->mStore.clear();
// remove object from the container it was coming from
object.getRefData().setCount(0);
mDragAndDrop->mIsOnDragAndDrop = false;
MyGUI::Gui::getInstance().destroyWidget(mDragAndDrop->mDraggedWidget);
mDragAndDrop->mDraggedWidget = 0;

View file

@ -142,7 +142,7 @@ namespace MWGui
{
if (mDragAndDrop->mIsOnDragAndDrop)
{
MWWorld::Ptr ptr = *mDragAndDrop->mStore.begin();
MWWorld::Ptr ptr = *mDragAndDrop->mDraggedWidget->getUserData<MWWorld::Ptr>();
// can the object be equipped?
std::pair<std::vector<int>, bool> slots = MWWorld::Class::get(ptr).getEquipmentSlots(ptr);
@ -166,35 +166,22 @@ namespace MWGui
mWindowManager.getBookWindow()->setTakeButtonShow(false);
mWindowManager.getScrollWindow()->setTakeButtonShow(false);
}
// put back in inventory
MWWorld::ContainerStore& containerStore = MWWorld::Class::get(mContainer).getContainerStore(mContainer);
containerStore.add(ptr);
}
else
{
// put back in inventory
MWWorld::InventoryStore& invStore = static_cast<MWWorld::InventoryStore&>(MWWorld::Class::get(mContainer).getContainerStore(mContainer));
MWWorld::ContainerStoreIterator it = invStore.add(ptr);
// retrieve iterator to the item
MWWorld::ContainerStoreIterator it = invStore.begin();
for (; it != invStore.end(); ++it)
{
if (*it == ptr)
{
break;
}
}
// retrieve iterator to the item we just re-added (if stacking didn't happen).
// if stacking happened, the iterator was already returned by the add() call
/// \todo this does not work!
if (it == invStore.end())
{
std::cout << "stacking didn't happen" << std::endl;
for (MWWorld::ContainerStoreIterator it2 = invStore.begin();
it2 != invStore.end(); ++it2)
{
if (*it2 == ptr)
{
std::cout << "found iterator" << std::endl;
it = it2;
return;
}
}
}
assert(it != invStore.end());
// equip the item in the first available slot
invStore.equip(slots.first.front(), it);
@ -202,13 +189,12 @@ namespace MWGui
std::cout << "Equipped item in slot " << slots.first.front() << std::endl;
}
drawItems();
mDragAndDrop->mStore.clear();
mDragAndDrop->mIsOnDragAndDrop = false;
MyGUI::Gui::getInstance().destroyWidget(mDragAndDrop->mDraggedWidget);
mWindowManager.setDragDrop(false);
drawItems();
}
}