summaryrefslogtreecommitdiff
path: root/src/gui/inventorywindow.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/gui/inventorywindow.cpp')
-rw-r--r--src/gui/inventorywindow.cpp218
1 files changed, 158 insertions, 60 deletions
diff --git a/src/gui/inventorywindow.cpp b/src/gui/inventorywindow.cpp
index b4a96394..d18490a4 100644
--- a/src/gui/inventorywindow.cpp
+++ b/src/gui/inventorywindow.cpp
@@ -1,56 +1,57 @@
/*
* The Mana World
- * Copyright 2004 The Mana World Development Team
+ * Copyright (C) 2004 The Mana World Development Team
*
* This file is part of The Mana World.
*
- * The Mana World is free software; you can redistribute it and/or modify
+ * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* any later version.
*
- * The Mana World is distributed in the hope that it will be useful,
+ * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
- * along with The Mana World; if not, write to the Free Software
+ * along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
-#include "inventorywindow.h"
-
#include <string>
+#include <guichan/font.hpp>
#include <guichan/mouseinput.hpp>
#include <guichan/widgets/label.hpp>
-#include <guichan/widgets/checkbox.hpp>
-#include <guichan/widgets/textbox.hpp>
#include "button.h"
-#include "gui.h"
+#include "inventorywindow.h"
#include "item_amount.h"
#include "itemcontainer.h"
-#include "itempopup.h"
+#include "progressbar.h"
#include "scrollarea.h"
#include "sdlinput.h"
#include "viewport.h"
#include "widgets/layout.h"
+#include "../inventory.h"
#include "../item.h"
#include "../localplayer.h"
#include "../log.h"
+#include "../units.h"
#include "../resources/iteminfo.h"
#include "../utils/gettext.h"
+#include "../utils/stringutils.h"
#include "../utils/strprintf.h"
-InventoryWindow::InventoryWindow():
+InventoryWindow::InventoryWindow(int invSize):
Window(_("Inventory")),
+ mMaxSlots(invSize),
mSplit(false),
mItemDesc(false)
{
@@ -65,28 +66,67 @@ InventoryWindow::InventoryWindow():
setDefaultSize(115, 30, 375, 283);
addKeyListener(this);
- mUseButton = new Button(_("Use"), "use", this);
+ std::string longestUseString = getFont()->getWidth(_("Equip")) >
+ getFont()->getWidth(_("Use")) ?
+ _("Equip") : _("Use");
+
+ if (getFont()->getWidth(longestUseString) <
+ getFont()->getWidth(_("Unequip")))
+ {
+ longestUseString = _("Unequip");
+ }
+
+ mUseButton = new Button(longestUseString, "use", this);
mDropButton = new Button(_("Drop"), "drop", this);
+#ifdef TMWSERV_SUPPORT
mSplitButton = new Button(_("Split"), "split", this);
+#endif
- mItems = new ItemContainer(player_node->mInventory, 10, 5);
+#ifdef TMWSERV_SUPPORT
+ mItems = new ItemContainer(player_node->getInventory(), 10, 5);
+#else
+ mItems = new ItemContainer(player_node->getInventory(), 10, 5, 2);
+#endif
mItems->addSelectionListener(this);
mInvenScroll = new ScrollArea(mItems);
+ mInvenScroll->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER);
+
+ mTotalWeight = -1;
+ mMaxWeight = -1;
+ mUsedSlots = toString(player_node->getInventory()->getNumberOfSlotsUsed());
+
+ mSlotsLabel = new gcn::Label(_("Slots: "));
+ mWeightLabel = new gcn::Label(_("Weight: "));
+
+ mSlotsBar = new ProgressBar(1.0f, 100, 20, 225, 200, 25);
+ mWeightBar = new ProgressBar(1.0f, 100, 20, 0, 0, 255);
+
+ setMinHeight(130);
+ setMinWidth(mWeightLabel->getWidth() + mSlotsLabel->getWidth() + 280);
+
+ place(0, 0, mWeightLabel).setPadding(3);
+ place(1, 0, mWeightBar, 3);
+ place(4, 0, mSlotsLabel).setPadding(3);
+ place(5, 0, mSlotsBar, 2);
+ place(0, 1, mInvenScroll, 100).setPadding(3);
+ place(0, 2, mUseButton);
+ place(1, 2, mDropButton);
+#ifdef TMWSERV_SUPPORT
+ place(2, 2, mSplitButton);
+#endif
- place(0, 0, mInvenScroll, 100).setPadding(3);
- place(0, 1, mUseButton);
- place(1, 1, mDropButton);
- place(2, 1, mSplitButton);
Layout &layout = getLayout();
- layout.setColWidth(0, 48);
- layout.setColWidth(1, 48);
- layout.setColWidth(2, 48);
layout.setRowHeight(0, Layout::AUTO_SET);
loadWindowState();
}
+InventoryWindow::~InventoryWindow()
+{
+ delete mItems;
+}
+
void InventoryWindow::logic()
{
Window::logic();
@@ -95,24 +135,69 @@ void InventoryWindow::logic()
// redesign of InventoryWindow and ItemContainer probably.
updateButtons();
- // Update weight information
- // mWeightLabel->setCaption(strprintf(_("Total Weight: %d - Maximum Weight: %d"), player_node->getTotalWeight(), player_node->getMaxWeight()));
+ if (mMaxWeight != player_node->getMaxWeight() ||
+ mTotalWeight != player_node->getTotalWeight() ||
+ mUsedSlots != toString(player_node->getInventory()->getNumberOfSlotsUsed()))
+ {
+ mTotalWeight = player_node->getTotalWeight();
+ mMaxWeight = player_node->getMaxWeight();
+ mUsedSlots = toString(player_node->getInventory()->getNumberOfSlotsUsed());
+
+ // Weight Bar coloration
+ if (int(player_node->getTotalWeight()) < int(player_node->getMaxWeight() / 3))
+ {
+ mWeightBar->setColor(0, 0, 255); // Blue
+ }
+ else if (int(player_node->getTotalWeight()) <
+ int((player_node->getMaxWeight() / 3) * 2))
+ {
+ mWeightBar->setColor(255, 255, 0); // Yellow
+ }
+ else
+ {
+ mWeightBar->setColor(255, 0, 0); // Red
+ }
+
+ // Adjust progress bars
+ mSlotsBar->setProgress((float)
+ player_node->getInventory()->getNumberOfSlotsUsed() / mMaxSlots);
+ mWeightBar->setProgress((float) player_node->getTotalWeight() /
+ player_node->getMaxWeight());
+
+ mSlotsBar->setText(strprintf("%s/%d", mUsedSlots.c_str(), mMaxSlots));
+ mWeightBar->setText(strprintf("%s/%s",
+ Units::formatWeight(mTotalWeight).c_str(),
+ Units::formatWeight(mMaxWeight).c_str()));
+ }
}
void InventoryWindow::action(const gcn::ActionEvent &event)
{
- Item *item = mItems->getItem();
+ Item *item = mItems->getSelectedItem();
+
if (!item)
return;
if (event.getId() == "use")
{
+#ifdef TMWSERV_SUPPORT
if (item->isEquipment()) {
player_node->equipItem(item);
}
else {
player_node->useItem(item->getInvIndex());
}
+#else
+ if (item->isEquipment())
+ {
+ if (item->isEquipped())
+ player_node->unequipItem(item);
+ else
+ player_node->equipItem(item);
+ }
+ else
+ player_node->useItem(item);
+#endif
}
else if (event.getId() == "drop")
{
@@ -134,18 +219,9 @@ void InventoryWindow::action(const gcn::ActionEvent &event)
}
}
-void InventoryWindow::valueChanged(const gcn::SelectionEvent &event)
+Item* InventoryWindow::getSelectedItem() const
{
- Item *item = mItems->getItem();
-
- if (mSplit)
- {
- if (item && !item->isEquipment() && item->getQuantity() > 1)
- {
- mSplit = false;
- new ItemAmountWindow(AMOUNT_ITEM_SPLIT, this, item, (item->getQuantity() - 1));
- }
- }
+ return mItems->getSelectedItem();
}
void InventoryWindow::mouseClicked(gcn::MouseEvent &event)
@@ -154,7 +230,7 @@ void InventoryWindow::mouseClicked(gcn::MouseEvent &event)
if (event.getButton() == gcn::MouseEvent::RIGHT)
{
- Item *item = mItems->getItem();
+ Item *item = mItems->getSelectedItem();
if (!item)
return;
@@ -168,31 +244,7 @@ void InventoryWindow::mouseClicked(gcn::MouseEvent &event)
}
}
-void InventoryWindow::updateButtons()
-{
- Item *item = mItems->getItem();
-
- if (item && item->isEquipment()) {
- mUseButton->setCaption(_("Equip"));
- }
- else {
- mUseButton->setCaption(_("Use"));
- }
- mUseButton->setEnabled(!!item);
- mDropButton->setEnabled(!!item);
- if (item && !item->isEquipment() && item->getQuantity() > 1) {
- mSplitButton->setEnabled(true);
- }
- else {
- mSplitButton->setEnabled(false);
- }
-}
-
-Item* InventoryWindow::getItem()
-{
- return mItems->getItem();
-}
-
+#ifdef TMWSERV_SUPPORT
void InventoryWindow::keyPressed(gcn::KeyEvent &event)
{
switch (event.getKey().getValue())
@@ -214,3 +266,49 @@ void InventoryWindow::keyReleased(gcn::KeyEvent &event)
break;
}
}
+#endif
+
+void InventoryWindow::valueChanged(const gcn::SelectionEvent &event)
+{
+ if (mSplit)
+ {
+ Item *item = mItems->getSelectedItem();
+
+ if (item && !item->isEquipment() && item->getQuantity() > 1)
+ {
+ mSplit = false;
+ new ItemAmountWindow(AMOUNT_ITEM_SPLIT, this, item, (item->getQuantity() - 1));
+ }
+ }
+}
+
+void InventoryWindow::updateButtons()
+{
+ const Item *selectedItem = mItems->getSelectedItem();
+
+ if (selectedItem && selectedItem->isEquipment())
+ {
+#ifdef EATHENA_SUPPORT
+ if (selectedItem->isEquipped())
+ mUseButton->setCaption(_("Unequip"));
+ else
+#endif
+ mUseButton->setCaption(_("Equip"));
+ }
+ else
+ mUseButton->setCaption(_("Use"));
+
+ mUseButton->setEnabled(selectedItem != 0);
+ mDropButton->setEnabled(selectedItem != 0);
+
+#ifdef TMWSERV_SUPPORT
+ if (selectedItem && !selectedItem->isEquipment() &&
+ selectedItem->getQuantity() > 1)
+ {
+ mSplitButton->setEnabled(true);
+ }
+ else {
+ mSplitButton->setEnabled(false);
+ }
+#endif
+}