summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/gui/inventorywindow.cpp18
-rw-r--r--src/gui/itemamount.cpp70
-rw-r--r--src/gui/itemamount.h17
-rw-r--r--src/gui/popupmenu.cpp8
-rw-r--r--src/gui/storagewindow.cpp21
-rw-r--r--src/gui/trade.cpp11
6 files changed, 64 insertions, 81 deletions
diff --git a/src/gui/inventorywindow.cpp b/src/gui/inventorywindow.cpp
index 372e94d3..027c6aa8 100644
--- a/src/gui/inventorywindow.cpp
+++ b/src/gui/inventorywindow.cpp
@@ -174,20 +174,12 @@ void InventoryWindow::action(const gcn::ActionEvent &event)
}
else if (event.getId() == "drop")
{
- if (item->getQuantity() > 1) {
- // Choose amount of items to drop
- new ItemAmountWindow(ItemAmountWindow::ItemDrop, this, item);
- }
- else {
- player_node->dropItem(item, 1);
- }
+ ItemAmountWindow::showWindow(ItemAmountWindow::ItemDrop, this, item);
}
else if (event.getId() == "split")
{
- if (item && !item->isEquipment() && item->getQuantity() > 1) {
- new ItemAmountWindow(ItemAmountWindow::ItemSplit, this, item,
+ ItemAmountWindow::showWindow(ItemAmountWindow::ItemSplit, this, item,
(item->getQuantity() - 1));
- }
}
}
@@ -244,12 +236,8 @@ void InventoryWindow::valueChanged(const gcn::SelectionEvent &event)
{
Item *item = mItems->getSelectedItem();
- if (item && !item->isEquipment() && item->getQuantity() > 1)
- {
- mSplit = false;
- new ItemAmountWindow(ItemAmountWindow::ItemSplit, this, item,
+ ItemAmountWindow::showWindow(ItemAmountWindow::ItemSplit, this, item,
(item->getQuantity() - 1));
- }
}
}
diff --git a/src/gui/itemamount.cpp b/src/gui/itemamount.cpp
index 1bbd6fdd..190290e3 100644
--- a/src/gui/itemamount.cpp
+++ b/src/gui/itemamount.cpp
@@ -35,6 +35,30 @@
#include "utils/gettext.h"
#include "utils/stringutils.h"
+void ItemAmountWindow::finish(Item *item, int amount, Usage usage)
+{
+ switch (usage)
+ {
+ case TradeAdd:
+ tradeWindow->tradeItem(item, amount);
+ break;
+ case ItemDrop:
+ player_node->dropItem(item, amount);
+ break;
+ case ItemSplit:
+ player_node->splitItem(item, amount);
+ break;
+ case StoreAdd:
+ storageWindow->addStore(item, amount);
+ break;
+ case StoreRemove:
+ storageWindow->removeStore(item, amount);
+ break;
+ default:
+ break;
+ }
+}
+
ItemAmountWindow::ItemAmountWindow(Usage usage, Window *parent, Item *item,
int maxRange):
Window("", true, parent),
@@ -68,14 +92,6 @@ ItemAmountWindow::ItemAmountWindow(Usage usage, Window *parent, Item *item,
minusButton->adjustSize();
minusButton->setWidth(plusButton->getWidth());
- // If only one item is available, then the window isn't needed, so move on
- // To prevent problems, we still build the gui elements
- if (mMax <= 1)
- {
- action(gcn::ActionEvent(this, "All"));
- return;
- }
-
// Set positions
ContainerPlacer place;
place = getPlacer(0, 0);
@@ -145,27 +161,7 @@ void ItemAmountWindow::action(const gcn::ActionEvent &event)
if (event.getId() == "All")
amount = mMax;
- switch (mUsage)
- {
- case TradeAdd:
- tradeWindow->tradeItem(mItem, amount);
- break;
- case ItemDrop:
- player_node->dropItem(mItem, amount);
- break;
- case ItemSplit:
- player_node->splitItem(mItem, amount);
- break;
- case StoreAdd:
- storageWindow->addStore(mItem, amount);
- break;
- case StoreRemove:
- storageWindow->removeStore(mItem, amount);
- break;
- default:
- return;
- break;
- }
+ finish(mItem, amount, mUsage);
scheduleDelete();
return;
@@ -179,3 +175,19 @@ void ItemAmountWindow::close()
{
scheduleDelete();
}
+
+void ItemAmountWindow::showWindow(Usage usage, Window *parent, Item *item,
+ int maxRange)
+{
+ if (!maxRange)
+ maxRange = item->getQuantity();
+
+ if (maxRange <= 1)
+ {
+ finish(item, maxRange, usage);
+ }
+ else
+ {
+ new ItemAmountWindow(usage, parent, item, maxRange);
+ }
+}
diff --git a/src/gui/itemamount.h b/src/gui/itemamount.h
index 94ca8dc2..33d6d816 100644
--- a/src/gui/itemamount.h
+++ b/src/gui/itemamount.h
@@ -45,12 +45,6 @@ class ItemAmountWindow : public Window, public gcn::ActionListener
};
/**
- * Constructor.
- */
- ItemAmountWindow(Usage usage, Window *parent, Item *item,
- int maxRange = 0);
-
- /**
* Called when receiving actions from widget.
*/
void action(const gcn::ActionEvent &event);
@@ -65,7 +59,18 @@ class ItemAmountWindow : public Window, public gcn::ActionListener
*/
void close();
+ /**
+ * Creates the dialog, or bypass it if there aren't enough items.
+ */
+ static void showWindow(Usage usage, Window *parent, Item *item,
+ int maxRange = 0);
+
private:
+ static void finish(Item *item, int amount, Usage usage);
+
+ ItemAmountWindow(Usage usage, Window *parent, Item *item,
+ int maxRange = 0);
+
gcn::Label *mItemAmountLabel; /**< Item amount caption. */
Item *mItem;
diff --git a/src/gui/popupmenu.cpp b/src/gui/popupmenu.cpp
index 6f2f9be4..46a33d6c 100644
--- a/src/gui/popupmenu.cpp
+++ b/src/gui/popupmenu.cpp
@@ -278,25 +278,25 @@ void PopupMenu::handleLink(const std::string &link)
else if (link == "split")
{
- new ItemAmountWindow(ItemAmountWindow::ItemSplit,
+ ItemAmountWindow::showWindow(ItemAmountWindow::ItemSplit,
inventoryWindow, mItem);
}
else if (link == "drop")
{
- new ItemAmountWindow(ItemAmountWindow::ItemDrop,
+ ItemAmountWindow::showWindow(ItemAmountWindow::ItemDrop,
inventoryWindow, mItem);
}
else if (link == "store")
{
- new ItemAmountWindow(ItemAmountWindow::StoreAdd,
+ ItemAmountWindow::showWindow(ItemAmountWindow::StoreAdd,
inventoryWindow, mItem);
}
else if (link == "retrieve")
{
- new ItemAmountWindow(ItemAmountWindow::StoreRemove,
+ ItemAmountWindow::showWindow(ItemAmountWindow::StoreRemove,
storageWindow, mItem);
}
diff --git a/src/gui/storagewindow.cpp b/src/gui/storagewindow.cpp
index f3558830..309fcafc 100644
--- a/src/gui/storagewindow.cpp
+++ b/src/gui/storagewindow.cpp
@@ -128,15 +128,7 @@ void StorageWindow::action(const gcn::ActionEvent &event)
if (!item)
return;
- if (item->getQuantity() == 1)
- {
- addStore(item, 1);
- }
- else
- {
- // Choose amount of items to trade
- new ItemAmountWindow(ItemAmountWindow::StoreAdd, this, item);
- }
+ ItemAmountWindow::showWindow(ItemAmountWindow::StoreAdd, this, item);
}
else if (event.getId() == "retrieve")
{
@@ -145,15 +137,8 @@ void StorageWindow::action(const gcn::ActionEvent &event)
if (!item)
return;
- if (item->getQuantity() == 1)
- {
- removeStore(item, 1);
- }
- else
- {
- // Choose amount of items to trade
- new ItemAmountWindow(ItemAmountWindow::StoreRemove, this, item);
- }
+ ItemAmountWindow::showWindow(ItemAmountWindow::StoreRemove, this,
+ item);
}
}
diff --git a/src/gui/trade.cpp b/src/gui/trade.cpp
index fa662d1a..1e7faa97 100644
--- a/src/gui/trade.cpp
+++ b/src/gui/trade.cpp
@@ -271,15 +271,8 @@ void TradeWindow::action(const gcn::ActionEvent &event)
return;
}
- if (item->getQuantity() == 1)
- {
- tradeItem(item, 1);
- }
- else
- {
- // Choose amount of items to trade
- new ItemAmountWindow(ItemAmountWindow::TradeAdd, this, item);
- }
+ // Choose amount of items to trade
+ ItemAmountWindow::showWindow(ItemAmountWindow::TradeAdd, this, item);
setStatus(PREPARING);
}