00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "gui/sell.h"
00023
00024 #include "gui/shop.h"
00025 #include "gui/shoplistbox.h"
00026
00027 #include "gui/widgets/button.h"
00028 #include "gui/widgets/label.h"
00029 #include "gui/widgets/layout.h"
00030 #include "gui/widgets/scrollarea.h"
00031 #include "gui/widgets/slider.h"
00032
00033 #include "npc.h"
00034 #include "shopitem.h"
00035 #include "units.h"
00036
00037 #include "net/net.h"
00038 #include "net/npchandler.h"
00039
00040 #include "resources/iteminfo.h"
00041
00042 #include "utils/gettext.h"
00043 #include "utils/strprintf.h"
00044
00045 SellDialog::SellDialog():
00046 Window(_("Sell")),
00047 mMaxItems(0), mAmountItems(0)
00048 {
00049 setWindowName("Sell");
00050 setResizable(true);
00051 setCloseButton(true);
00052 setMinWidth(260);
00053 setMinHeight(230);
00054 setDefaultSize(260, 230, ImageRect::CENTER);
00055
00056
00057 mShopItems = new ShopItems(true);
00058
00059 mShopItemList = new ShopListBox(mShopItems, mShopItems);
00060 mScrollArea = new ScrollArea(mShopItemList);
00061 mScrollArea->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER);
00062
00063 mSlider = new Slider(1.0);
00064
00065 mQuantityLabel = new Label(strprintf("%d / %d", mAmountItems, mMaxItems));
00066 mQuantityLabel->setAlignment(gcn::Graphics::CENTER);
00067 mMoneyLabel = new Label(strprintf(_("Price: %s / Total: %s"),
00068 "", ""));
00069
00070 mIncreaseButton = new Button("+", "+", this);
00071 mDecreaseButton = new Button("-", "-", this);
00072 mSellButton = new Button(_("Sell"), "sell", this);
00073 mQuitButton = new Button(_("Quit"), "quit", this);
00074 mAddMaxButton = new Button(_("Max"), "max", this);
00075 mItemDescLabel = new Label(strprintf(_("Description: %s"), ""));
00076 mItemEffectLabel = new Label(strprintf(_("Effect: %s"), ""));
00077
00078 mDecreaseButton->adjustSize();
00079 mDecreaseButton->setWidth(mIncreaseButton->getWidth());
00080
00081 mIncreaseButton->setEnabled(false);
00082 mDecreaseButton->setEnabled(false);
00083 mSellButton->setEnabled(false);
00084 mSlider->setEnabled(false);
00085
00086 mShopItemList->setPriceCheck(false);
00087 mShopItemList->addSelectionListener(this);
00088 mSlider->setActionEventId("slider");
00089 mSlider->addActionListener(this);
00090
00091 ContainerPlacer place;
00092 place = getPlacer(0, 0);
00093
00094 place(0, 0, mScrollArea, 8, 5).setPadding(3);
00095 place(0, 5, mDecreaseButton);
00096 place(1, 5, mSlider, 3);
00097 place(4, 5, mIncreaseButton);
00098 place(5, 5, mQuantityLabel, 2);
00099 place(7, 5, mAddMaxButton);
00100 place(0, 6, mMoneyLabel, 8);
00101 place(0, 7, mItemEffectLabel, 8);
00102 place(0, 8, mItemDescLabel, 8);
00103 place(6, 9, mSellButton);
00104 place(7, 9, mQuitButton);
00105
00106 Layout &layout = getLayout();
00107 layout.setRowHeight(0, Layout::AUTO_SET);
00108
00109 center();
00110 loadWindowState();
00111 }
00112
00113 SellDialog::~SellDialog()
00114 {
00115 delete mShopItems;
00116 }
00117
00118 void SellDialog::reset()
00119 {
00120 mShopItems->clear();
00121 mSlider->setValue(0);
00122
00123
00124 mShopItemList->setSelected(-1);
00125
00126 updateButtonsAndLabels();
00127 }
00128
00129 void SellDialog::addItem(const Item *item, int price)
00130 {
00131 if (!item)
00132 return;
00133
00134 mShopItems->addItem(item->getInvIndex(), item->getId(),
00135 item->getQuantity(), price);
00136
00137 mShopItemList->adjustSize();
00138 }
00139
00140 void SellDialog::action(const gcn::ActionEvent &event)
00141 {
00142 if (event.getId() == "quit")
00143 {
00144 close();
00145 return;
00146 }
00147
00148 int selectedItem = mShopItemList->getSelected();
00149
00150
00151 if (selectedItem == -1 ||
00152 selectedItem >= (int) mShopItems->getNumberOfElements())
00153 {
00154 return;
00155 }
00156
00157 if (event.getId() == "slider")
00158 {
00159 mAmountItems = (int) mSlider->getValue();
00160 updateButtonsAndLabels();
00161 }
00162 else if (event.getId() == "+" && mAmountItems < mMaxItems)
00163 {
00164 mAmountItems++;
00165 mSlider->setValue(mAmountItems);
00166 updateButtonsAndLabels();
00167 }
00168 else if (event.getId() == "-" && mAmountItems > 1)
00169 {
00170 mAmountItems--;
00171 mSlider->setValue(mAmountItems);
00172 updateButtonsAndLabels();
00173 }
00174 else if (event.getId() == "max")
00175 {
00176 mAmountItems = mMaxItems;
00177 mSlider->setValue(mAmountItems);
00178 updateButtonsAndLabels();
00179 }
00180 else if (event.getId() == "sell" && mAmountItems > 0
00181 && mAmountItems <= mMaxItems)
00182 {
00183
00184 ShopItem *item = mShopItems->at(selectedItem);
00185 int sellCount;
00186 mPlayerMoney +=
00187 mAmountItems * mShopItems->at(selectedItem)->getPrice();
00188 mMaxItems -= mAmountItems;
00189 while (mAmountItems > 0) {
00190
00191
00192 sellCount = item->sellCurrentDuplicate(mAmountItems);
00193 mAmountItems -= sellCount;
00194 Net::getNpcHandler()->sellItem(current_npc, item->getCurrentInvIndex(), sellCount);
00195 }
00196
00197 mPlayerMoney +=
00198 mAmountItems * mShopItems->at(selectedItem)->getPrice();
00199 mAmountItems = 1;
00200 mSlider->setValue(0);
00201
00202 if (!mMaxItems)
00203 {
00204
00205 mShopItemList->setSelected(-1);
00206 delete mShopItems->at(selectedItem);
00207 mShopItems->erase(selectedItem);
00208
00209 gcn::Rectangle scroll;
00210 scroll.y = mShopItemList->getRowHeight() * (selectedItem + 1);
00211 scroll.height = mShopItemList->getRowHeight();
00212 mShopItemList->showPart(scroll);
00213 }
00214 else
00215 {
00216 mSlider->gcn::Slider::setScale(1, mMaxItems);
00217
00218
00219 updateButtonsAndLabels();
00220 }
00221 }
00222 }
00223
00224 void SellDialog::valueChanged(const gcn::SelectionEvent &event)
00225 {
00226
00227 mAmountItems = 1;
00228 mSlider->setValue(0);
00229
00230 updateButtonsAndLabels();
00231 mSlider->gcn::Slider::setScale(1, mMaxItems);
00232 }
00233
00234 void SellDialog::setMoney(int amount)
00235 {
00236 mPlayerMoney = amount;
00237 mShopItemList->setPlayersMoney(amount);
00238 }
00239
00240 void SellDialog::updateButtonsAndLabels()
00241 {
00242 int selectedItem = mShopItemList->getSelected();
00243 int income = 0;
00244
00245 if (selectedItem > -1)
00246 {
00247 const ItemInfo &info = mShopItems->at(selectedItem)->getInfo();
00248 mItemDescLabel->setCaption
00249 (strprintf(_("Description: %s"), info.getDescription().c_str()));
00250 mItemEffectLabel->setCaption
00251 (strprintf(_("Effect: %s"), info.getEffect().c_str()));
00252
00253 mMaxItems = mShopItems->at(selectedItem)->getQuantity();
00254 if (mAmountItems > mMaxItems)
00255 {
00256 mAmountItems = mMaxItems;
00257 }
00258
00259 income = mAmountItems * mShopItems->at(selectedItem)->getPrice();
00260 }
00261 else
00262 {
00263 mItemDescLabel->setCaption(strprintf(_("Description: %s"), ""));
00264 mItemEffectLabel->setCaption(strprintf(_("Effect: %s"), ""));
00265 mMaxItems = 0;
00266 mAmountItems = 0;
00267 }
00268
00269
00270 mSellButton->setEnabled(mAmountItems > 0);
00271 mDecreaseButton->setEnabled(mAmountItems > 1);
00272 mIncreaseButton->setEnabled(mAmountItems < mMaxItems);
00273 mSlider->setEnabled(mMaxItems > 1);
00274
00275
00276 mQuantityLabel->setCaption(strprintf("%d / %d", mAmountItems, mMaxItems));
00277 mMoneyLabel->setCaption(strprintf(_("Price: %s / Total: %s"),
00278 Units::formatCurrency(income).c_str(),
00279 Units::formatCurrency(mPlayerMoney + income).c_str()));
00280 }
00281
00282 void SellDialog::logic()
00283 {
00284 Window::logic();
00285
00286 if (!current_npc)
00287 setVisible(false);
00288 }
00289
00290 void SellDialog::setVisible(bool visible)
00291 {
00292 Window::setVisible(visible);
00293
00294 if (visible)
00295 requestFocus();
00296 }
00297
00298 void SellDialog::close()
00299 {
00300 setVisible(false);
00301 current_npc = 0;
00302 }