00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "gui/inventorywindow.h"
00023
00024 #include "gui/item_amount.h"
00025 #include "gui/itemcontainer.h"
00026 #include "gui/sdlinput.h"
00027 #include "gui/viewport.h"
00028
00029 #include "gui/widgets/button.h"
00030 #include "gui/widgets/label.h"
00031 #include "gui/widgets/layout.h"
00032 #include "gui/widgets/progressbar.h"
00033 #include "gui/widgets/scrollarea.h"
00034
00035 #include "inventory.h"
00036 #include "item.h"
00037 #include "localplayer.h"
00038 #include "units.h"
00039
00040 #include "resources/iteminfo.h"
00041
00042 #include "utils/gettext.h"
00043 #include "utils/strprintf.h"
00044
00045 #include <guichan/font.hpp>
00046 #include <guichan/mouseinput.hpp>
00047
00048 #include <string>
00049
00050 InventoryWindow::InventoryWindow(int invSize):
00051 Window(_("Inventory")),
00052 mMaxSlots(invSize),
00053 mSplit(false),
00054 mItemDesc(false)
00055 {
00056 setWindowName("Inventory");
00057 setResizable(true);
00058 setCloseButton(true);
00059 setSaveVisible(true);
00060
00061 setDefaultSize(387, 307, ImageRect::CENTER);
00062 setMinWidth(316);
00063 setMinHeight(179);
00064 addKeyListener(this);
00065
00066 std::string longestUseString = getFont()->getWidth(_("Equip")) >
00067 getFont()->getWidth(_("Use")) ?
00068 _("Equip") : _("Use");
00069
00070 if (getFont()->getWidth(longestUseString) <
00071 getFont()->getWidth(_("Unequip")))
00072 {
00073 longestUseString = _("Unequip");
00074 }
00075
00076 mUseButton = new Button(longestUseString, "use", this);
00077 mDropButton = new Button(_("Drop"), "drop", this);
00078 #ifdef TMWSERV_SUPPORT
00079 mSplitButton = new Button(_("Split"), "split", this);
00080 mItems = new ItemContainer(player_node->getInventory());
00081 #else
00082 mItems = new ItemContainer(player_node->getInventory());
00083 #endif
00084 mItems->addSelectionListener(this);
00085
00086 gcn::ScrollArea *invenScroll = new ScrollArea(mItems);
00087 invenScroll->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER);
00088
00089 mTotalWeight = -1;
00090 mMaxWeight = -1;
00091 mUsedSlots = -1;
00092
00093 mSlotsLabel = new Label(_("Slots:"));
00094 mWeightLabel = new Label(_("Weight:"));
00095
00096 mSlotsBar = new ProgressBar(1.0f, 100, 20, 225, 200, 25);
00097 mWeightBar = new ProgressBar(1.0f, 100, 20, 0, 0, 255);
00098
00099 place(0, 0, mWeightLabel).setPadding(3);
00100 place(1, 0, mWeightBar, 3);
00101 place(4, 0, mSlotsLabel).setPadding(3);
00102 place(5, 0, mSlotsBar, 2);
00103 place(0, 1, invenScroll, 7).setPadding(3);
00104 place(0, 2, mUseButton);
00105 place(1, 2, mDropButton);
00106 #ifdef TMWSERV_SUPPORT
00107 place(2, 2, mSplitButton);
00108 #endif
00109
00110 Layout &layout = getLayout();
00111 layout.setRowHeight(1, Layout::AUTO_SET);
00112
00113 loadWindowState();
00114 }
00115
00116 InventoryWindow::~InventoryWindow()
00117 {
00118 }
00119
00120 void InventoryWindow::logic()
00121 {
00122 if (!isVisible())
00123 return;
00124
00125 Window::logic();
00126
00127
00128
00129 updateButtons();
00130
00131 const int usedSlots = player_node->getInventory()->getNumberOfSlotsUsed();
00132
00133 if (mMaxWeight != player_node->getMaxWeight() ||
00134 mTotalWeight != player_node->getTotalWeight() ||
00135 mUsedSlots != usedSlots)
00136 {
00137 mTotalWeight = player_node->getTotalWeight();
00138 mMaxWeight = player_node->getMaxWeight();
00139 mUsedSlots = usedSlots;
00140
00141
00142 if (mTotalWeight < (mMaxWeight / 3))
00143 mWeightBar->setColor(0, 0, 255);
00144 else if (mTotalWeight < ((mMaxWeight * 2) / 3))
00145 mWeightBar->setColor(255, 255, 0);
00146 else
00147 mWeightBar->setColor(255, 0, 0);
00148
00149
00150 mSlotsBar->setProgress((float) mUsedSlots / mMaxSlots);
00151 mWeightBar->setProgress((float) mTotalWeight / mMaxWeight);
00152
00153 mSlotsBar->setText(strprintf("%d/%d", mUsedSlots, mMaxSlots));
00154 mWeightBar->setText(strprintf("%s/%s",
00155 Units::formatWeight(mTotalWeight).c_str(),
00156 Units::formatWeight(mMaxWeight).c_str()));
00157 }
00158 }
00159
00160 void InventoryWindow::action(const gcn::ActionEvent &event)
00161 {
00162 Item *item = mItems->getSelectedItem();
00163
00164 if (!item)
00165 return;
00166
00167 if (event.getId() == "use")
00168 {
00169 if (item->isEquipment()) {
00170 #ifdef TMWSERV_SUPPORT
00171 player_node->equipItem(item);
00172 #else
00173 if (item->isEquipped())
00174 player_node->unequipItem(item);
00175 else
00176 player_node->equipItem(item);
00177 #endif
00178 }
00179 else
00180 player_node->useItem(item);
00181 }
00182 else if (event.getId() == "drop")
00183 {
00184 if (item->getQuantity() > 1) {
00185
00186 new ItemAmountWindow(ItemAmountWindow::ItemDrop, this, item);
00187 }
00188 else {
00189 player_node->dropItem(item, 1);
00190 }
00191 }
00192 else if (event.getId() == "split")
00193 {
00194 if (item && !item->isEquipment() && item->getQuantity() > 1) {
00195 new ItemAmountWindow(ItemAmountWindow::ItemSplit, this, item,
00196 (item->getQuantity() - 1));
00197 }
00198 }
00199 }
00200
00201 Item* InventoryWindow::getSelectedItem() const
00202 {
00203 return mItems->getSelectedItem();
00204 }
00205
00206 void InventoryWindow::mouseClicked(gcn::MouseEvent &event)
00207 {
00208 Window::mouseClicked(event);
00209
00210 if (event.getButton() == gcn::MouseEvent::RIGHT)
00211 {
00212 Item *item = mItems->getSelectedItem();
00213
00214 if (!item)
00215 return;
00216
00217
00218
00219
00220 const int mx = event.getX() + getX();
00221 const int my = event.getY() + getY();
00222 viewport->showPopup(mx, my, item);
00223 }
00224 }
00225
00226 #ifdef TMWSERV_SUPPORT
00227 void InventoryWindow::keyPressed(gcn::KeyEvent &event)
00228 {
00229 switch (event.getKey().getValue())
00230 {
00231 case Key::LEFT_SHIFT:
00232 case Key::RIGHT_SHIFT:
00233 mSplit = true;
00234 break;
00235 }
00236 }
00237
00238 void InventoryWindow::keyReleased(gcn::KeyEvent &event)
00239 {
00240 switch (event.getKey().getValue())
00241 {
00242 case Key::LEFT_SHIFT:
00243 case Key::RIGHT_SHIFT:
00244 mSplit = false;
00245 break;
00246 }
00247 }
00248 #endif
00249
00250 void InventoryWindow::valueChanged(const gcn::SelectionEvent &event)
00251 {
00252 if (mSplit)
00253 {
00254 Item *item = mItems->getSelectedItem();
00255
00256 if (item && !item->isEquipment() && item->getQuantity() > 1)
00257 {
00258 mSplit = false;
00259 new ItemAmountWindow(ItemAmountWindow::ItemSplit, this, item,
00260 (item->getQuantity() - 1));
00261 }
00262 }
00263 }
00264
00265 void InventoryWindow::updateButtons()
00266 {
00267 const Item *selectedItem = mItems->getSelectedItem();
00268
00269 if (selectedItem && selectedItem->isEquipment())
00270 {
00271 #ifdef EATHENA_SUPPORT
00272 if (selectedItem->isEquipped())
00273 mUseButton->setCaption(_("Unequip"));
00274 else
00275 #endif
00276 mUseButton->setCaption(_("Equip"));
00277 }
00278 else
00279 mUseButton->setCaption(_("Use"));
00280
00281 mUseButton->setEnabled(selectedItem != 0);
00282 mDropButton->setEnabled(selectedItem != 0);
00283
00284 #ifdef TMWSERV_SUPPORT
00285 if (selectedItem && !selectedItem->isEquipment() &&
00286 selectedItem->getQuantity() > 1)
00287 {
00288 mSplitButton->setEnabled(true);
00289 }
00290 else {
00291 mSplitButton->setEnabled(false);
00292 }
00293 #endif
00294 }