diff options
-rw-r--r-- | src/gui/widgets/itemcontainer.cpp | 33 | ||||
-rw-r--r-- | src/gui/widgets/itemcontainer.h | 3 | ||||
-rw-r--r-- | src/gui/windows/npcdialog.cpp | 96 | ||||
-rw-r--r-- | src/gui/windows/npcdialog.h | 3 | ||||
-rw-r--r-- | src/resources/inventory/complexinventory.cpp | 132 | ||||
-rw-r--r-- | src/resources/inventory/complexinventory.h | 16 | ||||
-rw-r--r-- | src/resources/inventory/inventory.cpp | 29 | ||||
-rw-r--r-- | src/resources/inventory/inventory.h | 32 |
8 files changed, 276 insertions, 68 deletions
diff --git a/src/gui/widgets/itemcontainer.cpp b/src/gui/widgets/itemcontainer.cpp index d96d89868..af614bcb1 100644 --- a/src/gui/widgets/itemcontainer.cpp +++ b/src/gui/widgets/itemcontainer.cpp @@ -799,7 +799,8 @@ void ItemContainer::mouseReleased(MouseEvent &event) Item *const item = inventory->getItem(dragDrop.getTag()); if (mInventory->addVirtualItem( item, - getSlotByXY(event.getX(), event.getY()))) + getSlotByXY(event.getX(), event.getY()), + 1)) { inventory->virtualRemove(item, 1); } @@ -863,7 +864,8 @@ void ItemContainer::mouseReleased(MouseEvent &event) Item *const item = inventory->getItem(dragDrop.getTag()); if (mInventory->addVirtualItem( item, - getSlotByXY(event.getX(), event.getY()))) + getSlotByXY(event.getX(), event.getY()), + 1)) { inventory->virtualRemove(item, 1); } @@ -881,26 +883,14 @@ void ItemContainer::mouseReleased(MouseEvent &event) if (index == Inventory::NO_SLOT_INDEX) { if (inventory) - inventory->virtualRestore(item, 1); - mInventory->removeItemAt(dragDrop.getTag()); + { + inventory->virtualRestore(item, + item->getQuantity()); + mInventory->removeItemAt(dragDrop.getTag()); + } return; } - mInventory->removeItemAt(index); - mInventory->setItem(index, - item->getId(), - item->getType(), - 1, - 1, - item->getColor(), - item->getIdentified(), - item->getDamaged(), - item->getFavorite(), - Equipm_false, - Equipped_false); - Item *const item2 = mInventory->getItem(index); - if (item2) - item2->setTag(item->getTag()); - mInventory->removeItemAt(dragDrop.getTag()); + mInventory->moveItem(index, dragDrop.getTag()); } else { @@ -910,7 +900,8 @@ void ItemContainer::mouseReleased(MouseEvent &event) dragDrop.getTag()); if (item) { - inventory->virtualRestore(item, 1); + inventory->virtualRestore(item, + item->getQuantity()); mInventory->removeItemAt(dragDrop.getTag()); } } diff --git a/src/gui/widgets/itemcontainer.h b/src/gui/widgets/itemcontainer.h index a7b62bbd6..e40ad4f17 100644 --- a/src/gui/widgets/itemcontainer.h +++ b/src/gui/widgets/itemcontainer.h @@ -138,6 +138,9 @@ class ItemContainer final : public Widget, void unsetInventory() { mInventory = nullptr; } + void setInventory(Inventory *const inventory) + { mInventory = inventory; } + void setCellBackgroundImage(const std::string &xmlName); void setMaxColumns(const int maxColumns); diff --git a/src/gui/windows/npcdialog.cpp b/src/gui/windows/npcdialog.cpp index 54ba7ca60..66cdc4bb3 100644 --- a/src/gui/windows/npcdialog.cpp +++ b/src/gui/windows/npcdialog.cpp @@ -63,7 +63,7 @@ #include "resources/inventory/complexinventory.h" -#include "resources/item/item.h" +#include "resources/item/complexitem.h" #include "net/npchandler.h" #include "net/packetlimiter.h" @@ -129,7 +129,7 @@ NpcDialog::NpcDialog(const BeingId npcId) : // TRANSLATORS: npc dialog button mResetButton(new Button(this, _("Reset"), "reset", this)), mInventory(new Inventory(InventoryType::Craft, 1)), - mComplexInventory(new ComplexInventory(InventoryType::Npc, 1)), + mComplexInventory(new ComplexInventory(InventoryType::Craft, 1)), mItemContainer(new ItemContainer(this, mInventory, 10000, ShowEmptyRows_true)), mItemScrollArea(new ScrollArea(this, mItemContainer, @@ -473,38 +473,22 @@ void NpcDialog::action(const ActionEvent &event) } std::string str; - const int sz = mInventory->getSize(); + const int sz = mComplexInventory->getSize(); if (sz == 0) { str = ""; } else { - const Item *item = mInventory->getItem(0); - if (item) - { - str = strprintf("%d,%d", - item->getTag(), - item->getQuantity()); - } - else - { - str = ""; - } + const ComplexItem *item = dynamic_cast<ComplexItem*>( + mComplexInventory->getItem(0)); + str = complexItemToStr(item); for (int f = 1; f < sz; f ++) { str.append("|"); - item = mInventory->getItem(f); - if (item) - { - str.append(strprintf("%d,%d", - item->getTag(), - item->getQuantity())); - } - else - { - str.append(""); - } + item = dynamic_cast<ComplexItem*>( + mComplexInventory->getItem(f)); + str.append(complexItemToStr(item)); } } @@ -543,9 +527,11 @@ void NpcDialog::action(const ActionEvent &event) break; case NPC_INPUT_ITEM: case NPC_INPUT_ITEM_INDEX: - case NPC_INPUT_ITEM_CRAFT: mInventory->clear(); break; + case NPC_INPUT_ITEM_CRAFT: + mComplexInventory->clear(); + break; case NPC_INPUT_NONE: case NPC_INPUT_LIST: default: @@ -566,9 +552,11 @@ void NpcDialog::action(const ActionEvent &event) { case NPC_INPUT_ITEM: case NPC_INPUT_ITEM_INDEX: - case NPC_INPUT_ITEM_CRAFT: mInventory->clear(); break; + case NPC_INPUT_ITEM_CRAFT: + mComplexInventory->clear(); + break; case NPC_INPUT_STRING: case NPC_INPUT_INTEGER: case NPC_INPUT_LIST: @@ -611,8 +599,19 @@ void NpcDialog::action(const ActionEvent &event) { Item *const item = inventoryWindow->getSelectedItem(); Inventory *const inventory = PlayerInfo::getInventory(); - if (mInventory->addVirtualItem(item, 0) && inventory) - inventory->virtualRemove(item, 1); + if (inventory) + { + if (mInputState == NPC_INPUT_ITEM_CRAFT) + { + if (mComplexInventory->addVirtualItem(item, 0, 1)) + inventory->virtualRemove(item, 1); + } + else + { + if (mInventory->addVirtualItem(item, 0, 1)) + inventory->virtualRemove(item, 1); + } + } } } else if (eventId.find("skin_") == 0) @@ -792,7 +791,7 @@ void NpcDialog::itemCraftRequest(const int size) { mActionState = NPC_ACTION_INPUT; mInputState = NPC_INPUT_ITEM_CRAFT; - mInventory->resize(size); + mComplexInventory->resize(size); buildLayout(); } @@ -988,6 +987,11 @@ void NpcDialog::placeItemInputControls() mItemContainer->setMaxColumns(10000); } + if (mInputState == NPC_INPUT_ITEM_CRAFT) + mItemContainer->setInventory(mComplexInventory); + else + mItemContainer->setInventory(mInventory); + if (mDialogInfo && mDialogInfo->hideText) { if (mShowAvatar) @@ -1334,3 +1338,35 @@ void NpcDialog::restoreVirtuals() if (inventory) inventory->restoreVirtuals(); } + +std::string NpcDialog::complexItemToStr(const ComplexItem *const item) +{ + std::string str; + if (item) + { + const std::vector<Item*> &items = item->getChilds(); + const int sz = items.size(); + logger->log("complexItemToStr size=%d", sz); + if (!sz) + return str; + + const Item *item2 = items[0]; + + str = strprintf("%d,%d", + item2->getInvIndex(), + item2->getQuantity()); + for (int f = 1; f < sz; f ++) + { + str.append(";"); + item2 = items[f]; + str.append(strprintf("%d,%d", + item2->getInvIndex(), + item2->getQuantity())); + } + } + else + { + str = ""; + } + return str; +} diff --git a/src/gui/windows/npcdialog.h b/src/gui/windows/npcdialog.h index 7868fd7d1..db9310f29 100644 --- a/src/gui/windows/npcdialog.h +++ b/src/gui/windows/npcdialog.h @@ -36,6 +36,7 @@ class Being; class Button; class BrowserBox; class ComplexInventory; +class ComplexItem; class Container; class ExtendedListBox; class ItemLinkHandler; @@ -253,6 +254,8 @@ class NpcDialog final : public Window, void restoreVirtuals(); + std::string complexItemToStr(const ComplexItem *const item); + BeingId mNpcId; int mDefaultInt; diff --git a/src/resources/inventory/complexinventory.cpp b/src/resources/inventory/complexinventory.cpp index 15d40f499..06a8cf5c0 100644 --- a/src/resources/inventory/complexinventory.cpp +++ b/src/resources/inventory/complexinventory.cpp @@ -22,6 +22,12 @@ #include "resources/inventory/complexinventory.h" +#include "logger.h" + +#include "being/playerinfo.h" + +#include "resources/item/complexitem.h" + #include "debug.h" ComplexInventory::ComplexInventory(const InventoryTypeT type, @@ -33,3 +39,129 @@ ComplexInventory::ComplexInventory(const InventoryTypeT type, ComplexInventory::~ComplexInventory() { } + +bool ComplexInventory::addVirtualItem(const Item *const item, + int index, + const int amount) +{ + if (!item || PlayerInfo::isItemProtected(item->getId())) + return false; + + if (index >= 0 && index < static_cast<int>(mSize)) + { + ComplexItem *citem = nullptr; + if (mItems[index] != nullptr) + { + Item *const item2 = mItems[index]; + if (item->getId() != item2->getId() || + item->getColor() != item2->getColor()) + { // not same id or color + return false; + } + citem = dynamic_cast<ComplexItem*>(mItems[index]); + if (!citem) + { // if in inventory not complex item, converting it to complex + citem = new ComplexItem(item2->getId(), + item2->getType(), + item2->getQuantity(), + item2->getRefine(), + item2->getColor(), + item2->getIdentified(), + item2->getDamaged(), + item2->getFavorite(), + Equipm_false, + Equipped_false); + citem->setTag(item2->getTag()); + delete mItems[index]; + mItems[index] = citem; + } + } + else + { + citem = new ComplexItem(item->getId(), + item->getType(), + 0, + item->getRefine(), + item->getColor(), + item->getIdentified(), + item->getDamaged(), + item->getFavorite(), + Equipm_false, + Equipped_false); + mItems[index] = citem; + } + citem->addChild(item, amount); + } + else + { + index = addItem(item->getId(), + item->getType(), + 1, + 1, + item->getColor(), + item->getIdentified(), + item->getDamaged(), + item->getFavorite(), + Equipm_false, + Equipped_false); + } + if (index == -1) + return false; + + Item *const item2 = getItem(index); + if (item2) + item2->setTag(item->getInvIndex()); + return true; +} + +void ComplexInventory::setItem(const int index, + const int id, + const int type, + const int quantity, + const uint8_t refine, + const ItemColor color, + const Identified identified, + const Damaged damaged, + const Favorite favorite, + const Equipm equipment, + const Equipped equipped) +{ + if (index < 0 || index >= static_cast<int>(mSize)) + { + logger->log("Warning: invalid inventory index: %d", index); + return; + } + + Item *const item1 = mItems[index]; + if (!item1 && id > 0) + { + ComplexItem *const item = new ComplexItem(id, + type, + quantity, + refine, + color, + identified, + damaged, + favorite, + equipment, + equipped); + item->setInvIndex(index); + + Item *const item2 = new Item(id, + type, + quantity, + refine, + color, + identified, + damaged, + favorite, + equipment, + equipped); + item2->setInvIndex(index); + item->addChild(item2, quantity); + + mItems[index] = item; + mUsed ++; + distributeSlotsChangedEvent(); + } +} diff --git a/src/resources/inventory/complexinventory.h b/src/resources/inventory/complexinventory.h index 520779c28..95183cb7f 100644 --- a/src/resources/inventory/complexinventory.h +++ b/src/resources/inventory/complexinventory.h @@ -44,6 +44,22 @@ class ComplexInventory final : public Inventory * Destructor. */ ~ComplexInventory(); + + bool addVirtualItem(const Item *const item, + int index, + const int amount) override final; + + void setItem(const int index, + const int id, + const int type, + const int quantity, + const uint8_t refine, + const ItemColor color, + const Identified identified, + const Damaged damaged, + const Favorite favorite, + const Equipm equipment, + const Equipped equipped) override final; }; #endif // RESOURCES_INVENTORY_COMPLEXINVENTORY_H diff --git a/src/resources/inventory/inventory.cpp b/src/resources/inventory/inventory.cpp index 180a1b4f3..c73d9d8b6 100644 --- a/src/resources/inventory/inventory.cpp +++ b/src/resources/inventory/inventory.cpp @@ -372,7 +372,8 @@ int Inventory::findIndexByTag(const int tag) const } bool Inventory::addVirtualItem(const Item *const item, - int index) + int index, + const int amount) { if (item && !PlayerInfo::isItemProtected(item->getId())) { @@ -383,7 +384,7 @@ bool Inventory::addVirtualItem(const Item *const item, setItem(index, item->getId(), item->getType(), - 1, + amount, 1, item->getColor(), item->getIdentified(), @@ -396,7 +397,7 @@ bool Inventory::addVirtualItem(const Item *const item, { index = addItem(item->getId(), item->getType(), - 1, + amount, 1, item->getColor(), item->getIdentified(), @@ -465,3 +466,25 @@ void Inventory::virtualRestore(const Item *const item, mItems[index]->mQuantity += amount; } } + +void Inventory::moveItem(const int index1, + const int index2) +{ + if (index1 < 0 || + index1 >= static_cast<int>(mSize) || + index2 < 0 || + index2 >= static_cast<int>(mSize)) + { + return; + } + + Item *const item1 = mItems[index1]; + Item *const item2 = mItems[index2]; + if (item1) + item1->setInvIndex(index2); + if (item2) + item2->setInvIndex(index1); + mItems[index1] = item2; + mItems[index2] = item1; + distributeSlotsChangedEvent(); +} diff --git a/src/resources/inventory/inventory.h b/src/resources/inventory/inventory.h index 1e1a97dd4..9186fbd73 100644 --- a/src/resources/inventory/inventory.h +++ b/src/resources/inventory/inventory.h @@ -63,7 +63,7 @@ class Inventory notfinal /** * Destructor. */ - ~Inventory(); + virtual ~Inventory(); /** * Returns the size that this instance is configured for. @@ -103,22 +103,25 @@ class Inventory notfinal /** * Sets the item at the given position. */ - void setItem(const int index, - const int id, - const int type, - const int quantity, - const uint8_t refine, - const ItemColor color, - const Identified identified, - const Damaged damaged, - const Favorite favorite, - const Equipm equipment, - const Equipped equipped); + virtual void setItem(const int index, + const int id, + const int type, + const int quantity, + const uint8_t refine, + const ItemColor color, + const Identified identified, + const Damaged damaged, + const Favorite favorite, + const Equipm equipment, + const Equipped equipped); void setCards(const int index, const int *const cards, const int size) const; + void moveItem(const int index1, + const int index2); + /** * Remove a item from the inventory. */ @@ -176,8 +179,9 @@ class Inventory notfinal int findIndexByTag(const int tag) const; - bool addVirtualItem(const Item *const item, - int index); + virtual bool addVirtualItem(const Item *const item, + int index, + const int amount); void virtualRemove(Item *const item, const int amount); |