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