diff options
Diffstat (limited to 'src/gui/inventorywindow.cpp')
-rw-r--r-- | src/gui/inventorywindow.cpp | 168 |
1 files changed, 129 insertions, 39 deletions
diff --git a/src/gui/inventorywindow.cpp b/src/gui/inventorywindow.cpp index 16ac5409..0dbeb352 100644 --- a/src/gui/inventorywindow.cpp +++ b/src/gui/inventorywindow.cpp @@ -21,16 +21,16 @@ #include "gui/inventorywindow.h" +#include "client.h" #include "inventory.h" #include "item.h" -#include "localplayer.h" #include "units.h" #include "keyboardconfig.h" +#include "playerinfo.h" #include "gui/itemamount.h" #include "gui/setup.h" #include "gui/sdlinput.h" -#include "gui/theme.h" #include "gui/viewport.h" #include "gui/widgets/button.h" @@ -44,6 +44,7 @@ #include "net/net.h" #include "resources/iteminfo.h" +#include "resources/theme.h" #include "utils/gettext.h" #include "utils/stringutils.h" @@ -58,8 +59,11 @@ InventoryWindow::WindowList InventoryWindow::instances; InventoryWindow::InventoryWindow(Inventory *inventory): Window(inventory->isMainInventory() ? _("Inventory") : _("Storage")), mInventory(inventory), + mFilterText(new TextField), mSplit(false) { + listen(Event::AttributesChannel); + setWindowName(isMainInventory() ? "Inventory" : "Storage"); setupWindow->registerWindowForReset(this); setResizable(true); @@ -71,6 +75,9 @@ InventoryWindow::InventoryWindow(Inventory *inventory): setMinHeight(179); addKeyListener(this); + mFilterText->setWidth(150); + mFilterText->addKeyListener(this); + mItems = new ItemContainer(mInventory); mItems->addSelectionListener(this); @@ -78,6 +85,7 @@ InventoryWindow::InventoryWindow(Inventory *inventory): invenScroll->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER); mSlotsLabel = new Label(_("Slots:")); + mFilterLabel = new Label(_("Search:")); mSlotsBar = new ProgressBar(0.0f, 100, 20, Theme::PROG_INVY_SLOTS); if (isMainInventory()) @@ -95,7 +103,8 @@ InventoryWindow::InventoryWindow(Inventory *inventory): longestUseString = unequip; } - mUseButton = new Button(longestUseString, "use", this); + mEquipButton = new Button(_("Equip"), "equip", this); + mUseButton = new Button(_("Activate"), "activate", this); mDropButton = new Button(_("Drop..."), "drop", this); mSplitButton = new Button(_("Split"), "split", this); mOutfitButton = new Button(_("Outfits"), "outfit", this); @@ -106,12 +115,15 @@ InventoryWindow::InventoryWindow(Inventory *inventory): place(0, 0, mWeightLabel).setPadding(3); place(1, 0, mWeightBar, 3); place(4, 0, mSlotsLabel).setPadding(3); - place(5, 0, mSlotsBar, 2); - place(0, 1, invenScroll, 7).setPadding(3); - place(0, 2, mUseButton); - place(1, 2, mDropButton); - place(2, 2, mSplitButton); - place(6, 2, mOutfitButton); + place(5, 0, mSlotsBar, 3); + place(0, 1, mFilterLabel, 4); + place(1, 1, mFilterText, 4); + place(0, 2, invenScroll, 8).setPadding(3); + place(0, 3, mUseButton); + place(1, 3, mEquipButton); + place(2, 3, mDropButton); + place(3, 3, mSplitButton); + place(7, 3, mOutfitButton); updateWeight(); } @@ -122,15 +134,17 @@ InventoryWindow::InventoryWindow(Inventory *inventory): place(0, 0, mSlotsLabel).setPadding(3); place(1, 0, mSlotsBar, 3); - place(0, 1, invenScroll, 4, 4); - place(0, 5, mStoreButton); - place(1, 5, mRetrieveButton); + place(0, 1, mFilterLabel).setPadding(3); + place(1, 1, mFilterText, 3); + place(0, 2, invenScroll, 4, 4); + place(0, 6, mStoreButton); + place(1, 6, mRetrieveButton); } Layout &layout = getLayout(); - layout.setRowHeight(1, Layout::AUTO_SET); + layout.setRowHeight(2, Layout::AUTO_SET); - mInventory->addInventoyListener(this); + mInventory->addInventoryListener(this); instances.push_back(this); @@ -138,13 +152,19 @@ InventoryWindow::InventoryWindow(Inventory *inventory): slotsChanged(mInventory); if (!isMainInventory()) + { setVisible(true); + PlayerInfo::setStorageCount(PlayerInfo::getStorageCount() + 1); + } } InventoryWindow::~InventoryWindow() { instances.remove(this); - mInventory->removeInventoyListener(this); + mInventory->removeInventoryListener(this); + + if (!isMainInventory()) + PlayerInfo::setStorageCount(PlayerInfo::getStorageCount() - 1); } void InventoryWindow::action(const gcn::ActionEvent &event) @@ -175,17 +195,21 @@ void InventoryWindow::action(const gcn::ActionEvent &event) if (!item) return; - if (event.getId() == "use") + if (event.getId() == "activate") + item->doEvent(Event::DoUse); + else if (event.getId() == "equip") { - if (item->isEquipment()) + if (item->isEquippable()) { if (item->isEquipped()) - Net::getInventoryHandler()->unequipItem(item); + item->doEvent(Event::DoUnequip); else - Net::getInventoryHandler()->equipItem(item); + item->doEvent(Event::DoEquip); } else - Net::getInventoryHandler()->useItem(item); + { + item->doEvent(Event::DoUse); + } } else if (event.getId() == "drop") { @@ -217,10 +241,17 @@ void InventoryWindow::mouseClicked(gcn::MouseEvent &event) { Window::mouseClicked(event); - if (event.getButton() == gcn::MouseEvent::RIGHT) + Item *item = mItems->getSelectedItem(); + + if (event.getSource() == mItems && item && isDoubleClick(item->getInvIndex())) { - Item *item = mItems->getSelectedItem(); + if (isMainInventory() && item->getInfo().getActivatable()) + action(gcn::ActionEvent(mUseButton, mUseButton->getActionEventId())); + } + + if (event.getButton() == gcn::MouseEvent::RIGHT) + { if (!item) return; @@ -234,20 +265,30 @@ void InventoryWindow::mouseClicked(gcn::MouseEvent &event) if (event.getButton() == gcn::MouseEvent::LEFT) { - if (isStorageActive() && keyboard.isKeyActive(keyboard.KEY_EMOTE)) + if (instances.size() > 1 && keyboard.isKeyActive(keyboard.KEY_EMOTE)) { Item *item = mItems->getSelectedItem(); if(!item) return; if (mInventory->isMainInventory()) - Net::getInventoryHandler()->moveItem(Inventory::INVENTORY, - item->getInvIndex(), item->getQuantity(), - Inventory::STORAGE); + { + Event event(Event::DoMove); + event.setItem("item", item); + event.setInt("amount", item->getQuantity()); + event.setInt("source", Inventory::INVENTORY); + event.setInt("destination", Inventory::STORAGE); + event.trigger(Event::ItemChannel); + } else - Net::getInventoryHandler()->moveItem(Inventory::STORAGE, - item->getInvIndex(), item->getQuantity(), - Inventory::INVENTORY); + { + Event event(Event::DoMove); + event.setItem("item", item); + event.setInt("amount", item->getQuantity()); + event.setInt("source", Inventory::STORAGE); + event.setInt("destination", Inventory::INVENTORY); + event.trigger(Event::ItemChannel); + } } } } @@ -265,6 +306,12 @@ void InventoryWindow::keyPressed(gcn::KeyEvent &event) void InventoryWindow::keyReleased(gcn::KeyEvent &event) { + if (isInputFocused()) + { + mItems->setFilter(mFilterText->getText()); + return; + } + switch (event.getKey().getValue()) { case Key::LEFT_SHIFT: @@ -298,26 +345,27 @@ void InventoryWindow::updateButtons() if (!item || item->getQuantity() == 0) { mUseButton->setEnabled(false); + mEquipButton->setEnabled(false); mDropButton->setEnabled(false); mSplitButton->setEnabled(false); return; } - mUseButton->setEnabled(true); mDropButton->setEnabled(true); - if (item->isEquipment()) + if (item->getInfo().getEquippable()) { if (item->isEquipped()) - mUseButton->setCaption(_("Unequip")); + mEquipButton->setCaption(_("Unequip")); else - mUseButton->setCaption(_("Equip")); + mEquipButton->setCaption(_("Equip")); + mEquipButton->setEnabled(true); } else - { - mUseButton->setCaption(_("Use")); - } + mEquipButton->setEnabled(false); + + mUseButton->setEnabled(item->getInfo().getActivatable()); if (item->getQuantity() > 1) mDropButton->setCaption(_("Drop...")); @@ -343,15 +391,36 @@ void InventoryWindow::close() } else { - Net::getInventoryHandler()->closeStorage(Inventory::STORAGE); + Event event(Event::DoCloseInventory); + event.setInt("type", mInventory->getType()); + event.trigger(Event::ItemChannel); scheduleDelete(); } } +void InventoryWindow::event(Event::Channel channel, const Event &event) +{ + if (event.getType() == Event::UpdateAttribute) + { + int id = event.getInt("id"); + if (id == TOTAL_WEIGHT || + id == MAX_WEIGHT) + { + updateWeight(); + } + } +} + void InventoryWindow::updateWeight() { - int total = player_node->getTotalWeight(); - int max = player_node->getMaxWeight(); + if (!isMainInventory()) + return; + + int total = PlayerInfo::getAttribute(TOTAL_WEIGHT); + int max = PlayerInfo::getAttribute(MAX_WEIGHT); + + if (max <= 0) + return; // Adjust progress bar mWeightBar->setProgress((float) total / max); @@ -359,6 +428,27 @@ void InventoryWindow::updateWeight() Units::formatWeight(max).c_str())); } +bool InventoryWindow::isInputFocused() const +{ + return mFilterText->isFocused(); +} + +bool InventoryWindow::isAnyInputFocused() +{ + WindowList::iterator it = instances.begin(); + WindowList::iterator it_end = instances.end(); + + for (; it != it_end; it++) + { + if ((*it)->isInputFocused()) + { + return true; + } + } + + return false; +} + void InventoryWindow::slotsChanged(Inventory* inventory) { if (inventory == mInventory) |