summaryrefslogtreecommitdiff
path: root/src/gui
diff options
context:
space:
mode:
Diffstat (limited to 'src/gui')
-rw-r--r--src/gui/buy.cpp15
-rw-r--r--src/gui/equipmentwindow.cpp10
-rw-r--r--src/gui/inventorywindow.cpp27
-rw-r--r--src/gui/inventorywindow.h5
-rw-r--r--src/gui/itemcontainer.cpp11
-rw-r--r--src/gui/itemcontainer.h2
-rw-r--r--src/gui/itemshortcutcontainer.cpp13
-rw-r--r--src/gui/sell.cpp29
-rw-r--r--src/gui/sell.h2
-rw-r--r--src/gui/shop.cpp37
-rw-r--r--src/gui/shop.h29
-rw-r--r--src/gui/shoplistbox.cpp4
-rw-r--r--src/gui/trade.cpp12
13 files changed, 88 insertions, 108 deletions
diff --git a/src/gui/buy.cpp b/src/gui/buy.cpp
index 17e5dba7..259fcfd9 100644
--- a/src/gui/buy.cpp
+++ b/src/gui/buy.cpp
@@ -33,8 +33,6 @@
#include "../npc.h"
-#include "../resources/itemdb.h"
-
#include "../net/messageout.h"
#include "../net/protocol.h"
@@ -170,7 +168,7 @@ void BuyDialog::action(const gcn::ActionEvent &event)
outMsg.writeInt16(CMSG_NPC_BUY_REQUEST);
outMsg.writeInt16(8);
outMsg.writeInt16(mAmountItems);
- outMsg.writeInt16(mShopItems->at(selectedItem).id);
+ outMsg.writeInt16(mShopItems->at(selectedItem)->getId());
// Reset selection
mAmountItems = 1;
@@ -179,7 +177,8 @@ void BuyDialog::action(const gcn::ActionEvent &event)
// Update money and adjust the max number of items that can be bought
mMaxItems -= mAmountItems;
- setMoney(mMoney - mAmountItems * mShopItems->at(selectedItem).price);
+ setMoney(mMoney -
+ mAmountItems * mShopItems->at(selectedItem)->getPrice());
}
}
@@ -246,20 +245,22 @@ BuyDialog::updateButtonsAndLabels()
if (selectedItem > -1)
{
- const ItemInfo &info = ItemDB::get(mShopItems->at(selectedItem).id);
+ const ItemInfo &info = mShopItems->at(selectedItem)->getInfo();
mItemDescLabel->setCaption("Description: " + info.getDescription());
mItemEffectLabel->setCaption("Effect: " + info.getEffect());
+ int itemPrice = mShopItems->at(selectedItem)->getPrice();
+
// Calculate how many the player can afford
- mMaxItems = mMoney / mShopItems->at(selectedItem).price;
+ mMaxItems = mMoney / itemPrice;
if (mAmountItems > mMaxItems)
{
mAmountItems = mMaxItems;
}
// Calculate price of pending purchase
- price = mAmountItems * mShopItems->at(selectedItem).price;
+ price = mAmountItems * itemPrice;
}
else
{
diff --git a/src/gui/equipmentwindow.cpp b/src/gui/equipmentwindow.cpp
index 4df18b19..93b9ea37 100644
--- a/src/gui/equipmentwindow.cpp
+++ b/src/gui/equipmentwindow.cpp
@@ -61,22 +61,20 @@ void EquipmentWindow::draw(gcn::Graphics *graphics)
graphics->drawRectangle(gcn::Rectangle(10 + 36 * (i % 4),
36 * (i / 4) + 25, 32, 32));
- if (!(item = mEquipment->getEquipment(i))) {
+ if (!(item = mEquipment->getEquipment(i)))
continue;
- }
- image = item->getInfo().getImage();
+ image = item->getImage();
static_cast<Graphics*>(graphics)->drawImage(
image, 36 * (i % 4) + 10, 36 * (i / 4) + 25);
}
graphics->drawRectangle(gcn::Rectangle(160, 25, 32, 32));
- if (!(item = mEquipment->getArrows())) {
+ if (!(item = mEquipment->getArrows()))
return;
- }
- image = item->getInfo().getImage();
+ image = item->getImage();
static_cast<Graphics*>(graphics)->drawImage(image, 160, 25);
graphics->drawText(toString(item->getQuantity()), 170, 62,
diff --git a/src/gui/inventorywindow.cpp b/src/gui/inventorywindow.cpp
index 4172a532..fcc602f9 100644
--- a/src/gui/inventorywindow.cpp
+++ b/src/gui/inventorywindow.cpp
@@ -57,7 +57,7 @@ InventoryWindow::InventoryWindow():
mUseButton = new Button("Use", "use", this);
mDropButton = new Button("Drop", "drop", this);
- mItems = new ItemContainer(player_node->mInventory);
+ mItems = new ItemContainer(player_node->getInventory());
mItems->addSelectionListener(this);
mInvenScroll = new ScrollArea(mItems);
@@ -100,11 +100,10 @@ void InventoryWindow::logic()
void InventoryWindow::action(const gcn::ActionEvent &event)
{
- Item *item = mItems->getItem();
+ Item *item = mItems->getSelectedItem();
- if (!item) {
+ if (!item)
return;
- }
if (event.getId() == "use") {
if (item->isEquipment()) {
@@ -133,7 +132,7 @@ void InventoryWindow::action(const gcn::ActionEvent &event)
void InventoryWindow::valueChanged(const gcn::SelectionEvent &event)
{
- Item *item = mItems->getItem();
+ const Item *item = mItems->getSelectedItem();
// Update name, effect and description
if (!item)
@@ -161,7 +160,7 @@ void InventoryWindow::mouseClicked(gcn::MouseEvent &event)
if (event.getButton() == gcn::MouseEvent::RIGHT)
{
- Item *item = mItems->getItem();
+ Item *item = mItems->getSelectedItem();
if (!item) return;
@@ -208,11 +207,11 @@ void InventoryWindow::widgetResized(const gcn::Event &event)
void InventoryWindow::updateButtons()
{
- Item *item;
+ const Item *selectedItem = mItems->getSelectedItem();
- if ((item = mItems->getItem()) && item->isEquipment())
+ if (selectedItem && selectedItem->isEquipment())
{
- if (item->isEquipped()) {
+ if (selectedItem->isEquipped()) {
mUseButton->setCaption("Unequip");
}
else {
@@ -220,14 +219,14 @@ void InventoryWindow::updateButtons()
}
}
else {
- mUseButton ->setCaption("Use");
+ mUseButton->setCaption("Use");
}
- mUseButton->setEnabled(!!item);
- mDropButton->setEnabled(!!item);
+ mUseButton->setEnabled(selectedItem != 0);
+ mDropButton->setEnabled(selectedItem != 0);
}
-Item* InventoryWindow::getItem()
+Item* InventoryWindow::getSelectedItem() const
{
- return mItems->getItem();
+ return mItems->getSelectedItem();
}
diff --git a/src/gui/inventorywindow.h b/src/gui/inventorywindow.h
index 3222009f..d45602d2 100644
--- a/src/gui/inventorywindow.h
+++ b/src/gui/inventorywindow.h
@@ -58,7 +58,10 @@ class InventoryWindow : public Window, gcn::ActionListener,
*/
void action(const gcn::ActionEvent &event);
- Item* getItem();
+ /**
+ * Returns the selected item.
+ */
+ Item* getSelectedItem() const;
/**
* Updates labels to currently selected item.
diff --git a/src/gui/itemcontainer.cpp b/src/gui/itemcontainer.cpp
index eb8b91a7..cf5dcb92 100644
--- a/src/gui/itemcontainer.cpp
+++ b/src/gui/itemcontainer.cpp
@@ -101,9 +101,8 @@ ItemContainer::draw(gcn::Graphics *graphics)
{
Item *item = mInventory->getItem(i);
- if (item->getQuantity() <= 0) {
+ if (!item || item->getQuantity() <= 0)
continue;
- }
int itemX = ((i - 2) % columns) * gridWidth;
int itemY = ((i - 2) / columns) * gridHeight;
@@ -116,8 +115,8 @@ ItemContainer::draw(gcn::Graphics *graphics)
}
// Draw item icon
- Image* image;
- if ((image = item->getInfo().getImage()) != NULL)
+ Image* image = item->getImage();
+ if (image)
{
static_cast<Graphics*>(graphics)->drawImage(
image, itemX, itemY);
@@ -151,7 +150,7 @@ void ItemContainer::recalculateHeight()
}
Item*
-ItemContainer::getItem()
+ItemContainer::getSelectedItem() const
{
return mSelectedItem;
}
@@ -206,7 +205,7 @@ ItemContainer::mousePressed(gcn::MouseEvent &event)
}
Item *item = mInventory->getItem(index);
setSelectedItem(item);
- if (!item->isEquipment())
+ if (item && !item->isEquipment())
{
itemShortcut->setItemSelected(item->getId());
}
diff --git a/src/gui/itemcontainer.h b/src/gui/itemcontainer.h
index 9320cdcf..db8c6f3d 100644
--- a/src/gui/itemcontainer.h
+++ b/src/gui/itemcontainer.h
@@ -80,7 +80,7 @@ class ItemContainer : public gcn::Widget, public gcn::MouseListener,
/**
* Returns the selected item.
*/
- Item* getItem();
+ Item* getSelectedItem() const;
/**
* Sets selected item to NULL.
diff --git a/src/gui/itemshortcutcontainer.cpp b/src/gui/itemshortcutcontainer.cpp
index b15c8983..0ce4f6b7 100644
--- a/src/gui/itemshortcutcontainer.cpp
+++ b/src/gui/itemshortcutcontainer.cpp
@@ -25,6 +25,7 @@
#include "../localplayer.h"
#include "../graphics.h"
+#include "../inventory.h"
#include "../item.h"
#include "../itemshortcut.h"
#include "../keyboardconfig.h"
@@ -93,10 +94,11 @@ ItemShortcutContainer::draw(gcn::Graphics *graphics)
if (itemShortcut->getItem(i) < 0)
continue;
- Item *item = player_node->searchForItem(itemShortcut->getItem(i));
+ Item *item =
+ player_node->getInventory()->findItem(itemShortcut->getItem(i));
if (item) {
// Draw item icon.
- Image* image = item->getInfo().getImage();
+ Image* image = item->getImage();
if (image) {
g->drawImage(image, itemX, itemY);
g->drawText(
@@ -110,7 +112,7 @@ ItemShortcutContainer::draw(gcn::Graphics *graphics)
if (mItemMoved)
{
// Draw the item image being dragged by the cursor.
- Image* image = mItemMoved->getInfo().getImage();
+ Image* image = mItemMoved->getImage();
if (image)
{
const int tPosX = mCursorPosX - (image->getWidth() / 2);
@@ -151,9 +153,10 @@ ItemShortcutContainer::mouseDragged(gcn::MouseEvent &event)
if (index == -1) {
return;
}
- if (itemShortcut->getItem(index) < 0)
+ const int itemId = itemShortcut->getItem(index);
+ if (itemId < 0)
return;
- Item *item = player_node->searchForItem(itemShortcut->getItem(index));
+ Item *item = player_node->getInventory()->findItem(itemId);
if (item)
{
mItemMoved = item;
diff --git a/src/gui/sell.cpp b/src/gui/sell.cpp
index 0a590ec6..82d340fb 100644
--- a/src/gui/sell.cpp
+++ b/src/gui/sell.cpp
@@ -37,7 +37,6 @@
#include "../npc.h"
#include "../resources/iteminfo.h"
-#include "../resources/itemdb.h"
#include "../net/messageout.h"
#include "../net/protocol.h"
@@ -117,22 +116,15 @@ void SellDialog::reset()
updateButtonsAndLabels();
}
-void SellDialog::addItem(Item *item, int price)
+void SellDialog::addItem(const Item *item, int price)
{
if (!item)
return;
- ITEM_SHOP item_shop;
+ mShopItems->addItem(
+ item->getInvIndex(), item->getId(),
+ item->getQuantity(), price);
- item_shop.name = item->getInfo().getName()
- + " (" + toString(price) + " GP)";
- item_shop.price = price;
- item_shop.index = item->getInvIndex();
- item_shop.id = item->getId();
- item_shop.quantity = item->getQuantity();
- item_shop.image = item->getInfo().getImage();
-
- mShopItems->push_back(item_shop);
mShopItemList->adjustSize();
}
@@ -178,12 +170,13 @@ void SellDialog::action(const gcn::ActionEvent &event)
MessageOut outMsg(mNetwork);
outMsg.writeInt16(CMSG_NPC_SELL_REQUEST);
outMsg.writeInt16(8);
- outMsg.writeInt16(mShopItems->at(selectedItem).index);
+ outMsg.writeInt16(mShopItems->at(selectedItem)->getInvIndex());
outMsg.writeInt16(mAmountItems);
mMaxItems -= mAmountItems;
- mShopItems->getShop()->at(selectedItem).quantity = mMaxItems;
- mPlayerMoney += (mAmountItems * mShopItems->at(selectedItem).price);
+ mShopItems->getShop()->at(selectedItem)->setQuantity(mMaxItems);
+ mPlayerMoney +=
+ mAmountItems * mShopItems->at(selectedItem)->getPrice();
mAmountItems = 1;
if (!mMaxItems)
@@ -277,15 +270,15 @@ SellDialog::updateButtonsAndLabels()
if (selectedItem > -1)
{
- mMaxItems = mShopItems->at(selectedItem).quantity;
+ mMaxItems = mShopItems->at(selectedItem)->getQuantity();
if (mAmountItems > mMaxItems)
{
mAmountItems = mMaxItems;
}
- income = mAmountItems * mShopItems->at(selectedItem).price;
+ income = mAmountItems * mShopItems->at(selectedItem)->getPrice();
- const ItemInfo &info = ItemDB::get(mShopItems->at(selectedItem).id);
+ const ItemInfo &info = mShopItems->at(selectedItem)->getInfo();
mItemDescLabel->setCaption("Description: " + info.getDescription());
mItemEffectLabel->setCaption("Effect: " + info.getEffect());
}
diff --git a/src/gui/sell.h b/src/gui/sell.h
index c56337ef..0c1a2007 100644
--- a/src/gui/sell.h
+++ b/src/gui/sell.h
@@ -64,7 +64,7 @@ class SellDialog : public Window, gcn::ActionListener, gcn::SelectionListener
/**
* Adds an item to the inventory.
*/
- void addItem(Item *item, int price);
+ void addItem(const Item *item, int price);
/**
* Called when receiving actions from the widgets.
diff --git a/src/gui/shop.cpp b/src/gui/shop.cpp
index 3d972bc2..ff6e3d68 100644
--- a/src/gui/shop.cpp
+++ b/src/gui/shop.cpp
@@ -22,8 +22,8 @@
*/
#include "shop.h"
-#include "../utils/tostring.h"
-#include "../resources/itemdb.h"
+
+#include "../utils/dtor.h"
ShopItems::~ShopItems()
{
@@ -32,43 +32,38 @@ ShopItems::~ShopItems()
int ShopItems::getNumberOfElements()
{
- return mItemsShop.size();
+ return mShopItems.size();
}
std::string ShopItems::getElementAt(int i)
{
- return mItemsShop.at(i).name;
+ return mShopItems.at(i)->getDisplayName();
}
-void ShopItems::addItem(short id, int price)
+void ShopItems::addItem(int inventoryIndex, short id, int amount, int price)
{
- ITEM_SHOP item_shop;
-
- item_shop.name = ItemDB::get(id).getName()
- + " (" + toString(price) + " GP)";
- item_shop.price = price;
- item_shop.id = id;
- item_shop.image = ItemDB::get(id).getImage();
-
- mItemsShop.push_back(item_shop);
+ ShopItem *item = new ShopItem(id, amount, price);
+ item->setInvIndex(inventoryIndex);
+ mShopItems.push_back(item);
}
-ITEM_SHOP ShopItems::at(int i)
+void ShopItems::addItem(short id, int price)
{
- return mItemsShop.at(i);
+ mShopItems.push_back(new ShopItem(id, 0, price));
}
-void ShopItems::push_back(ITEM_SHOP item_shop)
+ShopItem* ShopItems::at(int i) const
{
- mItemsShop.push_back(item_shop);
+ return mShopItems.at(i);
}
void ShopItems::clear()
{
- mItemsShop.clear();
+ std::for_each(mShopItems.begin(), mShopItems.end(), make_dtor(mShopItems));
+ mShopItems.clear();
}
-std::vector<ITEM_SHOP>* ShopItems::getShop()
+std::vector<ShopItem*>* ShopItems::getShop()
{
- return &mItemsShop;
+ return &mShopItems;
}
diff --git a/src/gui/shop.h b/src/gui/shop.h
index de452b5c..22e715c9 100644
--- a/src/gui/shop.h
+++ b/src/gui/shop.h
@@ -28,34 +28,28 @@
#include <vector>
#include <guichan/listmodel.hpp>
+
#include "../resources/image.h"
-struct ITEM_SHOP {
- short id;
- std::string name;
- Image *image;
- int price;
- int index;
- int quantity;
-};
+#include "../shopitem.h"
class ShopItems : public gcn::ListModel
{
public:
/**
- * Destructor
+ * Destructor.
*/
~ShopItems();
/**
- * Adds an item and its associated picture
+ * Adds an item to the list (used by sell dialog).
*/
- void addItem(short id, int price);
+ void addItem(int inventoryIndex, short id, int amount, int price);
/**
- * Convenience function for adding items
+ * Adds an item to the list (used by buy dialog).
*/
- void push_back(ITEM_SHOP item_shop);
+ void addItem(short id, int price);
/**
* Returns the number of items in the shop.
@@ -70,7 +64,7 @@ class ShopItems : public gcn::ListModel
/**
* Returns the item number i in the shop.
*/
- ITEM_SHOP at(int i);
+ ShopItem* at(int i) const;
/**
* Clear the vector.
@@ -78,13 +72,12 @@ class ShopItems : public gcn::ListModel
void clear();
/**
- * Direct access to the vector
+ * Direct access to the vector.
*/
- std::vector<ITEM_SHOP>* getShop();
+ std::vector<ShopItem*>* getShop();
private:
- std::vector<ITEM_SHOP> mItemsShop;
-
+ std::vector<ShopItem*> mShopItems;
};
#endif
diff --git a/src/gui/shoplistbox.cpp b/src/gui/shoplistbox.cpp
index f3f98062..7a8b52ed 100644
--- a/src/gui/shoplistbox.cpp
+++ b/src/gui/shoplistbox.cpp
@@ -77,7 +77,7 @@ void ShopListBox::draw(gcn::Graphics *gcnGraphics)
backgroundColor = gcn::Color(110, 160, 255);
}
else if (mShopItems &&
- mPlayerMoney < mShopItems->at(i).price && mPriceCheck)
+ mPlayerMoney < mShopItems->at(i)->getPrice() && mPriceCheck)
{
backgroundColor = gcn::Color(0x919191);
}
@@ -87,7 +87,7 @@ void ShopListBox::draw(gcn::Graphics *gcnGraphics)
if (mShopItems)
{
- Image *icon = mShopItems->at(i).image;
+ Image *icon = mShopItems->at(i)->getImage();
if (icon)
{
graphics->drawImage(icon, 1, y);
diff --git a/src/gui/trade.cpp b/src/gui/trade.cpp
index db33fb12..54544250 100644
--- a/src/gui/trade.cpp
+++ b/src/gui/trade.cpp
@@ -227,17 +227,17 @@ void TradeWindow::tradeItem(Item *item, int quantity)
void TradeWindow::valueChanged(const gcn::SelectionEvent &event)
{
- Item *item;
+ const Item *item;
/* If an item is selected in one container, make sure no item is selected
* in the other container.
*/
if (event.getSource() == mMyItemContainer &&
- (item = mMyItemContainer->getItem()))
+ (item = mMyItemContainer->getSelectedItem()))
{
mPartnerItemContainer->selectNone();
}
- else if ((item = mPartnerItemContainer->getItem()))
+ else if ((item = mPartnerItemContainer->getSelectedItem()))
{
mMyItemContainer->selectNone();
}
@@ -262,19 +262,15 @@ void TradeWindow::valueChanged(const gcn::SelectionEvent &event)
void TradeWindow::action(const gcn::ActionEvent &event)
{
- Item *item = inventoryWindow->getItem();
+ Item *item = inventoryWindow->getSelectedItem();
if (event.getId() == "add")
{
if (!item)
- {
return;
- }
if (mMyInventory->getFreeSlot() < 1)
- {
return;
- }
if (mMyInventory->contains(item)) {
chatWindow->chatLog("Failed adding item. You can not "