diff options
author | Blue Sans Douze <bluesansdouze@gmail.com> | 2011-03-26 13:44:08 +0100 |
---|---|---|
committer | Blue Sans Douze <bluesansdouze@gmail.com> | 2011-03-26 16:59:35 +0100 |
commit | c176ecada07af6235d6b65b839018e4e0bf07298 (patch) | |
tree | d49fdaf009c52e163b783c22b2105cf794ece068 | |
parent | e35b3bdd0a74464e4b4d3b931ca416f0c7864ba6 (diff) | |
download | mana-c176ecada07af6235d6b65b839018e4e0bf07298.tar.gz mana-c176ecada07af6235d6b65b839018e4e0bf07298.tar.bz2 mana-c176ecada07af6235d6b65b839018e4e0bf07298.tar.xz mana-c176ecada07af6235d6b65b839018e4e0bf07298.zip |
Add search bar to storage and inventory windows
Reviewed-by: thorbjorn
-rw-r--r-- | src/game.cpp | 2 | ||||
-rw-r--r-- | src/gui/inventorywindow.cpp | 58 | ||||
-rw-r--r-- | src/gui/inventorywindow.h | 9 | ||||
-rw-r--r-- | src/gui/widgets/itemcontainer.cpp | 48 | ||||
-rw-r--r-- | src/gui/widgets/itemcontainer.h | 13 |
5 files changed, 112 insertions, 18 deletions
diff --git a/src/game.cpp b/src/game.cpp index fd453434..18e72b6d 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -604,7 +604,7 @@ void Game::handleInput() break; } if (keyboard.isEnabled() && !chatWindow->isInputFocused() && - !NpcDialog::isAnyInputFocused()) + !NpcDialog::isAnyInputFocused() && !InventoryWindow::isAnyInputFocused()) { const int tKey = keyboard.getKeyIndex(event.key.keysym.sym); diff --git a/src/gui/inventorywindow.cpp b/src/gui/inventorywindow.cpp index 31743c57..02801ced 100644 --- a/src/gui/inventorywindow.cpp +++ b/src/gui/inventorywindow.cpp @@ -58,6 +58,7 @@ InventoryWindow::WindowList InventoryWindow::instances; InventoryWindow::InventoryWindow(Inventory *inventory): Window(inventory->isMainInventory() ? _("Inventory") : _("Storage")), mInventory(inventory), + mFilterText(new TextField), mSplit(false) { listen(CHANNEL_ATTRIBUTES); @@ -73,6 +74,9 @@ InventoryWindow::InventoryWindow(Inventory *inventory): setMinHeight(179); addKeyListener(this); + mFilterText->setWidth(150); + mFilterText->addKeyListener(this); + mItems = new ItemContainer(mInventory); mItems->addSelectionListener(this); @@ -80,6 +84,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()) @@ -109,13 +114,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, mEquipButton); - place(2, 2, mDropButton); - place(3, 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(); } @@ -126,13 +133,15 @@ 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); @@ -289,6 +298,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: @@ -405,6 +420,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) diff --git a/src/gui/inventorywindow.h b/src/gui/inventorywindow.h index 0ddd13f7..2ea59001 100644 --- a/src/gui/inventorywindow.h +++ b/src/gui/inventorywindow.h @@ -26,6 +26,7 @@ #include "listener.h" #include "gui/widgets/window.h" +#include "gui/widgets/textfield.h" #include "net/inventoryhandler.h" @@ -107,6 +108,10 @@ class InventoryWindow : public Window, */ void updateButtons(); + bool isInputFocused() const; + + static bool isAnyInputFocused(); + void slotsChanged(Inventory* inventory); bool isMainInventory() { return mInventory->isMainInventory(); } @@ -126,12 +131,14 @@ class InventoryWindow : public Window, Inventory *mInventory; ItemContainer *mItems; + TextField *mFilterText; + std::string mWeight, mSlots; gcn::Button *mUseButton, *mEquipButton, *mDropButton, *mSplitButton, *mOutfitButton, *mStoreButton, *mRetrieveButton; - gcn::Label *mWeightLabel, *mSlotsLabel; + gcn::Label *mWeightLabel, *mSlotsLabel, *mFilterLabel; ProgressBar *mWeightBar, *mSlotsBar; diff --git a/src/gui/widgets/itemcontainer.cpp b/src/gui/widgets/itemcontainer.cpp index 2e9b698d..15fcbfe2 100644 --- a/src/gui/widgets/itemcontainer.cpp +++ b/src/gui/widgets/itemcontainer.cpp @@ -101,14 +101,38 @@ void ItemContainer::draw(gcn::Graphics *graphics) g->setFont(getFont()); + mFilteredMap.clear(); + int currentIndex = 0; + //Filter checking + for (int i = 0; i < mGridColumns; i++) + { + for (int j = 0; j < mGridRows; j++) + { + int itemIndex = j * mGridColumns + i; + Item *item = mInventory->getItem(itemIndex); + if (!item || item->getId() == 0) + continue; + + if (mFilter.size() > 0) + { + if (normalize(item->getInfo().getName()).find(mFilter) == std::string::npos) + continue; + } + + mFilteredMap[currentIndex] = item; + currentIndex++; + } + } + for (int i = 0; i < mGridColumns; i++) { for (int j = 0; j < mGridRows; j++) { int itemX = i * BOX_WIDTH; int itemY = j * BOX_HEIGHT; - int itemIndex = (j * mGridColumns) + i; - Item *item = mInventory->getItem(itemIndex); + int itemIndex = j * mGridColumns + i; + + Item *item = getItemAt(itemIndex); if (!item || item->getId() == 0) continue; @@ -178,7 +202,18 @@ void ItemContainer::setSelectedIndex(int newIndex) Item *ItemContainer::getSelectedItem() const { - return mInventory->getItem(mSelectedIndex); + return getItemAt(mSelectedIndex); +} + +Item *ItemContainer::getItemAt(int index) const +{ + std::map<int, Item*>::const_iterator i = mFilteredMap.find(index); + return i == mFilteredMap.end() ? 0 : i->second; +} + +void ItemContainer::setFilter(const std::string &filter) +{ + mFilter = normalize(filter); } void ItemContainer::distributeValueChangedEvent() @@ -245,7 +280,10 @@ void ItemContainer::mousePressed(gcn::MouseEvent &event) if (index == Inventory::NO_SLOT_INDEX) return; - Item *item = mInventory->getItem(index); + Item *item = getItemAt(index); + + if (!item) + return; // put item name into chat window if (mDescItems) @@ -320,7 +358,7 @@ void ItemContainer::mouseReleased(gcn::MouseEvent &event) // Show ItemTooltip void ItemContainer::mouseMoved(gcn::MouseEvent &event) { - Item *item = mInventory->getItem(getSlotIndex(event.getX(), event.getY())); + Item *item = getItemAt(getSlotIndex(event.getX(), event.getY())); if (item) { diff --git a/src/gui/widgets/itemcontainer.h b/src/gui/widgets/itemcontainer.h index 38ccf9ca..89012a37 100644 --- a/src/gui/widgets/itemcontainer.h +++ b/src/gui/widgets/itemcontainer.h @@ -27,6 +27,7 @@ #include <guichan/widget.hpp> #include <guichan/widgetlistener.hpp> +#include <map> #include <list> class Image; @@ -98,6 +99,12 @@ class ItemContainer : public gcn::Widget, */ void selectNone(); + + /** + * Sets item filter + */ + void setFilter(const std::string &filter); + /** * Adds a listener to the list that's notified each time a change to * the selection occurs. @@ -170,6 +177,8 @@ class ItemContainer : public gcn::Widget, */ int getSlotIndex(int x, int y) const; + Item *getItemAt(int) const; + Inventory *mInventory; int mGridColumns, mGridRows; Image *mSelImg; @@ -181,6 +190,10 @@ class ItemContainer : public gcn::Widget, bool mDescItems; int mDragPosX, mDragPosY; + std::map<int, Item*> mFilteredMap; + + std::string mFilter; + ItemPopup *mItemPopup; typedef std::list<gcn::SelectionListener*> SelectionListenerList; |