diff options
author | Björn Steinbrink <B.Steinbrink@gmx.de> | 2005-07-18 00:27:11 +0000 |
---|---|---|
committer | Björn Steinbrink <B.Steinbrink@gmx.de> | 2005-07-18 00:27:11 +0000 |
commit | 290aab364a310b18bb02567f6e4e424d94148bf9 (patch) | |
tree | 13a860174c635db34e6087aa31c001386cbd6a7c /src/gui | |
parent | 19b58f22f7e77382183c70d2b6a1c79b5e1584e7 (diff) | |
download | mana-client-290aab364a310b18bb02567f6e4e424d94148bf9.tar.gz mana-client-290aab364a310b18bb02567f6e4e424d94148bf9.tar.bz2 mana-client-290aab364a310b18bb02567f6e4e424d94148bf9.tar.xz mana-client-290aab364a310b18bb02567f6e4e424d94148bf9.zip |
Created Inventory class. (Really) Small code simplifications and cleanups.
Diffstat (limited to 'src/gui')
-rw-r--r-- | src/gui/inventorywindow.cpp (renamed from src/gui/inventory.cpp) | 60 | ||||
-rw-r--r-- | src/gui/inventorywindow.h (renamed from src/gui/inventory.h) | 21 | ||||
-rw-r--r-- | src/gui/item_amount.cpp | 8 | ||||
-rw-r--r-- | src/gui/itemcontainer.cpp | 143 | ||||
-rw-r--r-- | src/gui/itemcontainer.h | 46 | ||||
-rw-r--r-- | src/gui/sell.cpp | 4 | ||||
-rw-r--r-- | src/gui/status.cpp | 2 | ||||
-rw-r--r-- | src/gui/trade.cpp | 60 | ||||
-rw-r--r-- | src/gui/trade.h | 8 |
9 files changed, 100 insertions, 252 deletions
diff --git a/src/gui/inventory.cpp b/src/gui/inventorywindow.cpp index 98f17134..06eb8ce0 100644 --- a/src/gui/inventory.cpp +++ b/src/gui/inventorywindow.cpp @@ -21,12 +21,11 @@ * $Id$ */ +#include "inventorywindow.h" #include "../playerinfo.h" -#include "inventory.h" -#include "../equipment.h" +#include "../inventory.h" #include "button.h" #include "scrollarea.h" -#include "../net/network.h" #include "item_amount.h" #include <string> @@ -37,7 +36,7 @@ InventoryWindow::InventoryWindow(): useButton = new Button("Use"); dropButton = new Button("Drop"); - items = new ItemContainer(); + items = new ItemContainer(inventory); invenScroll = new ScrollArea(items); invenScroll->setPosition(8, 8); invenScroll->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER); @@ -96,48 +95,6 @@ void InventoryWindow::logic() weightLabel->adjustSize(); } -int InventoryWindow::useItem(Item *item) -{ - WFIFOW(0) = net_w_value(0x00a7); - WFIFOW(2) = net_w_value(item->getInvIndex()); - WFIFOL(4) = net_l_value(item->getId()); - // Note: id is dest of item, usually player_node->account_ID ?? - WFIFOSET(8); - while ((out_size > 0)) flush(); - return 0; -} - -int InventoryWindow::dropItem(Item *item, int quantity) -{ - // TODO: Fix wrong coordinates of drops, serverside? - WFIFOW(0) = net_w_value(0x00a2); - WFIFOW(2) = net_w_value(item->getInvIndex()); - WFIFOW(4) = net_w_value(quantity); - WFIFOSET(6); - while ((out_size > 0)) flush(); - return 0; -} - -void InventoryWindow::equipItem(Item *item) -{ - WFIFOW(0) = net_w_value(0x00a9); - WFIFOW(2) = net_w_value(item->getInvIndex()); - WFIFOW(4) = net_w_value(0); - WFIFOSET(6); - while ((out_size > 0)) flush(); -} - -void InventoryWindow::unequipItem(Item *item) -{ - WFIFOW(0) = net_w_value(0x00ab); - WFIFOW(2) = net_w_value(item->getInvIndex()); - WFIFOSET(4); - while ((out_size > 0)) flush(); - - // Tidy equipment directly to avoid weapon still shown bug, by instance - Equipment::getInstance()->removeEquipment(item); -} - void InventoryWindow::action(const std::string &eventId) { Item *item = items->getItem(); @@ -149,14 +106,14 @@ void InventoryWindow::action(const std::string &eventId) if (eventId == "use") { if (item->isEquipment()) { if (item->isEquipped()) { - unequipItem(item); + inventory->unequipItem(item); } else { - equipItem(item); + inventory->equipItem(item); } } else { - useItem(item); + inventory->useItem(item); } } else if (eventId == "drop") @@ -237,3 +194,8 @@ void InventoryWindow::updateButtons() useButton->setEnabled(!!item); dropButton->setEnabled(!!item); } + +Item* InventoryWindow::getItem() +{ + return items->getItem(); +} diff --git a/src/gui/inventory.h b/src/gui/inventorywindow.h index e9924ead..cb7f08f5 100644 --- a/src/gui/inventory.h +++ b/src/gui/inventorywindow.h @@ -21,8 +21,8 @@ * $Id$ */ -#ifndef _TMW_INVENTORY_H -#define _TMW_INVENTORY_H +#ifndef _TMW_INVENTORYWINDOW_H +#define _TMW_INVENTORYWINDOW_H #include "itemcontainer.h" #include "window.h" @@ -51,33 +51,22 @@ class InventoryWindow : public Window, gcn::ActionListener void logic(); /** - * Equips an item. - */ - void equipItem(Item *item); - - /** - * Unequips an item. - */ - void unequipItem(Item *item); - - /** * Called when receiving actions from the widgets. */ void action(const std::string& eventId); - int dropItem(Item *item, int quantity); - void mouseClick(int x, int y, int button, int count); void mouseMotion(int mx, int my); - ItemContainer *items; + Item* getItem(); private: - int useItem(Item *item); void updateWidgets(); /** Updates widgets size/position */ void updateButtons(); /** Updates button states */ + ItemContainer *items; + gcn::Button *useButton, *dropButton; gcn::ScrollArea *invenScroll; gcn::Label *itemNameLabel; diff --git a/src/gui/item_amount.cpp b/src/gui/item_amount.cpp index 021da3eb..eb3a4c5c 100644 --- a/src/gui/item_amount.cpp +++ b/src/gui/item_amount.cpp @@ -22,7 +22,7 @@ */ #include "item_amount.h" -#include "inventory.h" +#include "inventorywindow.h" #include "trade.h" #include "button.h" @@ -38,7 +38,7 @@ ItemAmountWindow::ItemAmountWindow(int usage, Window *parent): itemAmountOkButton = new Button("Okay"); itemAmountCancelButton = new Button("Cancel"); - itemAmountTextBox->setRange(1, inventoryWindow->items->getItem()->getQuantity()); + itemAmountTextBox->setRange(1, inventoryWindow->getItem()->getQuantity()); // Set button events Id itemAmountMinusButton->setEventId("Minus"); @@ -108,12 +108,12 @@ void ItemAmountWindow::action(const std::string& eventId) } else if (eventId == "Drop") { - inventoryWindow->dropItem(inventoryWindow->items->getItem(), itemAmountTextBox->getInt()); + inventory->dropItem(inventoryWindow->getItem(), itemAmountTextBox->getInt()); scheduleDelete(); } else if (eventId == "AddTrade") { - tradeWindow->tradeItem(inventoryWindow->items->getItem(), itemAmountTextBox->getInt()); + tradeWindow->tradeItem(inventoryWindow->getItem(), itemAmountTextBox->getInt()); scheduleDelete(); } else if (eventId == "Plus") diff --git a/src/gui/itemcontainer.cpp b/src/gui/itemcontainer.cpp index 51780fca..bda9f786 100644 --- a/src/gui/itemcontainer.cpp +++ b/src/gui/itemcontainer.cpp @@ -28,7 +28,8 @@ #include "../resources/resourcemanager.h" #include <sstream> -ItemContainer::ItemContainer() +ItemContainer::ItemContainer(Inventory *inventory): + inventory(inventory) { ResourceManager *resman = ResourceManager::getInstance(); Image *itemImg = resman->getImage("graphics/sprites/items.png"); @@ -41,10 +42,6 @@ ItemContainer::ItemContainer() selectedItem = 0; // No item selected - for (int i = 0; i < INVENTORY_SIZE; i++) { - items[i].setInvIndex(i); - } - addMouseListener(this); } @@ -82,41 +79,44 @@ void ItemContainer::draw(gcn::Graphics* graphics) */ for (int i = 2; i < INVENTORY_SIZE; i++) { - if (items[i].getQuantity() > 0) + Item *item = inventory->getItem(i); + + if (item->getQuantity() <= 0) { + continue; + } + + int itemX = ((i - 2) % columns) * gridWidth; + int itemY = ((i - 2) / columns) * gridHeight; + + // Draw selection image below selected item + if (selectedItem == item) { - int itemX = ((i - 2) % columns) * gridWidth; - int itemY = ((i - 2) / columns) * gridHeight; - - // Draw selection image below selected item - if (selectedItem == &items[i]) - { - dynamic_cast<Graphics*>(graphics)->drawImage( - selImg, x + itemX, y + itemY); - } - - // Draw item icon - int idx; - if ((idx = items[i].getInfo()->getImage()) > 0) - { - dynamic_cast<Graphics*>(graphics)->drawImage( - itemset->spriteset[idx - 1], x + itemX, y + itemY); - } - - // Draw item caption - std::stringstream ss; - - if (!items[i].isEquipped()) { - ss << items[i].getQuantity(); - } - else { - ss << "Eq."; - } - - graphics->drawText(ss.str(), - itemX + gridWidth / 2, - itemY + gridHeight - 11, - gcn::Graphics::CENTER); + dynamic_cast<Graphics*>(graphics)->drawImage( + selImg, x + itemX, y + itemY); + } + + // Draw item icon + int idx; + if ((idx = item->getInfo()->getImage()) > 0) + { + dynamic_cast<Graphics*>(graphics)->drawImage( + itemset->spriteset[idx - 1], x + itemX, y + itemY); + } + + // Draw item caption + std::stringstream ss; + + if (!item->isEquipped()) { + ss << item->getQuantity(); } + else { + ss << "Eq."; + } + + graphics->drawText(ss.str(), + itemX + gridWidth / 2, + itemY + gridHeight - 11, + gcn::Graphics::CENTER); } } @@ -137,67 +137,16 @@ void ItemContainer::setWidth(int width) (INVENTORY_SIZE % columns > 0 ? 1 : 0)) * gridHeight); } -int ItemContainer::getIndex(int id) -{ - for (int i = 0; i < INVENTORY_SIZE; i++) { - if (items[i].getId() == id) { - return i; - } - } - return -1; -} - Item* ItemContainer::getItem() { return selectedItem; } -Item* ItemContainer::getItem(int index) -{ - return &items[index]; -} - -void ItemContainer::addItem(int index, int id, int quantity, bool equipment) -{ - items[index].setId(id); - items[index].increaseQuantity(quantity); - items[index].setEquipment(equipment); -} - -int ItemContainer::getFreeSlot() -{ - for (int i = 2; i < INVENTORY_SIZE; i++) { - if (items[i].getId() == -1) { - return i; - } - } - return -1; -} - -void ItemContainer::resetItems() -{ - for (int i = 0; i < INVENTORY_SIZE; i++) { - items[i].setId(-1); - items[i].setQuantity(0); - items[i].setEquipped(false); - } -} - void ItemContainer::selectNone() { selectedItem = 0; } -void ItemContainer::removeItem(int id) -{ - for (int i = 0; i < INVENTORY_SIZE; i++) { - if (items[i].getId() == id) { - items[i].setId(-1); - items[i].setQuantity(0); - } - } -} - void ItemContainer::mousePress(int mx, int my, int button) { int gridWidth = itemset->spriteset[0]->getWidth() + 4; @@ -212,20 +161,6 @@ void ItemContainer::mousePress(int mx, int my, int button) if (index > INVENTORY_SIZE) { index = INVENTORY_SIZE - 1; } - selectedItem = &items[index]; + selectedItem = inventory->getItem(index); } } - -int ItemContainer::getNumberOfSlotsUsed() -{ - int NumberOfFilledSlot = 0; - for (int i = 0; i < INVENTORY_SIZE; i++) - { - if (items[i].getId() > -1 || items[i].getQuantity() > 0) - { - NumberOfFilledSlot++; - } - } - - return NumberOfFilledSlot; -} diff --git a/src/gui/itemcontainer.h b/src/gui/itemcontainer.h index 6e72eba0..6def991a 100644 --- a/src/gui/itemcontainer.h +++ b/src/gui/itemcontainer.h @@ -28,13 +28,10 @@ #include <guichan.hpp> #include "../item.h" - -#include "../item.h" +#include "../inventory.h" #include "../resources/image.h" #include "../graphic/spriteset.h" -#define INVENTORY_SIZE 100 - /** * An item container. Used to show items in inventory and trade dialog. * @@ -46,7 +43,7 @@ class ItemContainer : public gcn::Widget, public gcn::MouseListener /** * Constructor. Initializes the graphic. */ - ItemContainer(); + ItemContainer(Inventory *inventory); /** * Destructor. @@ -70,57 +67,20 @@ class ItemContainer : public gcn::Widget, public gcn::MouseListener void mousePress(int mx, int my, int button); /** - * Finds the index of an item. - */ - int getIndex(int id); - - /** * Returns the selected item. */ Item* getItem(); /** - * Returns the item at the specified index. - */ - Item* getItem(int index); - - /** - * Returns id of next free slot or -1 if all occupied. - */ - int getFreeSlot(); - - /** - * Adds a new item. - */ - void addItem(int index, int id, int quantity, bool equipment); - - /** * Set selected item to -1. */ void selectNone(); - /** - * Reset all item slots. - */ - void resetItems(); - - /** - * Remove a item from the inventory. - */ - void removeItem(int id); - - /** - * Get the number of slots filled with an item - */ - int getNumberOfSlotsUsed(); - private: + Inventory *inventory; Spriteset *itemset; Image *selImg; Item *selectedItem; - int itemNumber; - Item items[INVENTORY_SIZE]; /**< The holder of items */ - }; #endif diff --git a/src/gui/sell.cpp b/src/gui/sell.cpp index 5ce56438..26487b9a 100644 --- a/src/gui/sell.cpp +++ b/src/gui/sell.cpp @@ -22,12 +22,12 @@ */ #include "sell.h" -#include "inventory.h" #include "button.h" #include "slider.h" #include "scrollarea.h" #include "listbox.h" #include "../game.h" +#include "../inventory.h" #include "../net/network.h" #include <sstream> @@ -90,7 +90,7 @@ void SellDialog::reset() void SellDialog::addItem(short index, int price) { - Item *item = inventoryWindow->items->getItem(index); + Item *item = inventory->getItem(index); if (!item) return; diff --git a/src/gui/status.cpp b/src/gui/status.cpp index 4cb4f010..e507c395 100644 --- a/src/gui/status.cpp +++ b/src/gui/status.cpp @@ -23,7 +23,7 @@ #include "status.h" #include "stats.h" -#include "inventory.h" +#include "inventorywindow.h" #include "setup.h" #include "equipmentwindow.h" #include "button.h" diff --git a/src/gui/trade.cpp b/src/gui/trade.cpp index ddf9bc0f..87328f0c 100644 --- a/src/gui/trade.cpp +++ b/src/gui/trade.cpp @@ -22,14 +22,14 @@ */ #include "trade.h" -#include "equipment.h" -#include "inventory.h" #include "chat.h" +#include "inventorywindow.h" #include "item_amount.h" #include "button.h" #include "scrollarea.h" #include "textfield.h" #include "../net/network.h" +#include "../equipment.h" #include <sstream> TradeWindow::TradeWindow(): @@ -42,16 +42,16 @@ TradeWindow::TradeWindow(): cancelButton = new Button("Cancel"); tradeButton = new Button("Trade"); - myItems = new ItemContainer(); - myItems->setPosition(2, 2); + myItemContainer = new ItemContainer(&myInventory); + myItemContainer->setPosition(2, 2); - myScroll = new ScrollArea(myItems); + myScroll = new ScrollArea(myItemContainer); myScroll->setPosition(8, 8); - partnerItems = new ItemContainer(); - partnerItems->setPosition(2, 58); + partnerItemContainer = new ItemContainer(&partnerInventory); + partnerItemContainer->setPosition(2, 58); - partnerScroll = new ScrollArea(partnerItems); + partnerScroll = new ScrollArea(partnerItemContainer); partnerScroll->setPosition(8, 64); moneyLabel = new gcn::Label("You get: 0z"); @@ -101,11 +101,11 @@ TradeWindow::TradeWindow(): addButton->setPosition(okButton->getX() - 32, getHeight() - 24); - myItems->setSize(getWidth() - 24 - 12 - 1, + myItemContainer->setSize(getWidth() - 24 - 12 - 1, (INVENTORY_SIZE * 24) / (getWidth() / 24) - 1); myScroll->setSize(getWidth() - 16, (getHeight() - 76) / 2); - partnerItems->setSize(getWidth() - 24 - 12 - 1, + partnerItemContainer->setSize(getWidth() - 24 - 12 - 1, (INVENTORY_SIZE * 24) / (getWidth() / 24) - 1); partnerScroll->setSize(getWidth() - 16, (getHeight() - 76) / 2); @@ -123,9 +123,9 @@ TradeWindow::~TradeWindow() delete okButton; delete cancelButton; delete tradeButton; - delete myItems; + delete myItemContainer; delete myScroll; - delete partnerItems; + delete partnerItemContainer; delete partnerScroll; delete itemNameLabel; delete itemDescriptionLabel; @@ -141,47 +141,47 @@ void TradeWindow::addMoney(int amount) moneyLabel->adjustSize(); } -void TradeWindow::addItem(int index, int id, bool own, int quantity, +void TradeWindow::addItem(int id, bool own, int quantity, bool equipment) { if (own) { - myItems->addItem(index, id, quantity, equipment); + myInventory.addItem(id, quantity, equipment); } else { - partnerItems->addItem(index, id, quantity, equipment); + partnerInventory.addItem(id, quantity, equipment); } } void TradeWindow::removeItem(int id, bool own) { if (own) { - myItems->removeItem(id); + myInventory.removeItem(id); } else { - partnerItems->removeItem(id); + partnerInventory.removeItem(id); } } void TradeWindow::changeQuantity(int index, bool own, int quantity) { if (own) { - myItems->getItem(index)->setQuantity(quantity); + myInventory.getItem(index)->setQuantity(quantity); } else { - partnerItems->getItem(index)->setQuantity(quantity); + partnerInventory.getItem(index)->setQuantity(quantity); } } void TradeWindow::increaseQuantity(int index, bool own, int quantity) { if (own) { - myItems->getItem(index)->increaseQuantity(quantity); + myInventory.getItem(index)->increaseQuantity(quantity); } else { - partnerItems->getItem(index)->increaseQuantity(quantity); + partnerInventory.getItem(index)->increaseQuantity(quantity); } } void TradeWindow::reset() { - myItems->resetItems(); - partnerItems->resetItems(); + myInventory.resetItems(); + partnerInventory.resetItems(); tradeButton->setEnabled(false); okButton->setEnabled(true); ok_other = false; @@ -239,18 +239,18 @@ void TradeWindow::mouseClick(int x, int y, int button, int count) && x <= myScroll->getX() + myScroll->getWidth() - 10 && y >= myScroll->getY() + 16 && y <= myScroll->getY() + myScroll->getHeight() + 15 - && (item = myItems->getItem())) + && (item = myItemContainer->getItem())) { - partnerItems->selectNone(); + partnerItemContainer->selectNone(); // partnerItems selected } else if (x >= partnerScroll->getX() + 3 && x <= partnerScroll->getX() + partnerScroll->getWidth() - 20 && y >= partnerScroll->getY() + 16 && y <= partnerScroll->getY() + partnerScroll->getHeight() + 15 - && (item = partnerItems->getItem())) + && (item = partnerItemContainer->getItem())) { - myItems->selectNone(); + myItemContainer->selectNone(); } else { return; } @@ -267,18 +267,18 @@ void TradeWindow::mouseClick(int x, int y, int button, int count) void TradeWindow::action(const std::string &eventId) { - Item *item = inventoryWindow->items->getItem(); + Item *item = inventoryWindow->getItem(); if (eventId == "add") { if (!item) { return; } - if (tradeWindow->myItems->getFreeSlot() < 1) { + if (myInventory.getFreeSlot() < 1) { return; } - if (myItems->getIndex(item->getId()) != -1) { + if (myInventory.contains(item)) { chatWindow->chat_log("Failed adding item. You can not " "overlap one kind of item on the window.", BY_SERVER); return; diff --git a/src/gui/trade.h b/src/gui/trade.h index 6132a182..b767d47b 100644 --- a/src/gui/trade.h +++ b/src/gui/trade.h @@ -54,7 +54,7 @@ class TradeWindow : public Window, gcn::ActionListener /** * Add an item to the trade window. */ - void addItem(int index, int id, bool own, int quantity, bool equipment); + void addItem(int id, bool own, int quantity, bool equipment); /** * Remove a item from the trade window. @@ -101,8 +101,10 @@ class TradeWindow : public Window, gcn::ActionListener */ void action(const std::string& eventId); - ItemContainer *myItems; - ItemContainer *partnerItems; + Inventory myInventory; + Inventory partnerInventory; + ItemContainer *myItemContainer; + ItemContainer *partnerItemContainer; private: gcn::Label *itemNameLabel; |