summaryrefslogtreecommitdiff
path: root/src/gui
diff options
context:
space:
mode:
authorBjørn Lindeijer <bjorn@lindeijer.nl>2007-06-02 11:29:54 +0000
committerBjørn Lindeijer <bjorn@lindeijer.nl>2007-06-02 11:29:54 +0000
commit2237e5cda55151147fa3b0d23a4d74fb7f07c1fe (patch)
treebeea58a6442dedcf0a9517bf6b65ce1b8da4c741 /src/gui
parent00d3e974e0cd9da6806c80526f8d0f47ddf01dd2 (diff)
downloadmana-2237e5cda55151147fa3b0d23a4d74fb7f07c1fe.tar.gz
mana-2237e5cda55151147fa3b0d23a4d74fb7f07c1fe.tar.bz2
mana-2237e5cda55151147fa3b0d23a4d74fb7f07c1fe.tar.xz
mana-2237e5cda55151147fa3b0d23a4d74fb7f07c1fe.zip
Some cleanup of buy/sell dialog code.
Diffstat (limited to 'src/gui')
-rw-r--r--src/gui/buy.cpp112
-rw-r--r--src/gui/buy.h6
-rw-r--r--src/gui/sell.cpp150
-rw-r--r--src/gui/sell.h6
4 files changed, 112 insertions, 162 deletions
diff --git a/src/gui/buy.cpp b/src/gui/buy.cpp
index edd3fe2c..b3f97e57 100644
--- a/src/gui/buy.cpp
+++ b/src/gui/buy.cpp
@@ -114,27 +114,21 @@ void BuyDialog::setMoney(int amount)
{
mMoney = amount;
mShopItemList->setPlayersMoney(amount);
- mMoneyLabel->setCaption("Price: 0 GP / " + toString(mMoney) + " GP");
- mMoneyLabel->adjustSize();
+
+ updateButtonsAndLabels();
}
void BuyDialog::reset()
{
mShopItems->clear();
+ mShopItemList->adjustSize();
mMoney = 0;
mSlider->setValue(0.0);
- mAmountItems = 0;
// Reset Previous Selected Items to prevent failing asserts
mShopItemList->setSelected(-1);
- mIncreaseButton->setEnabled(false);
- mDecreaseButton->setEnabled(false);
- mQuantityLabel->setCaption("0");
- mQuantityLabel->adjustSize();
- mMoneyLabel->setCaption("Price: 0 GP / " + toString(mMoney) + " GP");
- mMoneyLabel->adjustSize();
- mItemDescLabel->setCaption("");
- mItemEffectLabel->setCaption("");
+
+ updateButtonsAndLabels();
}
void BuyDialog::addItem(short id, int price)
@@ -160,40 +154,30 @@ void BuyDialog::action(const gcn::ActionEvent &event)
return;
}
- bool updateButtonsAndLabels = false;
-
if (event.getId() == "slider")
{
mAmountItems = (int)(mSlider->getValue() * mMaxItems);
- updateButtonsAndLabels = true;
+ updateButtonsAndLabels();
}
- else if (event.getId() == "+")
+ else if (event.getId() == "+" && mAmountItems < mMaxItems)
{
- if (mAmountItems < mMaxItems) {
- mAmountItems++;
- } else {
- mAmountItems = mMaxItems;
- }
+ mAmountItems++;
mSlider->setValue((double) mAmountItems / (double) mMaxItems);
- updateButtonsAndLabels = true;
+ updateButtonsAndLabels();
}
- else if (event.getId() == "-")
+ else if (event.getId() == "-" && mAmountItems > 0)
{
- if (mAmountItems > 0) {
- mAmountItems--;
- } else {
- mAmountItems = 0;
- }
+ mAmountItems--;
- mSlider->setValue(double(mAmountItems)/double(mMaxItems));
- updateButtonsAndLabels = true;
+ mSlider->setValue((double) mAmountItems / (double) mMaxItems);
+ updateButtonsAndLabels();
}
// TODO: Actually we'd have a bug elsewhere if this check for the number
// of items to be bought ever fails, Bertram removed the assertions, is
// there a better way to ensure this fails in an _obivous_ way in C++?
- else if (event.getId() == "buy" && (mAmountItems > 0 &&
- mAmountItems <= mMaxItems))
+ else if (event.getId() == "buy" && mAmountItems > 0 &&
+ mAmountItems <= mMaxItems)
{
MessageOut outMsg(mNetwork);
outMsg.writeInt16(CMSG_NPC_BUY_REQUEST);
@@ -201,38 +185,15 @@ void BuyDialog::action(const gcn::ActionEvent &event)
outMsg.writeInt16(mAmountItems);
outMsg.writeInt16(mShopItems->at(selectedItem).id);
- // update money !
+ // Update money and adjust the max number of items that can be bought
mMoney -= mAmountItems * mShopItems->at(selectedItem).price;
- // Update number of items that can be bought at max
mMaxItems -= mAmountItems;
- if (!mMaxItems) {
- mSlider->setEnabled(false);
- }
-
// Reset selection
mAmountItems = 0;
- mSlider->setValue(0);
+ mSlider->setValue(0.0);
- updateButtonsAndLabels = true;
- }
-
- // If anything has changed, we have to update the buttons and labels
- if (updateButtonsAndLabels)
- {
- // Update buttons
- mIncreaseButton->setEnabled(mAmountItems < mMaxItems);
- mDecreaseButton->setEnabled(mAmountItems > 0);
- mBuyButton->setEnabled(mAmountItems > 0);
-
- // Update labels
- mQuantityLabel->setCaption(toString(mAmountItems));
- mQuantityLabel->adjustSize();
-
- int price = mAmountItems * mShopItems->at(selectedItem).price;
- mMoneyLabel->setCaption("Price: " + toString(price) + " GP / "
- + toString(mMoney - price) + " GP" );
- mMoneyLabel->adjustSize();
+ updateButtonsAndLabels();
}
}
@@ -240,17 +201,16 @@ void BuyDialog::selectionChanged(const SelectionEvent &event)
{
// Reset amount of items and update labels
mAmountItems = 0;
- mSlider->setValue(0);
- mQuantityLabel->setCaption("0");
- mQuantityLabel->adjustSize();
- mMoneyLabel->setCaption("Price: 0 GP / " + toString(mMoney) + " GP");
- mMoneyLabel->adjustSize();
+ mSlider->setValue(0.0);
- // Disable buttons for buying and decreasing
- mBuyButton->setEnabled(false);
- mDecreaseButton->setEnabled(false);
+ updateButtonsAndLabels();
+}
+void
+BuyDialog::updateButtonsAndLabels()
+{
int selectedItem = mShopItemList->getSelected();
+ int price = 0;
if (selectedItem > -1)
{
@@ -261,16 +221,32 @@ void BuyDialog::selectionChanged(const SelectionEvent &event)
// Calculate how many the player can afford
mMaxItems = mMoney / mShopItems->at(selectedItem).price;
+ if (mAmountItems > mMaxItems)
+ {
+ mAmountItems = mMaxItems;
+ }
+
+ // Calculate price of pending purchase
+ price = mAmountItems * mShopItems->at(selectedItem).price;
}
else
{
mItemDescLabel->setCaption("Description:");
mItemEffectLabel->setCaption("Effect:");
mMaxItems = 0;
+ mAmountItems = 0;
}
- // When at least one item can be bought, enable the slider and the
- // increase button
- mIncreaseButton->setEnabled(mMaxItems > 0);
+ // Enable or disable buttons and slider
+ mIncreaseButton->setEnabled(mAmountItems < mMaxItems);
+ mDecreaseButton->setEnabled(mAmountItems > 0);
+ mBuyButton->setEnabled(mAmountItems > 0);
mSlider->setEnabled(mMaxItems > 0);
+
+ // Update quantity and money labels
+ mQuantityLabel->setCaption(toString(mAmountItems));
+ mQuantityLabel->adjustSize();
+ mMoneyLabel->setCaption("Price: " + toString(price) + " GP / "
+ + toString(mMoney - price) + " GP" );
+ mMoneyLabel->adjustSize();
}
diff --git a/src/gui/buy.h b/src/gui/buy.h
index b2b88c40..8f1893dc 100644
--- a/src/gui/buy.h
+++ b/src/gui/buy.h
@@ -93,6 +93,12 @@ class BuyDialog : public Window, public gcn::ActionListener, SelectionListener
*/
std::string getElementAt(int i);
+ /**
+ * Updates the state of buttons and labels.
+ */
+ void
+ updateButtonsAndLabels();
+
private:
Network *mNetwork;
gcn::Button *mBuyButton;
diff --git a/src/gui/sell.cpp b/src/gui/sell.cpp
index b5476dd0..7cb49686 100644
--- a/src/gui/sell.cpp
+++ b/src/gui/sell.cpp
@@ -92,7 +92,7 @@ SellDialog::SellDialog(Network *network):
quitButton->setPosition(208, 186);
mShopItemList->setActionEventId("item");
- mSlider->setActionEventId("mSlider");
+ mSlider->setActionEventId("slider");
mShopItemList->setPriceCheck(false);
@@ -123,20 +123,11 @@ void SellDialog::reset()
{
mShopItems->clear();
mSlider->setValue(0.0);
- mAmountItems = 0;
-
- mQuantityLabel->setCaption("0");
- mQuantityLabel->adjustSize();
- mMoneyLabel->setCaption("Money: 0 GP / Total: "
- + toString(mPlayerMoney) + " GP");
- mMoneyLabel->adjustSize();
- mItemDescLabel->setCaption("");
- mItemEffectLabel->setCaption("");
- // Reset Previous Selected Items to prevent failing asserts
+ // Reset previous selected item to prevent failing asserts
mShopItemList->setSelected(-1);
- mIncreaseButton->setEnabled(false);
- mDecreaseButton->setEnabled(false);
+
+ updateButtonsAndLabels();
}
void SellDialog::addItem(Item *item, int price)
@@ -166,28 +157,7 @@ void SellDialog::action(const gcn::ActionEvent &event)
{
mAmountItems = 0;
mSlider->setValue(0);
- mDecreaseButton->setEnabled(false);
- mSellButton->setEnabled(false);
-
- mQuantityLabel->adjustSize();
- mMoneyLabel->setCaption("Money: 0 GP / Total: "
- + toString(mPlayerMoney) + " GP");
- mMoneyLabel->adjustSize();
-
- if (selectedItem > -1)
- {
- mSlider->setEnabled(true);
- mIncreaseButton->setEnabled(true);
- mMaxItems = mShopItems->at(selectedItem).quantity;
- mQuantityLabel->setCaption("0 / " + toString(mMaxItems));
- }
- else
- {
- mSlider->setEnabled(false);
- mIncreaseButton->setEnabled(false);
- mQuantityLabel->setCaption("0");
- }
- mQuantityLabel->adjustSize();
+ updateButtonsAndLabels();
}
else if (event.getId() == "quit")
{
@@ -197,39 +167,34 @@ void SellDialog::action(const gcn::ActionEvent &event)
// The following actions require a valid item selection
if (selectedItem == -1 ||
- selectedItem >= (int) mShopItems->getNumberOfElements()) {
+ selectedItem >= (int) mShopItems->getNumberOfElements())
+ {
return;
}
- bool updateButtonsAndLabels = false;
-
- if (event.getId() == "mSlider")
+ if (event.getId() == "slider")
{
mAmountItems = (int) (mSlider->getValue() * mMaxItems);
- updateButtonsAndLabels = true;
+ updateButtonsAndLabels();
}
- else if (event.getId() == "+")
+ else if (event.getId() == "+" && mAmountItems < mMaxItems)
{
- assert(mAmountItems < mMaxItems);
mAmountItems++;
- mSlider->setValue((double) mAmountItems /(double) mMaxItems);
- updateButtonsAndLabels = true;
+ mSlider->setValue((double) mAmountItems /(double) mMaxItems);
+ updateButtonsAndLabels();
}
- else if (event.getId() == "-")
+ else if (event.getId() == "-" && mAmountItems > 0)
{
- assert(mAmountItems > 0);
mAmountItems--;
mSlider->setValue((double) mAmountItems / (double) mMaxItems);
-
- updateButtonsAndLabels = true;
+ updateButtonsAndLabels();
}
- else if (event.getId() == "sell")
+ else if (event.getId() == "sell" && mAmountItems > 0
+ && mAmountItems <= mMaxItems)
{
// Attempt sell
- assert(mAmountItems > 0 && mAmountItems <= mMaxItems);
-
MessageOut outMsg(mNetwork);
outMsg.writeInt16(CMSG_NPC_SELL_REQUEST);
outMsg.writeInt16(8);
@@ -241,38 +206,20 @@ void SellDialog::action(const gcn::ActionEvent &event)
mPlayerMoney += (mAmountItems * mShopItems->at(selectedItem).price);
mAmountItems = 0;
mSlider->setValue(0);
- mSlider->setEnabled(mMaxItems != 0);
- // All were sold
if (!mMaxItems)
{
+ // All were sold
mShopItemList->setSelected(-1);
mShopItems->getShop()->erase(
mShopItems->getShop()->begin() + selectedItem);
}
-
- // Update only when there are items left, the entry doesn't exist
- // otherwise and can't be updated
- updateButtonsAndLabels = mMaxItems != 0;
- }
-
- // If anything changed, we need to update the buttons and labels
- if (updateButtonsAndLabels)
- {
- // Update Buttons
- mSellButton->setEnabled(mAmountItems > 0);
- mDecreaseButton->setEnabled(mAmountItems > 0);
- mIncreaseButton->setEnabled(mAmountItems < mMaxItems);
-
- // Update labels
- mQuantityLabel->setCaption(
- toString(mAmountItems) + " / " + toString(mMaxItems));
- mQuantityLabel->adjustSize();
-
- int sellSum = mAmountItems * mShopItems->at(selectedItem).price;
- mMoneyLabel->setCaption("Money: " + toString(sellSum) + " GP / Total: "
- + toString(mPlayerMoney + sellSum) + " GP");
- mMoneyLabel->adjustSize();
+ else
+ {
+ // Update only when there are items left, the entry doesn't exist
+ // otherwise and can't be updated
+ updateButtonsAndLabels();
+ }
}
}
@@ -281,40 +228,55 @@ void SellDialog::selectionChanged(const SelectionEvent &event)
// Reset amount of items and update labels
mAmountItems = 0;
mSlider->setValue(0);
- mSlider->setEnabled(true);
- mMoneyLabel->setCaption("Money: 0 GP / Total: "
- + toString(mPlayerMoney) + " GP");
- mMoneyLabel->adjustSize();
- // Disable buttons for selling and decreasing, enable button for increasing
- mIncreaseButton->setEnabled(true);
- mSellButton->setEnabled(false);
- mDecreaseButton->setEnabled(false);
+ updateButtonsAndLabels();
+}
+void SellDialog::setMoney(int amount)
+{
+ mPlayerMoney = amount;
+ mShopItemList->setPlayersMoney(amount);
+}
+
+void
+SellDialog::updateButtonsAndLabels()
+{
int selectedItem = mShopItemList->getSelected();
+ int income = 0;
if (selectedItem > -1)
{
mMaxItems = mShopItems->at(selectedItem).quantity;
+ if (mAmountItems > mMaxItems)
+ {
+ mAmountItems = mMaxItems;
+ }
- const ItemInfo &info = ItemDB::get(mShopItems->at(selectedItem).id);
+ income = mAmountItems * mShopItems->at(selectedItem).price;
+ const ItemInfo &info = ItemDB::get(mShopItems->at(selectedItem).id);
mItemDescLabel->setCaption("Description: " + info.getDescription());
mItemEffectLabel->setCaption("Effect: " + info.getEffect());
}
else
{
mMaxItems = 0;
- mItemDescLabel->setCaption("Description");
- mItemEffectLabel->setCaption("Effect");
+ mAmountItems = 0;
+ mItemDescLabel->setCaption("Description:");
+ mItemEffectLabel->setCaption("Effect:");
}
- mQuantityLabel->setCaption("0 / " + toString(mMaxItems));
- mQuantityLabel->adjustSize();
-}
+ // Update Buttons and slider
+ mSellButton->setEnabled(mAmountItems > 0);
+ mDecreaseButton->setEnabled(mAmountItems > 0);
+ mIncreaseButton->setEnabled(mAmountItems < mMaxItems);
+ mSlider->setEnabled(selectedItem > -1);
-void SellDialog::setMoney(int amount)
-{
- mPlayerMoney = amount;
- mShopItemList->setPlayersMoney(amount);
+ // Update the quantity and money labels
+ mQuantityLabel->setCaption(
+ toString(mAmountItems) + " / " + toString(mMaxItems));
+ mQuantityLabel->adjustSize();
+ mMoneyLabel->setCaption("Money: " + toString(income) + " GP / Total: "
+ + toString(mPlayerMoney + income) + " GP");
+ mMoneyLabel->adjustSize();
}
diff --git a/src/gui/sell.h b/src/gui/sell.h
index ab940f41..d3ea7136 100644
--- a/src/gui/sell.h
+++ b/src/gui/sell.h
@@ -83,6 +83,12 @@ class SellDialog : public Window, gcn::ActionListener, SelectionListener
*/
void setMoney(int amount);
+ /**
+ * Updates the state of buttons and labels.
+ */
+ void
+ updateButtonsAndLabels();
+
private:
Network *mNetwork;
gcn::Button *mSellButton;