diff options
author | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2007-06-02 11:29:54 +0000 |
---|---|---|
committer | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2007-06-02 11:29:54 +0000 |
commit | 2237e5cda55151147fa3b0d23a4d74fb7f07c1fe (patch) | |
tree | beea58a6442dedcf0a9517bf6b65ce1b8da4c741 /src/gui/sell.cpp | |
parent | 00d3e974e0cd9da6806c80526f8d0f47ddf01dd2 (diff) | |
download | mana-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/sell.cpp')
-rw-r--r-- | src/gui/sell.cpp | 150 |
1 files changed, 56 insertions, 94 deletions
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(); } |