summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2015-05-03 20:09:53 +0300
committerAndrei Karas <akaras@inbox.ru>2015-05-03 20:09:53 +0300
commit602fd127d09c995bc5470218c862b2cebfc558d5 (patch)
tree6937760eb81a234c2aaf9c7494f6bce02176a2f5
parentfef6ebca9c454f99d44e950c4628270b124c445f (diff)
downloadmv-602fd127d09c995bc5470218c862b2cebfc558d5.tar.gz
mv-602fd127d09c995bc5470218c862b2cebfc558d5.tar.bz2
mv-602fd127d09c995bc5470218c862b2cebfc558d5.tar.xz
mv-602fd127d09c995bc5470218c862b2cebfc558d5.zip
Add filter into buy dialogs.
-rw-r--r--src/gui/models/shopitems.cpp18
-rw-r--r--src/gui/models/shopitems.h7
-rw-r--r--src/gui/windows/buydialog.cpp43
-rw-r--r--src/gui/windows/buydialog.h5
-rw-r--r--src/shopitem.cpp6
-rw-r--r--src/shopitem.h7
6 files changed, 83 insertions, 3 deletions
diff --git a/src/gui/models/shopitems.cpp b/src/gui/models/shopitems.cpp
index 1a9b89994..2fd399cb0 100644
--- a/src/gui/models/shopitems.cpp
+++ b/src/gui/models/shopitems.cpp
@@ -29,6 +29,7 @@
#include "debug.h"
ShopItems::ShopItems(const bool mergeDuplicates) :
+ mAllShopItems(),
mShopItems(),
mMergeDuplicates(mergeDuplicates)
{
@@ -58,6 +59,7 @@ ShopItem *ShopItems::addItem(const int id,
{
ShopItem *const item = new ShopItem(-1, id, type, color, amount, price);
mShopItems.push_back(item);
+ mAllShopItems.push_back(item);
return item;
}
@@ -72,6 +74,7 @@ ShopItem *ShopItems::addItemNoDup(const int id,
{
item = new ShopItem(-1, id, type, color, amount, price);
mShopItems.push_back(item);
+ mAllShopItems.push_back(item);
}
return item;
}
@@ -95,6 +98,7 @@ ShopItem *ShopItems::addItem2(const int inventoryIndex,
{
item = new ShopItem(inventoryIndex, id, type, color, quantity, price);
mShopItems.push_back(item);
+ mAllShopItems.push_back(item);
}
return item;
}
@@ -127,7 +131,8 @@ void ShopItems::del(const unsigned int i)
void ShopItems::clear()
{
- delete_all(mShopItems);
+ delete_all(mAllShopItems);
+ mAllShopItems.clear();
mShopItems.clear();
}
@@ -146,3 +151,14 @@ ShopItem *ShopItems::findItem(const int id, const unsigned char color) const
return nullptr;
}
+
+void ShopItems::updateList()
+{
+ mShopItems.clear();
+ FOR_EACH (std::vector<ShopItem*>::iterator, it, mAllShopItems)
+ {
+ ShopItem *const item = *it;
+ if (item && item->isVisible())
+ mShopItems.push_back(item);
+ }
+}
diff --git a/src/gui/models/shopitems.h b/src/gui/models/shopitems.h
index 880bc4f71..50269cff1 100644
--- a/src/gui/models/shopitems.h
+++ b/src/gui/models/shopitems.h
@@ -129,9 +129,14 @@ class ShopItems final : public ListModel
std::vector<ShopItem*> &items() A_WARN_UNUSED
{ return mShopItems; }
+ std::vector<ShopItem*> &allItems() A_WARN_UNUSED
+ { return mAllShopItems; }
+
void setMergeDuplicates(const bool b)
{ mMergeDuplicates = b; }
+ void updateList();
+
private:
/**
* Searches the current items in the shop for the specified
@@ -143,6 +148,8 @@ class ShopItems final : public ListModel
const unsigned char color) const A_WARN_UNUSED;
/** The list of items in the shop. */
+ std::vector<ShopItem*> mAllShopItems;
+
std::vector<ShopItem*> mShopItems;
/** Look for duplicate entries on addition. */
diff --git a/src/gui/windows/buydialog.cpp b/src/gui/windows/buydialog.cpp
index 2fbe58ed2..7f5c79cf5 100644
--- a/src/gui/windows/buydialog.cpp
+++ b/src/gui/windows/buydialog.cpp
@@ -44,6 +44,7 @@
#include "gui/widgets/scrollarea.h"
#include "gui/widgets/shoplistbox.h"
#include "gui/widgets/slider.h"
+#include "gui/widgets/textfield.h"
#include "net/adminhandler.h"
#include "net/buysellhandler.h"
@@ -177,6 +178,8 @@ BuyDialog::BuyDialog() :
SelectionListener(),
mSortModel(nullptr),
mSortDropDown(nullptr),
+ mFilterTextField(new TextField(this, "", true, this, "namefilter", true)),
+ mFilterLabel(nullptr),
mNpcId(Items),
mMoney(0),
mAmountItems(0),
@@ -193,6 +196,8 @@ BuyDialog::BuyDialog(const int npcId) :
SelectionListener(),
mSortModel(nullptr),
mSortDropDown(nullptr),
+ mFilterTextField(new TextField(this, "", true, this, "namefilter", true)),
+ mFilterLabel(nullptr),
mNpcId(npcId),
mMoney(0),
mAmountItems(0),
@@ -209,6 +214,8 @@ BuyDialog::BuyDialog(std::string nick) :
SelectionListener(),
mSortModel(new SortListModelBuy),
mSortDropDown(new DropDown(this, mSortModel, false, false, this, "sort")),
+ mFilterTextField(new TextField(this, "", true, this, "namefilter", true)),
+ mFilterLabel(nullptr),
mNpcId(Nick),
mMoney(0),
mAmountItems(0),
@@ -287,6 +294,8 @@ void BuyDialog::init()
mShopItemList->addActionListener(this);
mShopItemList->addSelectionListener(this);
+ mFilterTextField->setWidth(100);
+
ContainerPlacer placer = getPlacer(0, 0);
placer(0, 0, mScrollArea, 9, 5).setPadding(3);
placer(0, 5, mDecreaseButton);
@@ -298,7 +307,17 @@ void BuyDialog::init()
placer(2, 6, mAmountField, 2);
placer(0, 7, mMoneyLabel, 8);
if (mSortDropDown)
+ {
placer(0, 8, mSortDropDown, 2);
+ }
+ else
+ {
+ // TRANSLATORS: buy dialog label
+ mFilterLabel = new Label(this, _("Filter:"));
+ mFilterLabel->adjustSize();
+ placer(0, 8, mFilterLabel, 2);
+ }
+ placer(2, 8, mFilterTextField, 2);
placer(7, 8, mBuyButton);
placer(8, 8, mQuitButton);
@@ -422,6 +441,10 @@ void BuyDialog::action(const ActionEvent &event)
config.setValue("buySortOrder", mSortDropDown->getSelected());
return;
}
+ else if (eventId == "namefilter")
+ {
+ applyNameFilter(mFilterTextField->getText());
+ }
const int selectedItem = mShopItemList->getSelected();
@@ -624,3 +647,23 @@ void BuyDialog::closeAll()
(*it)->close();
}
}
+
+void BuyDialog::applyNameFilter(const std::string &filter)
+{
+ std::vector<ShopItem*> &items = mShopItems->allItems();
+ std::string filterStr = filter;
+ toLower(filterStr);
+ FOR_EACH (std::vector<ShopItem*>::iterator, it, items)
+ {
+ ShopItem *const item = *it;
+ if (!item)
+ continue;
+ std::string name = item->getName();
+ toLower(name);
+ if (name.find(filterStr) != std::string::npos)
+ item->setVisible(true);
+ else
+ item->setVisible(false);
+ }
+ mShopItems->updateList();
+}
diff --git a/src/gui/windows/buydialog.h b/src/gui/windows/buydialog.h
index a96e0ff28..504a63154 100644
--- a/src/gui/windows/buydialog.h
+++ b/src/gui/windows/buydialog.h
@@ -38,6 +38,7 @@ class IntTextField;
class Label;
class ScrollArea;
class Slider;
+class TextField;
/**
* The buy dialog.
@@ -149,6 +150,8 @@ class BuyDialog final : public Window,
private:
void updateSlider(const int selectedItem);
+ void applyNameFilter(const std::string &filter);
+
typedef std::list<BuyDialog*> DialogList;
static DialogList instances;
@@ -167,6 +170,8 @@ class BuyDialog final : public Window,
ShopItems *mShopItems;
SortListModelBuy *mSortModel;
DropDown *mSortDropDown;
+ TextField *mFilterTextField;
+ Label *mFilterLabel;
int mNpcId;
int mMoney;
diff --git a/src/shopitem.cpp b/src/shopitem.cpp
index 2b9923eb7..3fa7c6480 100644
--- a/src/shopitem.cpp
+++ b/src/shopitem.cpp
@@ -47,7 +47,8 @@ ShopItem::ShopItem(const int inventoryIndex,
mDisplayName(),
mDuplicates(),
mPrice(price),
- mShowQuantity(true)
+ mShowQuantity(true),
+ mVisible(true)
{
updateDisplayName(quantity);
setInvIndex(inventoryIndex);
@@ -67,7 +68,8 @@ ShopItem::ShopItem(const int id,
mDisplayName(),
mDuplicates(),
mPrice(price),
- mShowQuantity(false)
+ mShowQuantity(false),
+ mVisible(true)
{
updateDisplayName(0);
setInvIndex(-1);
diff --git a/src/shopitem.h b/src/shopitem.h
index 9b85c8781..68bdc51b5 100644
--- a/src/shopitem.h
+++ b/src/shopitem.h
@@ -137,6 +137,12 @@ class ShopItem final : public Item
const std::string &getDisplayName() const A_WARN_UNUSED
{ return mDisplayName; }
+ void setVisible(const bool b)
+ { mVisible = b; }
+
+ bool isVisible() const
+ { return mVisible; }
+
protected:
void updateDisplayName(const int quantity);
@@ -153,6 +159,7 @@ class ShopItem final : public Item
std::stack<DuplicateItem*> mDuplicates; /** <-- Stores duplicates */
int mPrice;
bool mShowQuantity;
+ bool mVisible;
};
#endif // SHOPITEM_H