summaryrefslogtreecommitdiff
path: root/src/gui/itemcontainer.cpp
diff options
context:
space:
mode:
authorBjörn Steinbrink <B.Steinbrink@gmx.de>2005-07-18 00:27:11 +0000
committerBjörn Steinbrink <B.Steinbrink@gmx.de>2005-07-18 00:27:11 +0000
commit290aab364a310b18bb02567f6e4e424d94148bf9 (patch)
tree13a860174c635db34e6087aa31c001386cbd6a7c /src/gui/itemcontainer.cpp
parent19b58f22f7e77382183c70d2b6a1c79b5e1584e7 (diff)
downloadmana-290aab364a310b18bb02567f6e4e424d94148bf9.tar.gz
mana-290aab364a310b18bb02567f6e4e424d94148bf9.tar.bz2
mana-290aab364a310b18bb02567f6e4e424d94148bf9.tar.xz
mana-290aab364a310b18bb02567f6e4e424d94148bf9.zip
Created Inventory class. (Really) Small code simplifications and cleanups.
Diffstat (limited to 'src/gui/itemcontainer.cpp')
-rw-r--r--src/gui/itemcontainer.cpp143
1 files changed, 39 insertions, 104 deletions
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;
-}