From f1c17766c66394ce875854430f8f028c0fa5b3a5 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Thu, 8 Sep 2011 03:15:52 +0300 Subject: Add additional sorting methods to inventory: by weight, by amount, by type. --- src/gui/inventorywindow.cpp | 53 ++++++++++++++++++++++++------- src/gui/inventorywindow.h | 5 ++- src/gui/widgets/dropdown.cpp | 9 +++++- src/gui/widgets/dropdown.h | 4 ++- src/gui/widgets/itemcontainer.cpp | 67 ++++++++++++++++++++++++++++++++++++++- 5 files changed, 123 insertions(+), 15 deletions(-) (limited to 'src/gui') diff --git a/src/gui/inventorywindow.cpp b/src/gui/inventorywindow.cpp index d0454c4e1..a8c003019 100644 --- a/src/gui/inventorywindow.cpp +++ b/src/gui/inventorywindow.cpp @@ -38,6 +38,7 @@ #include "gui/widgets/button.h" #include "gui/widgets/container.h" +#include "gui/widgets/dropdown.h" #include "gui/widgets/inventoryfilter.h" #include "gui/widgets/itemcontainer.h" #include "gui/widgets/label.h" @@ -62,6 +63,34 @@ #include "debug.h" +const char *SORT_NAME[6] = +{ + N_("default"), + N_("by name"), + N_("by id"), + N_("by weight"), + N_("by amount"), + N_("by type") +}; + +class SortListModel : public gcn::ListModel +{ +public: + virtual ~SortListModel() + { } + + virtual int getNumberOfElements() + { return 6; } + + virtual std::string getElementAt(int i) + { + if (i >= getNumberOfElements() || i < 0) + return _("???"); + + return gettext(SORT_NAME[i]); + } +}; + InventoryWindow::WindowList InventoryWindow::instances; InventoryWindow::InventoryWindow(Inventory *inventory): @@ -109,9 +138,9 @@ InventoryWindow::InventoryWindow(Inventory *inventory): mFilter->addActionListener(this); mFilter->setActionEventId("tag_"); - mSorter = new InventoryFilter("sorter_" + getWindowName(), 20, 0); - mSorter->addActionListener(this); - mSorter->setActionEventId("sort_"); + mSortModel = new SortListModel(); + mSortDropDown = new DropDown(mSortModel, this, "sort"); + mSortDropDown->setSelected(0); mFilterLabel = new Label(_("Filter:")); mSorterLabel = new Label(_("Sort:")); @@ -120,10 +149,6 @@ InventoryWindow::InventoryWindow(Inventory *inventory): for (unsigned f = 0; f < tags.size(); f ++) mFilter->addButton(tags[f]); - mSorter->addButton(_("na"), "na"); - mSorter->addButton(_("az"), "az"); - mSorter->addButton(_("id"), "id"); - if (isMainInventory()) { std::string equip = _("Equip"); @@ -154,7 +179,7 @@ InventoryWindow::InventoryWindow(Inventory *inventory): place(4, 0, mSlotsLabel, 1).setPadding(3); place(5, 0, mSlotsBar, 2); place(7, 0, mSorterLabel, 1); - place(8, 0, mSorter, 3); + place(8, 0, mSortDropDown, 3); place(0, 1, mFilterLabel, 1).setPadding(3); place(1, 1, mFilter, 10).setPadding(3); @@ -176,9 +201,9 @@ InventoryWindow::InventoryWindow(Inventory *inventory): mCloseButton = new Button(_("Close"), "close", this); place(0, 0, mSlotsLabel).setPadding(3); - place(1, 0, mSlotsBar, 3); - place(4, 0, mSorterLabel, 1); - place(5, 0, mSorter, 2); + place(1, 0, mSlotsBar, 4); + place(5, 0, mSorterLabel, 1); + place(6, 0, mSortDropDown, 1); place(0, 1, mFilterLabel, 1).setPadding(3); place(1, 1, mFilter, 6).setPadding(3); @@ -219,6 +244,8 @@ InventoryWindow::~InventoryWindow() mInventory->removeInventoyListener(this); if (!instances.empty()) instances.front()->updateDropButton(); + delete mSortModel; + mSortModel = 0; } void InventoryWindow::action(const gcn::ActionEvent &event) @@ -258,6 +285,10 @@ void InventoryWindow::action(const gcn::ActionEvent &event) ItemAmountWindow::showWindow(ItemAmountWindow::StoreAdd, this, item); } + else if (event.getId() == "sort") + { + mItems->setSortType(mSortDropDown->getSelected()); + } else if (!event.getId().find("tag_") && mItems) { std::string tagName = event.getId().substr(4); diff --git a/src/gui/inventorywindow.h b/src/gui/inventorywindow.h index 503423ad3..c07b5b0e1 100644 --- a/src/gui/inventorywindow.h +++ b/src/gui/inventorywindow.h @@ -41,10 +41,12 @@ #define A_UNUSED #endif +class DropDown; class Item; class ItemContainer; class InventoryFilter; class ProgressBar; +class SortListModel; class TextBox; /** @@ -151,7 +153,8 @@ class InventoryWindow : public Window, ProgressBar *mWeightBar, *mSlotsBar; InventoryFilter *mFilter; - InventoryFilter *mSorter; + DropDown *mSortDropDown; + SortListModel *mSortModel; bool mSplit; }; diff --git a/src/gui/widgets/dropdown.cpp b/src/gui/widgets/dropdown.cpp index f01d3fb55..687d7dc6d 100644 --- a/src/gui/widgets/dropdown.cpp +++ b/src/gui/widgets/dropdown.cpp @@ -46,7 +46,8 @@ Image *DropDown::buttons[2][2]; ImageRect DropDown::skin; float DropDown::mAlpha = 1.0; -DropDown::DropDown(gcn::ListModel *listModel): +DropDown::DropDown(gcn::ListModel *listModel, gcn::ActionListener* listener, + std::string eventId): gcn::DropDown::DropDown(listModel, new ScrollArea, new ListBox(listModel)) @@ -105,6 +106,12 @@ DropDown::DropDown(gcn::ListModel *listModel): mHighlightColor = Theme::getThemeColor(Theme::HIGHLIGHT); mShadowColor = Theme::getThemeColor(Theme::DROPDOWN_SHADOW); setForegroundColor(Theme::getThemeColor(Theme::TEXT)); + + if (!eventId.empty()) + setActionEventId(eventId); + + if (listener) + addActionListener(listener); } DropDown::~DropDown() diff --git a/src/gui/widgets/dropdown.h b/src/gui/widgets/dropdown.h index 6a22ba497..4cbd23314 100644 --- a/src/gui/widgets/dropdown.h +++ b/src/gui/widgets/dropdown.h @@ -46,7 +46,9 @@ class DropDown : public gcn::DropDown * @param listBox the listBox to use. * @see ListModel, ScrollArea, ListBox. */ - DropDown(gcn::ListModel *listModel = 0); + DropDown(gcn::ListModel *listModel = 0, + gcn::ActionListener* listener = NULL, + std::string eventId = ""); ~DropDown(); diff --git a/src/gui/widgets/itemcontainer.cpp b/src/gui/widgets/itemcontainer.cpp index 3d6d24868..75968d668 100644 --- a/src/gui/widgets/itemcontainer.cpp +++ b/src/gui/widgets/itemcontainer.cpp @@ -96,6 +96,63 @@ class SortItemIdFunctor } } itemIdSorter; +class SortItemWeightFunctor +{ + public: + bool operator() (ItemIdPair* pair1, ItemIdPair* pair2) + { + if (!pair1 || !pair2) + return false; + + const int w1 = pair1->mItem->getInfo().getWeight(); + const int w2 = pair2->mItem->getInfo().getWeight(); + if (w1 == w2) + { + return (pair1->mItem->getInfo().getName() + < pair2->mItem->getInfo().getName()); + } + return w1 < w2; + } +} itemWeightSorter; + +class SortItemAmountFunctor +{ + public: + bool operator() (ItemIdPair* pair1, ItemIdPair* pair2) + { + if (!pair1 || !pair2) + return false; + + const int c1 = pair1->mItem->getQuantity(); + const int c2 = pair2->mItem->getQuantity(); + if (c1 == c2) + { + return (pair1->mItem->getInfo().getName() + < pair2->mItem->getInfo().getName()); + } + return c1 < c2;; + } +} itemAmountSorter; + +class SortItemTypeFunctor +{ + public: + bool operator() (ItemIdPair* pair1, ItemIdPair* pair2) + { + if (!pair1 || !pair2) + return false; + + const int t1 = pair1->mItem->getInfo().getType(); + const int t2 = pair2->mItem->getInfo().getType(); + if (t1 == t2) + { + return (pair1->mItem->getInfo().getName() + < pair2->mItem->getInfo().getName()); + } + return t1 < t2;; + } +} itemTypeSorter; + ItemContainer::ItemContainer(Inventory *inventory, bool forceQuantity): mInventory(inventory), mGridColumns(1), @@ -477,6 +534,15 @@ void ItemContainer::updateMatrix() case 2: sort(sortedItems.begin(), sortedItems.end(), itemIdSorter); break; + case 3: + sort(sortedItems.begin(), sortedItems.end(), itemWeightSorter); + break; + case 4: + sort(sortedItems.begin(), sortedItems.end(), itemAmountSorter); + break; + case 5: + sort(sortedItems.begin(), sortedItems.end(), itemTypeSorter); + break; } std::vector::const_iterator iter; @@ -608,6 +674,5 @@ void ItemContainer::setFilter (int tag) void ItemContainer::setSortType (int sortType) { mSortType = sortType; - logger->log("setSortType: %d", sortType); updateMatrix(); } -- cgit v1.2.3-70-g09d2